forked from phuein/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.lua
More file actions
142 lines (118 loc) · 5.28 KB
/
Copy pathcheckbox.lua
File metadata and controls
142 lines (118 loc) · 5.28 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
--[[checkboxData = {
type = "checkbox",
name = "My Checkbox", -- or string id or function returning a string
getFunc = function() return db.var end,
setFunc = function(value) db.var = value doStuff() end,
tooltip = "Checkbox's tooltip text.", -- or string id or function returning a string (optional)
width = "full", -- or "half" (optional)
disabled = function() return db.someBooleanSetting end, --or boolean (optional)
warning = "May cause permanent awesomeness.", -- or string id or function returning a string (optional)
requiresReload = false, -- boolean, if set to true, the warning text will contain a notice that changes are only applied after an UI reload and any change to the value will make the "Apply Settings" button appear on the panel which will reload the UI when pressed (optional)
default = defaults.var, -- a boolean or function that returns a boolean (optional)
reference = "MyAddonCheckbox", -- unique global reference to control (optional)
} ]]
local widgetVersion = 14
local LAM = LibStub("LibAddonMenu-2.0")
if not LAM:RegisterWidget("checkbox", widgetVersion) then return end
local wm = WINDOW_MANAGER
--label
local enabledColor = ZO_DEFAULT_ENABLED_COLOR
local enabledHLcolor = ZO_HIGHLIGHT_TEXT
local disabledColor = ZO_DEFAULT_DISABLED_COLOR
local disabledHLcolor = ZO_DEFAULT_DISABLED_MOUSEOVER_COLOR
--checkbox
local checkboxColor = ZO_NORMAL_TEXT
local checkboxHLcolor = ZO_HIGHLIGHT_TEXT
local function UpdateDisabled(control)
local disable
if type(control.data.disabled) == "function" then
disable = control.data.disabled()
else
disable = control.data.disabled
end
control.label:SetColor((disable and ZO_DEFAULT_DISABLED_COLOR or control.value and ZO_DEFAULT_ENABLED_COLOR or ZO_DEFAULT_DISABLED_COLOR):UnpackRGBA())
control.checkbox:SetColor((disable and ZO_DEFAULT_DISABLED_COLOR or ZO_NORMAL_TEXT):UnpackRGBA())
--control:SetMouseEnabled(not disable)
--control:SetMouseEnabled(true)
control.isDisabled = disable
end
local function ToggleCheckbox(control)
if control.value then
control.label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA())
control.checkbox:SetText(control.checkedText)
else
control.label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA())
control.checkbox:SetText(control.uncheckedText)
end
end
local function UpdateValue(control, forceDefault, value)
if forceDefault then --if we are forcing defaults
value = LAM.util.GetDefaultValue(control.data.default)
control.data.setFunc(value)
elseif value ~= nil then --our value could be false
control.data.setFunc(value)
--after setting this value, let's refresh the others to see if any should be disabled or have their settings changed
LAM.util.RequestRefreshIfNeeded(control)
else
value = control.data.getFunc()
end
control.value = value
ToggleCheckbox(control)
end
local function OnMouseEnter(control)
ZO_Options_OnMouseEnter(control)
if control.isDisabled then return end
local label = control.label
if control.value then
label:SetColor(ZO_HIGHLIGHT_TEXT:UnpackRGBA())
else
label:SetColor(ZO_DEFAULT_DISABLED_MOUSEOVER_COLOR:UnpackRGBA())
end
control.checkbox:SetColor(ZO_HIGHLIGHT_TEXT:UnpackRGBA())
end
local function OnMouseExit(control)
ZO_Options_OnMouseExit(control)
if control.isDisabled then return end
local label = control.label
if control.value then
label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA())
else
label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA())
end
control.checkbox:SetColor(ZO_NORMAL_TEXT:UnpackRGBA())
end
--controlName is optional
function LAMCreateControl.checkbox(parent, checkboxData, controlName)
local control = LAM.util.CreateLabelAndContainerControl(parent, checkboxData, controlName)
control:SetHandler("OnMouseEnter", OnMouseEnter)
control:SetHandler("OnMouseExit", OnMouseExit)
control:SetHandler("OnMouseUp", function(control)
if control.isDisabled then return end
PlaySound(SOUNDS.DEFAULT_CLICK)
control.value = not control.value
control:UpdateValue(false, control.value)
end)
control.checkbox = wm:CreateControl(nil, control.container, CT_LABEL)
local checkbox = control.checkbox
checkbox:SetAnchor(LEFT, control.container, LEFT, 0, 0)
checkbox:SetFont("ZoFontGameBold")
checkbox:SetColor(ZO_NORMAL_TEXT:UnpackRGBA())
control.checkedText = GetString(SI_CHECK_BUTTON_ON):upper()
control.uncheckedText = GetString(SI_CHECK_BUTTON_OFF):upper()
if checkboxData.warning ~= nil or checkboxData.requiresReload then
control.warning = wm:CreateControlFromVirtual(nil, control, "ZO_Options_WarningIcon")
control.warning:SetAnchor(RIGHT, checkbox, LEFT, -5, 0)
control.UpdateWarning = LAM.util.UpdateWarning
control:UpdateWarning()
end
control.data.tooltipText = LAM.util.GetStringFromValue(checkboxData.tooltip)
control.UpdateValue = UpdateValue
control:UpdateValue()
if checkboxData.disabled ~= nil then
control.UpdateDisabled = UpdateDisabled
control:UpdateDisabled()
end
LAM.util.RegisterForRefreshIfNeeded(control)
LAM.util.RegisterForReloadIfNeeded(control)
return control
end