annotate classes/Button.lua @ 213:8ba8ab8bf6dd

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