annotate State.lua @ 108:b2fb8f7dc780

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