-
Notifications
You must be signed in to change notification settings - Fork 27
/
Checkbox.lua
288 lines (228 loc) · 6.66 KB
/
Checkbox.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
--- @type StdUi
local StdUi = LibStub and LibStub('StdUi', true);
if not StdUi then
return
end
local module, version = 'Checkbox', 5;
if not StdUi:UpgradeNeeded(module, version) then
return
end
----------------------------------------------------
--- Checkbox
----------------------------------------------------
local CheckboxMethods = {
--- Set checkbox state
---
--- @param flag boolean
--- @param internal boolean - indicates to not run OnValueChanged
SetChecked = function(self, flag, internal)
self.isChecked = flag;
if not internal and self.OnValueChanged then
self:OnValueChanged(flag, self.value);
end
if not flag then
self.checkedTexture:Hide();
self.disabledCheckedTexture:Hide();
return
end
if self.isDisabled then
self.checkedTexture:Hide();
self.disabledCheckedTexture:Show();
else
self.checkedTexture:Show();
self.disabledCheckedTexture:Hide();
end
end,
GetChecked = function(self)
return self.isChecked;
end,
SetText = function(self, t)
self.text:SetText(t);
end,
SetValue = function(self, value)
self.value = value;
end,
GetValue = function(self)
if self:GetChecked() then
return self.value;
else
return nil;
end
end,
Disable = function(self)
self.isDisabled = true;
self:SetChecked(self.isChecked);
end,
Enable = function(self)
self.isDisabled = false;
self:SetChecked(self.isChecked);
end,
AutoWidth = function(self)
self:SetWidth(self.target:GetWidth() + 15 + self.text:GetWidth());
end
};
local CheckboxEvents = {
OnClick = function(self)
if not self.isDisabled then
self:SetChecked(not self:GetChecked());
end
end
}
---@return CheckButton
function StdUi:Checkbox(parent, text, width, height)
local checkbox = CreateFrame('Button', nil, parent);
checkbox.stdUi = self;
checkbox:EnableMouse(true);
self:SetObjSize(checkbox, width, height or 20);
self:InitWidget(checkbox);
checkbox.target = self:Panel(checkbox, 16, 16);
checkbox.target.stdUi = self;
checkbox.target:SetPoint('LEFT', 0, 0);
checkbox.value = true;
checkbox.isChecked = false;
checkbox.text = self:Label(checkbox, text);
checkbox.text:SetPoint('LEFT', checkbox.target, 'RIGHT', 5, 0);
checkbox.text:SetPoint('RIGHT', checkbox, 'RIGHT', -5, 0);
checkbox.target.text = checkbox.text; -- reference for disabled
checkbox.checkedTexture = self:Texture(checkbox.target, nil, nil, [[Interface\Buttons\UI-CheckBox-Check]]);
checkbox.checkedTexture:SetAllPoints();
checkbox.checkedTexture:Hide();
checkbox.disabledCheckedTexture = self:Texture(checkbox.target, nil, nil,
[[Interface\Buttons\UI-CheckBox-Check-Disabled]]);
checkbox.disabledCheckedTexture:SetAllPoints();
checkbox.disabledCheckedTexture:Hide();
for k, v in pairs(CheckboxMethods) do
checkbox[k] = v;
end
self:ApplyBackdrop(checkbox.target);
self:HookDisabledBackdrop(checkbox);
self:HookHoverBorder(checkbox);
if width == nil then
checkbox:AutoWidth();
end
for k, v in pairs(CheckboxEvents) do
checkbox:SetScript(k, v);
end
return checkbox;
end
----------------------------------------------------
--- IconCheckbox
----------------------------------------------------
function StdUi:IconCheckbox(parent, icon, text, width, height, iconSize)
iconSize = iconSize or 16
local checkbox = self:Checkbox(parent, text, width, height);
checkbox.icon = self:Texture(checkbox, iconSize, iconSize, icon);
checkbox.icon:SetPoint('LEFT', checkbox.target, 'RIGHT', 5, 0);
checkbox.text:ClearAllPoints();
checkbox.text:SetPoint('LEFT', checkbox.target, 'RIGHT', iconSize + 5, 0);
checkbox.text:SetPoint('RIGHT', checkbox, 'RIGHT', -5, 0);
return checkbox;
end
----------------------------------------------------
--- Radio
----------------------------------------------------
local RadioEvents = {
OnClick = function(self)
if not self.isDisabled then
self:SetChecked(true);
end
end
};
---@return CheckButton
function StdUi:Radio(parent, text, groupName, width, height)
local radio = self:Checkbox(parent, text, width, height);
radio.checkedTexture = self:Texture(radio.target, nil, nil, [[Interface\Buttons\UI-RadioButton]]);
radio.checkedTexture:SetAllPoints(radio.target);
radio.checkedTexture:Hide();
radio.checkedTexture:SetTexCoord(0.25, 0.5, 0, 1);
radio.disabledCheckedTexture = self:Texture(radio.target, nil, nil,
[[Interface\Buttons\UI-RadioButton]]);
radio.disabledCheckedTexture:SetAllPoints(radio.target);
radio.disabledCheckedTexture:Hide();
radio.disabledCheckedTexture:SetTexCoord(0.75, 1, 0, 1);
for k, v in pairs(RadioEvents) do
radio:SetScript(k, v);
end
if groupName then
self:AddToRadioGroup(radio, groupName);
end
return radio;
end
StdUi.radioGroups = {};
StdUi.radioGroupValues = {};
---@return CheckButton[]
function StdUi:RadioGroup(groupName)
if not self.radioGroups[groupName] then
self.radioGroups[groupName] = {};
end
if not self.radioGroupValues[groupName] then
self.radioGroupValues[groupName] = {};
end
return self.radioGroups[groupName];
end
function StdUi:GetRadioGroupValue(groupName)
local group = self:RadioGroup(groupName);
for i = 1, #group do
local radio = group[i];
if radio:GetChecked() then
return radio:GetValue();
end
end
return nil;
end
function StdUi:SetRadioGroupValue(groupName, value)
local group = self:RadioGroup(groupName);
for i = 1, #group do
local radio = group[i];
radio:SetChecked(radio.value == value)
end
return nil;
end
local radioGroupOnValueChanged = function(radio)
radio.notified = true;
local group = radio.radioGroup;
local groupName = radio.radioGroupName;
-- We must get all notifications from group
for i = 1, #group do
if not group[i].notified then
return
end
end
local newValue = radio.stdUi:GetRadioGroupValue(groupName);
if radio.stdUi.radioGroupValues[groupName] ~= newValue then
radio.OnValueChangedCallback(newValue, groupName);
end
radio.stdUi.radioGroupValues[groupName] = newValue;
for i = 1, #group do
group[i].notified = false;
end
end
function StdUi:OnRadioGroupValueChanged(groupName, callback)
local group = self:RadioGroup(groupName);
for i = 1, #group do
local radio = group[i];
radio.OnValueChangedCallback = callback;
radio.OnValueChanged = radioGroupOnValueChanged;
end
return nil;
end
local RadioGroupEvents = {
OnClick = function(radio)
for i = 1, #radio.radioGroup do
local otherRadio = radio.radioGroup[i];
if otherRadio ~= radio then
otherRadio:SetChecked(false);
end
end
end
};
function StdUi:AddToRadioGroup(radio, groupName)
local group = self:RadioGroup(groupName);
tinsert(group, radio);
radio.radioGroup = group;
radio.radioGroupName = groupName;
for k, v in pairs(RadioGroupEvents) do
radio:HookScript(k, v);
end
end
StdUi:RegisterModule(module, version);