annotate classes/Bar.lua @ 241:09c8e9baa35a

Collect table utility functions
author Flick
date Fri, 25 Mar 2011 16:50:43 -0700
parents 704f4a05a1d7
children b56cff349bd6
rev   line source
flickerstreak@175 1 local addonName, addonTable = ...
flickerstreak@175 2 local ReAction = addonTable.ReAction
flickerstreak@122 3 local L = ReAction.L
flickerstreak@183 4 local LKB = ReAction.LKB
flickerstreak@122 5 local _G = _G
flickerstreak@122 6 local CreateFrame = CreateFrame
flickerstreak@122 7 local floor = math.floor
flickerstreak@122 8 local fmod = math.fmod
flickerstreak@122 9 local format = string.format
Flick@241 10 local tfetch = addonTable.tfetch
Flick@241 11 local fieldsort = addonTable.fieldsort
flickerstreak@122 12
flickerstreak@193 13 local LSG = LibStub("ReAction-LibShowActionGrid-1.0")
flickerstreak@147 14
flickerstreak@155 15 ---- Secure snippets ----
flickerstreak@155 16 local _reaction_init =
flickerstreak@155 17 [[
flickerstreak@155 18 anchorKeys = newtable("point","relPoint","x","y")
flickerstreak@155 19
flickerstreak@155 20 state = nil
flickerstreak@155 21 set_state = nil
flickerstreak@155 22 state_override = nil
flickerstreak@156 23 unit_exists = nil
flickerstreak@155 24
flickerstreak@155 25 showAll = false
flickerstreak@155 26 hidden = false
flickerstreak@155 27
flickerstreak@155 28 defaultAlpha = 1.0
flickerstreak@155 29 defaultScale = 1.0
flickerstreak@155 30 defaultAnchor = newtable()
flickerstreak@155 31
flickerstreak@155 32 activeStates = newtable()
flickerstreak@155 33 settings = newtable()
flickerstreak@155 34 extensions = newtable()
flickerstreak@155 35 ]]
flickerstreak@155 36
flickerstreak@155 37 local _reaction_refresh =
flickerstreak@155 38 [[
flickerstreak@155 39 local oldState = state
flickerstreak@155 40 state = state_override or set_state or state
flickerstreak@155 41
flickerstreak@156 42 local hide = nil
flickerstreak@155 43 if state then
flickerstreak@155 44 local settings = settings[state]
flickerstreak@155 45 if settings then
flickerstreak@155 46 -- show/hide
flickerstreak@156 47 hide = settings.hide
flickerstreak@155 48 -- re-anchor
flickerstreak@155 49 local old_anchor = activeStates.anchor
flickerstreak@155 50 activeStates.anchor = settings.anchorEnable and state
flickerstreak@155 51 if old_anchor ~= activeStates.anchor or not set_state then
flickerstreak@155 52 if activeStates.anchor then
flickerstreak@155 53 if settings.anchorPoint then
flickerstreak@155 54 self:ClearAllPoints()
flickerstreak@155 55 local f = self:GetAttribute("frameref-anchor-"..state)
flickerstreak@155 56 if f then
flickerstreak@155 57 self:SetPoint(settings.anchorPoint, f, settings.anchorRelPoint, settings.anchorX, settings.anchorY)
flickerstreak@155 58 end
flickerstreak@155 59 end
flickerstreak@155 60 elseif defaultAnchor.point then
flickerstreak@155 61 self:ClearAllPoints()
flickerstreak@155 62 self:SetPoint(defaultAnchor.point, defaultAnchor.frame,
flickerstreak@155 63 defaultAnchor.relPoint, defaultAnchor.x, defaultAnchor.y)
flickerstreak@155 64 end
flickerstreak@155 65 end
flickerstreak@155 66 -- re-scale
flickerstreak@155 67 local old_scale = activeStates.scale
flickerstreak@155 68 activeStates.scale = settings.enableScale and state
flickerstreak@155 69 if old_scale ~= activeStates.scale or not set_state then
flickerstreak@155 70 self:SetScale(activeStates.scale and settings.scale or defaultScale)
flickerstreak@155 71 end
flickerstreak@155 72 -- alpha
flickerstreak@155 73 local old_alpha = activeStates.alpha
flickerstreak@155 74 activeStates.alpha = settings.enableAlpha and state
flickerstreak@155 75 if old_alpha ~= activeStates.alpha or not set_state then
flickerstreak@155 76 self:SetAlpha(activeStates.alpha and settings.alpha or defaultAlpha)
flickerstreak@155 77 end
flickerstreak@155 78 end
flickerstreak@155 79 end
flickerstreak@155 80
flickerstreak@156 81 -- hide if state or unit_exists says to
flickerstreak@156 82 hide = not showAll and (hide or unithide)
flickerstreak@156 83 if hide ~= hidden then
flickerstreak@156 84 hidden = hide
flickerstreak@156 85 if hide then
flickerstreak@156 86 self:Hide()
flickerstreak@156 87 else
flickerstreak@156 88 self:Show()
flickerstreak@156 89 end
flickerstreak@156 90 end
flickerstreak@156 91
flickerstreak@155 92 for _, attr in pairs(extensions) do
flickerstreak@155 93 control:RunAttribute(attr)
flickerstreak@155 94 end
flickerstreak@155 95
flickerstreak@155 96 control:ChildUpdate()
flickerstreak@155 97
flickerstreak@155 98 if showAll then
flickerstreak@155 99 control:CallMethod("UpdateHiddenLabel", state and settings[state] and settings[state].hide)
flickerstreak@155 100 end
flickerstreak@155 101
flickerstreak@155 102 if oldState ~= state then
flickerstreak@155 103 control:CallMethod("StateRefresh", state)
flickerstreak@155 104 end
flickerstreak@155 105 ]]
flickerstreak@155 106
flickerstreak@155 107 local _onstate_reaction = -- function( self, stateid, newstate )
flickerstreak@155 108 [[
flickerstreak@155 109 set_state = newstate
flickerstreak@155 110 ]] .. _reaction_refresh
flickerstreak@155 111
flickerstreak@155 112 local _onstate_showgrid = -- function( self, stateid, newstate )
flickerstreak@155 113 [[
flickerstreak@155 114 control:ChildUpdate(stateid,newstate)
flickerstreak@155 115 control:CallMethod("UpdateShowGrid")
flickerstreak@155 116 ]]
flickerstreak@155 117
flickerstreak@155 118 local _onstate_unitexists = -- function( self, stateid, newstate )
flickerstreak@155 119 [[
flickerstreak@159 120 unithide = not newstate or newstate == "hide"
flickerstreak@155 121 ]] .. _reaction_refresh
flickerstreak@155 122
flickerstreak@155 123 local _onclick = -- function( self, button, down )
flickerstreak@155 124 [[
flickerstreak@155 125 if state_override == button then
flickerstreak@155 126 state_override = nil -- toggle
flickerstreak@155 127 else
flickerstreak@155 128 state_override = button
flickerstreak@155 129 end
flickerstreak@155 130 ]] .. _reaction_refresh
flickerstreak@155 131
flickerstreak@157 132 -- For reference
flickerstreak@157 133 -- the option field names must match the field names of the options table, below
flickerstreak@157 134 local stateProperties = {
flickerstreak@157 135 hide = true,
flickerstreak@157 136 --keybindState = true, TODO: broken
flickerstreak@157 137 anchorEnable = true,
flickerstreak@157 138 anchorFrame = true,
flickerstreak@157 139 anchorPoint = true,
flickerstreak@157 140 anchorRelPoint = true,
flickerstreak@157 141 anchorX = true,
flickerstreak@157 142 anchorY = true,
flickerstreak@157 143 enableScale = true,
flickerstreak@157 144 scale = true,
flickerstreak@157 145 enableAlpha = true,
flickerstreak@157 146 alpha = true,
flickerstreak@157 147 }
flickerstreak@157 148
flickerstreak@157 149
flickerstreak@155 150
flickerstreak@147 151 ---- Bar class ----
flickerstreak@122 152 local Bar = { }
flickerstreak@147 153 local frameList = { }
flickerstreak@122 154
flickerstreak@122 155 ReAction.Bar = Bar -- export to ReAction
flickerstreak@122 156
Flick@234 157 function Bar:New( name, config, buttonClass )
flickerstreak@122 158 if type(config) ~= "table" then
flickerstreak@122 159 error("ReAction.Bar: config table required")
flickerstreak@122 160 end
flickerstreak@122 161
flickerstreak@122 162 -- create new self
flickerstreak@122 163 self = setmetatable(
flickerstreak@122 164 {
Flick@234 165 config = config,
Flick@234 166 name = name,
Flick@234 167 buttons = { },
Flick@234 168 buttonClass = buttonClass,
Flick@234 169 width = config.width or 480,
Flick@234 170 height = config.height or 40,
flickerstreak@122 171 },
flickerstreak@147 172 {__index = self} )
flickerstreak@122 173
flickerstreak@122 174 -- The frame type is 'Button' in order to have an OnClick handler. However, the frame itself is
flickerstreak@122 175 -- not mouse-clickable by the user.
flickerstreak@122 176 local parent = config.parent and (ReAction:GetBar(config.parent) or _G[config.parent]) or UIParent
flickerstreak@147 177 name = name and "ReAction-"..name
flickerstreak@147 178 local f = name and frameList[name]
flickerstreak@147 179 if not f then
flickerstreak@147 180 f = CreateFrame("Button", name, parent, "SecureHandlerStateTemplate, SecureHandlerClickTemplate")
flickerstreak@147 181 if name then
flickerstreak@147 182 frameList[name] = f
flickerstreak@147 183 end
flickerstreak@147 184 end
flickerstreak@122 185 f:SetFrameStrata("MEDIUM")
flickerstreak@122 186 f:SetWidth(self.width)
flickerstreak@146 187 f:SetHeight(self.height)
flickerstreak@122 188 f:SetAlpha(config.alpha or 1.0)
flickerstreak@122 189 f:Show()
flickerstreak@122 190 f:EnableMouse(false)
flickerstreak@122 191 f:SetClampedToScreen(true)
flickerstreak@171 192 LSG:AddFrame(f)
flickerstreak@122 193
flickerstreak@155 194 -- secure handlers
flickerstreak@155 195 f:Execute(_reaction_init)
flickerstreak@155 196 f:SetAttribute("_onstate-reaction", _onstate_reaction)
flickerstreak@155 197 f:SetAttribute("_onstate-showgrid", _onstate_showgrid)
flickerstreak@155 198 f:SetAttribute("_onstate-unitexists", _onstate_unitexists)
flickerstreak@155 199 f:SetAttribute("_onclick", _onclick)
flickerstreak@155 200
flickerstreak@155 201 -- secure handler CallMethod()s
flickerstreak@155 202 f.UpdateShowGrid = function() self:UpdateShowGrid() end
flickerstreak@155 203 f.StateRefresh = function() self:RefreshControls() end
flickerstreak@155 204 f.UpdateHiddenLabel = function(f,hidden) self:SetLabelSubtext(hidden and L["Hidden"]) end
flickerstreak@122 205
flickerstreak@122 206 -- Override the default frame accessor to provide strict read-only access
flickerstreak@122 207 function self:GetFrame()
flickerstreak@122 208 return f
flickerstreak@122 209 end
flickerstreak@122 210
flickerstreak@122 211 self:ApplyAnchor()
flickerstreak@147 212 self:SetConfigMode(ReAction:GetConfigMode())
flickerstreak@147 213 self:SetKeybindMode(ReAction:GetKeybindMode())
flickerstreak@147 214
flickerstreak@213 215 if ReAction.LBF then
flickerstreak@213 216 local g = ReAction.LBF:Group(L["ReAction"], self.name)
flickerstreak@213 217 self.config.ButtonFacade = self.config.ButtonFacade or {
flickerstreak@213 218 skinID = "Blizzard",
flickerstreak@213 219 backdrop = true,
flickerstreak@213 220 gloss = 0,
flickerstreak@213 221 colors = {},
flickerstreak@213 222 }
flickerstreak@213 223 local c = self.config.ButtonFacade
flickerstreak@213 224 g:Skin(c.skinID, c.gloss, c.backdrop, c.colors)
flickerstreak@213 225 self.LBFGroup = g
flickerstreak@213 226 end
flickerstreak@213 227
flickerstreak@122 228 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@122 229
Flick@234 230 buttonClass:SetupBar(self)
Flick@234 231
flickerstreak@122 232 return self
flickerstreak@122 233 end
flickerstreak@122 234
flickerstreak@122 235 function Bar:Destroy()
flickerstreak@122 236 local f = self:GetFrame()
Flick@234 237 for idx, b in self:IterateButtons() do
Flick@234 238 b:Destroy()
Flick@234 239 end
flickerstreak@122 240 f:UnregisterAllEvents()
flickerstreak@125 241 self:ShowControls(false)
flickerstreak@122 242 ReAction.UnregisterAllCallbacks(self)
flickerstreak@171 243 LKB.UnregisterAllCallbacks(self)
flickerstreak@213 244 if self.LBFGroup then
flickerstreak@213 245 self.LBFGroup:Delete(true)
flickerstreak@213 246 end
flickerstreak@171 247 LSG:RemoveFrame(f)
flickerstreak@122 248 f:SetParent(UIParent)
flickerstreak@122 249 f:ClearAllPoints()
flickerstreak@147 250 f:Hide()
flickerstreak@122 251 end
flickerstreak@122 252
flickerstreak@147 253 --
flickerstreak@147 254 -- Events
flickerstreak@147 255 --
flickerstreak@147 256
flickerstreak@147 257 function Bar:OnConfigModeChanged(event, mode)
flickerstreak@147 258 self:SetConfigMode(mode)
flickerstreak@123 259 end
flickerstreak@123 260
flickerstreak@153 261 --
flickerstreak@153 262 -- Accessors
flickerstreak@153 263 --
flickerstreak@153 264
flickerstreak@153 265 function Bar:GetName()
flickerstreak@153 266 return self.name
flickerstreak@153 267 end
flickerstreak@153 268
flickerstreak@153 269 -- only ReAction:RenameBar() should call this function. Calling from any other
flickerstreak@153 270 -- context will desync the bar list in the ReAction class.
flickerstreak@153 271 function Bar:SetName(name)
flickerstreak@213 272 if self.LBFGroup then
flickerstreak@213 273 -- LBF doesn't offer a method of renaming a group, so delete and remake the group.
flickerstreak@213 274 local c = self.config.ButtonFacade
flickerstreak@213 275 local g = ReAction.LBF:Group(L["ReAction"], name)
Flick@234 276 for idx, b in self:IterateButtons() do
flickerstreak@213 277 self.LBFGroup:RemoveButton(b:GetFrame(), true)
flickerstreak@213 278 g:AddButton(b:GetFrame())
flickerstreak@213 279 end
flickerstreak@213 280 self.LBFGroup:Delete(true)
flickerstreak@213 281 self.LBFGroup = g
flickerstreak@213 282 self.LBFGroup:Skin(c.skinID, c.gloss, c.backdrop, c.colors)
flickerstreak@213 283 end
flickerstreak@153 284 self.name = name
flickerstreak@154 285 if self.overlay then
flickerstreak@154 286 self.overlay:SetLabel(self.name)
flickerstreak@154 287 end
flickerstreak@153 288 end
flickerstreak@153 289
flickerstreak@153 290 function Bar:GetFrame()
flickerstreak@153 291 -- this method is included for documentation purposes. It is overridden
flickerstreak@153 292 -- for each object in the :New() method.
flickerstreak@153 293 error("Invalid Bar object: used without initialization")
flickerstreak@153 294 end
flickerstreak@153 295
Flick@234 296 function Bar:GetButton(idx)
Flick@234 297 return self.buttons[idx]
Flick@234 298 end
Flick@234 299
Flick@237 300 function Bar:GetButtonClass()
Flick@237 301 return self.buttonClass
Flick@237 302 end
Flick@237 303
flickerstreak@153 304 function Bar:GetConfig()
flickerstreak@153 305 return self.config
flickerstreak@153 306 end
flickerstreak@153 307
flickerstreak@153 308 function Bar:GetAnchor()
flickerstreak@122 309 local c = self.config
flickerstreak@153 310 return (c.point or "CENTER"),
flickerstreak@153 311 (c.anchor or self:GetFrame():GetParent():GetName()),
flickerstreak@153 312 (c.relpoint or c.point or "CENTER"),
flickerstreak@153 313 (c.x or 0),
flickerstreak@153 314 (c.y or 0)
flickerstreak@122 315 end
flickerstreak@122 316
flickerstreak@122 317 function Bar:SetAnchor(point, frame, relativePoint, x, y)
flickerstreak@122 318 local c = self.config
flickerstreak@122 319 c.point = point or c.point
flickerstreak@122 320 c.anchor = frame or c.anchor
flickerstreak@122 321 c.relpoint = relativePoint or c.relpoint
flickerstreak@122 322 c.x = x or c.x
flickerstreak@122 323 c.y = y or c.y
flickerstreak@122 324 self:ApplyAnchor()
flickerstreak@122 325 ReAction:RefreshBar(self)
flickerstreak@122 326 end
flickerstreak@122 327
flickerstreak@122 328 function Bar:GetSize()
flickerstreak@122 329 local f = self:GetFrame()
flickerstreak@122 330 return f:GetWidth(), f:GetHeight()
flickerstreak@122 331 end
flickerstreak@122 332
flickerstreak@122 333 function Bar:SetSize(w,h)
flickerstreak@122 334 local f = self:GetFrame()
flickerstreak@122 335 self.config.width = w
flickerstreak@122 336 self.config.height = h
flickerstreak@122 337 f:SetWidth(w)
flickerstreak@122 338 f:SetHeight(h)
flickerstreak@122 339 end
flickerstreak@122 340
flickerstreak@122 341 function Bar:GetButtonSize()
flickerstreak@122 342 local w = self.config.btnWidth or 32
flickerstreak@122 343 local h = self.config.btnHeight or 32
flickerstreak@122 344 -- TODO: get from modules?
flickerstreak@122 345 return w,h
flickerstreak@122 346 end
flickerstreak@122 347
flickerstreak@122 348 function Bar:SetButtonSize(w,h)
flickerstreak@122 349 if w > 0 and h > 0 then
flickerstreak@122 350 self.config.btnWidth = w
flickerstreak@122 351 self.config.btnHeight = h
flickerstreak@122 352 end
flickerstreak@122 353 ReAction:RefreshBar(self)
flickerstreak@122 354 end
flickerstreak@122 355
flickerstreak@153 356 function Bar:GetNumButtons()
flickerstreak@153 357 local r,c = self:GetButtonGrid()
flickerstreak@153 358 return r*c
flickerstreak@153 359 end
flickerstreak@153 360
flickerstreak@122 361 function Bar:GetButtonGrid()
flickerstreak@122 362 local cfg = self.config
flickerstreak@122 363 local r = cfg.btnRows or 1
flickerstreak@122 364 local c = cfg.btnColumns or 1
flickerstreak@122 365 local s = cfg.spacing or 4
flickerstreak@122 366 return r,c,s
flickerstreak@122 367 end
flickerstreak@122 368
flickerstreak@122 369 function Bar:SetButtonGrid(r,c,s)
flickerstreak@122 370 if r > 0 and c > 0 and s > 0 then
flickerstreak@122 371 local cfg = self.config
flickerstreak@122 372 cfg.btnRows = r
flickerstreak@122 373 cfg.btnColumns = c
flickerstreak@122 374 cfg.spacing = s
flickerstreak@122 375 end
Flick@234 376 self.buttonClass:SetupBar(self)
flickerstreak@122 377 ReAction:RefreshBar(self)
flickerstreak@122 378 end
flickerstreak@122 379
flickerstreak@122 380 function Bar:GetAlpha()
flickerstreak@122 381 return self.config.alpha or 1.0
flickerstreak@122 382 end
flickerstreak@122 383
flickerstreak@122 384 function Bar:SetAlpha(value)
flickerstreak@122 385 self.config.alpha = value
flickerstreak@122 386 self:GetFrame():SetAlpha(value or 1.0)
flickerstreak@155 387 self:UpdateDefaultStateAlpha()
flickerstreak@122 388 ReAction:RefreshBar(self)
flickerstreak@122 389 end
flickerstreak@122 390
flickerstreak@122 391 function Bar:IterateButtons()
Flick@234 392 -- iterator returns idx, button, but does NOT iterate in index order
flickerstreak@122 393 return pairs(self.buttons)
flickerstreak@122 394 end
flickerstreak@122 395
flickerstreak@153 396 --
flickerstreak@153 397 -- Methods
flickerstreak@153 398 --
flickerstreak@153 399
flickerstreak@147 400 function Bar:SetConfigMode(mode)
flickerstreak@155 401 self:SetSecureData("showAll",mode)
flickerstreak@147 402 self:ShowControls(mode)
Flick@234 403 for idx, b in self:IterateButtons() do
flickerstreak@147 404 b:ShowGridTemp(mode)
flickerstreak@147 405 b:UpdateActionIDLabel(mode)
flickerstreak@147 406 end
flickerstreak@147 407 end
flickerstreak@147 408
flickerstreak@147 409 function Bar:SetKeybindMode(mode)
flickerstreak@155 410 self:SetSecureData("showAll",mode)
Flick@234 411 for idx, b in self:IterateButtons() do
flickerstreak@147 412 b:SetKeybindMode(mode)
flickerstreak@147 413 end
flickerstreak@147 414 end
flickerstreak@147 415
flickerstreak@153 416 function Bar:ApplyAnchor()
flickerstreak@153 417 local f = self:GetFrame()
flickerstreak@153 418 local c = self.config
flickerstreak@153 419 local p = c.point
flickerstreak@153 420
flickerstreak@153 421 f:SetWidth(c.width)
flickerstreak@153 422 f:SetHeight(c.height)
flickerstreak@153 423 f:ClearAllPoints()
flickerstreak@153 424
flickerstreak@153 425 if p then
flickerstreak@153 426 local a = f:GetParent()
flickerstreak@153 427 if c.anchor then
flickerstreak@153 428 local bar = ReAction:GetBar(c.anchor)
flickerstreak@153 429 if bar then
flickerstreak@153 430 a = bar:GetFrame()
flickerstreak@153 431 else
flickerstreak@153 432 a = _G[c.anchor]
flickerstreak@153 433 end
flickerstreak@153 434 end
flickerstreak@153 435 local fr = a or f:GetParent()
flickerstreak@153 436 f:SetPoint(p, a or f:GetParent(), c.relpoint, c.x or 0, c.y or 0)
flickerstreak@153 437 else
flickerstreak@153 438 f:SetPoint("CENTER")
flickerstreak@153 439 end
flickerstreak@155 440
flickerstreak@155 441 self:UpdateDefaultStateAnchor()
flickerstreak@153 442 end
flickerstreak@153 443
flickerstreak@153 444 function Bar:ClipNButtons( n )
flickerstreak@153 445 local cfg = self.config
flickerstreak@153 446 local r = cfg.btnRows or 1
flickerstreak@153 447 local c = cfg.btnColumns or 1
flickerstreak@153 448
flickerstreak@153 449 cfg.btnRows = ceil(n/c)
flickerstreak@153 450 cfg.btnColumns = min(n,c)
flickerstreak@153 451 end
flickerstreak@153 452
flickerstreak@153 453 function Bar:AddButton(idx, button)
flickerstreak@153 454 local f = self:GetFrame()
flickerstreak@153 455
Flick@234 456 self.buttons[idx] = button
flickerstreak@153 457
flickerstreak@153 458 -- Store a properly wrapped reference to the child frame as an attribute
flickerstreak@153 459 -- (accessible via "frameref-btn#")
flickerstreak@153 460 f:SetFrameRef(format("btn%d",idx), button:GetFrame())
flickerstreak@213 461
flickerstreak@213 462 -- button constructors are responsible for calling SkinButton
flickerstreak@153 463 end
flickerstreak@153 464
flickerstreak@153 465 function Bar:RemoveButton(button)
Flick@234 466 local idx = button:GetIndex()
flickerstreak@153 467 if idx then
flickerstreak@153 468 self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil)
Flick@234 469 self.buttons[idx] = nil
flickerstreak@153 470 end
flickerstreak@213 471 if self.LBFGroup then
flickerstreak@213 472 self.LBFGroup:RemoveButton(button:GetFrame(),true)
flickerstreak@213 473 end
flickerstreak@153 474 end
flickerstreak@153 475
flickerstreak@122 476 function Bar:PlaceButton(button, baseW, baseH)
Flick@234 477 local idx = button:GetIndex()
flickerstreak@122 478 if idx then
flickerstreak@122 479 local r, c, s = self:GetButtonGrid()
flickerstreak@122 480 local bh, bw = self:GetButtonSize()
flickerstreak@122 481 local row, col = floor((idx-1)/c), fmod((idx-1),c) -- zero-based
flickerstreak@122 482 local x, y = col*bw + (col+0.5)*s, -(row*bh + (row+0.5)*s)
flickerstreak@122 483 local scale = bw/baseW
flickerstreak@122 484 local b = button:GetFrame()
flickerstreak@122 485
flickerstreak@122 486 b:ClearAllPoints()
flickerstreak@122 487 b:SetPoint("TOPLEFT",x/scale,y/scale)
flickerstreak@122 488 b:SetScale(scale)
flickerstreak@122 489 end
flickerstreak@122 490 end
flickerstreak@122 491
flickerstreak@213 492 function Bar:SkinButton( button, data )
flickerstreak@213 493 if self.LBFGroup then
flickerstreak@213 494 self.LBFGroup:AddButton(button:GetFrame(), data)
flickerstreak@213 495 end
flickerstreak@147 496 end
flickerstreak@148 497
flickerstreak@155 498 function Bar:UpdateShowGrid()
Flick@234 499 for idx, button in self:IterateButtons() do
flickerstreak@155 500 button:UpdateShowGrid()
flickerstreak@155 501 end
flickerstreak@155 502 end
flickerstreak@155 503
flickerstreak@154 504 function Bar:ShowControls(show)
flickerstreak@154 505 if show then
flickerstreak@155 506 if not self.overlay then
flickerstreak@155 507 self.overlay = Bar.Overlay:New(self) -- see Overlay.lua
flickerstreak@154 508 end
flickerstreak@155 509 self.overlay:Show()
flickerstreak@155 510 self:RefreshSecureState()
flickerstreak@155 511 elseif self.overlay then
flickerstreak@155 512 self.overlay:Hide()
flickerstreak@154 513 end
flickerstreak@154 514 end
flickerstreak@154 515
flickerstreak@154 516 function Bar:RefreshControls()
flickerstreak@154 517 if self.overlay and self.overlay:IsShown() then
flickerstreak@154 518 self.overlay:RefreshControls()
flickerstreak@154 519 end
flickerstreak@154 520 end
flickerstreak@154 521
flickerstreak@154 522 function Bar:SetLabelSubtext(text)
flickerstreak@154 523 if self.overlay then
flickerstreak@154 524 self.overlay:SetLabelSubtext(text)
flickerstreak@154 525 end
flickerstreak@154 526 end
flickerstreak@154 527
flickerstreak@154 528 --
flickerstreak@154 529 -- Secure state functions
flickerstreak@154 530 --
flickerstreak@154 531
flickerstreak@155 532 function Bar:GetSecureState()
flickerstreak@155 533 local env = GetManagedEnvironment(self:GetFrame())
flickerstreak@155 534 return env and env.state
flickerstreak@155 535 end
flickerstreak@155 536
flickerstreak@155 537 function Bar:GetStateProperty(state, propname)
flickerstreak@155 538 -- override in modules/State.lua for now
flickerstreak@155 539 end
flickerstreak@155 540
flickerstreak@155 541 function Bar:SetStateProperty(state, propname, value)
flickerstreak@155 542 -- override in modules/State.lua for now
flickerstreak@155 543 end
flickerstreak@155 544
flickerstreak@155 545 function Bar:RefreshSecureState()
flickerstreak@155 546 self:GetFrame():Execute(_reaction_refresh)
flickerstreak@155 547 end
flickerstreak@155 548
flickerstreak@155 549 -- usage: SetSecureData(globalname, [tblkey1, tblkey2, ...], value)
flickerstreak@155 550 function Bar:SetSecureData( ... )
flickerstreak@155 551 local n = select('#',...)
flickerstreak@155 552 if n < 2 then
flickerstreak@155 553 error("ReAction.Bar:SetSecureData() requires at least 2 arguments")
flickerstreak@155 554 end
flickerstreak@155 555 local f = self:GetFrame()
flickerstreak@155 556 f:SetAttribute("data-depth",n-1)
flickerstreak@155 557 f:SetAttribute("data-value",select(n,...))
flickerstreak@155 558 for i = 1, n-1 do
flickerstreak@155 559 local key = select(i,...)
flickerstreak@155 560 if key == nil then
flickerstreak@155 561 error("ReAction.Bar:SetSecureData() - nil table key in argument list (#"..i..")")
flickerstreak@155 562 end
flickerstreak@155 563 f:SetAttribute("data-key-"..i, key)
flickerstreak@155 564 end
flickerstreak@155 565 f:Execute(
flickerstreak@155 566 [[
flickerstreak@155 567 local n = self:GetAttribute("data-depth")
flickerstreak@155 568 if n > 0 then
flickerstreak@155 569 local value = self:GetAttribute("data-value")
flickerstreak@155 570 local t = _G
flickerstreak@155 571 for i = 1, n do
flickerstreak@155 572 local key = self:GetAttribute("data-key-"..i)
flickerstreak@155 573 if not key then return end
flickerstreak@155 574 if not t[key] then
flickerstreak@155 575 t[key] = newtable()
flickerstreak@155 576 end
flickerstreak@155 577 if i == n then
flickerstreak@155 578 t[key] = value
flickerstreak@155 579 else
flickerstreak@155 580 t = t[key]
flickerstreak@155 581 end
flickerstreak@155 582 end
flickerstreak@155 583 end
flickerstreak@155 584 ]])
flickerstreak@155 585 self:RefreshSecureState()
flickerstreak@155 586 end
flickerstreak@155 587
flickerstreak@155 588 function Bar:SetSecureStateData( state, key, value )
flickerstreak@155 589 self:SetSecureData("settings",state,key,value)
flickerstreak@155 590 end
flickerstreak@155 591
flickerstreak@155 592 -- sets a snippet to be run as an extension to _onstate-reaction
flickerstreak@155 593 function Bar:SetSecureStateExtension( id, snippet )
flickerstreak@155 594 if id == nil then
flickerstreak@155 595 error("ReAction.Bar:SetSecureStateExtension() requires an id")
flickerstreak@155 596 end
flickerstreak@155 597 local f = self:GetFrame()
flickerstreak@155 598 f:SetAttribute("input-secure-ext-id",id)
flickerstreak@155 599 f:SetAttribute("secure-ext-"..id,snippet)
flickerstreak@155 600 f:Execute(
flickerstreak@155 601 [[
flickerstreak@155 602 local id = self:GetAttribute("input-secure-ext-id")
flickerstreak@155 603 if id then
flickerstreak@155 604 extensions[id] = self:GetAttribute("secure-ext-"..id) or nil
flickerstreak@155 605 end
flickerstreak@155 606 ]])
flickerstreak@155 607 self:RefreshSecureState()
flickerstreak@155 608 end
flickerstreak@155 609
flickerstreak@155 610 function Bar:SetFrameRef( name, refFrame )
flickerstreak@155 611 if refFrame then
flickerstreak@155 612 local _, explicit = refFrame:IsProtected()
flickerstreak@155 613 if not explicit then
flickerstreak@155 614 refFrame = nil
flickerstreak@155 615 end
flickerstreak@155 616 end
flickerstreak@155 617 if refFrame then
flickerstreak@155 618 self:GetFrame():SetFrameRef(name,refFrame)
flickerstreak@155 619 else
flickerstreak@155 620 self:GetFrame():SetAttribute("frameref-"..name,nil)
flickerstreak@155 621 end
flickerstreak@155 622 end
flickerstreak@155 623
flickerstreak@157 624 function Bar:SetStateDriver( states )
flickerstreak@157 625 if states then
flickerstreak@157 626 for state, props in pairs(states) do
flickerstreak@157 627 self:SetSecureStateData(state, "active_", true) -- make sure there's a 'settings' field for this state
flickerstreak@157 628 for propname, value in pairs(props) do
flickerstreak@157 629 if propname == "anchorFrame" then
flickerstreak@157 630 self:SetFrameRef("anchor-"..state, _G[value])
flickerstreak@157 631 elseif propname == "rule" then
flickerstreak@157 632 -- do nothing
flickerstreak@157 633 else
flickerstreak@157 634 self:SetSecureStateData(state, propname, value)
flickerstreak@157 635 end
flickerstreak@157 636 end
flickerstreak@157 637 end
flickerstreak@157 638 end
flickerstreak@157 639 local rule = states and self:BuildStateRule(states)
flickerstreak@155 640 if rule then
flickerstreak@155 641 RegisterStateDriver(self:GetFrame(),"reaction",rule)
flickerstreak@155 642 elseif self.statedriver then
flickerstreak@155 643 UnregisterStateDriver(self:GetFrame(),"reaction")
flickerstreak@155 644 end
flickerstreak@155 645 self.statedriver = rule
flickerstreak@157 646 self:BuildStateKeybinds(states)
flickerstreak@155 647 self:RefreshSecureState()
flickerstreak@155 648 end
flickerstreak@155 649
flickerstreak@148 650 -- pass unit=nil to set up the unit elsewhere, if you want something more complex
flickerstreak@148 651 function Bar:RegisterUnitWatch( unit, enable )
flickerstreak@148 652 local f = self:GetFrame()
flickerstreak@148 653 if unit then
flickerstreak@148 654 f:SetAttribute("unit",unit)
flickerstreak@148 655 end
flickerstreak@156 656 if enable then
Flick@234 657 if not self.unitwatch then
Flick@234 658 RegisterUnitWatch(self:GetFrame(),true)
Flick@234 659 end
flickerstreak@156 660 elseif self.unitwatch then
flickerstreak@156 661 UnregisterUnitWatch(self:GetFrame())
flickerstreak@156 662 end
flickerstreak@148 663 self.unitwatch = enable
flickerstreak@156 664 self:RefreshSecureState()
flickerstreak@148 665 end
flickerstreak@148 666
flickerstreak@155 667 function Bar:SetStateKeybind( key, state )
flickerstreak@155 668 local f = self:GetFrame()
flickerstreak@155 669 local binds = self.statebinds
flickerstreak@155 670 if not binds then
flickerstreak@155 671 binds = { }
flickerstreak@155 672 self.statebinds = binds
flickerstreak@155 673 end
flickerstreak@155 674
flickerstreak@155 675 -- clear the old binding, if any
flickerstreak@155 676 if binds[state] then
flickerstreak@155 677 SetOverrideBinding(f, false, binds[state], nil)
flickerstreak@155 678 end
flickerstreak@155 679
flickerstreak@155 680 if key then
flickerstreak@157 681 SetOverrideBindingClick(f, false, key, f:GetName(), state) -- state name is virtual mouse button
flickerstreak@155 682 end
flickerstreak@155 683 binds[state] = key
flickerstreak@155 684 end
flickerstreak@155 685
flickerstreak@155 686 function Bar:GetStateKeybind( state )
flickerstreak@155 687 if self.statebinds and state then
flickerstreak@155 688 return self.statebinds[state]
flickerstreak@155 689 end
flickerstreak@155 690 end
flickerstreak@155 691
flickerstreak@155 692 function Bar:UpdateDefaultStateAnchor()
flickerstreak@155 693 local point, frame, relPoint, x, y = self:GetAnchor()
flickerstreak@155 694 local f = self:GetFrame()
flickerstreak@155 695 f:SetAttribute("defaultAnchor-point",point)
flickerstreak@155 696 f:SetAttribute("defaultAnchor-relPoint",relPoint)
flickerstreak@155 697 f:SetAttribute("defaultAnchor-x",x)
flickerstreak@155 698 f:SetAttribute("defaultAnchor-y",y)
flickerstreak@155 699 self:SetFrameRef("defaultAnchor",_G[frame or "UIParent"])
flickerstreak@155 700 f:Execute([[
flickerstreak@155 701 for _, k in pairs(anchorKeys) do
flickerstreak@155 702 defaultAnchor[k] = self:GetAttribute("defaultAnchor-"..k)
flickerstreak@155 703 end
flickerstreak@155 704 defaultAnchor.frame = self:GetAttribute("frameref-defaultAnchor")
flickerstreak@155 705 ]])
flickerstreak@155 706 end
flickerstreak@155 707
flickerstreak@155 708 function Bar:UpdateDefaultStateAlpha()
flickerstreak@155 709 local f = self:GetFrame()
flickerstreak@155 710 f:SetAttribute("defaultAlpha",self:GetAlpha())
flickerstreak@155 711 f:Execute([[
flickerstreak@155 712 defaultAlpha = self:GetAttribute("defaultAlpha")
flickerstreak@155 713 ]])
flickerstreak@155 714 end
flickerstreak@157 715
flickerstreak@157 716 ---- secure state driver rules ----
flickerstreak@157 717
flickerstreak@157 718 local playerClass = select(2, UnitClass("player"))
flickerstreak@157 719 local function ClassFilter(...)
flickerstreak@157 720 for i = 1, select('#',...) do
flickerstreak@157 721 if playerClass == select(i,...) then
flickerstreak@157 722 return false
flickerstreak@157 723 end
flickerstreak@157 724 end
flickerstreak@157 725 return true
flickerstreak@157 726 end
flickerstreak@157 727
flickerstreak@157 728 local ruleformats = {
flickerstreak@157 729 stealth = { format = "stealth", filter = ClassFilter("ROGUE","DRUID") },
flickerstreak@157 730 nostealth = { format = "nostealth", filter = ClassFilter("ROGUE","DRUID") },
flickerstreak@157 731 shadowdance = { format = "bonusbar:2", filter = ClassFilter("ROGUE") },
flickerstreak@157 732 shadowform = { format = "form:1", filter = ClassFilter("PRIEST") },
flickerstreak@157 733 noshadowform = { format = "noform", filter = ClassFilter("PRIEST") },
flickerstreak@157 734 battle = { format = "stance:1", filter = ClassFilter("WARRIOR") },
flickerstreak@157 735 defensive = { format = "stance:2", filter = ClassFilter("WARRIOR") },
flickerstreak@157 736 berserker = { format = "stance:3", filter = ClassFilter("WARRIOR") },
flickerstreak@157 737 caster = { format = "form:0/2/4/5/6", filter = ClassFilter("DRUID") },
flickerstreak@157 738 bear = { format = "form:1", filter = ClassFilter("DRUID") },
flickerstreak@157 739 cat = { format = "form:3", filter = ClassFilter("DRUID") },
flickerstreak@157 740 tree = { format = "form:5", filter = ClassFilter("DRUID") },
flickerstreak@157 741 moonkin = { format = "form:5", filter = ClassFilter("DRUID") },
flickerstreak@178 742 demon = { format = "form:2", filter = ClassFilter("WARLOCK") },
flickerstreak@178 743 nodemon = { format = "noform", filter = ClassFilter("WARLOCK") },
flickerstreak@157 744 pet = { format = "pet" },
flickerstreak@157 745 nopet = { format = "nopet" },
flickerstreak@176 746 harm = { format = "@target,harm" },
flickerstreak@176 747 help = { format = "@target,help" },
flickerstreak@176 748 notarget = { format = "@target,noexists" },
flickerstreak@176 749 focusharm = { format = "@focus,harm" },
flickerstreak@176 750 focushelp = { format = "@focus,help" },
flickerstreak@176 751 nofocus = { format = "@focus,noexists" },
flickerstreak@157 752 raid = { format = "group:raid" },
flickerstreak@157 753 party = { format = "group:party" },
flickerstreak@157 754 solo = { format = "nogroup" },
flickerstreak@157 755 combat = { format = "combat" },
flickerstreak@157 756 nocombat = { format = "nocombat" },
flickerstreak@176 757 possess = { format = "@vehicle,noexists,bonusbar:5" },
flickerstreak@176 758 vehicle = { format = "@vehicle,exists,bonusbar:5" },
flickerstreak@157 759 }
flickerstreak@157 760
flickerstreak@157 761 function Bar.InitRuleFormats()
flickerstreak@157 762 local forms = { }
flickerstreak@157 763 for i = 1, GetNumShapeshiftForms() do
flickerstreak@157 764 local _, name = GetShapeshiftFormInfo(i)
flickerstreak@157 765 forms[name] = i;
flickerstreak@157 766 end
flickerstreak@157 767 -- use 9 if not found since 9 is never a valid stance/form
flickerstreak@157 768 local defensive = forms[GetSpellInfo(71)] or 9
flickerstreak@157 769 local berserker = forms[GetSpellInfo(2458)] or 9
flickerstreak@176 770 local bear = forms[GetSpellInfo(5487)] or 9
flickerstreak@157 771 local aquatic = forms[GetSpellInfo(1066)] or 9
flickerstreak@157 772 local cat = forms[GetSpellInfo(768)] or 9
flickerstreak@157 773 local travel = forms[GetSpellInfo(783)] or 9
flickerstreak@157 774 local tree = forms[GetSpellInfo(33891)] or 9
flickerstreak@157 775 local moonkin = forms[GetSpellInfo(24858)] or 9
flickerstreak@157 776 local flight = forms[GetSpellInfo(40120)] or forms[GetSpellInfo(33943)] or 9
flickerstreak@157 777
flickerstreak@157 778 ruleformats.defensive.format = "stance:"..defensive
flickerstreak@157 779 ruleformats.berserker.format = "stance:"..berserker
flickerstreak@157 780 ruleformats.caster.format = format("form:0/%d/%d/%d", aquatic, travel, flight)
flickerstreak@157 781 ruleformats.bear.format = "form:"..bear
flickerstreak@157 782 ruleformats.cat.format = "form:"..cat
flickerstreak@157 783 ruleformats.tree.format = "form:"..tree
flickerstreak@157 784 ruleformats.moonkin.format = "form:"..moonkin
flickerstreak@157 785 end
flickerstreak@157 786
flickerstreak@157 787 function Bar:BuildStateRule(states)
flickerstreak@157 788 -- states is a table :
flickerstreak@157 789 -- states[statename].rule = {
flickerstreak@157 790 -- order = #,
flickerstreak@157 791 -- type = "default"/"custom"/"any"/"all",
flickerstreak@157 792 -- values = { ... }, -- keys of ruleformats[]
flickerstreak@157 793 -- custom = "...",
flickerstreak@157 794 -- }
flickerstreak@157 795 local rules = { }
flickerstreak@157 796 local default
flickerstreak@157 797
flickerstreak@157 798 for idx, state in ipairs(fieldsort(states, "rule", "order")) do
flickerstreak@157 799 local c = states[state].rule
flickerstreak@157 800 local type = c.type
flickerstreak@157 801 if type == "default" then
flickerstreak@157 802 default = default or state
flickerstreak@157 803 elseif type == "custom" then
flickerstreak@157 804 if c.custom then
flickerstreak@157 805 -- strip out all spaces from the custom rule
flickerstreak@157 806 table.insert(rules, format("%s %s", c.custom:gsub("%s",""), state))
flickerstreak@157 807 end
flickerstreak@157 808 elseif type == "any" or type == "all" then
flickerstreak@157 809 if c.values then
flickerstreak@157 810 local clauses = { }
flickerstreak@157 811 for key, value in pairs(c.values) do
flickerstreak@157 812 if ruleformats[key] and not ruleformats[key].filter then
flickerstreak@157 813 table.insert(clauses, ruleformats[key].format)
flickerstreak@157 814 end
flickerstreak@157 815 end
flickerstreak@157 816 if #clauses > 0 then
flickerstreak@157 817 local sep = (type == "any") and "][" or ","
flickerstreak@157 818 table.insert(rules, format("[%s] %s", table.concat(clauses,sep), state))
flickerstreak@157 819 end
flickerstreak@157 820 end
flickerstreak@157 821 end
flickerstreak@157 822 end
flickerstreak@157 823 -- make sure that the default, if any, is last
flickerstreak@157 824 if default then
flickerstreak@157 825 table.insert(rules, default)
flickerstreak@157 826 end
flickerstreak@157 827 return table.concat(rules,";")
flickerstreak@157 828 end
flickerstreak@157 829
flickerstreak@157 830 function Bar:BuildStateKeybinds( states )
flickerstreak@157 831 if states then
flickerstreak@157 832 for name, state in pairs(states) do
flickerstreak@157 833 local rule = tfetch(state, "rule")
flickerstreak@157 834 if rule and rule.type == "keybind" then
flickerstreak@157 835 self:SetStateKeybind(rule.keybind, name)
flickerstreak@157 836 else
flickerstreak@157 837 self:SetStateKeybind(nil, name) -- this clears an existing keybind
flickerstreak@157 838 end
flickerstreak@157 839 end
flickerstreak@157 840 end
flickerstreak@157 841 end
flickerstreak@157 842