comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-CheckBox.lua @ 0:98c6f55e6619

First commit
author Xiiph
date Sat, 05 Feb 2011 16:45:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:98c6f55e6619
1 --[[-----------------------------------------------------------------------------
2 Checkbox Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "CheckBox", 21
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 else
106 self.frame:Enable()
107 self.text:SetTextColor(1, 1, 1)
108 if self.tristate and self.checked == nil then
109 SetDesaturation(self.check, true)
110 else
111 SetDesaturation(self.check, false)
112 end
113 end
114 end,
115
116 ["SetValue"] = function(self,value)
117 local check = self.check
118 self.checked = value
119 if value then
120 SetDesaturation(self.check, false)
121 self.check:Show()
122 else
123 --Nil is the unknown tristate value
124 if self.tristate and value == nil then
125 SetDesaturation(self.check, true)
126 self.check:Show()
127 else
128 SetDesaturation(self.check, false)
129 self.check:Hide()
130 end
131 end
132 self:SetDisabled(self.disabled)
133 end,
134
135 ["GetValue"] = function(self)
136 return self.checked
137 end,
138
139 ["SetTriState"] = function(self, enabled)
140 self.tristate = enabled
141 self:SetValue(self:GetValue())
142 end,
143
144 ["SetType"] = function(self, type)
145 local checkbg = self.checkbg
146 local check = self.check
147 local highlight = self.highlight
148
149 local size
150 if type == "radio" then
151 size = 16
152 checkbg:SetTexture("Interface\\Buttons\\UI-RadioButton")
153 checkbg:SetTexCoord(0, 0.25, 0, 1)
154 check:SetTexture("Interface\\Buttons\\UI-RadioButton")
155 check:SetTexCoord(0.25, 0.5, 0, 1)
156 check:SetBlendMode("ADD")
157 highlight:SetTexture("Interface\\Buttons\\UI-RadioButton")
158 highlight:SetTexCoord(0.5, 0.75, 0, 1)
159 else
160 size = 24
161 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
162 checkbg:SetTexCoord(0, 1, 0, 1)
163 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
164 check:SetTexCoord(0, 1, 0, 1)
165 check:SetBlendMode("BLEND")
166 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
167 highlight:SetTexCoord(0, 1, 0, 1)
168 end
169 checkbg:SetHeight(size)
170 checkbg:SetWidth(size)
171 end,
172
173 ["ToggleChecked"] = function(self)
174 local value = self:GetValue()
175 if self.tristate then
176 --cycle in true, nil, false order
177 if value then
178 self:SetValue(nil)
179 elseif value == nil then
180 self:SetValue(false)
181 else
182 self:SetValue(true)
183 end
184 else
185 self:SetValue(not self:GetValue())
186 end
187 end,
188
189 ["SetLabel"] = function(self, label)
190 self.text:SetText(label)
191 end,
192
193 ["SetDescription"] = function(self, desc)
194 if desc then
195 if not self.desc then
196 local desc = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
197 desc:ClearAllPoints()
198 desc:SetPoint("TOPLEFT", self.checkbg, "TOPRIGHT", 5, -21)
199 desc:SetWidth(self.frame.width - 30)
200 desc:SetJustifyH("LEFT")
201 desc:SetJustifyV("TOP")
202 self.desc = desc
203 end
204 self.desc:Show()
205 --self.text:SetFontObject(GameFontNormal)
206 self.desc:SetText(desc)
207 self:SetHeight(28 + self.desc:GetHeight())
208 else
209 if self.desc then
210 self.desc:SetText("")
211 self.desc:Hide()
212 end
213 --self.text:SetFontObject(GameFontHighlight)
214 self:SetHeight(24)
215 end
216 end,
217
218 ["SetImage"] = function(self, path, ...)
219 local image = self.image
220 image:SetTexture(path)
221
222 if image:GetTexture() then
223 local n = select("#", ...)
224 if n == 4 or n == 8 then
225 image:SetTexCoord(...)
226 else
227 image:SetTexCoord(0, 1, 0, 1)
228 end
229 end
230 AlignImage(self)
231 end
232 }
233
234 --[[-----------------------------------------------------------------------------
235 Constructor
236 -------------------------------------------------------------------------------]]
237 local function Constructor()
238 local frame = CreateFrame("Button", nil, UIParent)
239 frame:Hide()
240
241 frame:EnableMouse(true)
242 frame:SetScript("OnEnter", Control_OnEnter)
243 frame:SetScript("OnLeave", Control_OnLeave)
244 frame:SetScript("OnMouseDown", CheckBox_OnMouseDown)
245 frame:SetScript("OnMouseUp", CheckBox_OnMouseUp)
246
247 local checkbg = frame:CreateTexture(nil, "ARTWORK")
248 checkbg:SetWidth(24)
249 checkbg:SetHeight(24)
250 checkbg:SetPoint("TOPLEFT")
251 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
252
253 local check = frame:CreateTexture(nil, "OVERLAY")
254 check:SetAllPoints(checkbg)
255 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
256
257 local text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
258 text:SetJustifyH("LEFT")
259 text:SetHeight(18)
260 text:SetPoint("LEFT", checkbg, "RIGHT")
261 text:SetPoint("RIGHT")
262
263 local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
264 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
265 highlight:SetBlendMode("ADD")
266 highlight:SetAllPoints(checkbg)
267
268 local image = frame:CreateTexture(nil, "OVERLAY")
269 image:SetHeight(16)
270 image:SetWidth(16)
271 image:SetPoint("LEFT", checkbg, "RIGHT", 1, 0)
272
273 local widget = {
274 checkbg = checkbg,
275 check = check,
276 text = text,
277 highlight = highlight,
278 image = image,
279 frame = frame,
280 type = Type
281 }
282 for method, func in pairs(methods) do
283 widget[method] = func
284 end
285
286 return AceGUI:RegisterAsWidget(widget)
287 end
288
289 AceGUI:RegisterWidgetType(Type, Constructor, Version)