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