1 --[[-----------------------------------------------------------------------------
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "CheckBox", 23
5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
9 local select, pairs = select, pairs
12 local PlaySound = PlaySound
13 local CreateFrame, UIParent = CreateFrame, UIParent
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
19 --[[-----------------------------------------------------------------------------
21 -------------------------------------------------------------------------------]]
22 local function AlignImage(self)
23 local img = self.image:GetTexture()
24 self.text:ClearAllPoints()
26 self.text:SetPoint("LEFT", self.checkbg, "RIGHT")
27 self.text:SetPoint("RIGHT")
29 self.text:SetPoint("LEFT", self.image,"RIGHT", 1, 0)
30 self.text:SetPoint("RIGHT")
34 --[[-----------------------------------------------------------------------------
36 -------------------------------------------------------------------------------]]
37 local function Control_OnEnter(frame)
38 frame.obj:Fire("OnEnter")
41 local function Control_OnLeave(frame)
42 frame.obj:Fire("OnLeave")
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)
51 self.text:SetPoint("LEFT", self.checkbg, "RIGHT", 1, -1)
57 local function CheckBox_OnMouseUp(frame)
58 local self = frame.obj
59 if not self.disabled then
63 PlaySound(PlaySoundKitID and "igMainMenuOptionCheckBoxOn" or 856) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON
64 else -- for both nil and false (tristate)
65 PlaySound(PlaySoundKitID and "igMainMenuOptionCheckBoxOff" or 857) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_OFF
68 self:Fire("OnValueChanged", self.checked)
73 --[[-----------------------------------------------------------------------------
75 -------------------------------------------------------------------------------]]
77 ["OnAcquire"] = function(self)
81 -- height is calculated from the width and required space for the description
85 self:SetDescription(nil)
88 -- ["OnRelease"] = nil,
90 ["OnWidthSet"] = function(self, width)
92 self.desc:SetWidth(width - 30)
93 if self.desc:GetText() and self.desc:GetText() ~= "" then
94 self:SetHeight(28 + self.desc:GetHeight())
99 ["SetDisabled"] = function(self, disabled)
100 self.disabled = disabled
103 self.text:SetTextColor(0.5, 0.5, 0.5)
104 SetDesaturation(self.check, true)
106 self.desc:SetTextColor(0.5, 0.5, 0.5)
110 self.text:SetTextColor(1, 1, 1)
111 if self.tristate and self.checked == nil then
112 SetDesaturation(self.check, true)
114 SetDesaturation(self.check, false)
117 self.desc:SetTextColor(1, 1, 1)
122 ["SetValue"] = function(self,value)
123 local check = self.check
126 SetDesaturation(self.check, false)
129 --Nil is the unknown tristate value
130 if self.tristate and value == nil then
131 SetDesaturation(self.check, true)
134 SetDesaturation(self.check, false)
138 self:SetDisabled(self.disabled)
141 ["GetValue"] = function(self)
145 ["SetTriState"] = function(self, enabled)
146 self.tristate = enabled
147 self:SetValue(self:GetValue())
150 ["SetType"] = function(self, type)
151 local checkbg = self.checkbg
152 local check = self.check
153 local highlight = self.highlight
156 if type == "radio" then
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)
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)
175 checkbg:SetHeight(size)
176 checkbg:SetWidth(size)
179 ["ToggleChecked"] = function(self)
180 local value = self:GetValue()
181 if self.tristate then
182 --cycle in true, nil, false order
185 elseif value == nil then
191 self:SetValue(not self:GetValue())
195 ["SetLabel"] = function(self, label)
196 self.text:SetText(label)
199 ["SetDescription"] = function(self, desc)
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")
211 --self.text:SetFontObject(GameFontNormal)
212 self.desc:SetText(desc)
213 self:SetHeight(28 + self.desc:GetHeight())
216 self.desc:SetText("")
219 --self.text:SetFontObject(GameFontHighlight)
224 ["SetImage"] = function(self, path, ...)
225 local image = self.image
226 image:SetTexture(path)
228 if image:GetTexture() then
229 local n = select("#", ...)
230 if n == 4 or n == 8 then
231 image:SetTexCoord(...)
233 image:SetTexCoord(0, 1, 0, 1)
240 --[[-----------------------------------------------------------------------------
242 -------------------------------------------------------------------------------]]
243 local function Constructor()
244 local frame = CreateFrame("Button", nil, UIParent)
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)
253 local checkbg = frame:CreateTexture(nil, "ARTWORK")
255 checkbg:SetHeight(24)
256 checkbg:SetPoint("TOPLEFT")
257 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
259 local check = frame:CreateTexture(nil, "OVERLAY")
260 check:SetAllPoints(checkbg)
261 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
263 local text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
264 text:SetJustifyH("LEFT")
266 text:SetPoint("LEFT", checkbg, "RIGHT")
267 text:SetPoint("RIGHT")
269 local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
270 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
271 highlight:SetBlendMode("ADD")
272 highlight:SetAllPoints(checkbg)
274 local image = frame:CreateTexture(nil, "OVERLAY")
277 image:SetPoint("LEFT", checkbg, "RIGHT", 1, 0)
283 highlight = highlight,
288 for method, func in pairs(methods) do
289 widget[method] = func
292 return AceGUI:RegisterAsWidget(widget)
295 AceGUI:RegisterWidgetType(Type, Constructor, Version)