annotate classes/ReAction.lua @ 10:f3a7bfebc283

Version 0.33
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:37:38 +0000
parents c05fd3e18b4f
children 90bf38d48efd
rev   line source
flickerstreak@7 1 --
flickerstreak@7 2 -- ReAction is a base class for action button management. It provides a
flickerstreak@7 3 -- framework for button setup, placement, recycling, keybinding, etc.
flickerstreak@7 4 -- It does not define any layout or actual action functionality,
flickerstreak@7 5 -- which is deferred to derived classes.
flickerstreak@7 6 --
flickerstreak@7 7 -- ReAction implements the ReBar.IButton interface. It is designed to be used with ReBar
flickerstreak@7 8 -- for grouping and laying out buttons.
flickerstreak@7 9 --
flickerstreak@7 10 -- Each instance of a ReAction-derived object is associated with a single action
flickerstreak@7 11 -- button frame, which may or may not have a one-to-one mapping with actionID.
flickerstreak@7 12 --
flickerstreak@7 13 -- ReAction makes use of a configuration structure, which can (and should) be
flickerstreak@7 14 -- extended by implementations. A single config structure is shared amongst all
flickerstreak@7 15 -- ReAction class instances within a single ReBar group, so the structure should
flickerstreak@7 16 -- contain sub-tables for any property that needs to be defined on a per-button
flickerstreak@7 17 -- basis. Each button is passed a 'barIdx' parameter to be used as an index into
flickerstreak@7 18 -- such tables.
flickerstreak@7 19 --
flickerstreak@7 20 -- The base config structure is as follows:
flickerstreak@7 21 --
flickerstreak@7 22 -- config = {
flickerstreak@7 23 -- type = "ReAction", -- static string (used by ReBar)
flickerstreak@7 24 -- subtype = "string", -- ReAction implementation identifier (index into ReAction.buttonTypes)
flickerstreak@7 25 -- ids = { {paged list}, {paged list}, ... } -- indexed by self.barIdx
flickerstreak@7 26 -- }
flickerstreak@7 27 --
flickerstreak@7 28
flickerstreak@7 29
flickerstreak@7 30 local AceOO = AceLibrary("AceOO-2.0")
flickerstreak@7 31 local kbValidate = AceLibrary("AceConsole-2.0").keybindingValidateFunc
flickerstreak@7 32
flickerstreak@7 33
flickerstreak@1 34 -- private constants
flickerstreak@2 35 -- TODO: localize these key names with GetBindingText(KEY_)
flickerstreak@2 36 local keybindAbbreviations = {
flickerstreak@2 37 ["Mouse Button "] = "M-",
flickerstreak@2 38 ["Spacebar"] = "Sp",
flickerstreak@2 39 ["Num Pad "] = "Num-",
flickerstreak@2 40 ["Page Up"] = "PgUp",
flickerstreak@2 41 ["Page Down"] = "PgDn",
flickerstreak@2 42 [" Arrow"] = "",
flickerstreak@2 43 }
flickerstreak@2 44
flickerstreak@1 45
flickerstreak@7 46 ------------------------
flickerstreak@7 47 -- Interface Declarations
flickerstreak@7 48 ------------------------
flickerstreak@1 49
flickerstreak@7 50 -- The ActionType interface defines what the button does when it is clicked. At a
flickerstreak@7 51 -- minimum it must do the equivalent of SetAttribute("type", ...) and handle events that
flickerstreak@7 52 -- cause the action to change.
flickerstreak@7 53 -- ReAction implementations must provide this interface (implicitly or explicitly).
flickerstreak@7 54 local IActionType = AceOO.Interface {
flickerstreak@7 55 SetID = "function", -- SetID(id, [page]) optional argument indicates page #: omitting indicates default. self.config.idx[barIdx] must be updated.
flickerstreak@7 56 GetID = "function", -- id = GetID([page]) optional argument indicates page #: omitting indicates current page
flickerstreak@7 57 IsActionEmpty = "function", -- bool = IsActionEmpty()
flickerstreak@7 58 SetupAction = "function", -- one-time setup
flickerstreak@7 59 UpdateAction = "function", -- general action properties should be refreshed
flickerstreak@7 60 PickupAction = "function", -- pick up the action on the button and put on the cursor
flickerstreak@7 61 PlaceAction = "function", -- place the action on the cursor
flickerstreak@7 62 UpdateTooltip = "function", -- update the tooltip with the action's info
flickerstreak@7 63 }
flickerstreak@1 64
flickerstreak@7 65 -- The Display interface defines the "look and feel" of the action button. It should define the
flickerstreak@7 66 -- actual widgets and widget layout and provide methods to update the display.
flickerstreak@7 67 -- ReAction implementations must provide this interface (implicitly or explicitly).
flickerstreak@7 68 -- Note that ReAction implementations may also require additional display interfaces to be supported.
flickerstreak@7 69 --
flickerstreak@7 70 -- Also note: the class 'new' method must take *only* the primary button ID as an argument.
flickerstreak@7 71 local IDisplay = AceOO.Interface {
flickerstreak@7 72 SetupDisplay = "function", -- SetupDisplay(buttonName), one-time setup
flickerstreak@7 73 UpdateDisplay = "function", -- UpdateDisplay(), general display state should be refreshed
flickerstreak@7 74 TempShow = "function", -- TempShow(visible), calls to this can be nested so keep track.
flickerstreak@7 75 GetActionFrame = "function", -- f = GetActionFrame(), return a frame derived from SecureActionButtonTemplate (note: this is inherited unimplemented from ReBar.IButton)
flickerstreak@7 76 GetBaseButtonSize = "function", -- sz = GetBaseButtonSize(), return size in pixels of the nominal button (square)
flickerstreak@8 77 DisplayID = "function", -- DisplayID(show), true/false to show/hide the action ID (or equivalent)
flickerstreak@10 78 DisplayHotkey = "function", -- DisplayHotkey(keyText, button), set the hotkey display text. 2nd argument specifies which button the hotkey is for, default is "LeftButton".
flickerstreak@7 79 }
flickerstreak@1 80
flickerstreak@1 81
flickerstreak@7 82 ----------------------------
flickerstreak@7 83 -- ReAction class definition
flickerstreak@7 84 ----------------------------
flickerstreak@1 85
flickerstreak@7 86 ReAction = AceOO.Class("AceEvent-2.0", ReBar.IButton, IActionType, IDisplay)
flickerstreak@7 87 ReAction.virtual = true
flickerstreak@7 88 ReAction.IActionType = IActionType
flickerstreak@7 89 ReAction.IDisplay = IDisplay
flickerstreak@1 90
flickerstreak@1 91
flickerstreak@7 92 -----------------------
flickerstreak@7 93 -- Static class members
flickerstreak@7 94 -----------------------
flickerstreak@7 95
flickerstreak@7 96 ReAction.recycler = CreateFrame("Frame",nil,UIParent)
flickerstreak@7 97 ReAction.recycler:SetAllPoints(UIParent)
flickerstreak@7 98 ReAction.recycler:Hide()
flickerstreak@7 99
flickerstreak@7 100 ReAction.buttonTypes = { }
flickerstreak@7 101
flickerstreak@7 102
flickerstreak@7 103
flickerstreak@7 104 -----------------------
flickerstreak@7 105 -- Static class methods
flickerstreak@7 106 -----------------------
flickerstreak@7 107
flickerstreak@7 108 function ReAction:AddButtonType( name, class, maxIDs )
flickerstreak@7 109 self.buttonTypes[name] = { subtype = class, maxIDs = maxIDs }
flickerstreak@7 110 end
flickerstreak@7 111
flickerstreak@7 112 function ReAction:GetButtonType( name )
flickerstreak@7 113 if name then
flickerstreak@7 114 local t = self.buttonTypes[name]
flickerstreak@7 115 if t then
flickerstreak@7 116 return t.subtype, t.maxIDs
flickerstreak@7 117 end
flickerstreak@7 118 end
flickerstreak@7 119 end
flickerstreak@7 120
flickerstreak@7 121 function ReAction:GetButtonTypeList()
flickerstreak@7 122 local list = { }
flickerstreak@7 123 for name, _ in pairs(self.buttonTypes) do
flickerstreak@7 124 table.insert(list,name)
flickerstreak@7 125 end
flickerstreak@7 126 return list
flickerstreak@7 127 end
flickerstreak@7 128
flickerstreak@7 129 function ReAction:GetAvailableID( subtype, hint )
flickerstreak@7 130 local class, maxIDs = self:GetButtonType(subtype)
flickerstreak@7 131
flickerstreak@7 132 -- store the list of action buttons in use in the button type class factory
flickerstreak@7 133 if class._idTbl == nil then class._idTbl = { } end
flickerstreak@7 134 local t = class._idTbl
flickerstreak@1 135 local id = nil
flickerstreak@7 136
flickerstreak@7 137 -- find lowest ID not in use
flickerstreak@7 138 for i = 1, maxIDs do
flickerstreak@7 139 if t[i] == nil or t[i].inUse == false then
flickerstreak@1 140 id = i
flickerstreak@1 141 break
flickerstreak@1 142 end
flickerstreak@1 143 end
flickerstreak@1 144
flickerstreak@7 145 if id == nil then return nil end -- all action ids are in use
flickerstreak@1 146
flickerstreak@8 147 -- if a hint is given, see if that one is free instead, as long as it's < maxIDs
flickerstreak@8 148 if hint and hint > 0 and hint <= maxIDs and (t[hint] == nil or t[hint].inUse == false) then
flickerstreak@1 149 id = hint
flickerstreak@1 150 end
flickerstreak@1 151
flickerstreak@7 152 if t[id] == nil then
flickerstreak@7 153 t[id] = { }
flickerstreak@1 154 end
flickerstreak@7 155 t[id].inUse = true
flickerstreak@1 156
flickerstreak@7 157 return id, t[id]
flickerstreak@7 158 end
flickerstreak@7 159
flickerstreak@7 160 function ReAction:Acquire(config, barIdx, pages, buttonsPerPage)
flickerstreak@7 161 local btnType = self:GetButtonType(config.subtype)
flickerstreak@7 162 if not btnType then
flickerstreak@7 163 error("ReAction: Unknown button type specified.")
flickerstreak@7 164 end
flickerstreak@7 165 pages = pages or 1
flickerstreak@7 166
flickerstreak@7 167 local ids = { }
flickerstreak@7 168 local primary = nil
flickerstreak@7 169
flickerstreak@7 170 for i = 1, pages do
flickerstreak@7 171 local hint = config.ids[barIdx] and config.ids[barIdx][i]
flickerstreak@7 172 if hint == nil and i > 1 and ids[i-1] then
flickerstreak@7 173 hint = ids[i-1] + (buttonsPerPage or 0)
flickerstreak@7 174 end
flickerstreak@7 175 local id, p = self:GetAvailableID(config.subtype, hint)
flickerstreak@7 176 if id == nil then
flickerstreak@7 177 break
flickerstreak@7 178 end
flickerstreak@7 179 primary = primary or p
flickerstreak@7 180 if id then
flickerstreak@7 181 ids[i] = id
flickerstreak@7 182 end
flickerstreak@1 183 end
flickerstreak@1 184
flickerstreak@7 185 if primary then
flickerstreak@7 186 if not primary.button then
flickerstreak@7 187 primary.button = btnType:new(ids[1])
flickerstreak@7 188 end
flickerstreak@7 189 if primary.button then
flickerstreak@7 190 config.ids[barIdx] = ids
flickerstreak@7 191 primary.button:Configure(config,barIdx)
flickerstreak@7 192 end
flickerstreak@7 193 end
flickerstreak@7 194
flickerstreak@7 195 return primary and primary.button
flickerstreak@1 196 end
flickerstreak@1 197
flickerstreak@7 198 function ReAction:Release( b )
flickerstreak@1 199 if b then
flickerstreak@7 200 for i = 1, #b.config.ids[b.barIdx] do
flickerstreak@7 201 local id = b:GetID(i)
flickerstreak@7 202 if id then
flickerstreak@7 203 b.class._idTbl[id].inUse = false
flickerstreak@7 204 end
flickerstreak@7 205 end
flickerstreak@7 206 b.config = nil
flickerstreak@1 207 b:Recycle()
flickerstreak@1 208 end
flickerstreak@1 209 end
flickerstreak@1 210
flickerstreak@7 211 function ReAction:ShowAllIds()
flickerstreak@7 212 for _, t in pairs(self.buttonTypes) do
flickerstreak@7 213 if t.subtype._idTbl then
flickerstreak@7 214 for _, tbl in pairs(t.subtype._idTbl) do
flickerstreak@8 215 if tbl.button then tbl.button:DisplayID(true) end
flickerstreak@7 216 end
flickerstreak@7 217 end
flickerstreak@1 218 end
flickerstreak@7 219 self.showIDs_ = true
flickerstreak@1 220 end
flickerstreak@1 221
flickerstreak@7 222 function ReAction:HideAllIds()
flickerstreak@7 223 for _, t in pairs(self.buttonTypes) do
flickerstreak@7 224 if t.subtype._idTbl then
flickerstreak@7 225 for _, tbl in pairs(t.subtype._idTbl) do
flickerstreak@8 226 if tbl.button then tbl.button:DisplayID(false) end
flickerstreak@7 227 end
flickerstreak@7 228 end
flickerstreak@1 229 end
flickerstreak@7 230 self.showIDs_ = false
flickerstreak@1 231 end
flickerstreak@1 232
flickerstreak@1 233
flickerstreak@1 234 ----------------------
flickerstreak@1 235 -- Instance methods
flickerstreak@1 236 ----------------------
flickerstreak@1 237
flickerstreak@7 238 -- constructor
flickerstreak@1 239
flickerstreak@7 240 function ReAction.prototype:init()
flickerstreak@7 241 ReAction.super.prototype.init(self)
flickerstreak@2 242 end
flickerstreak@2 243
flickerstreak@7 244
flickerstreak@7 245 -- ReBar.IButton interface
flickerstreak@7 246
flickerstreak@7 247 function ReAction.prototype:BarUnlocked()
flickerstreak@7 248 self:TempShow(true)
flickerstreak@8 249 self:DisplayID(true)
flickerstreak@1 250 end
flickerstreak@1 251
flickerstreak@7 252 function ReAction.prototype:BarLocked()
flickerstreak@7 253 self:TempShow(false)
flickerstreak@8 254 self:DisplayID(false)
flickerstreak@1 255 end
flickerstreak@1 256
flickerstreak@7 257 function ReAction.prototype:PlaceButton(parent, point, x, y, sz)
flickerstreak@7 258 local b = self:GetActionFrame()
flickerstreak@7 259 local baseSize = self:GetBaseButtonSize()
flickerstreak@7 260 local scale = baseSize and baseSize ~= 0 and ((sz or 1) / baseSize) or 1
flickerstreak@7 261 if scale == 0 then scale = 1 end
flickerstreak@1 262 b:ClearAllPoints()
flickerstreak@7 263 b:SetParent(parent)
flickerstreak@7 264 b:SetScale(scale)
flickerstreak@1 265 b:SetPoint(point,x/scale,y/scale)
flickerstreak@1 266 end
flickerstreak@1 267
flickerstreak@7 268 function ReAction.prototype:SetPages( n )
flickerstreak@7 269 n = tonumber(n)
flickerstreak@7 270 local ids = self.config.ids[self.barIdx]
flickerstreak@7 271 if n and n >= 1 then
flickerstreak@7 272 -- note that as long as n >= 1 then id[1] will never be modified, which is what we want
flickerstreak@7 273 -- because then the button frame ID would be out of sync with the static button name
flickerstreak@7 274 while #ids < n do
flickerstreak@7 275 local id = ReAction:GetAvailableID(self.config.subtype, ids[#ids] + #self.config.ids)
flickerstreak@7 276 if id == nil then
flickerstreak@7 277 break
flickerstreak@1 278 end
flickerstreak@7 279 self:SetID( id, #ids + 1 )
flickerstreak@7 280 table.insert(ids, id)
flickerstreak@7 281 end
flickerstreak@7 282 while #ids > n do
flickerstreak@7 283 local id = table.remove(ids)
flickerstreak@7 284 self:SetID( nil, #ids + 1 )
flickerstreak@7 285 self.class._idTbl[id].inUse = false
flickerstreak@1 286 end
flickerstreak@1 287 end
flickerstreak@1 288 end
flickerstreak@1 289
flickerstreak@7 290 -- Event handlers
flickerstreak@1 291
flickerstreak@7 292 function ReAction.prototype:UPDATE_BINDINGS()
flickerstreak@10 293 self:DisplayHotkey(self:GetKeyBindingText("LeftButton", true),"LeftButton")
flickerstreak@10 294 self:DisplayHotkey(self:GetKeyBindingText("RightButton",true),"RightButton")
flickerstreak@7 295 end
flickerstreak@1 296
flickerstreak@1 297
flickerstreak@7 298 -- Internal functions
flickerstreak@7 299
flickerstreak@7 300 function ReAction.prototype:Recycle()
flickerstreak@7 301 self:UnregisterAllEvents()
flickerstreak@7 302
flickerstreak@7 303 -- tuck the frame away
flickerstreak@7 304 local b = self:GetActionFrame()
flickerstreak@7 305 b:SetParent(ReAction.recycler)
flickerstreak@7 306 b:ClearAllPoints()
flickerstreak@7 307 b:SetPoint("TOPLEFT",0,0)
flickerstreak@7 308 b:Hide()
flickerstreak@7 309 end
flickerstreak@7 310
flickerstreak@7 311 function ReAction.prototype:Configure( config, barIdx )
flickerstreak@7 312 self.config = config
flickerstreak@7 313 self.barIdx = barIdx
flickerstreak@7 314
flickerstreak@7 315 local ids = config.ids[barIdx]
flickerstreak@7 316 self:SetID(ids[1]) -- default id
flickerstreak@7 317 for i = 1, #ids do
flickerstreak@7 318 self:SetID(ids[i], i) -- paged ids
flickerstreak@7 319 end
flickerstreak@7 320 self:UpdateAction()
flickerstreak@7 321 self:UpdateDisplay()
flickerstreak@7 322 self:DisplayHotkey(self:GetKeyBindingText(nil, true))
flickerstreak@7 323
flickerstreak@7 324 self:RegisterEvent("UPDATE_BINDINGS")
flickerstreak@7 325 end
flickerstreak@7 326
flickerstreak@7 327 function ReAction.prototype:SetKeyBinding( k, mouseBtn )
flickerstreak@1 328 if k == nil or kbValidate(k) then
flickerstreak@7 329 local current = self:GetKeyBinding(mouseBtn)
flickerstreak@1 330 if current then
flickerstreak@1 331 SetBinding(current,nil)
flickerstreak@1 332 end
flickerstreak@1 333 if k then
flickerstreak@7 334 SetBindingClick(k, self:GetActionFrame():GetName(), mouseBtn or "LeftButton")
flickerstreak@1 335 end
flickerstreak@1 336 end
flickerstreak@1 337 end
flickerstreak@1 338
flickerstreak@7 339 function ReAction.prototype:GetKeyBinding( mouseBtn )
flickerstreak@7 340 return GetBindingKey("CLICK "..self:GetActionFrame():GetName()..":"..(mouseBtn or "LeftButton"))
flickerstreak@1 341 end
flickerstreak@1 342
flickerstreak@7 343 function ReAction.prototype:GetKeyBindingText( mouseBtn, abbrev )
flickerstreak@7 344 local key = self:GetKeyBinding(mouseBtn)
flickerstreak@7 345 local txt = key and GetBindingText(key, "KEY_", abbrev and 1) or ""
flickerstreak@7 346
flickerstreak@7 347 if txt and abbrev then
flickerstreak@7 348 -- further abbreviate some key names
flickerstreak@7 349 for pat, rep in pairs(keybindAbbreviations) do
flickerstreak@7 350 txt = string.gsub(txt,pat,rep)
flickerstreak@7 351 end
flickerstreak@7 352 end
flickerstreak@7 353 return txt
flickerstreak@1 354 end
flickerstreak@1 355
flickerstreak@7 356 function ReAction.prototype:SetTooltip()
flickerstreak@7 357 GameTooltip_SetDefaultAnchor(GameTooltip, self:GetActionFrame())
flickerstreak@1 358 self:UpdateTooltip()
flickerstreak@1 359 end
flickerstreak@1 360
flickerstreak@7 361 function ReAction.prototype:ClearTooltip()
flickerstreak@1 362 tooltipTime = nil
flickerstreak@1 363 GameTooltip:Hide()
flickerstreak@1 364 end