annotate classes/Button.lua @ 129:28b430de5875

More fixes for new action button
author Flick <flickerstreak@gmail.com>
date Fri, 06 Mar 2009 23:44:33 +0000
parents 729232aeeb5e
children e39d80bb0b7a
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@129 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@129 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@126 79 local f = self:GetFrame()
flickerstreak@126 80 if f then
flickerstreak@126 81 f:Hide()
flickerstreak@126 82 f:SetParent(trash)
flickerstreak@126 83 f:ClearAllPoints()
flickerstreak@126 84 end
flickerstreak@122 85 end
flickerstreak@122 86
flickerstreak@128 87 function Button:GetBar()
flickerstreak@128 88 return self.bar
flickerstreak@128 89 end
flickerstreak@128 90
flickerstreak@122 91 function Button:GetFrame()
flickerstreak@122 92 return self.frame
flickerstreak@122 93 end
flickerstreak@122 94
flickerstreak@122 95 function Button:GetName()
flickerstreak@122 96 return self.name
flickerstreak@122 97 end
flickerstreak@122 98
flickerstreak@122 99 function Button:GetConfig()
flickerstreak@122 100 return self.config
flickerstreak@122 101 end
flickerstreak@122 102
flickerstreak@122 103 function Button:GetActionID()
flickerstreak@122 104 -- derived classes should override this
flickerstreak@122 105 return nil
flickerstreak@122 106 end
flickerstreak@122 107
flickerstreak@122 108 function Button:SetActionIDPool( poolID, maxID )
flickerstreak@122 109 self.actionPoolID = poolID
flickerstreak@122 110 self.actionMaxID = maxID
flickerstreak@122 111 end
flickerstreak@122 112
flickerstreak@122 113 function Button:AcquireActionID( id, hint, unique )
flickerstreak@122 114 local poolID = self.actionPoolID
flickerstreak@122 115 local maxID = self.actionMaxID
flickerstreak@122 116 if not poolID or not maxID then
flickerstreak@122 117 error("AcquireActionID: must setup pool first with SetActionIDPool")
flickerstreak@122 118 end
flickerstreak@123 119 local pool = idPools[poolID]
flickerstreak@122 120 if not pool then
flickerstreak@122 121 pool = { nWraps = 0, useCount = { } }
flickerstreak@122 122 for i = 1, maxID do
flickerstreak@122 123 pool.useCount[i] = 0
flickerstreak@122 124 end
flickerstreak@123 125 idPools[poolID] = pool
flickerstreak@122 126 end
flickerstreak@122 127 local useCount = pool.useCount
flickerstreak@122 128 if id == nil then
flickerstreak@122 129 repeat
flickerstreak@126 130 local nWraps = pool.nWraps or 0
flickerstreak@126 131 if hint and (useCount[hint] == nil or useCount[hint] == nWraps) then
flickerstreak@122 132 id = hint
flickerstreak@122 133 else
flickerstreak@122 134 local start = hint or 1
flickerstreak@122 135 for i = start, maxID do
flickerstreak@122 136 if useCount[i] == nil or useCount[i] == nWraps then
flickerstreak@122 137 id = i
flickerstreak@122 138 break
flickerstreak@122 139 end
flickerstreak@122 140 end
flickerstreak@122 141 if not id then
flickerstreak@122 142 for i = 1, start do
flickerstreak@122 143 if useCount[i] == nil or useCount[i] == nWraps then
flickerstreak@122 144 id = i
flickerstreak@122 145 break
flickerstreak@122 146 end
flickerstreak@122 147 end
flickerstreak@122 148 end
flickerstreak@122 149 end
flickerstreak@122 150 if id == nil then
flickerstreak@122 151 if unique then
flickerstreak@122 152 return nil
flickerstreak@122 153 end
flickerstreak@122 154 pool.nWraps = nWraps + 1
flickerstreak@122 155 end
flickerstreak@126 156 until id ~= nil
flickerstreak@122 157 end
flickerstreak@122 158 useCount[id] = (useCount[id] or 0) + 1
flickerstreak@122 159 return id
flickerstreak@122 160 end
flickerstreak@122 161
flickerstreak@122 162 function Button:ReleaseActionID( id )
flickerstreak@122 163 local poolID = self.actionPoolID
flickerstreak@122 164 if not poolID then
flickerstreak@122 165 error("ReleaseActionID: must setup pool first with SetActionIDPool")
flickerstreak@122 166 end
flickerstreak@123 167 local pool = idPools[poolID]
flickerstreak@122 168 if pool and id and pool.useCount[id] then
flickerstreak@122 169 pool.useCount[id] = pool.useCount[id] - 1
flickerstreak@122 170 pool.nWraps = min(pool.useCount[id], pool.nWraps)
flickerstreak@122 171 end
flickerstreak@122 172 end
flickerstreak@122 173
flickerstreak@122 174 function Button:Refresh()
flickerstreak@129 175 local f = self:GetFrame()
flickerstreak@129 176 self.bar:PlaceButton( self, f:GetWidth(), f:GetHeight() )
flickerstreak@122 177 end
flickerstreak@122 178
flickerstreak@122 179 function Button:SetKeybindMode( mode )
flickerstreak@122 180 local f = self.frame
flickerstreak@122 181 if mode then
flickerstreak@122 182 self.oldOnEnter = f:GetScript("OnEnter")
flickerstreak@122 183 f:SetScript("OnEnter", kb_onEnter)
flickerstreak@124 184 elseif self.oldOnEnter then
flickerstreak@122 185 f:SetScript("OnEnter", self.oldOnEnter)
flickerstreak@122 186 self.oldOnEnter = nil
flickerstreak@122 187 end
flickerstreak@128 188 self:ShowGridTemp(mode)
flickerstreak@122 189 self:UpdateKeybindModeDisplay( mode )
flickerstreak@122 190 end
flickerstreak@122 191
flickerstreak@122 192 function Button:UpdateKeybindModeDisplay( mode )
flickerstreak@122 193 self.border = self.border or _G[format("%sBorder",tostring(self:GetName()))]
flickerstreak@122 194 if self.border then
flickerstreak@122 195 if mode then
flickerstreak@122 196 self.border:SetVertexColor(KB:GetColorKeyBoundMode())
flickerstreak@122 197 self.border:Show()
flickerstreak@122 198 else
flickerstreak@122 199 self.border:Hide()
flickerstreak@122 200 end
flickerstreak@122 201 end
flickerstreak@122 202 end
flickerstreak@122 203
flickerstreak@122 204 function Button:UpdateHotkey( hotkey )
flickerstreak@122 205 hotkey = hotkey or self.hotkey
flickerstreak@122 206 if not hotkey then
flickerstreak@129 207 hotkey = _G[self:GetName().."HotKey"]
flickerstreak@122 208 self.hotkey = hotkey
flickerstreak@122 209 end
flickerstreak@122 210 if hotkey then
flickerstreak@122 211 local txt = self.frame:GetHotkey()
flickerstreak@122 212 hotkey:SetText( txt )
flickerstreak@122 213 if txt == nil or txt == "" then
flickerstreak@122 214 hotkey:Hide()
flickerstreak@122 215 else
flickerstreak@122 216 hotkey:Show()
flickerstreak@122 217 end
flickerstreak@122 218 end
flickerstreak@122 219 end
flickerstreak@122 220
flickerstreak@122 221 function Button:GetActionIDLabel( create )
flickerstreak@129 222 local f = self:GetFrame()
flickerstreak@122 223 if not f.actionIDLabel and create then
flickerstreak@122 224 local label = f:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@122 225 label:SetAllPoints()
flickerstreak@122 226 label:SetJustifyH("CENTER")
flickerstreak@122 227 label:SetShadowColor(0,0,0,1)
flickerstreak@122 228 label:SetShadowOffset(2,-2)
flickerstreak@122 229 f.actionIDLabel = label -- store the label with the frame for recycling
flickerstreak@122 230 end
flickerstreak@122 231 return f.actionIDLabel
flickerstreak@122 232 end
flickerstreak@122 233
flickerstreak@122 234 function Button:UpdateActionIDLabel( show )
flickerstreak@122 235 local label = self:GetActionIDLabel( show )
flickerstreak@122 236 if label then
flickerstreak@122 237 if show then
flickerstreak@122 238 local id = self:GetActionID()
flickerstreak@122 239 if id then
flickerstreak@122 240 label:SetText(tostring(id))
flickerstreak@122 241 label:Show()
flickerstreak@122 242 return
flickerstreak@122 243 end
flickerstreak@122 244 end
flickerstreak@122 245 label:Hide()
flickerstreak@122 246 end
flickerstreak@122 247 end
flickerstreak@122 248
flickerstreak@122 249 function Button:SetNormalVertexColor( r, g, b, a )
flickerstreak@122 250 if LBF then
flickerstreak@129 251 LBF:SetNormalVertexColor(self:GetFrame(), r, g, b, a)
flickerstreak@122 252 else
flickerstreak@129 253 self:GetFrame():GetNormalTexture():SetVertexColor(r,g,b,a)
flickerstreak@122 254 end
flickerstreak@122 255 end
flickerstreak@122 256
flickerstreak@122 257 function Button:GetNormalVertexColor()
flickerstreak@122 258 if LBF then
flickerstreak@129 259 return LBF:GetNormalVertexColor(self:GetFrame())
flickerstreak@122 260 else
flickerstreak@129 261 return self:GetFrame():GetNormalTexture():GetVertexColor()
flickerstreak@122 262 end
flickerstreak@122 263 end
flickerstreak@122 264
flickerstreak@122 265 function Button:UpdateShowGrid()
flickerstreak@122 266 -- does nothing by default
flickerstreak@122 267 end
flickerstreak@128 268
flickerstreak@128 269 function Button:ShowGridTemp(show)
flickerstreak@128 270 -- does nothing by default
flickerstreak@128 271 end
flickerstreak@128 272
flickerstreak@128 273 function Button:ShowGrid(show)
flickerstreak@128 274 -- does nothing by default
flickerstreak@128 275 end