ESOUI SVN LibAddonMenu

[/] [branches/] [LibAddonMenu-2.0/] [LibAddonMenu-2.0/] [controls/] [checkbox.lua] - Blame information for rev 32

Details | Compare with Previous | View Log

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