comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-CheckBox.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children e31b02b24488
comparison
equal deleted inserted replaced
56:75431c084aa0 57:01b63b8ed811
1 --[[-----------------------------------------------------------------------------
2 Checkbox Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "CheckBox", 22
5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
7
8 -- Lua APIs
9 local select, pairs = select, pairs
10
11 -- WoW APIs
12 local PlaySound = PlaySound
13 local CreateFrame, UIParent = CreateFrame, UIParent
14
15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
16 -- List them here for Mikk's FindGlobals script
17 -- GLOBALS: SetDesaturation, GameFontHighlight
18
19 --[[-----------------------------------------------------------------------------
20 Support functions
21 -------------------------------------------------------------------------------]]
22 local function AlignImage(self)
23 local img = self.image:GetTexture()
24 self.text:ClearAllPoints()
25 if not img then
26 self.text:SetPoint("LEFT", self.checkbg, "RIGHT")
27 self.text:SetPoint("RIGHT")
28 else
29 self.text:SetPoint("LEFT", self.image,"RIGHT", 1, 0)
30 self.text:SetPoint("RIGHT")
31 end
32 end
33
34 --[[-----------------------------------------------------------------------------
35 Scripts
36 -------------------------------------------------------------------------------]]
37 local function Control_OnEnter(frame)
38 frame.obj:Fire("OnEnter")
39 end
40
41 local function Control_OnLeave(frame)
42 frame.obj:Fire("OnLeave")
43 end
44
45 local function CheckBox_OnMouseDown(frame)
46 local self = frame.obj
47 if not self.disabled then
48 if self.image:GetTexture() then
49 self.text:SetPoint("LEFT", self.image,"RIGHT", 2, -1)
50 else
51 self.text:SetPoint("LEFT", self.checkbg, "RIGHT", 1, -1)
52 end
53 end
54 AceGUI:ClearFocus()
55 end
56
57 local function CheckBox_OnMouseUp(frame)
58 local self = frame.obj
59 if not self.disabled then
60 self:ToggleChecked()
61
62 if self.checked then
63 PlaySound("igMainMenuOptionCheckBoxOn")
64 else -- for both nil and false (tristate)
65 PlaySound("igMainMenuOptionCheckBoxOff")
66 end
67
68 self:Fire("OnValueChanged", self.checked)
69 AlignImage(self)
70 end
71 end
72
73 --[[-----------------------------------------------------------------------------
74 Methods
75 -------------------------------------------------------------------------------]]
76 local methods = {
77 ["OnAcquire"] = function(self)
78 self:SetType()
79 self:SetValue(false)
80 self:SetTriState(nil)
81 -- height is calculated from the width and required space for the description
82 self:SetWidth(200)
83 self:SetImage()
84 self:SetDisabled(nil)
85 self:SetDescription(nil)
86 end,
87
88 -- ["OnRelease"] = nil,
89
90 ["OnWidthSet"] = function(self, width)
91 if self.desc then
92 self.desc:SetWidth(width - 30)
93 if self.desc:GetText() and self.desc:GetText() ~= "" then
94 self:SetHeight(28 + self.desc:GetHeight())
95 end
96 end
97 end,
98
99 ["SetDisabled"] = function(self, disabled)
100 self.disabled = disabled
101 if disabled then
102 self.frame:Disable()
103 self.text:SetTextColor(0.5, 0.5, 0.5)
104 SetDesaturation(self.check, true)
105 if self.desc then
106 self.desc:SetTextColor(0.5, 0.5, 0.5)
107 end
108 else
109 self.frame:Enable()
110 self.text:SetTextColor(1, 1, 1)
111 if self.tristate and self.checked == nil then
112 SetDesaturation(self.check, true)
113 else
114 SetDesaturation(self.check, false)
115 end
116 if self.desc then
117 self.desc:SetTextColor(1, 1, 1)
118 end
119 end
120 end,
121
122 ["SetValue"] = function(self,value)
123 local check = self.check
124 self.checked = value
125 if value then
126 SetDesaturation(self.check, false)
127 self.check:Show()
128 else
129 --Nil is the unknown tristate value
130 if self.tristate and value == nil then
131 SetDesaturation(self.check, true)
132 self.check:Show()
133 else
134 SetDesaturation(self.check, false)
135 self.check:Hide()
136 end
137 end
138 self:SetDisabled(self.disabled)
139 end,
140
141 ["GetValue"] = function(self)
142 return self.checked
143 end,
144
145 ["SetTriState"] = function(self, enabled)
146 self.tristate = enabled
147 self:SetValue(self:GetValue())
148 end,
149
150 ["SetType"] = function(self, type)
151 local checkbg = self.checkbg
152 local check = self.check
153 local highlight = self.highlight
154
155 local size
156 if type == "radio" then
157 size = 16
158 checkbg:SetTexture("Interface\\Buttons\\UI-RadioButton")
159 checkbg:SetTexCoord(0, 0.25, 0, 1)
160 check:SetTexture("Interface\\Buttons\\UI-RadioButton")
161 check:SetTexCoord(0.25, 0.5, 0, 1)
162 check:SetBlendMode("ADD")
163 highlight:SetTexture("Interface\\Buttons\\UI-RadioButton")
164 highlight:SetTexCoord(0.5, 0.75, 0, 1)
165 else
166 size = 24
167 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
168 checkbg:SetTexCoord(0, 1, 0, 1)
169 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
170 check:SetTexCoord(0, 1, 0, 1)
171 check:SetBlendMode("BLEND")
172 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
173 highlight:SetTexCoord(0, 1, 0, 1)
174 end
175 checkbg:SetHeight(size)
176 checkbg:SetWidth(size)
177 end,
178
179 ["ToggleChecked"] = function(self)
180 local value = self:GetValue()
181 if self.tristate then
182 --cycle in true, nil, false order
183 if value then
184 self:SetValue(nil)
185 elseif value == nil then
186 self:SetValue(false)
187 else
188 self:SetValue(true)
189 end
190 else
191 self:SetValue(not self:GetValue())
192 end
193 end,
194
195 ["SetLabel"] = function(self, label)
196 self.text:SetText(label)
197 end,
198
199 ["SetDescription"] = function(self, desc)
200 if desc then
201 if not self.desc then
202 local desc = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
203 desc:ClearAllPoints()
204 desc:SetPoint("TOPLEFT", self.checkbg, "TOPRIGHT", 5, -21)
205 desc:SetWidth(self.frame.width - 30)
206 desc:SetJustifyH("LEFT")
207 desc:SetJustifyV("TOP")
208 self.desc = desc
209 end
210 self.desc:Show()
211 --self.text:SetFontObject(GameFontNormal)
212 self.desc:SetText(desc)
213 self:SetHeight(28 + self.desc:GetHeight())
214 else
215 if self.desc then
216 self.desc:SetText("")
217 self.desc:Hide()
218 end
219 --self.text:SetFontObject(GameFontHighlight)
220 self:SetHeight(24)
221 end
222 end,
223
224 ["SetImage"] = function(self, path, ...)
225 local image = self.image
226 image:SetTexture(path)
227
228 if image:GetTexture() then
229 local n = select("#", ...)
230 if n == 4 or n == 8 then
231 image:SetTexCoord(...)
232 else
233 image:SetTexCoord(0, 1, 0, 1)
234 end
235 end
236 AlignImage(self)
237 end
238 }
239
240 --[[-----------------------------------------------------------------------------
241 Constructor
242 -------------------------------------------------------------------------------]]
243 local function Constructor()
244 local frame = CreateFrame("Button", nil, UIParent)
245 frame:Hide()
246
247 frame:EnableMouse(true)
248 frame:SetScript("OnEnter", Control_OnEnter)
249 frame:SetScript("OnLeave", Control_OnLeave)
250 frame:SetScript("OnMouseDown", CheckBox_OnMouseDown)
251 frame:SetScript("OnMouseUp", CheckBox_OnMouseUp)
252
253 local checkbg = frame:CreateTexture(nil, "ARTWORK")
254 checkbg:SetWidth(24)
255 checkbg:SetHeight(24)
256 checkbg:SetPoint("TOPLEFT")
257 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
258
259 local check = frame:CreateTexture(nil, "OVERLAY")
260 check:SetAllPoints(checkbg)
261 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
262
263 local text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
264 text:SetJustifyH("LEFT")
265 text:SetHeight(18)
266 text:SetPoint("LEFT", checkbg, "RIGHT")
267 text:SetPoint("RIGHT")
268
269 local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
270 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
271 highlight:SetBlendMode("ADD")
272 highlight:SetAllPoints(checkbg)
273
274 local image = frame:CreateTexture(nil, "OVERLAY")
275 image:SetHeight(16)
276 image:SetWidth(16)
277 image:SetPoint("LEFT", checkbg, "RIGHT", 1, 0)
278
279 local widget = {
280 checkbg = checkbg,
281 check = check,
282 text = text,
283 highlight = highlight,
284 image = image,
285 frame = frame,
286 type = Type
287 }
288 for method, func in pairs(methods) do
289 widget[method] = func
290 end
291
292 return AceGUI:RegisterAsWidget(widget)
293 end
294
295 AceGUI:RegisterWidgetType(Type, Constructor, Version)