annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-CheckBox.lua @ 48:a671a2cf52ee

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