annotate State.lua @ 101:3699d7dad312

added stateful anchoring
author Flick <flickerstreak@gmail.com>
date Sat, 25 Oct 2008 18:49:18 +0000
parents a44173c7a82c
children 890e4c4ab143
rev   line source
flickerstreak@25 1 --[[
flickerstreak@62 2 ReAction bar state driver interface
flickerstreak@25 3
flickerstreak@25 4 --]]
flickerstreak@25 5
flickerstreak@25 6 -- local imports
flickerstreak@25 7 local ReAction = ReAction
flickerstreak@25 8 local L = ReAction.L
flickerstreak@25 9 local _G = _G
flickerstreak@90 10 local format = string.format
flickerstreak@25 11 local InCombatLockdown = InCombatLockdown
flickerstreak@90 12 local RegisterStateDriver = RegisterStateDriver
flickerstreak@25 13
flickerstreak@80 14 ReAction:UpdateRevision("$Revision$")
flickerstreak@77 15
flickerstreak@25 16 -- module declaration
flickerstreak@25 17 local moduleID = "State"
flickerstreak@65 18 local module = ReAction:NewModule( moduleID, "AceEvent-3.0" )
flickerstreak@62 19
flickerstreak@64 20 -- Utility --
flickerstreak@62 21
flickerstreak@62 22 -- traverse a table tree by key list and fetch the result or first nil
flickerstreak@62 23 local function tfetch(t, ...)
flickerstreak@62 24 for i = 1, select('#', ...) do
flickerstreak@62 25 t = t and t[select(i, ...)]
flickerstreak@62 26 end
flickerstreak@62 27 return t
flickerstreak@62 28 end
flickerstreak@62 29
flickerstreak@62 30 -- traverse a table tree by key list and build tree as necessary
flickerstreak@62 31 local function tbuild(t, ...)
flickerstreak@62 32 for i = 1, select('#', ...) do
flickerstreak@62 33 local key = select(i, ...)
flickerstreak@62 34 if not t[key] then t[key] = { } end
flickerstreak@62 35 t = t[key]
flickerstreak@62 36 end
flickerstreak@62 37 return t
flickerstreak@62 38 end
flickerstreak@62 39
flickerstreak@75 40 -- return a new array of keys of table 't', sorted by comparing
flickerstreak@75 41 -- sub-fields (obtained via tfetch) of the table values
flickerstreak@75 42 local function fieldsort( t, ... )
flickerstreak@75 43 local r = { }
flickerstreak@75 44 for k in pairs(t) do
flickerstreak@75 45 table.insert(r,k)
flickerstreak@75 46 end
flickerstreak@75 47 local path = { ... }
flickerstreak@75 48 table.sort(r, function(lhs, rhs)
flickerstreak@75 49 local olhs = tfetch(t[lhs], unpack(path)) or 0
flickerstreak@75 50 local orhs = tfetch(t[rhs], unpack(path)) or 0
flickerstreak@75 51 return olhs < orhs
flickerstreak@75 52 end)
flickerstreak@75 53 return r
flickerstreak@75 54 end
flickerstreak@75 55
flickerstreak@101 56 -- set a frame-ref, if the frame is valid, or set nil to the
flickerstreak@101 57 -- corresponding attribute
flickerstreak@101 58 local function SetFrameRef(frame, name, refFrame)
flickerstreak@101 59 if refFrame then
flickerstreak@101 60 local _, explicit = refFrame:IsProtected()
flickerstreak@101 61 if not explicit then
flickerstreak@101 62 refFrame = nil
flickerstreak@101 63 end
flickerstreak@101 64 end
flickerstreak@101 65 if refFrame then
flickerstreak@101 66 frame:SetFrameRef(name,refFrame)
flickerstreak@101 67 else
flickerstreak@101 68 frame:SetAttribute("frameref-"..name,nil)
flickerstreak@101 69 end
flickerstreak@101 70 end
flickerstreak@101 71
flickerstreak@68 72
flickerstreak@90 73 local InitRules, ApplyStates, CleanupStates, SetProperty, GetProperty, RegisterProperty, ShowAll
flickerstreak@75 74
flickerstreak@75 75 -- PRIVATE --
flickerstreak@64 76 do
flickerstreak@90 77
flickerstreak@90 78 -- the field names must match the field names of the options table, below
flickerstreak@90 79 -- the field values are secure snippets
flickerstreak@90 80 local properties = {
flickerstreak@90 81 hide =
flickerstreak@90 82 [[
flickerstreak@90 83 local h = hide and hide[state] and not showAll
flickerstreak@90 84 if h ~= hidden then
flickerstreak@90 85 if h then
flickerstreak@90 86 self:Hide()
flickerstreak@90 87 else
flickerstreak@90 88 self:Show()
flickerstreak@90 89 end
flickerstreak@90 90 hidden = h
flickerstreak@90 91 end
flickerstreak@98 92 if showAll then
flickerstreak@98 93 control:CallMethod("UpdateHiddenLabel",hide[state])
flickerstreak@98 94 end
flickerstreak@90 95 ]],
flickerstreak@90 96
flickerstreak@90 97 --keybindState TODO: broken
flickerstreak@90 98
flickerstreak@101 99 anchorEnable =
flickerstreak@101 100 [[
flickerstreak@101 101 local old_anchor = anchorstate
flickerstreak@101 102 anchorstate = (anchorEnable and anchorEnable[state]) and state
flickerstreak@101 103 if old_anchor ~= anchorstate or not set_state then
flickerstreak@101 104 if anchorstate and anchorPoint then
flickerstreak@101 105 if anchorPoint[state] then
flickerstreak@101 106 self:ClearAllPoints()
flickerstreak@101 107 local f = self:GetAttribute("frameref-anchor-"..anchorstate)
flickerstreak@101 108 if f then
flickerstreak@101 109 self:SetPoint(anchorPoint[state], f, anchorRelPoint[state], anchorX[state], anchorY[state])
flickerstreak@101 110 end
flickerstreak@101 111 end
flickerstreak@101 112 elseif defaultAnchor and defaultAnchor.point then
flickerstreak@101 113 self:ClearAllPoints()
flickerstreak@101 114 self:SetPoint(defaultAnchor.point, defaultAnchor.frame,
flickerstreak@101 115 defaultAnchor.relPoint, defaultAnchor.x, defaultAnchor.y)
flickerstreak@101 116 end
flickerstreak@101 117 end
flickerstreak@101 118 ]],
flickerstreak@101 119 -- anchorEnable handles all the other bits
flickerstreak@101 120 anchorFrame = true,
flickerstreak@90 121 anchorPoint = true,
flickerstreak@90 122 anchorRelPoint = true,
flickerstreak@90 123 anchorX = true,
flickerstreak@90 124 anchorY = true,
flickerstreak@90 125 enableScale = true,
flickerstreak@90 126 scale = true,
flickerstreak@90 127 }
flickerstreak@90 128
flickerstreak@92 129 local weak = { __mode = "k" }
flickerstreak@92 130 local statedrivers = setmetatable( { }, weak )
flickerstreak@92 131 local keybinds = setmetatable( { }, weak )
flickerstreak@90 132
flickerstreak@90 133 --
flickerstreak@90 134 -- Secure Handler Snippets
flickerstreak@90 135 --
flickerstreak@90 136 local SetHandlerData, SetStateDriver, SetStateKeybind, RefreshState
flickerstreak@90 137 do
flickerstreak@90 138 local stateHandler_propInit =
flickerstreak@90 139 [[
flickerstreak@90 140 propfuncs = table.new()
flickerstreak@90 141 local proplist = self:GetAttribute("prop-func-list")
flickerstreak@90 142 for s in string.gmatch(proplist, "(%w+)") do
flickerstreak@90 143 table.insert(propfuncs, s)
flickerstreak@90 144 end
flickerstreak@90 145 ]]
flickerstreak@90 146
flickerstreak@90 147 local onStateHandler =
flickerstreak@90 148 -- function _onstate-reaction( self, stateid, newstate )
flickerstreak@90 149 [[
flickerstreak@101 150 set_state = newstate
flickerstreak@90 151
flickerstreak@90 152 local oldState = state
flickerstreak@90 153 state = state_override or set_state or state
flickerstreak@90 154 for i = 1, #propfuncs do
flickerstreak@90 155 control:RunAttribute("func-"..propfuncs[i])
flickerstreak@90 156 end
flickerstreak@90 157
flickerstreak@90 158 control:ChildUpdate()
flickerstreak@90 159 ]]
flickerstreak@90 160
flickerstreak@90 161 local onClickHandler =
flickerstreak@90 162 -- function OnClick( self, button, down )
flickerstreak@90 163 [[
flickerstreak@90 164 if state_override == button then
flickerstreak@90 165 state_override = nil -- toggle
flickerstreak@90 166 else
flickerstreak@90 167 state_override = button
flickerstreak@90 168 end
flickerstreak@90 169 ]] .. onStateHandler
flickerstreak@90 170
flickerstreak@90 171 -- Construct a lua assignment as a code string and execute it within the header
flickerstreak@90 172 -- frame's sandbox. 'value' must be a string, boolean, number, or nil. If called
flickerstreak@90 173 -- with four arguments, then it treats 'varname' as an existing global table and
flickerstreak@90 174 -- sets a key-value pair. For a slight efficiency boost, pass the values in as
flickerstreak@90 175 -- attributes and fetch them as attributes from the snippet code, to leverage snippet
flickerstreak@90 176 -- caching.
flickerstreak@90 177 function SetHandlerData( bar, varname, value, key )
flickerstreak@90 178 local f = bar:GetFrame()
flickerstreak@90 179 f:SetAttribute("data-varname",varname)
flickerstreak@90 180 f:SetAttribute("data-value", value)
flickerstreak@90 181 f:SetAttribute("data-key", key)
flickerstreak@90 182 f:Execute(
flickerstreak@90 183 [[
flickerstreak@90 184 local name = self:GetAttribute("data-varname")
flickerstreak@90 185 local value = self:GetAttribute("data-value")
flickerstreak@90 186 local key = self:GetAttribute("data-key")
flickerstreak@90 187 if name then
flickerstreak@90 188 if key then
flickerstreak@90 189 if not _G[name] then
flickerstreak@90 190 _G[name] = table.new()
flickerstreak@90 191 end
flickerstreak@90 192 _G[name][key] = value
flickerstreak@90 193 else
flickerstreak@90 194 _G[name] = value
flickerstreak@90 195 end
flickerstreak@90 196 end
flickerstreak@90 197 ]])
flickerstreak@90 198 end
flickerstreak@90 199
flickerstreak@90 200 function SetDefaultAnchor( bar )
flickerstreak@90 201 local point, frame, relPoint, x, y = bar:GetAnchor()
flickerstreak@90 202 SetHandlerData(bar, "defaultAnchor", point, "point")
flickerstreak@90 203 SetHandlerData(bar, "defaultAnchor", relPoint, "relPoint")
flickerstreak@90 204 SetHandlerData(bar, "defaultAnchor", x, "x")
flickerstreak@90 205 SetHandlerData(bar, "defaultAnchor", y, "y")
flickerstreak@90 206
flickerstreak@101 207 local f = bar:GetFrame()
flickerstreak@101 208 SetFrameRef(f, "defaultAnchor", _G[frame or "UIParent"])
flickerstreak@101 209 f:Execute(
flickerstreak@101 210 [[
flickerstreak@101 211 defaultAnchor.frame = self:GetAttribute("frameref-defaultAnchor")
flickerstreak@101 212 ]])
flickerstreak@90 213 end
flickerstreak@90 214
flickerstreak@90 215 function RefreshState( bar )
flickerstreak@90 216 SetDefaultAnchor(bar)
flickerstreak@95 217 bar:GetFrame():Execute(
flickerstreak@95 218 [[
flickerstreak@95 219 if self:GetAttribute("reaction-refresh") then
flickerstreak@95 220 control:RunAttribute("reaction-refresh")
flickerstreak@95 221 end
flickerstreak@95 222 ]])
flickerstreak@90 223 end
flickerstreak@90 224
flickerstreak@90 225 function SetStateDriver( bar, rule )
flickerstreak@90 226 local f = bar:GetFrame()
flickerstreak@90 227
flickerstreak@98 228 if not f.UpdateHiddenLabel then
flickerstreak@98 229 function f:UpdateHiddenLabel(hide)
flickerstreak@98 230 bar:SetLabelSubtext( hide and L["Hidden"] )
flickerstreak@98 231 end
flickerstreak@98 232 end
flickerstreak@98 233
flickerstreak@90 234 local props = { }
flickerstreak@90 235 for p, h in pairs(properties) do
flickerstreak@90 236 if type(h) == "string" then
flickerstreak@90 237 table.insert(props,p)
flickerstreak@90 238 f:SetAttribute("func-"..p, h)
flickerstreak@90 239 end
flickerstreak@90 240 end
flickerstreak@90 241 f:SetAttribute("prop-func-list", table.concat(props," "))
flickerstreak@90 242 f:Execute(stateHandler_propInit)
flickerstreak@90 243 f:SetAttribute("reaction-refresh", onStateHandler)
flickerstreak@90 244
flickerstreak@90 245 if rule and #rule > 0 then
flickerstreak@90 246 f:SetAttribute( "_onstate-reaction", onStateHandler )
flickerstreak@90 247 RegisterStateDriver(f, "reaction", rule)
flickerstreak@90 248 statedrivers[bar] = rule
flickerstreak@90 249 elseif statedrivers[bar] then
flickerstreak@90 250 UnregisterStateDriver(f, "reaction")
flickerstreak@90 251 f:SetAttribute( "_onstate-reaction", nil )
flickerstreak@90 252 statedrivers[bar] = nil
flickerstreak@90 253 end
flickerstreak@90 254 end
flickerstreak@90 255
flickerstreak@90 256 function SetStateKeybind( bar, key, state )
flickerstreak@90 257 local f = bar:GetFrame()
flickerstreak@90 258
flickerstreak@90 259 local kb = keybinds[bar]
flickerstreak@90 260 if kb == nil then
flickerstreak@90 261 if key == nil then
flickerstreak@90 262 -- nothing to do
flickerstreak@90 263 return
flickerstreak@90 264 end
flickerstreak@90 265 kb = { }
flickerstreak@90 266 keybinds[bar] = kb
flickerstreak@90 267 end
flickerstreak@90 268
flickerstreak@90 269 -- clear the old binding, if any
flickerstreak@90 270 if kb[state] then
flickerstreak@90 271 SetOverrideBinding(f, false, kb[state], nil)
flickerstreak@90 272 end
flickerstreak@90 273 kb[state] = key
flickerstreak@90 274
flickerstreak@90 275 if key then
flickerstreak@90 276 f:SetAttribute("_onclick", onClickHandler)
flickerstreak@90 277 SetOverrideBindingClick(f, false, key, state, nil) -- state name is the virtual mouse button
flickerstreak@90 278 end
flickerstreak@90 279 end
flickerstreak@90 280 end
flickerstreak@90 281
flickerstreak@67 282 -- As far as I can tell the macro clauses are NOT locale-specific.
flickerstreak@67 283 local ruleformats = {
flickerstreak@67 284 stealth = "stealth",
flickerstreak@67 285 nostealth = "nostealth",
flickerstreak@67 286 shadowform = "form:1",
flickerstreak@67 287 noshadowform = "noform",
flickerstreak@67 288 pet = "pet",
flickerstreak@67 289 nopet = "nopet",
flickerstreak@67 290 harm = "target=target,harm",
flickerstreak@67 291 help = "target=target,help",
flickerstreak@67 292 notarget = "target=target,noexists",
flickerstreak@67 293 focusharm = "target=focus,harm",
flickerstreak@67 294 focushelp = "target=focus,help",
flickerstreak@67 295 nofocus = "target=focus,noexists",
flickerstreak@67 296 raid = "group:raid",
flickerstreak@67 297 party = "group:party",
flickerstreak@67 298 solo = "nogroup",
flickerstreak@67 299 combat = "combat",
flickerstreak@67 300 nocombat = "nocombat",
flickerstreak@75 301 possess = "bonusbar:5",
flickerstreak@67 302 }
flickerstreak@65 303
flickerstreak@97 304 -- Have to do these shenanigans instead of hardcoding the stances/forms because the
flickerstreak@97 305 -- ordering varies if the character is missing a form. For warriors this is rarely
flickerstreak@97 306 -- a problem (c'mon, who actually skips the level 10 def stance quest?) but for druids
flickerstreak@97 307 -- it can be. Some people never bother to do the aquatic form quest until well past
flickerstreak@97 308 -- when they get cat form, and stance 5/6 can be flight, tree, or moonkin depending
flickerstreak@97 309 -- on talents.
flickerstreak@65 310 function InitRules()
flickerstreak@65 311 local forms = { }
flickerstreak@67 312 -- sort by icon since it's locale-independent
flickerstreak@65 313 for i = 1, GetNumShapeshiftForms() do
flickerstreak@97 314 local icon, name, active = GetShapeshiftFormInfo(i)
flickerstreak@97 315 -- if it's the current form, the icon is wrong (Ability_Spell_WispSplode)
flickerstreak@97 316 -- so capture it from the spell info directly
flickerstreak@97 317 if active then
flickerstreak@97 318 local _1, _2
flickerstreak@97 319 _1, _2, icon = GetSpellInfo(name)
flickerstreak@97 320 end
flickerstreak@65 321 forms[icon] = i;
flickerstreak@65 322 end
flickerstreak@65 323 -- use 9 if not found since 9 is never a valid stance/form
flickerstreak@65 324 local defensive = forms["Interface\\Icons\\Ability_Warrior_DefensiveStance"] or 9
flickerstreak@65 325 local berserker = forms["Interface\\Icons\\Ability_Racial_Avatar"] or 9
flickerstreak@65 326 local bear = forms["Interface\\Icons\\Ability_Racial_BearForm"] or 9 -- bear and dire bear share the same icon
flickerstreak@65 327 local aquatic = forms["Interface\\Icons\\Ability_Druid_AquaticForm"] or 9
flickerstreak@65 328 local cat = forms["Interface\\Icons\\Ability_Druid_CatForm"] or 9
flickerstreak@65 329 local travel = forms["Interface\\Icons\\Ability_Druid_TravelForm"] or 9
flickerstreak@75 330 local tree = forms["Interface\\Icons\\Ability_Druid_TreeofLife"] or 9
flickerstreak@75 331 local moonkin = forms["Interface\\Icons\\Spell_Nature_ForceOfNature"] or 9
flickerstreak@67 332 local flight = forms["Interface\\Icons\\Ability_Druid_FlightForm"] or 9 -- flight and swift flight share the same icon
flickerstreak@65 333
flickerstreak@75 334 ruleformats.battle = "stance:1"
flickerstreak@75 335 ruleformats.defensive = format("stance:%d",defensive)
flickerstreak@75 336 ruleformats.berserker = format("stance:%d",berserker)
flickerstreak@75 337 ruleformats.caster = format("form:0/%d/%d/%d",aquatic, travel, flight)
flickerstreak@75 338 ruleformats.bear = format("form:%d",bear)
flickerstreak@75 339 ruleformats.cat = format("form:%d",cat)
flickerstreak@75 340 ruleformats.tree = format("form:%d",tree)
flickerstreak@75 341 ruleformats.moonkin = format("form:%d",moonkin)
flickerstreak@64 342 end
flickerstreak@62 343
flickerstreak@90 344 local function BuildRule(states)
flickerstreak@75 345 local rules = { }
flickerstreak@75 346 local default
flickerstreak@75 347
flickerstreak@75 348 for idx, state in ipairs(fieldsort(states, "rule", "order")) do
flickerstreak@75 349 local c = states[state].rule
flickerstreak@75 350 local type = c.type
flickerstreak@75 351 if type == "default" then
flickerstreak@75 352 default = default or state
flickerstreak@75 353 elseif type == "custom" then
flickerstreak@75 354 if c.custom then
flickerstreak@75 355 -- strip out all spaces from the custom rule
flickerstreak@75 356 table.insert(rules, format("%s %s", c.custom:gsub("%s",""), state))
flickerstreak@75 357 end
flickerstreak@95 358 elseif type == "any" or type == "all" then
flickerstreak@75 359 if c.values then
flickerstreak@75 360 local clauses = { }
flickerstreak@75 361 for key, value in pairs(c.values) do
flickerstreak@75 362 table.insert(clauses, ruleformats[key])
flickerstreak@75 363 end
flickerstreak@75 364 if #clauses > 0 then
flickerstreak@95 365 local sep = (type == "any") and "][" or ","
flickerstreak@95 366 table.insert(rules, format("[%s] %s", table.concat(clauses,sep), state))
flickerstreak@75 367 end
flickerstreak@75 368 end
flickerstreak@75 369 end
flickerstreak@75 370 end
flickerstreak@75 371 -- make sure that the default, if any, is last
flickerstreak@75 372 if default then
flickerstreak@75 373 table.insert(rules, default)
flickerstreak@75 374 end
flickerstreak@90 375 return table.concat(rules,";")
flickerstreak@90 376 end
flickerstreak@90 377
flickerstreak@90 378 local function BuildKeybinds( bar, states )
flickerstreak@90 379 for name, state in pairs(states) do
flickerstreak@90 380 local type = tfetch(state, "rule", "type")
flickerstreak@90 381 if type == "keybind" then
flickerstreak@90 382 local key = tfetch(state, "rule", "keybind")
flickerstreak@90 383 SetStateKeybind(bar, key, name)
flickerstreak@90 384 else
flickerstreak@90 385 SetStateKeybind(bar, nil, name) -- this clears an existing keybind
flickerstreak@90 386 end
flickerstreak@90 387 end
flickerstreak@90 388 end
flickerstreak@90 389
flickerstreak@90 390 function GetProperty( bar, state, propname )
flickerstreak@90 391 return tfetch(module.db.profile.bars, bar:GetName(), "states", state, propname)
flickerstreak@90 392 end
flickerstreak@90 393
flickerstreak@90 394 function SetProperty( bar, state, propname, value )
flickerstreak@90 395 local s = tbuild(module.db.profile.bars, bar:GetName(), "states", state)
flickerstreak@90 396 s[propname] = value
flickerstreak@90 397 SetHandlerData(bar, propname, value, state)
flickerstreak@90 398 RefreshState(bar)
flickerstreak@90 399 end
flickerstreak@90 400
flickerstreak@90 401 function RegisterProperty( propname, snippet )
flickerstreak@90 402 properties[propname] = snippet or true
flickerstreak@90 403 for _, bar in ReAction:IterateBars() do
flickerstreak@90 404 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@90 405 if states then
flickerstreak@90 406 for name, s in pairs(states) do
flickerstreak@90 407 SetHandlerData(bar, propname, s[propname], name)
flickerstreak@90 408 end
flickerstreak@90 409 SetStateDriver(bar, BuildRule(states))
flickerstreak@90 410 RefreshState(bar)
flickerstreak@90 411 end
flickerstreak@90 412 end
flickerstreak@90 413 end
flickerstreak@90 414
flickerstreak@90 415 function UnregisterProperty( propname )
flickerstreak@90 416 properties[propname] = nil
flickerstreak@90 417 for _, bar in ReAction:IterateBars() do
flickerstreak@90 418 SetHandlerData(bar, propname, nil)
flickerstreak@90 419 SetStateDriver(bar, BuildRule(states))
flickerstreak@90 420 RefreshState(bar)
flickerstreak@90 421 end
flickerstreak@75 422 end
flickerstreak@75 423
flickerstreak@75 424 function ApplyStates( bar )
flickerstreak@75 425 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@75 426 if states then
flickerstreak@90 427 for propname in pairs(properties) do
flickerstreak@90 428 for name, s in pairs(states) do
flickerstreak@101 429 if propname == "anchorFrame" then
flickerstreak@101 430 SetFrameRef(bar:GetFrame(), "anchor-"..name, _G[s.anchorFrame])
flickerstreak@101 431 else
flickerstreak@101 432 SetHandlerData(bar, propname, s[propname], name)
flickerstreak@101 433 end
flickerstreak@90 434 end
flickerstreak@75 435 end
flickerstreak@90 436 BuildKeybinds(bar, states)
flickerstreak@96 437 SetHandlerData(bar, "showAll", ReAction:GetConfigMode())
flickerstreak@90 438 SetStateDriver(bar, BuildRule(states))
flickerstreak@90 439 RefreshState(bar)
flickerstreak@68 440 end
flickerstreak@68 441 end
flickerstreak@68 442
flickerstreak@90 443 function CleanupStates( bar )
flickerstreak@90 444 SetStateDriver(bar, nil)
flickerstreak@90 445 end
flickerstreak@90 446
flickerstreak@90 447 function ShowAll( bar, show )
flickerstreak@92 448 if statedrivers[bar] then
flickerstreak@92 449 SetHandlerData(bar, "showAll", show)
flickerstreak@92 450 RefreshState(bar)
flickerstreak@92 451 end
flickerstreak@90 452 end
flickerstreak@64 453 end
flickerstreak@64 454
flickerstreak@64 455
flickerstreak@68 456
flickerstreak@68 457 -- module event handlers --
flickerstreak@68 458
flickerstreak@65 459 function module:OnInitialize()
flickerstreak@65 460 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@65 461 {
flickerstreak@65 462 profile = {
flickerstreak@65 463 bars = { },
flickerstreak@65 464 }
flickerstreak@65 465 }
flickerstreak@65 466 )
flickerstreak@65 467
flickerstreak@97 468 self:RegisterEvent("UPDATE_SHAPESHIFT_FORMS")
flickerstreak@65 469
flickerstreak@65 470 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@65 471
flickerstreak@65 472 ReAction.RegisterCallback(self, "OnCreateBar","OnRefreshBar")
flickerstreak@90 473 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@65 474 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@65 475 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@65 476 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@65 477 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@65 478 end
flickerstreak@65 479
flickerstreak@97 480 function module:OnEnable()
flickerstreak@97 481 self:UPDATE_SHAPESHIFT_FORMS() -- it doesn't fire on a /reloadui
flickerstreak@97 482 end
flickerstreak@97 483
flickerstreak@97 484 function module:UPDATE_SHAPESHIFT_FORMS()
flickerstreak@97 485 -- Re-parse the rules table according to the new form list.
flickerstreak@97 486 -- This happens both at initial login (after PLAYER_ENTERING_WORLD)
flickerstreak@97 487 -- as well as when gaining new abilities.
flickerstreak@97 488 InitRules()
flickerstreak@97 489 for name, bar in ReAction:IterateBars() do
flickerstreak@97 490 self:OnRefreshBar(nil,bar,name)
flickerstreak@66 491 end
flickerstreak@65 492 end
flickerstreak@65 493
flickerstreak@65 494 function module:OnRefreshBar(event, bar, name)
flickerstreak@65 495 local c = self.db.profile.bars[name]
flickerstreak@65 496 if c then
flickerstreak@68 497 ApplyStates(bar)
flickerstreak@65 498 end
flickerstreak@65 499 end
flickerstreak@65 500
flickerstreak@90 501 function module:OnDestroyBar(event, bar, name)
flickerstreak@90 502 CleanupStates(bar)
flickerstreak@90 503 end
flickerstreak@90 504
flickerstreak@65 505 function module:OnEraseBar(event, bar, name)
flickerstreak@65 506 self.db.profile.bars[name] = nil
flickerstreak@65 507 end
flickerstreak@65 508
flickerstreak@65 509 function module:OnRenameBar(event, bar, oldname, newname)
flickerstreak@75 510 local bars = self.db.profile.bars
flickerstreak@65 511 bars[newname], bars[oldname] = bars[oldname], nil
flickerstreak@65 512 end
flickerstreak@65 513
flickerstreak@65 514 function module:OnConfigModeChanged(event, mode)
flickerstreak@90 515 for name, bar in ReAction:IterateBars() do
flickerstreak@90 516 if self.db.profile.bars[name] then
flickerstreak@90 517 ShowAll(bar, mode)
flickerstreak@90 518 end
flickerstreak@90 519 end
flickerstreak@65 520 end
flickerstreak@65 521
flickerstreak@64 522
flickerstreak@64 523
flickerstreak@64 524 -- Options --
flickerstreak@64 525
flickerstreak@79 526 local CreateBarOptions, RegisterPropertyOptions
flickerstreak@62 527 do
flickerstreak@79 528 local playerClass = select(2, UnitClass("player"))
flickerstreak@62 529 local function ClassCheck(...)
flickerstreak@62 530 for i = 1, select('#',...) do
flickerstreak@79 531 if playerClass == select(i,...) then
flickerstreak@62 532 return false
flickerstreak@62 533 end
flickerstreak@62 534 end
flickerstreak@62 535 return true
flickerstreak@62 536 end
flickerstreak@62 537
flickerstreak@64 538 -- pre-sorted by the order they should appear in
flickerstreak@64 539 local rules = {
flickerstreak@64 540 -- rule hidden fields
flickerstreak@64 541 { "stance", ClassCheck("WARRIOR"), { {battle = L["Battle Stance"]}, {defensive = L["Defensive Stance"]}, {berserker = L["Berserker Stance"]} } },
flickerstreak@75 542 { "form", ClassCheck("DRUID"), { {caster = L["Caster Form"]}, {bear = L["Bear Form"]}, {cat = L["Cat Form"]}, {tree = L["Tree of Life"]}, {moonkin = L["Moonkin Form"]} } },
flickerstreak@64 543 { "stealth", ClassCheck("ROGUE","DRUID"), { {stealth = L["Stealth"]}, {nostealth = L["No Stealth"]} } },
flickerstreak@64 544 { "shadow", ClassCheck("PRIEST"), { {shadowform = L["Shadowform"]}, {noshadowform = L["No Shadowform"]} } },
flickerstreak@64 545 { "pet", ClassCheck("HUNTER","WARLOCK"), { {pet = L["With Pet"]}, {nopet = L["Without Pet"]} } },
flickerstreak@64 546 { "target", false, { {harm = L["Hostile Target"]}, {help = L["Friendly Target"]}, {notarget = L["No Target"]} } },
flickerstreak@64 547 { "focus", false, { {focusharm = L["Hostile Focus"]}, {focushelp = L["Friendly Focus"]}, {nofocus = L["No Focus"]} } },
flickerstreak@75 548 { "possess", false, { {possess = L["Mind Control"]} } },
flickerstreak@64 549 { "group", false, { {raid = L["Raid"]}, {party = L["Party"]}, {solo = L["Solo"]} } },
flickerstreak@64 550 { "combat", false, { {combat = L["In Combat"]}, {nocombat = L["Out of Combat"]} } },
flickerstreak@62 551 }
flickerstreak@62 552
flickerstreak@64 553 local ruleSelect = { }
flickerstreak@64 554 local ruleMap = { }
flickerstreak@64 555 local optionMap = setmetatable({},{__mode="k"})
flickerstreak@62 556
flickerstreak@68 557 local pointTable = {
flickerstreak@68 558 NONE = " ",
flickerstreak@68 559 CENTER = L["Center"],
flickerstreak@68 560 LEFT = L["Left"],
flickerstreak@68 561 RIGHT = L["Right"],
flickerstreak@68 562 TOP = L["Top"],
flickerstreak@68 563 BOTTOM = L["Bottom"],
flickerstreak@68 564 TOPLEFT = L["Top Left"],
flickerstreak@68 565 TOPRIGHT = L["Top Right"],
flickerstreak@68 566 BOTTOMLEFT = L["Bottom Left"],
flickerstreak@68 567 BOTTOMRIGHT = L["Bottom Right"],
flickerstreak@68 568 }
flickerstreak@68 569
flickerstreak@64 570 -- unpack rules table into ruleSelect and ruleMap
flickerstreak@64 571 for _, c in ipairs(rules) do
flickerstreak@64 572 local rule, hidden, fields = unpack(c)
flickerstreak@64 573 if not hidden then
flickerstreak@64 574 for _, field in ipairs(fields) do
flickerstreak@64 575 local key, label = next(field)
flickerstreak@64 576 table.insert(ruleSelect, label)
flickerstreak@64 577 table.insert(ruleMap, key)
flickerstreak@62 578 end
flickerstreak@62 579 end
flickerstreak@62 580 end
flickerstreak@62 581
flickerstreak@79 582 local stateOptions = {
flickerstreak@79 583 ordering = {
flickerstreak@79 584 name = L["Info"],
flickerstreak@79 585 order = 1,
flickerstreak@79 586 type = "group",
flickerstreak@79 587 args = {
flickerstreak@79 588 delete = {
flickerstreak@79 589 name = L["Delete this State"],
flickerstreak@79 590 order = -1,
flickerstreak@79 591 type = "execute",
flickerstreak@79 592 func = "DeleteState",
flickerstreak@79 593 },
flickerstreak@79 594 rename = {
flickerstreak@79 595 name = L["Name"],
flickerstreak@79 596 order = 1,
flickerstreak@79 597 type = "input",
flickerstreak@79 598 get = "GetName",
flickerstreak@79 599 set = "SetStateName",
flickerstreak@79 600 pattern = "^%w*$",
flickerstreak@79 601 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@79 602 },
flickerstreak@79 603 ordering = {
flickerstreak@79 604 name = L["Evaluation Order"],
flickerstreak@79 605 desc = L["State transitions are evaluated in the order listed:\nMove a state up or down to change the order"],
flickerstreak@79 606 order = 2,
flickerstreak@79 607 type = "group",
flickerstreak@79 608 inline = true,
flickerstreak@79 609 args = {
flickerstreak@79 610 up = {
flickerstreak@79 611 name = L["Up"],
flickerstreak@79 612 order = 1,
flickerstreak@79 613 type = "execute",
flickerstreak@79 614 width = "half",
flickerstreak@79 615 func = "MoveStateUp",
flickerstreak@79 616 },
flickerstreak@79 617 down = {
flickerstreak@79 618 name = L["Down"],
flickerstreak@79 619 order = 2,
flickerstreak@79 620 type = "execute",
flickerstreak@79 621 width = "half",
flickerstreak@79 622 func = "MoveStateDown",
flickerstreak@79 623 }
flickerstreak@79 624 }
flickerstreak@79 625 }
flickerstreak@79 626 }
flickerstreak@79 627 },
flickerstreak@79 628 properties = {
flickerstreak@79 629 name = L["Properties"],
flickerstreak@79 630 order = 2,
flickerstreak@79 631 type = "group",
flickerstreak@79 632 args = {
flickerstreak@79 633 desc = {
flickerstreak@79 634 name = L["Set the properties for the bar when in this state"],
flickerstreak@79 635 order = 1,
flickerstreak@79 636 type = "description"
flickerstreak@79 637 },
flickerstreak@79 638 hide = {
flickerstreak@79 639 name = L["Hide Bar"],
flickerstreak@81 640 order = 90,
flickerstreak@79 641 type = "toggle",
flickerstreak@79 642 set = "SetProp",
flickerstreak@79 643 get = "GetProp",
flickerstreak@79 644 },
flickerstreak@90 645 --[[ BROKEN
flickerstreak@90 646 keybindState = {
flickerstreak@79 647 name = L["Override Keybinds"],
flickerstreak@79 648 desc = L["Set this state to maintain its own set of keybinds which override the defaults when active"],
flickerstreak@81 649 order = 91,
flickerstreak@79 650 type = "toggle",
flickerstreak@79 651 set = "SetProp",
flickerstreak@79 652 get = "GetProp",
flickerstreak@90 653 }, ]]
flickerstreak@79 654 position = {
flickerstreak@79 655 name = L["Position"],
flickerstreak@81 656 order = 92,
flickerstreak@79 657 type = "group",
flickerstreak@79 658 inline = true,
flickerstreak@79 659 args = {
flickerstreak@90 660 anchorEnable = {
flickerstreak@101 661 name = L["Reposition"],
flickerstreak@79 662 order = 1,
flickerstreak@79 663 type = "toggle",
flickerstreak@79 664 set = "SetProp",
flickerstreak@79 665 get = "GetProp",
flickerstreak@79 666 },
flickerstreak@90 667 anchorFrame = {
flickerstreak@90 668 name = L["Anchor Frame"],
flickerstreak@90 669 order = 2,
flickerstreak@90 670 type = "select",
flickerstreak@90 671 values = "GetAnchorFrames",
flickerstreak@101 672 set = "SetAnchorFrame",
flickerstreak@101 673 get = "GetAnchorFrame",
flickerstreak@101 674 disabled = "GetAnchorDisabled",
flickerstreak@101 675 hidden = "GetAnchorDisabled",
flickerstreak@101 676 },
flickerstreak@79 677 anchorPoint = {
flickerstreak@79 678 name = L["Point"],
flickerstreak@90 679 order = 3,
flickerstreak@79 680 type = "select",
flickerstreak@79 681 values = pointTable,
flickerstreak@79 682 set = "SetAnchorPointProp",
flickerstreak@79 683 get = "GetAnchorPointProp",
flickerstreak@79 684 disabled = "GetAnchorDisabled",
flickerstreak@79 685 hidden = "GetAnchorDisabled",
flickerstreak@79 686 },
flickerstreak@79 687 anchorRelPoint = {
flickerstreak@79 688 name = L["Relative Point"],
flickerstreak@90 689 order = 4,
flickerstreak@79 690 type = "select",
flickerstreak@79 691 values = pointTable,
flickerstreak@79 692 set = "SetAnchorPointProp",
flickerstreak@79 693 get = "GetAnchorPointProp",
flickerstreak@79 694 disabled = "GetAnchorDisabled",
flickerstreak@79 695 hidden = "GetAnchorDisabled",
flickerstreak@79 696 },
flickerstreak@79 697 anchorX = {
flickerstreak@79 698 name = L["X Offset"],
flickerstreak@90 699 order = 5,
flickerstreak@79 700 type = "range",
flickerstreak@79 701 min = -100,
flickerstreak@79 702 max = 100,
flickerstreak@79 703 step = 1,
flickerstreak@79 704 set = "SetProp",
flickerstreak@79 705 get = "GetProp",
flickerstreak@79 706 disabled = "GetAnchorDisabled",
flickerstreak@79 707 hidden = "GetAnchorDisabled",
flickerstreak@79 708 },
flickerstreak@79 709 anchorY = {
flickerstreak@79 710 name = L["Y Offset"],
flickerstreak@90 711 order = 6,
flickerstreak@79 712 type = "range",
flickerstreak@79 713 min = -100,
flickerstreak@79 714 max = 100,
flickerstreak@79 715 step = 1,
flickerstreak@79 716 set = "SetProp",
flickerstreak@79 717 get = "GetProp",
flickerstreak@79 718 disabled = "GetAnchorDisabled",
flickerstreak@79 719 hidden = "GetAnchorDisabled",
flickerstreak@79 720 },
flickerstreak@79 721 },
flickerstreak@79 722 },
flickerstreak@79 723 scale = {
flickerstreak@79 724 name = L["Scale"],
flickerstreak@81 725 order = 93,
flickerstreak@79 726 type = "group",
flickerstreak@79 727 inline = true,
flickerstreak@79 728 args = {
flickerstreak@79 729 enableScale = {
flickerstreak@79 730 name = L["Set New Scale"],
flickerstreak@79 731 order = 1,
flickerstreak@79 732 type = "toggle",
flickerstreak@79 733 set = "SetProp",
flickerstreak@79 734 get = "GetProp",
flickerstreak@79 735 },
flickerstreak@79 736 scale = {
flickerstreak@79 737 name = L["Scale"],
flickerstreak@79 738 order = 2,
flickerstreak@79 739 type = "range",
flickerstreak@79 740 min = 0.1,
flickerstreak@79 741 max = 2.5,
flickerstreak@79 742 step = 0.05,
flickerstreak@79 743 isPercent = true,
flickerstreak@79 744 set = "SetProp",
flickerstreak@79 745 get = "GetProp",
flickerstreak@79 746 disabled = "GetScaleDisabled",
flickerstreak@79 747 hidden = "GetScaleDisabled",
flickerstreak@79 748 },
flickerstreak@79 749 },
flickerstreak@79 750 },
flickerstreak@79 751 },
flickerstreak@79 752 plugins = { }
flickerstreak@79 753 },
flickerstreak@79 754 rules = {
flickerstreak@79 755 name = L["Rule"],
flickerstreak@79 756 order = 3,
flickerstreak@79 757 type = "group",
flickerstreak@79 758 args = {
flickerstreak@79 759 mode = {
flickerstreak@79 760 name = L["Select this state"],
flickerstreak@79 761 order = 2,
flickerstreak@79 762 type = "select",
flickerstreak@79 763 style = "radio",
flickerstreak@79 764 values = {
flickerstreak@79 765 default = L["by default"],
flickerstreak@79 766 any = L["when ANY of these"],
flickerstreak@79 767 all = L["when ALL of these"],
flickerstreak@79 768 custom = L["via custom rule"],
flickerstreak@79 769 keybind = L["via keybinding"],
flickerstreak@79 770 },
flickerstreak@79 771 set = "SetType",
flickerstreak@79 772 get = "GetType",
flickerstreak@79 773 },
flickerstreak@79 774 clear = {
flickerstreak@79 775 name = L["Clear All"],
flickerstreak@79 776 order = 3,
flickerstreak@79 777 type = "execute",
flickerstreak@79 778 hidden = "GetClearAllDisabled",
flickerstreak@79 779 disabled = "GetClearAllDisabled",
flickerstreak@79 780 func = "ClearAllConditions",
flickerstreak@79 781 },
flickerstreak@79 782 inputs = {
flickerstreak@79 783 name = L["Conditions"],
flickerstreak@79 784 order = 4,
flickerstreak@79 785 type = "multiselect",
flickerstreak@79 786 hidden = "GetConditionsDisabled",
flickerstreak@79 787 disabled = "GetConditionsDisabled",
flickerstreak@79 788 values = ruleSelect,
flickerstreak@79 789 set = "SetCondition",
flickerstreak@79 790 get = "GetCondition",
flickerstreak@79 791 },
flickerstreak@79 792 custom = {
flickerstreak@79 793 name = L["Custom Rule"],
flickerstreak@79 794 order = 5,
flickerstreak@79 795 type = "input",
flickerstreak@79 796 multiline = true,
flickerstreak@79 797 hidden = "GetCustomDisabled",
flickerstreak@79 798 disabled = "GetCustomDisabled",
flickerstreak@79 799 desc = L["Syntax like macro rules: see preset rules for examples"],
flickerstreak@79 800 set = "SetCustomRule",
flickerstreak@79 801 get = "GetCustomRule",
flickerstreak@79 802 validate = "ValidateCustomRule",
flickerstreak@79 803 },
flickerstreak@79 804 keybind = {
flickerstreak@79 805 name = L["Keybinding"],
flickerstreak@79 806 order = 6,
flickerstreak@79 807 inline = true,
flickerstreak@79 808 hidden = "GetKeybindDisabled",
flickerstreak@79 809 disabled = "GetKeybindDisabled",
flickerstreak@79 810 type = "group",
flickerstreak@79 811 args = {
flickerstreak@79 812 desc = {
flickerstreak@79 813 name = L["Invoking a state keybind toggles an override of all other transition rules."],
flickerstreak@79 814 order = 1,
flickerstreak@79 815 type = "description",
flickerstreak@79 816 },
flickerstreak@79 817 keybind = {
flickerstreak@79 818 name = L["State Hotkey"],
flickerstreak@79 819 desc = L["Define an override toggle keybind"],
flickerstreak@79 820 order = 2,
flickerstreak@79 821 type = "keybinding",
flickerstreak@79 822 set = "SetKeybind",
flickerstreak@79 823 get = "GetKeybind",
flickerstreak@79 824 },
flickerstreak@79 825 },
flickerstreak@79 826 },
flickerstreak@79 827 },
flickerstreak@79 828 },
flickerstreak@79 829 }
flickerstreak@79 830
flickerstreak@90 831 local handlers = { }
flickerstreak@90 832 local meta = {
flickerstreak@90 833 __index = function(self, key)
flickerstreak@90 834 for _, h in pairs(handlers) do
flickerstreak@90 835 if h[key] then
flickerstreak@90 836 return h[key]
flickerstreak@90 837 end
flickerstreak@90 838 end
flickerstreak@90 839 end,
flickerstreak@90 840 }
flickerstreak@90 841 local StateHandler = setmetatable({ }, meta)
flickerstreak@90 842 local proto = { __index = StateHandler }
flickerstreak@90 843
flickerstreak@90 844 function RegisterPropertyOptions( field, options, handler )
flickerstreak@90 845 stateOptions.properties.plugins[field] = options
flickerstreak@90 846 handlers[field] = handler
flickerstreak@90 847 end
flickerstreak@90 848
flickerstreak@90 849 function UnregisterPropertyOptions( field )
flickerstreak@90 850 stateOptions.properties.plugins[field] = nil
flickerstreak@90 851 handlers[field] = nil
flickerstreak@90 852 end
flickerstreak@79 853
flickerstreak@79 854 function StateHandler:New( bar, opts )
flickerstreak@90 855 local self = setmetatable(
flickerstreak@90 856 {
flickerstreak@90 857 bar = bar
flickerstreak@90 858 },
flickerstreak@90 859 proto )
flickerstreak@79 860
flickerstreak@79 861 function self:GetName()
flickerstreak@79 862 return opts.name
flickerstreak@79 863 end
flickerstreak@79 864
flickerstreak@79 865 function self:SetName(name)
flickerstreak@79 866 opts.name = name
flickerstreak@79 867 end
flickerstreak@79 868
flickerstreak@79 869 function self:GetOrder()
flickerstreak@79 870 return opts.order
flickerstreak@79 871 end
flickerstreak@79 872
flickerstreak@79 873 -- get reference to states table: even if the bar
flickerstreak@79 874 -- name changes the states table ref won't
flickerstreak@79 875 self.states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@90 876 self.state = tbuild(self.states, opts.name)
flickerstreak@79 877
flickerstreak@90 878 opts.order = self:GetRuleField("order")
flickerstreak@79 879 if opts.order == nil then
flickerstreak@79 880 -- add after the highest
flickerstreak@79 881 opts.order = 100
flickerstreak@79 882 for _, state in pairs(self.states) do
flickerstreak@79 883 local x = tonumber(tfetch(state, "rule", "order"))
flickerstreak@79 884 if x and x >= opts.order then
flickerstreak@79 885 opts.order = x + 1
flickerstreak@79 886 end
flickerstreak@79 887 end
flickerstreak@90 888 self:SetRuleField("order",opts.order)
flickerstreak@79 889 end
flickerstreak@79 890
flickerstreak@79 891 return self
flickerstreak@79 892 end
flickerstreak@79 893
flickerstreak@79 894 -- helper methods
flickerstreak@79 895
flickerstreak@90 896 function StateHandler:SetRuleField( key, value, ... )
flickerstreak@90 897 tbuild(self.state, "rule", ...)[key] = value
flickerstreak@79 898 end
flickerstreak@79 899
flickerstreak@90 900 function StateHandler:GetRuleField( ... )
flickerstreak@90 901 return tfetch(self.state, "rule", ...)
flickerstreak@79 902 end
flickerstreak@79 903
flickerstreak@79 904 function StateHandler:FixAll( setkey )
flickerstreak@79 905 -- if multiple selections in the same group are chosen when 'all' is selected,
flickerstreak@79 906 -- keep only one of them. If changing the mode, the first in the fields list will
flickerstreak@79 907 -- be chosen arbitrarily. Otherwise, if selecting a new checkbox from the field-set,
flickerstreak@79 908 -- it will be retained.
flickerstreak@79 909 local notified = false
flickerstreak@90 910 if self:GetRuleField("type") == "all" then
flickerstreak@79 911 for _, c in ipairs(rules) do
flickerstreak@79 912 local rule, hidden, fields = unpack(c)
flickerstreak@79 913 local once = false
flickerstreak@79 914 if setkey then
flickerstreak@79 915 for idx, field in ipairs(fields) do
flickerstreak@79 916 if next(field) == setkey then
flickerstreak@79 917 once = true
flickerstreak@79 918 end
flickerstreak@79 919 end
flickerstreak@79 920 end
flickerstreak@79 921 for idx, field in ipairs(fields) do
flickerstreak@79 922 local key = next(field)
flickerstreak@90 923 if self:GetRuleField("values",key) then
flickerstreak@79 924 if once and key ~= setkey then
flickerstreak@90 925 self:SetRuleField(key,false,"values")
flickerstreak@79 926 if not setkey and not notified then
flickerstreak@79 927 ReAction:UserError(L["Warning: one or more incompatible rules were turned off"])
flickerstreak@79 928 notified = true
flickerstreak@79 929 end
flickerstreak@79 930 end
flickerstreak@79 931 once = true
flickerstreak@79 932 end
flickerstreak@79 933 end
flickerstreak@79 934 end
flickerstreak@79 935 end
flickerstreak@79 936 end
flickerstreak@79 937
flickerstreak@79 938 function StateHandler:GetNeighbors()
flickerstreak@79 939 local before, after
flickerstreak@79 940 for k, v in pairs(self.states) do
flickerstreak@79 941 local o = tonumber(tfetch(v, "rule", "order"))
flickerstreak@79 942 if o and k ~= self:GetName() then
flickerstreak@79 943 local obefore = tfetch(self.states,before,"rule","order")
flickerstreak@79 944 local oafter = tfetch(self.states,after,"rule","order")
flickerstreak@79 945 if o < self:GetOrder() and (not obefore or obefore < o) then
flickerstreak@79 946 before = k
flickerstreak@79 947 end
flickerstreak@79 948 if o > self:GetOrder() and (not oafter or oafter > o) then
flickerstreak@79 949 after = k
flickerstreak@79 950 end
flickerstreak@79 951 end
flickerstreak@79 952 end
flickerstreak@79 953 return before, after
flickerstreak@79 954 end
flickerstreak@79 955
flickerstreak@79 956 function StateHandler:SwapOrder( a, b )
flickerstreak@79 957 -- do options table
flickerstreak@79 958 local args = optionMap[self.bar].args
flickerstreak@79 959 args[a].order, args[b].order = args[b].order, args[a].order
flickerstreak@79 960 -- do profile
flickerstreak@79 961 a = tbuild(self.states, a, "rule")
flickerstreak@79 962 b = tbuild(self.states, b, "rule")
flickerstreak@79 963 a.order, b.order = b.order, a.order
flickerstreak@79 964 end
flickerstreak@79 965
flickerstreak@79 966 -- handler methods
flickerstreak@79 967
flickerstreak@79 968 function StateHandler:GetProp( info )
flickerstreak@79 969 -- gets property of the same name as the options arg
flickerstreak@79 970 return GetProperty(self.bar, self:GetName(), info[#info])
flickerstreak@79 971 end
flickerstreak@79 972
flickerstreak@79 973 function StateHandler:SetProp( info, value )
flickerstreak@79 974 -- sets property of the same name as the options arg
flickerstreak@79 975 SetProperty(self.bar, self:GetName(), info[#info], value)
flickerstreak@79 976 end
flickerstreak@79 977
flickerstreak@79 978 function StateHandler:DeleteState()
flickerstreak@79 979 if self.states[self:GetName()] then
flickerstreak@79 980 self.states[self:GetName()] = nil
flickerstreak@79 981 ApplyStates(self.bar)
flickerstreak@79 982 end
flickerstreak@79 983 optionMap[self.bar].args[self:GetName()] = nil
flickerstreak@79 984 end
flickerstreak@79 985
flickerstreak@79 986 function StateHandler:SetStateName(info, value)
flickerstreak@79 987 -- check for existing state name
flickerstreak@79 988 if self.states[value] then
flickerstreak@79 989 ReAction:UserError(format(L["State named '%s' already exists"],value))
flickerstreak@79 990 return
flickerstreak@79 991 end
flickerstreak@79 992 local args = optionMap[self.bar].args
flickerstreak@79 993 local name = self:GetName()
flickerstreak@79 994 self.states[value], args[value], self.states[name], args[name] = self.states[name], args[name], nil, nil
flickerstreak@79 995 self:SetName(value)
flickerstreak@79 996 ApplyStates(self.bar)
flickerstreak@83 997 ReAction:ShowEditor(self.bar, moduleID, value)
flickerstreak@83 998 end
flickerstreak@79 999
flickerstreak@79 1000 function StateHandler:MoveStateUp()
flickerstreak@79 1001 local before, after = self:GetNeighbors()
flickerstreak@79 1002 if before then
flickerstreak@79 1003 self:SwapOrder(before, self:GetName())
flickerstreak@79 1004 ApplyStates(self.bar)
flickerstreak@79 1005 end
flickerstreak@79 1006 end
flickerstreak@79 1007
flickerstreak@79 1008 function StateHandler:MoveStateDown()
flickerstreak@79 1009 local before, after = self:GetNeighbors()
flickerstreak@79 1010 if after then
flickerstreak@79 1011 self:SwapOrder(self:GetName(), after)
flickerstreak@79 1012 ApplyStates(self.bar)
flickerstreak@79 1013 end
flickerstreak@79 1014 end
flickerstreak@79 1015
flickerstreak@79 1016 function StateHandler:GetAnchorDisabled()
flickerstreak@101 1017 return not GetProperty(self.bar, self:GetName(), "anchorEnable")
flickerstreak@101 1018 end
flickerstreak@101 1019
flickerstreak@101 1020 function StateHandler:GetAnchorFrames(info)
flickerstreak@101 1021 self._anchorframes = self._anchorframes or { }
flickerstreak@101 1022 table.wipe(self._anchorframes)
flickerstreak@101 1023
flickerstreak@101 1024 table.insert(self._anchorframes, "UIParent")
flickerstreak@101 1025 for name, bar in ReAction:IterateBars() do
flickerstreak@101 1026 table.insert(self._anchorframes, bar:GetFrame():GetName())
flickerstreak@101 1027 end
flickerstreak@101 1028 return self._anchorframes
flickerstreak@101 1029 end
flickerstreak@101 1030
flickerstreak@101 1031 function StateHandler:GetAnchorFrame(info)
flickerstreak@101 1032 local value = self:GetProp(info)
flickerstreak@101 1033 for k,v in pairs(self._anchorframes) do
flickerstreak@101 1034 if v == value then
flickerstreak@101 1035 return k
flickerstreak@101 1036 end
flickerstreak@101 1037 end
flickerstreak@101 1038 end
flickerstreak@101 1039
flickerstreak@101 1040 function StateHandler:SetAnchorFrame(info, value)
flickerstreak@101 1041 local f = _G[self._anchorframes[value]]
flickerstreak@101 1042 if f then
flickerstreak@101 1043 SetFrameRef(self.bar:GetFrame(), "anchor-"..self:GetName(), f)
flickerstreak@101 1044 self:SetProp(info, f:GetName())
flickerstreak@101 1045 end
flickerstreak@79 1046 end
flickerstreak@79 1047
flickerstreak@79 1048 function StateHandler:SetAnchorPointProp(info, value)
flickerstreak@79 1049 self:SetProp(info, value ~= "NONE" and value or nil)
flickerstreak@79 1050 end
flickerstreak@79 1051
flickerstreak@79 1052 function StateHandler:GetAnchorPointProp(info)
flickerstreak@79 1053 return self:GetProp(info) or "NONE"
flickerstreak@79 1054 end
flickerstreak@79 1055
flickerstreak@79 1056 function StateHandler:GetScaleDisabled()
flickerstreak@79 1057 return not GetProperty(self.bar, self:GetName(), "enableScale")
flickerstreak@79 1058 end
flickerstreak@79 1059
flickerstreak@79 1060 function StateHandler:SetType(info, value)
flickerstreak@90 1061 self:SetRuleField("type", value)
flickerstreak@79 1062 self:FixAll()
flickerstreak@79 1063 ApplyStates(self.bar)
flickerstreak@79 1064 end
flickerstreak@79 1065
flickerstreak@79 1066 function StateHandler:GetType()
flickerstreak@90 1067 return self:GetRuleField("type")
flickerstreak@79 1068 end
flickerstreak@79 1069
flickerstreak@79 1070 function StateHandler:GetClearAllDisabled()
flickerstreak@90 1071 local t = self:GetRuleField("type")
flickerstreak@79 1072 return not( t == "any" or t == "all" or t == "custom")
flickerstreak@79 1073 end
flickerstreak@79 1074
flickerstreak@79 1075 function StateHandler:ClearAllConditions()
flickerstreak@90 1076 local t = self:GetRuleField("type")
flickerstreak@79 1077 if t == "custom" then
flickerstreak@90 1078 self:SetRuleField("custom","")
flickerstreak@79 1079 elseif t == "any" or t == "all" then
flickerstreak@90 1080 self:SetRuleField("values", {})
flickerstreak@79 1081 end
flickerstreak@79 1082 ApplyStates(self.bar)
flickerstreak@79 1083 end
flickerstreak@79 1084
flickerstreak@79 1085 function StateHandler:GetConditionsDisabled()
flickerstreak@90 1086 local t = self:GetRuleField("type")
flickerstreak@79 1087 return not( t == "any" or t == "all")
flickerstreak@79 1088 end
flickerstreak@79 1089
flickerstreak@79 1090 function StateHandler:SetCondition(info, key, value)
flickerstreak@90 1091 self:SetRuleField(ruleMap[key], value or nil, "values")
flickerstreak@79 1092 if value then
flickerstreak@79 1093 self:FixAll(ruleMap[key])
flickerstreak@79 1094 end
flickerstreak@79 1095 ApplyStates(self.bar)
flickerstreak@79 1096 end
flickerstreak@79 1097
flickerstreak@79 1098 function StateHandler:GetCondition(info, key)
flickerstreak@90 1099 return self:GetRuleField("values", ruleMap[key]) or false
flickerstreak@79 1100 end
flickerstreak@79 1101
flickerstreak@79 1102 function StateHandler:GetCustomDisabled()
flickerstreak@90 1103 return self:GetRuleField("type") ~= "custom"
flickerstreak@79 1104 end
flickerstreak@79 1105
flickerstreak@79 1106 function StateHandler:SetCustomRule(info, value)
flickerstreak@90 1107 self:SetRuleField("custom",value)
flickerstreak@79 1108 ApplyStates(self.bar)
flickerstreak@79 1109 end
flickerstreak@79 1110
flickerstreak@79 1111 function StateHandler:GetCustomRule()
flickerstreak@90 1112 return self:GetRuleField("custom") or ""
flickerstreak@79 1113 end
flickerstreak@79 1114
flickerstreak@79 1115 function StateHandler:ValidateCustomRule(info, value)
flickerstreak@79 1116 local s = value:gsub("%s","") -- remove all spaces
flickerstreak@79 1117 -- unfortunately %b and captures don't support the '+' notation, or this would be considerably simpler
flickerstreak@79 1118 repeat
flickerstreak@79 1119 if s == "" then
flickerstreak@79 1120 return true
flickerstreak@79 1121 end
flickerstreak@79 1122 local c, r = s:match("(%b[])(.*)")
flickerstreak@79 1123 if c == nil and s and #s > 0 then
flickerstreak@79 1124 return format(L["Invalid custom rule '%s': each clause must appear within [brackets]"],value or "")
flickerstreak@79 1125 end
flickerstreak@79 1126 s = r
flickerstreak@79 1127 until c == nil
flickerstreak@79 1128 return true
flickerstreak@79 1129 end
flickerstreak@79 1130
flickerstreak@79 1131 function StateHandler:GetKeybindDisabled()
flickerstreak@90 1132 return self:GetRuleField("type") ~= "keybind"
flickerstreak@79 1133 end
flickerstreak@79 1134
flickerstreak@79 1135 function StateHandler:GetKeybind()
flickerstreak@90 1136 return self:GetRuleField("keybind")
flickerstreak@79 1137 end
flickerstreak@79 1138
flickerstreak@79 1139 function StateHandler:SetKeybind(info, value)
flickerstreak@79 1140 if value and #value == 0 then
flickerstreak@79 1141 value = nil
flickerstreak@79 1142 end
flickerstreak@90 1143 self:SetRuleField("keybind",value)
flickerstreak@79 1144 ApplyStates(self.bar)
flickerstreak@79 1145 end
flickerstreak@79 1146
flickerstreak@62 1147 local function CreateStateOptions(bar, name)
flickerstreak@62 1148 local opts = {
flickerstreak@62 1149 type = "group",
flickerstreak@62 1150 name = name,
flickerstreak@64 1151 childGroups = "tab",
flickerstreak@79 1152 args = stateOptions
flickerstreak@25 1153 }
flickerstreak@62 1154
flickerstreak@79 1155 opts.handler = StateHandler:New(bar,opts)
flickerstreak@64 1156
flickerstreak@62 1157 return opts
flickerstreak@25 1158 end
flickerstreak@62 1159
flickerstreak@79 1160 function module:GetBarOptions(bar)
flickerstreak@62 1161 local private = { }
flickerstreak@75 1162 local states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@62 1163 local options = {
flickerstreak@77 1164 name = L["Dynamic State"],
flickerstreak@62 1165 type = "group",
flickerstreak@77 1166 order = -1,
flickerstreak@64 1167 childGroups = "tree",
flickerstreak@62 1168 disabled = InCombatLockdown,
flickerstreak@62 1169 args = {
flickerstreak@64 1170 __desc__ = {
flickerstreak@68 1171 name = L["States are evaluated in the order they are listed"],
flickerstreak@68 1172 order = 1,
flickerstreak@64 1173 type = "description",
flickerstreak@64 1174 },
flickerstreak@64 1175 __new__ = {
flickerstreak@64 1176 name = L["New State..."],
flickerstreak@64 1177 order = 2,
flickerstreak@68 1178 type = "group",
flickerstreak@62 1179 args = {
flickerstreak@64 1180 name = {
flickerstreak@64 1181 name = L["State Name"],
flickerstreak@64 1182 desc = L["Set a name for the new state"],
flickerstreak@68 1183 order = 1,
flickerstreak@68 1184 type = "input",
flickerstreak@64 1185 get = function() return private.newstatename or "" end,
flickerstreak@64 1186 set = function(info,value) private.newstatename = value end,
flickerstreak@64 1187 pattern = "^%w*$",
flickerstreak@64 1188 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@64 1189 },
flickerstreak@64 1190 create = {
flickerstreak@68 1191 name = L["Create State"],
flickerstreak@68 1192 order = 2,
flickerstreak@64 1193 type = "execute",
flickerstreak@64 1194 func = function ()
flickerstreak@64 1195 local name = private.newstatename
flickerstreak@68 1196 if states[name] then
flickerstreak@75 1197 ReAction:UserError(format(L["State named '%s' already exists"],name))
flickerstreak@68 1198 else
flickerstreak@68 1199 -- TODO: select default state options and pass as final argument
flickerstreak@68 1200 states[name] = { }
flickerstreak@68 1201 optionMap[bar].args[name] = CreateStateOptions(bar,name)
flickerstreak@81 1202 ReAction:ShowEditor(bar, moduleID, name)
flickerstreak@68 1203 private.newstatename = ""
flickerstreak@68 1204 end
flickerstreak@64 1205 end,
flickerstreak@64 1206 disabled = function()
flickerstreak@64 1207 local name = private.newstatename or ""
flickerstreak@64 1208 return #name == 0 or name:find("%W")
flickerstreak@64 1209 end,
flickerstreak@62 1210 }
flickerstreak@62 1211 }
flickerstreak@64 1212 }
flickerstreak@62 1213 }
flickerstreak@62 1214 }
flickerstreak@79 1215 for name, config in pairs(states) do
flickerstreak@79 1216 options.args[name] = CreateStateOptions(bar,name)
flickerstreak@62 1217 end
flickerstreak@64 1218 optionMap[bar] = options
flickerstreak@62 1219 return options
flickerstreak@62 1220 end
flickerstreak@25 1221 end
flickerstreak@25 1222
flickerstreak@79 1223 -- Module API --
flickerstreak@79 1224
flickerstreak@90 1225 -- Pass in a property field-name, an implementation secure snippet, a static options table, and an
flickerstreak@79 1226 -- optional options handler method-table
flickerstreak@79 1227 --
flickerstreak@79 1228 -- The options table is static, i.e. not bar-specific and should only reference handler method
flickerstreak@79 1229 -- strings (either existing ones or those added via optHandler). The existing options are ordered
flickerstreak@81 1230 -- 90-99. Order #1 is reserved for the heading.
flickerstreak@79 1231 --
flickerstreak@90 1232 -- The contents of optHandler, if provided, will be added to the existing StateHandler options metatable.
flickerstreak@79 1233 -- See above, for existing API. In particular see the properties set up in the New method: self.bar,
flickerstreak@79 1234 -- self.states, and self:GetName(), and the generic property handlers self:GetProp() and self:SetProp().
flickerstreak@79 1235 --
flickerstreak@90 1236 function module:RegisterStateProperty( field, snippetHandler, options, optHandler )
flickerstreak@90 1237 RegisterProperty(field, snippetHandler)
flickerstreak@79 1238 RegisterPropertyOptions(field, options, optHandler)
flickerstreak@25 1239 end
flickerstreak@79 1240
flickerstreak@90 1241 function module:UnregisterStateProperty( field )
flickerstreak@90 1242 UnregisterProperty(field)
flickerstreak@90 1243 UnregisterPropertyOptions(field)
flickerstreak@90 1244 end