annotate classes/Button.lua @ 193:576c50e51fc5

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