-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionsLib.lua
More file actions
492 lines (418 loc) · 15.3 KB
/
Copy pathOptionsLib.lua
File metadata and controls
492 lines (418 loc) · 15.3 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
--[[ NeonSign Options Library v1.0 ]]--
-- Caveats:
-- CheckButton variables must be boolean (not 1/0)
-- ColorPicker variables must be format {r = 0, g = 0, b = 0}
-- Not yet implemented:
-- "Requires UI reload" popup
-- ColorPicker opacity
-- Dependent controls (control:Enable(), control:Disable())
-- Option subcategories
local addonName, addonTable = ...;
local MyAddon = addonTable;
--[[ Options handling ]]--
function MyAddon.CopyTable(source)
local copy = {};
for key, value in pairs(source) do
if ( type(value) == "table" ) then
copy[key] = CopyTable(value);
else
copy[key] = value;
end
end
return copy;
end
local CopyTable = MyAddon.CopyTable;
local function UpdateTable(destination, source)
for key, value in pairs(source) do
if ( type(value) == "table" ) then
destination[key] = destination[key] or {};
destination[key] = UpdateTable(destination[key], value);
elseif ( destination[key] == nil ) then
destination[key] = value;
end
end
return destination;
end
function MyAddon:UpdateOptions(savedVariablesName, defaults, reset)
local options = _G[savedVariablesName];
if ( options and not reset ) then
options = UpdateTable(options, defaults);
options.Version = GetAddOnMetadata(addonName, "Version");
else
options = CopyTable(defaults);
_G[savedVariablesName] = options;
end
return options;
end
function MyAddon:GetOption(optionName, options)
return options[optionName];
end
function MyAddon:SetOption(optionName, value, options)
options[optionName] = value;
end
--[[ Interface options panel ]]--
function MyAddon:CreateOptionsPanel()
local name = "InterfaceOptions"..addonName.."Panel"
local optionsPanel = CreateFrame("Frame", name, InterfaceOptionsFramePanelContainer);
local title = GetAddOnMetadata(addonName, "Title");
local version = GetAddOnMetadata(addonName, "Version");
optionsPanel.title = optionsPanel:CreateFontString(nil, nil, "GameFontNormalLarge");
optionsPanel.title:SetPoint("TOPLEFT", 16, -16);
optionsPanel.title:SetText(title.." v"..version);
optionsPanel.subtext = optionsPanel:CreateFontString(nil, nil, "GameFontHighlightSmall");
optionsPanel.subtext:SetPoint("TOPLEFT", optionsPanel.title, "BOTTOMLEFT", 0, -8);
optionsPanel.subtext:SetPoint("RIGHT", optionsPanel, "RIGHT", -32, 0);
optionsPanel.subtext:SetJustifyH("LEFT");
optionsPanel.subtext:SetText("");
optionsPanel.CreateCheckButton = MyAddon.CreateCheckButton;
optionsPanel.CreateSlider = MyAddon.CreateSlider;
optionsPanel.CreateDropDownMenu = MyAddon.CreateDropDownMenu;
optionsPanel.CreateColorPicker = MyAddon.CreateColorPicker;
optionsPanel.CreateTextArea = MyAddon.CreateTextArea;
optionsPanel.CreateNumericTextArea = MyAddon.CreateNumericTextArea;
optionsPanel.name = addonName;
optionsPanel.refresh = MyAddon.OptionsPanelRefresh;
optionsPanel.cancel = MyAddon.OptionsPanelCancel;
optionsPanel.default = MyAddon.OptionsPanelDefaults;
optionsPanel.okay = MyAddon.OptionsPanelOkay;
InterfaceOptions_AddCategory(optionsPanel);
optionsPanel:RegisterEvent("ADDON_LOADED");
optionsPanel:SetScript("OnEvent", MyAddon.OptionsPanel_OnEvent);
return optionsPanel;
end
-- Usage:
-- optionsPanel = MyAddon:CreateOptionsPanel()
-- optionsPanel.savedVariablesName = "MySavedVariables"
-- optionsPanel.defaults = myDefaults
-- optionsPanel.defaultsFunc = DoOnClickDefaults
-- optionsPanel.okayFunc = DoOnClickOkay
function MyAddon:OptionsPanel_OnEvent(event, ...)
if ( event == "ADDON_LOADED" ) then
local arg1 = ...;
if ( arg1 == addonName ) then
self.options = _G[self.savedVariablesName];
-- This can throw an error if saved variables don't exist yet and
-- and UpdateOptions() is called on ADDON_LOADED before this is
-- Putting OptionsLibrary.lua later in the toc will fix it, but it's not ideal.
end
end
end
function MyAddon:RegisterControl(control, optionsPanel)
if ( control and optionsPanel ) then
optionsPanel.controls = optionsPanel.controls or {};
tinsert(optionsPanel.controls, control);
end
end
function MyAddon:OptionsPanelRefresh()
for _, control in next, self.controls do
control:Refresh();
control.oldValue = control.value;
end
end
function MyAddon:OptionsPanelCancel()
for _, control in next, self.controls do
MyAddon:SetOption(control.optionName, control.oldValue, self.options);
if ( control.onValueChanged ) then
control.onValueChanged(control.oldValue);
end
end
end
function MyAddon:OptionsPanelDefaults()
_G[self.savedVariablesName] = CopyTable(self.defaults);
self.options = _G[self.savedVariablesName];
for _, control in next, self.controls do
local defaultValue = self.defaults[control.optionName];
MyAddon:SetOption(control.optionName, defaultValue, self.options);
control:Refresh();
if ( control.onValueChanged ) then
control.onValueChanged(control.value);
end
end
if ( self.defaultsFunc ) then
self.defaultsFunc(); -- For options without panel controls
end
end
function MyAddon:OptionsPanelOkay()
if ( self.okayFunc ) then
self.okayFunc();
end
end
--[[ TextArea ]]--
function MyAddon:CreateTextArea(optionName)
local name = self:GetName()..optionName.."TextArea";
local textArea = CreateFrame("EditBox", name, self, "InputBoxTemplate");
textArea.optionName = optionName;
textArea:SetScript("OnEnterPressed", MyAddon.TextAreaOnEnterPressed);
MyAddon:RegisterControl(textArea, self);
textArea.Refresh = MyAddon.TextAreaRefresh;
textArea:SetHeight(20);
return textArea;
end
function MyAddon:CreateNumericTextArea(optionName)
local name = self:GetName()..optionName.."TextArea";
local textArea = CreateFrame("EditBox", name, self, "InputBoxTemplate");
textArea.optionName = optionName;
textArea:SetScript("OnEnterPressed", MyAddon.NumericTextAreaOnEnterPressed);
MyAddon:RegisterControl(textArea, self);
textArea.Refresh = MyAddon.TextAreaRefresh;
textArea:SetHeight(20);
return textArea;
end
function MyAddon:TextAreaRefresh()
local value = MyAddon:GetOption(self.optionName, self:GetParent().options);
if (self.value == nil or self.value == "") then
self:SetText(value);
self.value = value;
self:SetCursorPosition(0);
end
end
function MyAddon:NumericTextAreaOnEnterPressed()
local newValue = tonumber(self:GetText());
if (newValue == nil) then
print("Value must be numeric");
local oldValue = MyAddon:GetOption(self.optionName, self:GetParent().options);
self:SetText(oldValue);
return;
end
MyAddon:SetOption(self.optionName, newValue, self:GetParent().options);
if (self.OnOptionChange ~= nil) then
self.OnOptionChange(newValue);
end
self:ClearFocus();
end
function MyAddon:TextAreaOnEnterPressed()
local newText = self:GetText();
MyAddon:SetOption(self.optionName, newText, self:GetParent().options);
self:ClearFocus();
end
function MyAddon.TextAreaOnLeave()
--MyAddon:SetOption(self.optionName, self.value, self:GetParent().options);
end
--[[ CheckButton ]]--
function MyAddon:CreateCheckButton(optionName)
local name = self:GetName()..optionName.."CheckButton";
local checkButton = CreateFrame("CheckButton", name, self, "InterfaceOptionsCheckButtonTemplate");
checkButton.optionName = optionName;
checkButton:SetScript("OnClick", MyAddon.CheckButtonOnClick);
checkButton:SetScript("OnEnter", MyAddon.CheckButtonOnEnter);
checkButton:SetScript("OnLeave", MyAddon.CheckButtonOnLeave);
checkButton.Refresh = MyAddon.CheckButtonRefresh;
MyAddon:RegisterControl(checkButton, self);
return checkButton;
end
-- Usage:
-- myButton = optionsPanel:CreateCheckButton("SomeOption")
-- myButton.Text:SetText("Some option")
-- myButton.onValueChanged = DoSomethingOnClick
function MyAddon:CheckButtonOnClick()
--[[
local isChecked = self:GetChecked();
if ( isChecked ) then
PlaySound("igMainMenuOptionCheckBoxOn");
else
PlaySound("igMainMenuOptionCheckBoxOff");
end
self.value = isChecked;
]]--
if ( self:GetChecked() ) then
--PlaySound("igMainMenuOptionCheckBoxOn");
else
--PlaySound("igMainMenuOptionCheckBoxOff");
end
local value = false;
if ( self:GetChecked() ) then
value = true;
end
self.value = value;
MyAddon:SetOption(self.optionName, value, self:GetParent().options);
if ( self.onValueChanged ) then
self.onValueChanged(value);
end
end
function MyAddon:CheckButtonRefresh()
local value = MyAddon:GetOption(self.optionName, self:GetParent().options);
self:SetChecked(value);
self.value = value;
end
function MyAddon:CheckButtonOnEnter()
if ( self.tooltipText ) then
GameTooltip:SetOwner(self, self.tooltipOwnerPoint or "ANCHOR_RIGHT");
GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, true);
end
if ( self.tooltipRequirement ) then
GameTooltip:AddLine(self.tooltipRequirement, 1.0, 1.0, 1.0, true);
GameTooltip:Show();
end
end
function MyAddon:CheckButtonOnLeave()
GameTooltip:Hide();
end
--[[ Slider ]]--
function MyAddon:CreateSlider(optionName)
local name = self:GetName()..optionName.."Slider";
local slider = CreateFrame("Slider", name, self, "OptionsSliderTemplate");
slider.optionName = optionName;
slider:SetScript("OnValueChanged", MyAddon.SliderOnValueChanged);
slider:SetScript("OnEnter", MyAddon.SliderOnEnter);
slider:SetScript("OnLeave", MyAddon.SliderOnLeave);
slider.Refresh = MyAddon.SliderRefresh;
MyAddon:RegisterControl(slider, self);
slider.Text = _G[slider:GetName().."Text"]; -- No key in template :(
return slider;
end
-- Usage:
-- mySlider = optionsPanel:CreateSlider("SomeOption")
-- mySlider.Text:SetText("Some option")
-- mySlider:SetMinMaxValues(min, max)
-- mySlider.onValueChanged = DoSomethingOnAdjust
function MyAddon:SliderOnValueChanged(value)
MyAddon:SetOption(self.optionName, value, self:GetParent().options);
self.value = value;
if ( self.onValueChanged ) then
self.onValueChanged(value);
end
end
function MyAddon:SliderRefresh()
value = MyAddon:GetOption(self.optionName, self:GetParent().options);
self:SetValue(value);
self.value = value;
end
function MyAddon:SliderOnEnter()
if ( self.tooltipText ) then
GameTooltip:SetOwner(self, self.tooltipOwnerPoint or "ANCHOR_RIGHT");
GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, true);
end
if ( self.tooltipRequirement ) then
GameTooltip:AddLine(self.tooltipRequirement, 1.0, 1.0, 1.0, true);
GameTooltip:Show();
end
end
function MyAddon:SliderOnLeave()
GameTooltip:Hide();
end
--[[ DropDownMenu]]--
function MyAddon:CreateDropDownMenu(optionName)
local name = self:GetName()..optionName.."DropDown";
local dropDownMenu = CreateFrame("Frame", name, self, "UIDropDownMenuTemplate");
dropDownMenu.optionName = optionName;
dropDownMenu.Text = dropDownMenu:CreateFontString(nil, nil, "GameFontHighlight");
dropDownMenu.Text:SetPoint("BOTTOMLEFT", dropDownMenu, "TOPLEFT", 16, 3);
dropDownMenu.Text:SetText("Dropdown menu text");
dropDownMenu.Refresh = MyAddon.DropDownMenuRefresh;
MyAddon:RegisterControl(dropDownMenu, self);
return dropDownMenu;
end
-- Usage:
-- myMenu = optionsPanel:CreateDropDownMenu("SomeOption")
-- myMenu.Text:SetText("Some option")
-- myMenu.onValueChanged = DoSomethingOnItemSelect
-- myMenu.optionList = menuList -- Each item formatted {text = text, value = value}
function MyAddon:DropDownMenuRefresh()
local value = MyAddon:GetOption(self.optionName, self:GetParent().options);
self.value = value;
UIDropDownMenu_Initialize(self, MyAddon.DropDownMenuInitialize);
UIDropDownMenu_SetSelectedValue(self, self.value);
end
function MyAddon:DropDownMenuInitialize(level, menuList)
-- When called, self is DropDownMenu
local selectedValue = UIDropDownMenu_GetSelectedValue(self);
local info = UIDropDownMenu_CreateInfo();
for _, listItem in next, self.optionList do
info.text = listItem.text;
info.value = listItem.value;
info.func = MyAddon.DropDownMenuOnClick;
info.arg1 = self;
if ( info.value == selectedValue ) then
info.checked = 1;
else
info.checked = nil;
end
UIDropDownMenu_AddButton(info);
end
end
function MyAddon:DropDownMenuOnClick(dropDownMenu)
-- When called, self is DropDownList button
MyAddon:SetOption(dropDownMenu.optionName, self.value, _G[dropDownMenu:GetParent().savedVariablesName]);
if ( dropDownMenu.onValueChanged ) then
dropDownMenu.onValueChanged(self.value);
end
UIDropDownMenu_SetSelectedValue(dropDownMenu, self.value);
end
--[[ ColorPicker ]]--
function MyAddon:GetColor(color)
local r, g, b = color.r, color.g, color.b;
return r, g, b;
end
function MyAddon:CreateColorPicker(optionName)
local colorPicker = CreateFrame("Button", nil, self);
colorPicker.optionName = optionName;
colorPicker:SetSize(16, 16);
colorPicker:SetNormalTexture("Interface\\ChatFrame\\ChatFrameColorSwatch");
colorPicker.normalTexture = colorPicker:GetNormalTexture();
colorPicker.normalTexture:SetDrawLayer("ARTWORK", 1);
colorPicker.background = colorPicker:CreateTexture("BACKGROUND", nil, -5);
colorPicker.background:SetSize(14, 14);
colorPicker.background:SetColorTexture(1, 1, 1);
colorPicker.background:SetPoint("CENTER");
colorPicker.Text = colorPicker:CreateFontString(nil, nil, "GameFontHighlight");
colorPicker.Text:SetPoint("LEFT", colorPicker, "RIGHT", 8, 0);
colorPicker.Text:SetText("Color picker text");
colorPicker:SetScript("OnClick", MyAddon.ColorPickerOnClick);
colorPicker:SetScript("OnEnter", MyAddon.ColorPickerOnEnter);
colorPicker:SetScript("OnLeave", MyAddon.ColorPickerOnLeave);
colorPicker.Refresh = MyAddon.ColorPickerRefresh;
MyAddon:RegisterControl(colorPicker, self);
return colorPicker;
end
-- Usage:
-- myColorPicker = optionsPanel:CreateColorPicker("SomeOption")
-- myColorPicker.Text:SetText("Some color option")
-- myColorPicker.onValueChanged = DoSomethingOnAdjust -- Takes color as argument
function MyAddon:ColorPickerRefresh()
local value = MyAddon:GetOption(self.optionName, self:GetParent().options);
self.normalTexture:SetVertexColor(value.r, value.g, value.b);
self.value = value;
end
function MyAddon:ColorPickerOnClick()
ColorPickerFrame.previousValues = self.value;
ColorPickerFrame.func = MyAddon.ColorPickerFunc;
ColorPickerFrame.cancelFunc = MyAddon.ColorPickerCancelFunc;
ColorPickerFrame.colorPicker = self;
ColorPickerFrame.savedVariablesName = self:GetParent().savedVariablesName;
ColorPickerFrame:SetColorRGB(MyAddon:GetColor(self.value));
ColorPickerFrame:Hide(); -- Need to run the OnShow handler
ColorPickerFrame:Show();
end
function MyAddon:ColorPickerFunc()
-- called by ColorPickerFrame with no self when color selected
local r, g, b = ColorPickerFrame:GetColorRGB();
local value = {r = r, g = g, b = b};
local colorPicker = ColorPickerFrame.colorPicker;
MyAddon:SetOption(colorPicker.optionName, value, _G[ColorPickerFrame.savedVariablesName]);
if ( colorPicker.onValueChanged ) then
colorPicker.onValueChanged(value);
end
colorPicker:Refresh();
end
function MyAddon:ColorPickerCancelFunc()
local previousValues = ColorPickerFrame.previousValues;
local colorPicker = ColorPickerFrame.colorPicker;
MyAddon:SetOption(colorPicker.optionName, previousValues, _G[ColorPickerFrame.savedVariablesName]);
if ( colorPicker.onValueChanged ) then
colorPicker.onValueChanged(previousValues);
end
colorPicker:Refresh();
end
function MyAddon:ColorPickerOnEnter()
if ( self.tooltipText ) then
GameTooltip:SetOwner(self, self.tooltipOwnerPoint or "ANCHOR_RIGHT");
GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, true);
end
if ( self.tooltipRequirement ) then
GameTooltip:AddLine(self.tooltipRequirement, 1.0, 1.0, 1.0, true);
GameTooltip:Show();
end
end
function MyAddon:ColorPickeOnLeave()
GameTooltip:Hide();
end