Details | Compare with Previous | View Log
Line No. | Rev | Author | Line |
---|---|---|---|
1 | 14 | Seerah-7 | --[[checkboxData = { |
2 | Seerah-7 | type = "checkbox", |
|
3 | Seerah-7 | name = "My Checkbox", |
|
4 | Seerah-7 | tooltip = "Checkbox's tooltip text.", |
|
5 | Seerah-7 | getFunc = function() return db.var end, |
|
6 | Seerah-7 | setFunc = function(value) db.var = value doStuff() end, |
|
7 | Seerah-7 | width = "full", --or "half" (optional) |
|
8 | Seerah-7 | disabled = function() return db.someBooleanSetting end, --or boolean (optional) |
|
9 | Seerah-7 | warning = "Will need to reload the UI.", --(optional) |
|
10 | Seerah-7 | default = defaults.var, --(optional) |
|
11 | Seerah-7 | reference = "MyAddonCheckbox" --(optional) unique global reference to control |
|
12 | Seerah-7 | } ]] |
|
13 | Seerah-7 | ||
14 | Seerah-7 | ||
15 | 16 | Seerah-7 | local widgetVersion = 7 |
16 | 14 | Seerah-7 | local LAM = LibStub("LibAddonMenu-2.0") |
17 | Seerah-7 | if not LAM:RegisterWidget("checkbox", widgetVersion) then return end |
|
18 | Seerah-7 | ||
19 | Seerah-7 | local wm = WINDOW_MANAGER |
|
20 | Seerah-7 | local cm = CALLBACK_MANAGER |
|
21 | Seerah-7 | local tinsert = table.insert |
|
22 | Seerah-7 | --label |
|
23 | Seerah-7 | local enabledColor = ZO_DEFAULT_ENABLED_COLOR |
|
24 | Seerah-7 | local enabledHLcolor = ZO_HIGHLIGHT_TEXT |
|
25 | Seerah-7 | local disabledColor = ZO_DEFAULT_DISABLED_COLOR |
|
26 | Seerah-7 | local disabledHLcolor = ZO_DEFAULT_DISABLED_MOUSEOVER_COLOR |
|
27 | Seerah-7 | --checkbox |
|
28 | Seerah-7 | local checkboxColor = ZO_NORMAL_TEXT |
|
29 | Seerah-7 | local checkboxHLcolor = ZO_HIGHLIGHT_TEXT |
|
30 | Seerah-7 | ||
31 | Seerah-7 | ||
32 | Seerah-7 | local function UpdateDisabled(control) |
|
33 | Seerah-7 | local disable |
|
34 | Seerah-7 | if type(control.data.disabled) == "function" then |
|
35 | Seerah-7 | disable = control.data.disabled() |
|
36 | Seerah-7 | else |
|
37 | Seerah-7 | disable = control.data.disabled |
|
38 | Seerah-7 | end |
|
39 | Seerah-7 | ||
40 | Seerah-7 | control.label:SetColor((disable and ZO_DEFAULT_DISABLED_COLOR or control.value and ZO_DEFAULT_ENABLED_COLOR or ZO_DEFAULT_DISABLED_COLOR):UnpackRGBA()) |
|
41 | Seerah-7 | control.checkbox:SetColor((disable and ZO_DEFAULT_DISABLED_COLOR or ZO_NORMAL_TEXT):UnpackRGBA()) |
|
42 | Seerah-7 | --control:SetMouseEnabled(not disable) |
|
43 | Seerah-7 | --control:SetMouseEnabled(true) |
|
44 | Seerah-7 | ||
45 | Seerah-7 | control.isDisabled = disable |
|
46 | Seerah-7 | end |
|
47 | Seerah-7 | ||
48 | Seerah-7 | local function ToggleCheckbox(control) |
|
49 | Seerah-7 | if control.value then |
|
50 | Seerah-7 | control.label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA()) |
|
51 | Seerah-7 | control.checkbox:SetText(control.checkedText) |
|
52 | Seerah-7 | else |
|
53 | Seerah-7 | control.label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA()) |
|
54 | Seerah-7 | control.checkbox:SetText(control.uncheckedText) |
|
55 | Seerah-7 | end |
|
56 | Seerah-7 | end |
|
57 | Seerah-7 | ||
58 | Seerah-7 | local function UpdateValue(control, forceDefault, value) |
|
59 | Seerah-7 | if forceDefault then --if we are forcing defaults |
|
60 | Seerah-7 | value = control.data.default |
|
61 | Seerah-7 | control.data.setFunc(value) |
|
62 | Seerah-7 | elseif value ~= nil then --our value could be false |
|
63 | Seerah-7 | control.data.setFunc(value) |
|
64 | Seerah-7 | --after setting this value, let's refresh the others to see if any should be disabled or have their settings changed |
|
65 | Seerah-7 | if control.panel.data.registerForRefresh then |
|
66 | Seerah-7 | cm:FireCallbacks("LAM-RefreshPanel", control) |
|
67 | Seerah-7 | end |
|
68 | Seerah-7 | else |
|
69 | Seerah-7 | value = control.data.getFunc() |
|
70 | Seerah-7 | end |
|
71 | Seerah-7 | control.value = value |
|
72 | Seerah-7 | ||
73 | Seerah-7 | ToggleCheckbox(control) |
|
74 | Seerah-7 | end |
|
75 | Seerah-7 | ||
76 | Seerah-7 | local function OnMouseEnter(control) |
|
77 | Seerah-7 | ZO_Options_OnMouseEnter(control) |
|
78 | Seerah-7 | ||
79 | Seerah-7 | if control.isDisabled then return end |
|
80 | Seerah-7 | ||
81 | Seerah-7 | local label = control.label |
|
82 | Seerah-7 | if control.value then |
|
83 | Seerah-7 | label:SetColor(ZO_HIGHLIGHT_TEXT:UnpackRGBA()) |
|
84 | Seerah-7 | else |
|
85 | Seerah-7 | label:SetColor(ZO_DEFAULT_DISABLED_MOUSEOVER_COLOR:UnpackRGBA()) |
|
86 | Seerah-7 | end |
|
87 | Seerah-7 | control.checkbox:SetColor(ZO_HIGHLIGHT_TEXT:UnpackRGBA()) |
|
88 | Seerah-7 | end |
|
89 | Seerah-7 | ||
90 | Seerah-7 | local function OnMouseExit(control) |
|
91 | Seerah-7 | ZO_Options_OnMouseExit(control) |
|
92 | Seerah-7 | ||
93 | Seerah-7 | if control.isDisabled then return end |
|
94 | Seerah-7 | ||
95 | Seerah-7 | local label = control.label |
|
96 | Seerah-7 | if control.value then |
|
97 | Seerah-7 | label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA()) |
|
98 | Seerah-7 | else |
|
99 | Seerah-7 | label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA()) |
|
100 | Seerah-7 | end |
|
101 | Seerah-7 | control.checkbox:SetColor(ZO_NORMAL_TEXT:UnpackRGBA()) |
|
102 | Seerah-7 | end |
|
103 | Seerah-7 | ||
104 | Seerah-7 | ||
105 | Seerah-7 | --controlName is optional |
|
106 | Seerah-7 | function LAMCreateControl.checkbox(parent, checkboxData, controlName) |
|
107 | Seerah-7 | local control = wm:CreateTopLevelWindow(controlName or checkboxData.reference) |
|
108 | Seerah-7 | control:SetParent(parent.scroll or parent) |
|
109 | Seerah-7 | control:SetMouseEnabled(true) |
|
110 | 16 | Seerah-7 | --control.tooltipText = checkboxData.tooltip |
111 | 14 | Seerah-7 | control:SetHandler("OnMouseEnter", OnMouseEnter) |
112 | Seerah-7 | control:SetHandler("OnMouseExit", OnMouseExit) |
|
113 | Seerah-7 | control:SetHandler("OnMouseUp", function(control) |
|
114 | Seerah-7 | if control.isDisabled then return end |
|
115 | Seerah-7 | PlaySound(SOUNDS.DEFAULT_CLICK) |
|
116 | Seerah-7 | control.value = not control.value |
|
117 | Seerah-7 | control:UpdateValue(false, control.value) |
|
118 | Seerah-7 | end) |
|
119 | Seerah-7 | ||
120 | Seerah-7 | control.label = wm:CreateControl(nil, control, CT_LABEL) |
|
121 | Seerah-7 | local label = control.label |
|
122 | Seerah-7 | label:SetFont("ZoFontWinH4") |
|
123 | Seerah-7 | label:SetText(checkboxData.name) |
|
124 | Seerah-7 | label:SetWrapMode(TEXT_WRAP_MODE_ELLIPSIS) |
|
125 | Seerah-7 | label:SetHeight(26) |
|
126 | Seerah-7 | ||
127 | Seerah-7 | control.checkbox = wm:CreateControl(nil, control, CT_LABEL) |
|
128 | Seerah-7 | local checkbox = control.checkbox |
|
129 | Seerah-7 | checkbox:SetFont("ZoFontGameBold") |
|
130 | Seerah-7 | checkbox:SetColor(ZO_NORMAL_TEXT:UnpackRGBA()) |
|
131 | Seerah-7 | control.checkedText = GetString(SI_CHECK_BUTTON_ON):upper() |
|
132 | Seerah-7 | control.uncheckedText = GetString(SI_CHECK_BUTTON_OFF):upper() |
|
133 | Seerah-7 | ||
134 | Seerah-7 | local isHalfWidth = checkboxData.width == "half" |
|
135 | Seerah-7 | if isHalfWidth then |
|
136 | Seerah-7 | control:SetDimensions(250, 55) |
|
137 | Seerah-7 | checkbox:SetDimensions(100, 26) |
|
138 | Seerah-7 | checkbox:SetAnchor(BOTTOMRIGHT) |
|
139 | Seerah-7 | label:SetAnchor(TOPLEFT) |
|
140 | Seerah-7 | label:SetAnchor(TOPRIGHT) |
|
141 | Seerah-7 | else |
|
142 | Seerah-7 | control:SetDimensions(510, 30) |
|
143 | Seerah-7 | checkbox:SetDimensions(200, 26) |
|
144 | Seerah-7 | checkbox:SetAnchor(RIGHT) |
|
145 | Seerah-7 | label:SetAnchor(LEFT) |
|
146 | Seerah-7 | label:SetAnchor(RIGHT, checkbox, LEFT, -5, 0) |
|
147 | Seerah-7 | end |
|
148 | Seerah-7 | ||
149 | Seerah-7 | if checkboxData.warning then |
|
150 | Seerah-7 | control.warning = wm:CreateControlFromVirtual(nil, control, "ZO_Options_WarningIcon") |
|
151 | Seerah-7 | control.warning:SetAnchor(RIGHT, checkbox, LEFT, -5, 0) |
|
152 | 16 | Seerah-7 | --control.warning.tooltipText = checkboxData.warning |
153 | Seerah-7 | control.warning.data = {tooltipText = checkboxData.warning} |
|
154 | 14 | Seerah-7 | end |
155 | Seerah-7 | ||
156 | Seerah-7 | control.panel = parent.panel or parent --if this is in a submenu, panel is its parent |
|
157 | Seerah-7 | control.data = checkboxData |
|
158 | 16 | Seerah-7 | control.data.tooltipText = checkboxData.tooltip |
159 | 14 | Seerah-7 | |
160 | Seerah-7 | if checkboxData.disabled then |
|
161 | Seerah-7 | control.UpdateDisabled = UpdateDisabled |
|
162 | Seerah-7 | control:UpdateDisabled() |
|
163 | Seerah-7 | end |
|
164 | Seerah-7 | control.UpdateValue = UpdateValue |
|
165 | Seerah-7 | control:UpdateValue() |
|
166 | Seerah-7 | ||
167 | Seerah-7 | if control.panel.data.registerForRefresh or control.panel.data.registerForDefaults then --if our parent window wants to refresh controls, then add this to the list |
|
168 | Seerah-7 | tinsert(control.panel.controlsToRefresh, control) |
|
169 | Seerah-7 | end |
|
170 | Seerah-7 | ||
171 | Seerah-7 | return control |
|
172 | Seerah-7 | end |