annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-CheckBox.lua @ 0:c6ff7ba0e8f6

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