annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-CheckBox.lua @ 25:ac501d71c890 tip

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