annotate classes/Button.lua @ 123:943eed2c7def

Fixes for new ActionButton implementation
author Flick <flickerstreak@gmail.com>
date Mon, 23 Feb 2009 19:41:11 +0000
parents a2d2f23137c8
children 0c5017f6062d
rev   line source
flickerstreak@122 1 --[[
flickerstreak@122 2 ReAction Button base class
flickerstreak@122 3 --]]
flickerstreak@122 4
flickerstreak@122 5 -- local imports
flickerstreak@122 6 local ReAction = ReAction
flickerstreak@122 7 local L = ReAction.L
flickerstreak@122 8 local _G = _G
flickerstreak@122 9 local CreateFrame = CreateFrame
flickerstreak@122 10 local GetBindingKey = GetBindingKey
flickerstreak@122 11 local format = string.format
flickerstreak@122 12
flickerstreak@122 13 ReAction:UpdateRevision("$Revision: 154 $")
flickerstreak@122 14
flickerstreak@122 15 -- libraries
flickerstreak@122 16 local KB = LibStub("LibKeyBound-1.0")
flickerstreak@122 17 local LBF = LibStub("LibButtonFacade",true) -- optional
flickerstreak@122 18
flickerstreak@122 19 -- private
flickerstreak@122 20 local trash = CreateFrame("Frame")
flickerstreak@122 21 local frameList = { }
flickerstreak@122 22 local idPools = { }
flickerstreak@122 23
flickerstreak@122 24 local function kb_onEnter( frame )
flickerstreak@122 25 KB:Set(frame)
flickerstreak@122 26 end
flickerstreak@122 27
flickerstreak@122 28 -- Button class
flickerstreak@122 29 local Button = { }
flickerstreak@122 30
flickerstreak@122 31 ReAction.Button = Button -- export to ReAction
flickerstreak@122 32
flickerstreak@122 33 function Button:New( name, config, bar, idx, inherits, buttonType )
flickerstreak@122 34 buttonType = buttonType or "CheckButton"
flickerstreak@122 35
flickerstreak@122 36 -- create new self
flickerstreak@122 37 self = setmetatable(
flickerstreak@122 38 {
flickerstreak@122 39 bar = bar,
flickerstreak@122 40 idx = idx,
flickerstreak@122 41 config = config,
flickerstreak@122 42 name = name,
flickerstreak@122 43 },
flickerstreak@122 44 { __index = self } )
flickerstreak@122 45
flickerstreak@122 46 -- have to recycle frames with the same name: CreateFrame() doesn't overwrite
flickerstreak@122 47 -- existing globals. Can't set to nil in the global because it's then tainted.
flickerstreak@122 48 -- Caller is responsible for ensuring global uniqueness of names.
flickerstreak@122 49 local f = name and frameList[name]
flickerstreak@122 50 if f then
flickerstreak@122 51 f:SetParent(bar:GetFrame())
flickerstreak@122 52 else
flickerstreak@122 53 f = CreateFrame(buttonType, name, bar:GetFrame(), inherits)
flickerstreak@122 54 if name then
flickerstreak@122 55 frameList[name] = f
flickerstreak@122 56 end
flickerstreak@122 57 end
flickerstreak@122 58
flickerstreak@122 59 self.frame = f
flickerstreak@122 60
flickerstreak@122 61 if config then
flickerstreak@122 62 config.name = name
flickerstreak@122 63 end
flickerstreak@122 64
flickerstreak@122 65 -- install LibKeyBound handlers onto frame
flickerstreak@122 66 function f:GetActionName()
flickerstreak@122 67 return format("%s:%s", bar:GetName(), idx)
flickerstreak@122 68 end
flickerstreak@122 69
flickerstreak@122 70 local clickBinding = format("CLICK %s:LeftButton", name)
flickerstreak@122 71 function f:GetHotkey()
flickerstreak@122 72 return KB:ToShortKey(GetBindingKey(clickBinding))
flickerstreak@122 73 end
flickerstreak@122 74
flickerstreak@122 75 return self
flickerstreak@122 76 end
flickerstreak@122 77
flickerstreak@122 78 function Button:Destroy()
flickerstreak@122 79 local f = self.frame
flickerstreak@122 80 gridProxy:RemoveGridFrame(f)
flickerstreak@122 81 f:Hide()
flickerstreak@122 82 f:SetParent(trash)
flickerstreak@122 83 f:ClearAllPoints()
flickerstreak@122 84 end
flickerstreak@122 85
flickerstreak@122 86 function Button:GetFrame()
flickerstreak@122 87 return self.frame
flickerstreak@122 88 end
flickerstreak@122 89
flickerstreak@122 90 function Button:GetName()
flickerstreak@122 91 return self.name
flickerstreak@122 92 end
flickerstreak@122 93
flickerstreak@122 94 function Button:GetConfig()
flickerstreak@122 95 return self.config
flickerstreak@122 96 end
flickerstreak@122 97
flickerstreak@122 98 function Button:GetActionID()
flickerstreak@122 99 -- derived classes should override this
flickerstreak@122 100 return nil
flickerstreak@122 101 end
flickerstreak@122 102
flickerstreak@122 103 function Button:SetActionIDPool( poolID, maxID )
flickerstreak@122 104 self.actionPoolID = poolID
flickerstreak@122 105 self.actionMaxID = maxID
flickerstreak@122 106 end
flickerstreak@122 107
flickerstreak@122 108 function Button:AcquireActionID( id, hint, unique )
flickerstreak@122 109 local poolID = self.actionPoolID
flickerstreak@122 110 local maxID = self.actionMaxID
flickerstreak@122 111 if not poolID or not maxID then
flickerstreak@122 112 error("AcquireActionID: must setup pool first with SetActionIDPool")
flickerstreak@122 113 end
flickerstreak@123 114 local pool = idPools[poolID]
flickerstreak@122 115 if not pool then
flickerstreak@122 116 pool = { nWraps = 0, useCount = { } }
flickerstreak@122 117 for i = 1, maxID do
flickerstreak@122 118 pool.useCount[i] = 0
flickerstreak@122 119 end
flickerstreak@123 120 idPools[poolID] = pool
flickerstreak@122 121 end
flickerstreak@122 122 local useCount = pool.useCount
flickerstreak@122 123 if id == nil then
flickerstreak@122 124 repeat
flickerstreak@122 125 local nWraps = pool.nWraps
flickerstreak@122 126 if useCount[hint] == nil or useCount[hint] == nWraps then
flickerstreak@122 127 id = hint
flickerstreak@122 128 else
flickerstreak@122 129 local start = hint or 1
flickerstreak@122 130 for i = start, maxID do
flickerstreak@122 131 if useCount[i] == nil or useCount[i] == nWraps then
flickerstreak@122 132 id = i
flickerstreak@122 133 break
flickerstreak@122 134 end
flickerstreak@122 135 end
flickerstreak@122 136 if not id then
flickerstreak@122 137 for i = 1, start do
flickerstreak@122 138 if useCount[i] == nil or useCount[i] == nWraps then
flickerstreak@122 139 id = i
flickerstreak@122 140 break
flickerstreak@122 141 end
flickerstreak@122 142 end
flickerstreak@122 143 end
flickerstreak@122 144 end
flickerstreak@122 145 if id == nil then
flickerstreak@122 146 if unique then
flickerstreak@122 147 return nil
flickerstreak@122 148 end
flickerstreak@122 149 pool.nWraps = nWraps + 1
flickerstreak@122 150 end
flickerstreak@122 151 until id
flickerstreak@122 152 end
flickerstreak@122 153 useCount[id] = (useCount[id] or 0) + 1
flickerstreak@122 154 return id
flickerstreak@122 155 end
flickerstreak@122 156
flickerstreak@122 157 function Button:ReleaseActionID( id )
flickerstreak@122 158 local poolID = self.actionPoolID
flickerstreak@122 159 if not poolID then
flickerstreak@122 160 error("ReleaseActionID: must setup pool first with SetActionIDPool")
flickerstreak@122 161 end
flickerstreak@123 162 local pool = idPools[poolID]
flickerstreak@122 163 if pool and id and pool.useCount[id] then
flickerstreak@122 164 pool.useCount[id] = pool.useCount[id] - 1
flickerstreak@122 165 pool.nWraps = min(pool.useCount[id], pool.nWraps)
flickerstreak@122 166 end
flickerstreak@122 167 end
flickerstreak@122 168
flickerstreak@122 169 function Button:Refresh()
flickerstreak@122 170 self.bar:PlaceButton( self, self.frame:GetWidth(), self.frame:GetHeight() )
flickerstreak@122 171 end
flickerstreak@122 172
flickerstreak@122 173 function Button:SetKeybindMode( mode )
flickerstreak@122 174 local f = self.frame
flickerstreak@122 175 if mode then
flickerstreak@122 176 self.oldOnEnter = f:GetScript("OnEnter")
flickerstreak@122 177 f:SetScript("OnEnter", kb_onEnter)
flickerstreak@122 178 else
flickerstreak@122 179 f:SetScript("OnEnter", self.oldOnEnter)
flickerstreak@122 180 self.oldOnEnter = nil
flickerstreak@122 181 end
flickerstreak@122 182 self:UpdateKeybindModeDisplay( mode )
flickerstreak@122 183 end
flickerstreak@122 184
flickerstreak@122 185 function Button:UpdateKeybindModeDisplay( mode )
flickerstreak@122 186 self.border = self.border or _G[format("%sBorder",tostring(self:GetName()))]
flickerstreak@122 187 if self.border then
flickerstreak@122 188 if mode then
flickerstreak@122 189 self.border:SetVertexColor(KB:GetColorKeyBoundMode())
flickerstreak@122 190 self.border:Show()
flickerstreak@122 191 else
flickerstreak@122 192 self.border:Hide()
flickerstreak@122 193 end
flickerstreak@122 194 end
flickerstreak@122 195 end
flickerstreak@122 196
flickerstreak@122 197 function Button:UpdateHotkey( hotkey )
flickerstreak@122 198 hotkey = hotkey or self.hotkey
flickerstreak@122 199 if not hotkey then
flickerstreak@122 200 hotkey = _G[format("%sHotKey", tostring(self:GetName()))]
flickerstreak@122 201 self.hotkey = hotkey
flickerstreak@122 202 end
flickerstreak@122 203 if hotkey then
flickerstreak@122 204 local txt = self.frame:GetHotkey()
flickerstreak@122 205 hotkey:SetText( txt )
flickerstreak@122 206 if txt == nil or txt == "" then
flickerstreak@122 207 hotkey:Hide()
flickerstreak@122 208 else
flickerstreak@122 209 hotkey:Show()
flickerstreak@122 210 end
flickerstreak@122 211 end
flickerstreak@122 212 end
flickerstreak@122 213
flickerstreak@122 214 function Button:GetActionIDLabel( create )
flickerstreak@122 215 local f = self.frame
flickerstreak@122 216 if not f.actionIDLabel and create then
flickerstreak@122 217 local label = f:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@122 218 label:SetAllPoints()
flickerstreak@122 219 label:SetJustifyH("CENTER")
flickerstreak@122 220 label:SetShadowColor(0,0,0,1)
flickerstreak@122 221 label:SetShadowOffset(2,-2)
flickerstreak@122 222 f.actionIDLabel = label -- store the label with the frame for recycling
flickerstreak@122 223 end
flickerstreak@122 224 return f.actionIDLabel
flickerstreak@122 225 end
flickerstreak@122 226
flickerstreak@122 227 function Button:UpdateActionIDLabel( show )
flickerstreak@122 228 local label = self:GetActionIDLabel( show )
flickerstreak@122 229 if label then
flickerstreak@122 230 if show then
flickerstreak@122 231 local id = self:GetActionID()
flickerstreak@122 232 if id then
flickerstreak@122 233 label:SetText(tostring(id))
flickerstreak@122 234 label:Show()
flickerstreak@122 235 return
flickerstreak@122 236 end
flickerstreak@122 237 end
flickerstreak@122 238 label:Hide()
flickerstreak@122 239 end
flickerstreak@122 240 end
flickerstreak@122 241
flickerstreak@122 242 function Button:SetNormalVertexColor( r, g, b, a )
flickerstreak@122 243 if LBF then
flickerstreak@122 244 LBF:SetNormalVertexColor(self.frame, r, g, b, a)
flickerstreak@122 245 else
flickerstreak@122 246 self.frame:GetNormalTexture():SetVertexColor(r,g,b,a)
flickerstreak@122 247 end
flickerstreak@122 248 end
flickerstreak@122 249
flickerstreak@122 250 function Button:GetNormalVertexColor()
flickerstreak@122 251 if LBF then
flickerstreak@122 252 return LBF:GetNormalVertexColor(self.frame)
flickerstreak@122 253 else
flickerstreak@122 254 return self.frame:GetNormalTexture():GetVertexColor()
flickerstreak@122 255 end
flickerstreak@122 256 end
flickerstreak@122 257
flickerstreak@122 258 function Button:ShowGrid( show )
flickerstreak@122 259 if not InCombatLockdown() then
flickerstreak@122 260 local f = self.frame
flickerstreak@122 261 local count = f:GetAttribute("showgrid")
flickerstreak@122 262 if show then
flickerstreak@122 263 count = count + 1
flickerstreak@122 264 else
flickerstreak@122 265 count = count - 1
flickerstreak@122 266 end
flickerstreak@122 267 if count < 0 then
flickerstreak@122 268 count = 0
flickerstreak@122 269 end
flickerstreak@122 270 f:SetAttribute("showgrid",count)
flickerstreak@122 271 self:UpdateShowGrid()
flickerstreak@122 272 end
flickerstreak@122 273 end
flickerstreak@122 274
flickerstreak@122 275 function Button:UpdateShowGrid()
flickerstreak@122 276 -- does nothing by default
flickerstreak@122 277 end