comparison AceGUIWidget-CheckBoxSmallLabel.lua @ 1:822b6ca3ef89

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