-
Notifications
You must be signed in to change notification settings - Fork 27
/
Button.lua
126 lines (98 loc) · 3.41 KB
/
Button.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
--- @type StdUi
local StdUi = LibStub and LibStub('StdUi', true);
if not StdUi then
return
end
local module, version = 'Button', 6;
if not StdUi:UpgradeNeeded(module, version) then return end;
local SquareButtonCoords = {
UP = { 0.45312500, 0.64062500, 0.01562500, 0.20312500};
DOWN = { 0.45312500, 0.64062500, 0.20312500, 0.01562500};
LEFT = { 0.23437500, 0.42187500, 0.01562500, 0.20312500};
RIGHT = { 0.42187500, 0.23437500, 0.01562500, 0.20312500};
DELETE = { 0.01562500, 0.20312500, 0.01562500, 0.20312500};
};
local SquareButtonMethods = {
SetIconDisabled = function(self, texture, iconWidth, iconHeight)
self.iconDisabled = self.stdUi:Texture(self, iconWidth, iconHeight, texture);
self.iconDisabled:SetDesaturated(true);
self.iconDisabled:SetPoint('CENTER', 0, 0);
self:SetDisabledTexture(self.iconDisabled);
end,
SetIcon = function(self, texture, iconWidth, iconHeight, alsoDisabled)
self.icon = self.stdUi:Texture(self, iconWidth, iconHeight, texture);
self.icon:SetPoint('CENTER', 0, 0);
self:SetNormalTexture(self.icon);
if alsoDisabled then
self:SetIconDisabled(texture, iconWidth, iconHeight);
end
end
};
function StdUi:SquareButton(parent, width, height, icon, name)
local button = CreateFrame('Button', name, parent);
button.stdUi = self;
self:InitWidget(button);
self:SetObjSize(button, width, height);
self:ApplyBackdrop(button);
self:HookDisabledBackdrop(button);
self:HookHoverBorder(button);
for k, v in pairs(SquareButtonMethods) do
button[k] = v;
end
local coords = SquareButtonCoords[icon];
if coords then
button:SetIcon([[Interface\Buttons\SquareButtonTextures]], 16, 16, true);
button.icon:SetTexCoord(coords[1], coords[2], coords[3], coords[4]);
button.iconDisabled:SetTexCoord(coords[1], coords[2], coords[3], coords[4]);
end
return button;
end
function StdUi:ButtonLabel(parent, text)
local label = self:Label(parent, text);
label:SetJustifyH('CENTER');
self:GlueAcross(label, parent, 2, -2, -2, 2);
parent:SetFontString(label);
return label;
end
function StdUi:HighlightButtonTexture(button)
local hTex = self:Texture(button, nil, nil, nil);
hTex:SetColorTexture(
self.config.highlight.color.r,
self.config.highlight.color.g,
self.config.highlight.color.b,
self.config.highlight.color.a
);
hTex:SetAllPoints();
return hTex;
end
--- Creates a button with only a highlight
--- @return Button
function StdUi:HighlightButton(parent, width, height, text, inherit, name)
local button = CreateFrame('Button', name, parent, inherit);
self:InitWidget(button);
self:SetObjSize(button, width, height);
button.text = self:ButtonLabel(button, text);
function button:SetFontSize(newSize)
self.text:SetFontSize(newSize);
end
local hTex = self:HighlightButtonTexture(button);
hTex:SetBlendMode('ADD');
button:SetHighlightTexture(hTex);
button.highlightTexture = hTex;
return button;
end
--- @return Button
function StdUi:Button(parent, width, height, text, inherit, name)
local button = self:HighlightButton(parent, width, height, text, inherit, name)
button.stdUi = self;
button:SetHighlightTexture('');
self:ApplyBackdrop(button);
self:HookDisabledBackdrop(button);
self:HookHoverBorder(button);
return button;
end
function StdUi:ButtonAutoWidth(button, padding)
padding = padding or 5;
button:SetWidth(button.text:GetStringWidth() + padding * 2);
end
StdUi:RegisterModule(module, version);