annotate classes/ReAction.lua @ 21:90bf38d48efd

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