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