annotate modules/State.lua @ 114:d39cfbdde301

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