annotate State.lua @ 83:1ad208c25618

Fixed display issues with renaming states/bars
author Flick <flickerstreak@gmail.com>
date Wed, 25 Jun 2008 23:35:24 +0000
parents 57f8151ea0f0
children 7cabc8ac6c16
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@25 10 local InCombatLockdown = InCombatLockdown
flickerstreak@75 11 local format = string.format
flickerstreak@25 12
flickerstreak@80 13 ReAction:UpdateRevision("$Revision$")
flickerstreak@77 14
flickerstreak@25 15 -- module declaration
flickerstreak@25 16 local moduleID = "State"
flickerstreak@65 17 local module = ReAction:NewModule( moduleID, "AceEvent-3.0" )
flickerstreak@62 18
flickerstreak@64 19 -- Utility --
flickerstreak@62 20
flickerstreak@62 21 -- traverse a table tree by key list and fetch the result or first nil
flickerstreak@62 22 local function tfetch(t, ...)
flickerstreak@62 23 for i = 1, select('#', ...) do
flickerstreak@62 24 t = t and t[select(i, ...)]
flickerstreak@62 25 end
flickerstreak@62 26 return t
flickerstreak@62 27 end
flickerstreak@62 28
flickerstreak@62 29 -- traverse a table tree by key list and build tree as necessary
flickerstreak@62 30 local function tbuild(t, ...)
flickerstreak@62 31 for i = 1, select('#', ...) do
flickerstreak@62 32 local key = select(i, ...)
flickerstreak@62 33 if not t[key] then t[key] = { } end
flickerstreak@62 34 t = t[key]
flickerstreak@62 35 end
flickerstreak@62 36 return t
flickerstreak@62 37 end
flickerstreak@62 38
flickerstreak@75 39 -- return a new array of keys of table 't', sorted by comparing
flickerstreak@75 40 -- sub-fields (obtained via tfetch) of the table values
flickerstreak@75 41 local function fieldsort( t, ... )
flickerstreak@75 42 local r = { }
flickerstreak@75 43 for k in pairs(t) do
flickerstreak@75 44 table.insert(r,k)
flickerstreak@75 45 end
flickerstreak@75 46 local path = { ... }
flickerstreak@75 47 table.sort(r, function(lhs, rhs)
flickerstreak@75 48 local olhs = tfetch(t[lhs], unpack(path)) or 0
flickerstreak@75 49 local orhs = tfetch(t[rhs], unpack(path)) or 0
flickerstreak@75 50 return olhs < orhs
flickerstreak@75 51 end)
flickerstreak@75 52 return r
flickerstreak@75 53 end
flickerstreak@75 54
flickerstreak@68 55
flickerstreak@79 56 local InitRules, ApplyStates, SetProperty, GetProperty, RegisterProperty
flickerstreak@75 57
flickerstreak@75 58 -- PRIVATE --
flickerstreak@64 59 do
flickerstreak@67 60 -- As far as I can tell the macro clauses are NOT locale-specific.
flickerstreak@67 61 local ruleformats = {
flickerstreak@67 62 stealth = "stealth",
flickerstreak@67 63 nostealth = "nostealth",
flickerstreak@67 64 shadowform = "form:1",
flickerstreak@67 65 noshadowform = "noform",
flickerstreak@67 66 pet = "pet",
flickerstreak@67 67 nopet = "nopet",
flickerstreak@67 68 harm = "target=target,harm",
flickerstreak@67 69 help = "target=target,help",
flickerstreak@67 70 notarget = "target=target,noexists",
flickerstreak@67 71 focusharm = "target=focus,harm",
flickerstreak@67 72 focushelp = "target=focus,help",
flickerstreak@67 73 nofocus = "target=focus,noexists",
flickerstreak@67 74 raid = "group:raid",
flickerstreak@67 75 party = "group:party",
flickerstreak@67 76 solo = "nogroup",
flickerstreak@67 77 combat = "combat",
flickerstreak@67 78 nocombat = "nocombat",
flickerstreak@75 79 possess = "bonusbar:5",
flickerstreak@67 80 }
flickerstreak@65 81
flickerstreak@67 82 -- Have to do these shenanigans instead of hardcoding the stances/forms because
flickerstreak@67 83 -- the ordering varies if the character is missing a form. For warriors
flickerstreak@67 84 -- this is rarely a problem (c'mon, who actually skips the level 10 def stance quest?)
flickerstreak@67 85 -- but for druids it can be. Some people never bother to do the aquatic form quest
flickerstreak@75 86 -- until well past when they get cat form, and stance 5/6 can be flight, tree, or moonkin
flickerstreak@67 87 -- depending on talents.
flickerstreak@65 88 function InitRules()
flickerstreak@65 89 local forms = { }
flickerstreak@67 90 -- sort by icon since it's locale-independent
flickerstreak@65 91 for i = 1, GetNumShapeshiftForms() do
flickerstreak@65 92 local icon = GetShapeshiftFormInfo(i)
flickerstreak@65 93 forms[icon] = i;
flickerstreak@65 94 end
flickerstreak@65 95 -- use 9 if not found since 9 is never a valid stance/form
flickerstreak@65 96 local defensive = forms["Interface\\Icons\\Ability_Warrior_DefensiveStance"] or 9
flickerstreak@65 97 local berserker = forms["Interface\\Icons\\Ability_Racial_Avatar"] or 9
flickerstreak@65 98 local bear = forms["Interface\\Icons\\Ability_Racial_BearForm"] or 9 -- bear and dire bear share the same icon
flickerstreak@65 99 local aquatic = forms["Interface\\Icons\\Ability_Druid_AquaticForm"] or 9
flickerstreak@65 100 local cat = forms["Interface\\Icons\\Ability_Druid_CatForm"] or 9
flickerstreak@65 101 local travel = forms["Interface\\Icons\\Ability_Druid_TravelForm"] or 9
flickerstreak@75 102 local tree = forms["Interface\\Icons\\Ability_Druid_TreeofLife"] or 9
flickerstreak@75 103 local moonkin = forms["Interface\\Icons\\Spell_Nature_ForceOfNature"] or 9
flickerstreak@67 104 local flight = forms["Interface\\Icons\\Ability_Druid_FlightForm"] or 9 -- flight and swift flight share the same icon
flickerstreak@65 105
flickerstreak@75 106 ruleformats.battle = "stance:1"
flickerstreak@75 107 ruleformats.defensive = format("stance:%d",defensive)
flickerstreak@75 108 ruleformats.berserker = format("stance:%d",berserker)
flickerstreak@75 109 ruleformats.caster = format("form:0/%d/%d/%d",aquatic, travel, flight)
flickerstreak@75 110 ruleformats.bear = format("form:%d",bear)
flickerstreak@75 111 ruleformats.cat = format("form:%d",cat)
flickerstreak@75 112 ruleformats.tree = format("form:%d",tree)
flickerstreak@75 113 ruleformats.moonkin = format("form:%d",moonkin)
flickerstreak@64 114 end
flickerstreak@62 115
flickerstreak@75 116
flickerstreak@75 117 -- state property functions
flickerstreak@75 118 local ofskeys = {
flickerstreak@75 119 anchorPoint = "point",
flickerstreak@75 120 anchorRelPoint = "relpoint",
flickerstreak@75 121 anchorX = "x",
flickerstreak@75 122 anchorY = "y"
flickerstreak@75 123 }
flickerstreak@75 124
flickerstreak@75 125 local barofsidx = {
flickerstreak@75 126 anchorPoint = 1,
flickerstreak@75 127 anchorRelPoint = 3,
flickerstreak@75 128 anchorX = 4,
flickerstreak@75 129 anchorY = 5
flickerstreak@75 130 }
flickerstreak@75 131
flickerstreak@75 132 local function UpdatePartialAnchor(bar, states, ckey)
flickerstreak@75 133 local map = { }
flickerstreak@75 134 local bc = bar.config
flickerstreak@75 135 for state, c in pairs(states) do
flickerstreak@75 136 if c.enableAnchor then
flickerstreak@75 137 map[state] = c[ckey]
flickerstreak@75 138 end
flickerstreak@68 139 end
flickerstreak@75 140 local ofskey = ofskeys[ckey]
flickerstreak@75 141 local default = select(barofsidx[ckey], bar:GetAnchor())
flickerstreak@75 142 bar:SetStateAttribute(format("headofs%s",ofskeys[ckey]), map, default)
flickerstreak@75 143 end
flickerstreak@75 144
flickerstreak@79 145 -- the table key name for each function maps to the name of the config element
flickerstreak@75 146 local propertyFuncs = {
flickerstreak@75 147 hide = function( bar, states )
flickerstreak@75 148 local hs = { }
flickerstreak@75 149 for state, config in pairs(states) do
flickerstreak@75 150 if config.hide then
flickerstreak@75 151 table.insert(hs, state)
flickerstreak@75 152 end
flickerstreak@75 153 end
flickerstreak@81 154 bar:GetButtonFrame():SetAttribute("hidestates", table.concat(hs,","))
flickerstreak@75 155 end,
flickerstreak@75 156
flickerstreak@75 157 keybindstate = function( bar, states )
flickerstreak@75 158 local map = { }
flickerstreak@75 159 for state, config in pairs(states) do
flickerstreak@75 160 local kbset = config.keybindstate and state
flickerstreak@75 161 map[state] = kbset
flickerstreak@75 162 for button in bar:IterateButtons() do
flickerstreak@75 163 -- TODO: inform children they should maintain multiple binding sets
flickerstreak@75 164 -- ?? button:UpdateBindingSet(kbset)
flickerstreak@75 165 end
flickerstreak@75 166 end
flickerstreak@77 167 bar:SetStateAttribute("statebindings", map, true) -- apply to button frame, bindings only work for direct children
flickerstreak@75 168 end,
flickerstreak@75 169
flickerstreak@75 170 enableAnchor = function( bar, states )
flickerstreak@75 171 for ckey in pairs(ofskeys) do
flickerstreak@75 172 UpdatePartialAnchor(bar, states, ckey)
flickerstreak@75 173 end
flickerstreak@75 174 end,
flickerstreak@75 175
flickerstreak@75 176 enableScale = function( bar, states )
flickerstreak@75 177 local map = { }
flickerstreak@75 178 for state, c in pairs(states) do
flickerstreak@75 179 if c.enableScale then
flickerstreak@75 180 map[state] = c.scale
flickerstreak@75 181 end
flickerstreak@75 182 end
flickerstreak@75 183 bar:SetStateAttribute("headscale", map, 1.0)
flickerstreak@75 184 end,
flickerstreak@75 185 }
flickerstreak@75 186
flickerstreak@75 187 -- generate some table entries
flickerstreak@75 188 propertyFuncs.scale = propertyFuncs.enableScale
flickerstreak@75 189 for ckey in pairs(ofskeys) do
flickerstreak@75 190 propertyFuncs[ckey] = function( bar, states )
flickerstreak@75 191 UpdatePartialAnchor(bar, states, ckey)
flickerstreak@75 192 end
flickerstreak@68 193 end
flickerstreak@68 194
flickerstreak@68 195
flickerstreak@68 196 function GetProperty( bar, state, propname )
flickerstreak@68 197 return tfetch(module.db.profile.bars, bar:GetName(), "states", state, propname)
flickerstreak@68 198 end
flickerstreak@68 199
flickerstreak@68 200 function SetProperty( bar, state, propname, value )
flickerstreak@68 201 local states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@68 202 tbuild(states, state)[propname] = value
flickerstreak@68 203 local f = propertyFuncs[propname]
flickerstreak@68 204 if f then
flickerstreak@68 205 f(bar, states)
flickerstreak@68 206 end
flickerstreak@68 207 end
flickerstreak@68 208
flickerstreak@79 209 function RegisterProperty( propname, f )
flickerstreak@79 210 propertyFuncs[propname] = f
flickerstreak@79 211 for bar in ReAction:IterateBars() do
flickerstreak@79 212 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@79 213 if states then
flickerstreak@79 214 f(bar,states)
flickerstreak@79 215 end
flickerstreak@79 216 end
flickerstreak@79 217 end
flickerstreak@79 218
flickerstreak@75 219
flickerstreak@75 220
flickerstreak@75 221 --
flickerstreak@75 222 -- Build a state-transition spec string and statemap to be passed to
flickerstreak@75 223 -- Bar:SetStateDriver().
flickerstreak@75 224 --
flickerstreak@75 225 -- The statemap building is complex: keybound states override all
flickerstreak@75 226 -- other transitions, so must remain in their current state, but must
flickerstreak@75 227 -- also remember other transitions that happen while they're stuck there
flickerstreak@75 228 -- so that when the binding is toggled off it can return to the proper state
flickerstreak@75 229 --
flickerstreak@75 230 local function BuildStateMap(states)
flickerstreak@75 231 local rules = { }
flickerstreak@75 232 local statemap = { }
flickerstreak@75 233 local keybinds = { }
flickerstreak@75 234 local default
flickerstreak@75 235
flickerstreak@75 236 -- first grab all the keybind override states
flickerstreak@75 237 -- and construct an override template
flickerstreak@75 238 local override
flickerstreak@75 239 do
flickerstreak@75 240 local overrides = { }
flickerstreak@75 241 for name, state in pairs(states) do
flickerstreak@75 242 local type = tfetch(state, "rule", "type")
flickerstreak@75 243 if type == "keybind" then
flickerstreak@75 244 -- use the state-stack to remember the current transition
flickerstreak@75 245 -- use $s as a marker for a later call to gsub()
flickerstreak@75 246 table.insert(overrides, format("%s:$s set() %s", name, name))
flickerstreak@75 247 end
flickerstreak@75 248 end
flickerstreak@75 249 if #overrides > 0 then
flickerstreak@75 250 table.insert(overrides, "") -- for a trailing ';'
flickerstreak@75 251 end
flickerstreak@75 252 override = table.concat(overrides, ";") or ""
flickerstreak@75 253 end
flickerstreak@75 254
flickerstreak@75 255 -- now iterate the rules in order
flickerstreak@75 256 for idx, state in ipairs(fieldsort(states, "rule", "order")) do
flickerstreak@75 257 local c = states[state].rule
flickerstreak@75 258 local type = c.type
flickerstreak@75 259 if type == "default" then
flickerstreak@75 260 default = default or state
flickerstreak@75 261 elseif type == "custom" then
flickerstreak@75 262 if c.custom then
flickerstreak@75 263 -- strip out all spaces from the custom rule
flickerstreak@75 264 table.insert(rules, format("%s %s", c.custom:gsub("%s",""), state))
flickerstreak@75 265 end
flickerstreak@75 266 elseif type == "any" then
flickerstreak@75 267 if c.values then
flickerstreak@75 268 local clauses = { }
flickerstreak@75 269 for key, value in pairs(c.values) do
flickerstreak@75 270 table.insert(clauses, format("[%s]", ruleformats[key]))
flickerstreak@75 271 end
flickerstreak@75 272 if #clauses > 0 then
flickerstreak@75 273 table.insert(rules, format("%s %s", table.concat(clauses), state))
flickerstreak@75 274 end
flickerstreak@75 275 end
flickerstreak@75 276 elseif type == "all" then
flickerstreak@75 277 if c.values then
flickerstreak@75 278 local clauses = { }
flickerstreak@75 279 for key, value in pairs(c.values) do
flickerstreak@75 280 table.insert(clauses, ruleformats[key])
flickerstreak@75 281 end
flickerstreak@75 282 if #clauses > 0 then
flickerstreak@75 283 table.insert(rules, format("%s %s", format("[%s]", table.concat(clauses, ",")), state))
flickerstreak@75 284 end
flickerstreak@75 285 end
flickerstreak@75 286 end
flickerstreak@75 287
flickerstreak@75 288 -- use a different virtual button for the actual keybind transition,
flickerstreak@75 289 -- to implement a toggle. You have to clear it regardless of the type
flickerstreak@75 290 -- (which is usually a no-op) to unbind state transitions when switching
flickerstreak@75 291 -- transition types.
flickerstreak@75 292 local bindbutton = format("%s_binding",state)
flickerstreak@75 293 if type == "keybind" then
flickerstreak@75 294 keybinds[bindbutton] = c.keybind or false
flickerstreak@75 295 statemap[bindbutton] = format("%s:pop();*:set(%s)", state, state)
flickerstreak@75 296 else
flickerstreak@75 297 keybinds[bindbutton] = false
flickerstreak@75 298 end
flickerstreak@75 299
flickerstreak@75 300 -- construct the statemap. gsub() the state name into the override template.
flickerstreak@75 301 statemap[state] = format("%s%s", override:gsub("%$s",state), state)
flickerstreak@75 302 end
flickerstreak@75 303 -- make sure that the default, if any, is last
flickerstreak@75 304 if default then
flickerstreak@75 305 table.insert(rules, default)
flickerstreak@75 306 end
flickerstreak@75 307 return table.concat(rules,";"), statemap, keybinds
flickerstreak@75 308 end
flickerstreak@75 309
flickerstreak@75 310 function ApplyStates( bar )
flickerstreak@75 311 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@75 312 if states then
flickerstreak@75 313 local rule, statemap, keybinds = BuildStateMap(states)
flickerstreak@75 314 bar:SetStateDriver("reaction", rule, statemap)
flickerstreak@75 315 for state, key in pairs(keybinds) do
flickerstreak@75 316 bar:SetAttributeBinding(state, key, "state-reaction", state)
flickerstreak@75 317 end
flickerstreak@75 318 for k, f in pairs(propertyFuncs) do
flickerstreak@75 319 f(bar, states)
flickerstreak@68 320 end
flickerstreak@68 321 end
flickerstreak@68 322 end
flickerstreak@68 323
flickerstreak@64 324 end
flickerstreak@64 325
flickerstreak@64 326
flickerstreak@68 327
flickerstreak@68 328 -- module event handlers --
flickerstreak@68 329
flickerstreak@65 330 function module:OnInitialize()
flickerstreak@65 331 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@65 332 {
flickerstreak@65 333 profile = {
flickerstreak@65 334 bars = { },
flickerstreak@65 335 }
flickerstreak@65 336 }
flickerstreak@65 337 )
flickerstreak@65 338
flickerstreak@65 339 InitRules()
flickerstreak@65 340 self:RegisterEvent("PLAYER_AURAS_CHANGED")
flickerstreak@65 341
flickerstreak@65 342 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@65 343
flickerstreak@65 344 ReAction.RegisterCallback(self, "OnCreateBar","OnRefreshBar")
flickerstreak@65 345 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@65 346 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@65 347 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@65 348 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@65 349 end
flickerstreak@65 350
flickerstreak@65 351 function module:PLAYER_AURAS_CHANGED()
flickerstreak@65 352 self:UnregisterEvent("PLAYER_AURAS_CHANGED")
flickerstreak@66 353 -- on login the number of stances is 0 until this event fires during the init sequence.
flickerstreak@66 354 -- however if you reload just the UI the number of stances is correct immediately
flickerstreak@66 355 -- and this event won't fire until you gain/lose buffs/debuffs, at which point you might
flickerstreak@66 356 -- be in combat.
flickerstreak@66 357 if not InCombatLockdown() then
flickerstreak@66 358 InitRules()
flickerstreak@66 359 for name, bar in ReAction:IterateBars() do
flickerstreak@67 360 self:OnRefreshBar(nil,bar,name)
flickerstreak@66 361 end
flickerstreak@66 362 end
flickerstreak@65 363 end
flickerstreak@65 364
flickerstreak@65 365 function module:OnRefreshBar(event, bar, name)
flickerstreak@65 366 local c = self.db.profile.bars[name]
flickerstreak@65 367 if c then
flickerstreak@68 368 ApplyStates(bar)
flickerstreak@65 369 end
flickerstreak@65 370 end
flickerstreak@65 371
flickerstreak@65 372 function module:OnEraseBar(event, bar, name)
flickerstreak@65 373 self.db.profile.bars[name] = nil
flickerstreak@65 374 end
flickerstreak@65 375
flickerstreak@65 376 function module:OnRenameBar(event, bar, oldname, newname)
flickerstreak@75 377 local bars = self.db.profile.bars
flickerstreak@65 378 bars[newname], bars[oldname] = bars[oldname], nil
flickerstreak@65 379 end
flickerstreak@65 380
flickerstreak@65 381 function module:OnConfigModeChanged(event, mode)
flickerstreak@79 382 -- nothing to do (yet)
flickerstreak@65 383 end
flickerstreak@65 384
flickerstreak@64 385
flickerstreak@64 386
flickerstreak@64 387 -- Options --
flickerstreak@64 388
flickerstreak@79 389 local CreateBarOptions, RegisterPropertyOptions
flickerstreak@62 390 do
flickerstreak@79 391 local playerClass = select(2, UnitClass("player"))
flickerstreak@62 392 local function ClassCheck(...)
flickerstreak@62 393 for i = 1, select('#',...) do
flickerstreak@79 394 if playerClass == select(i,...) then
flickerstreak@62 395 return false
flickerstreak@62 396 end
flickerstreak@62 397 end
flickerstreak@62 398 return true
flickerstreak@62 399 end
flickerstreak@62 400
flickerstreak@64 401 -- pre-sorted by the order they should appear in
flickerstreak@64 402 local rules = {
flickerstreak@64 403 -- rule hidden fields
flickerstreak@64 404 { "stance", ClassCheck("WARRIOR"), { {battle = L["Battle Stance"]}, {defensive = L["Defensive Stance"]}, {berserker = L["Berserker Stance"]} } },
flickerstreak@75 405 { "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 406 { "stealth", ClassCheck("ROGUE","DRUID"), { {stealth = L["Stealth"]}, {nostealth = L["No Stealth"]} } },
flickerstreak@64 407 { "shadow", ClassCheck("PRIEST"), { {shadowform = L["Shadowform"]}, {noshadowform = L["No Shadowform"]} } },
flickerstreak@64 408 { "pet", ClassCheck("HUNTER","WARLOCK"), { {pet = L["With Pet"]}, {nopet = L["Without Pet"]} } },
flickerstreak@64 409 { "target", false, { {harm = L["Hostile Target"]}, {help = L["Friendly Target"]}, {notarget = L["No Target"]} } },
flickerstreak@64 410 { "focus", false, { {focusharm = L["Hostile Focus"]}, {focushelp = L["Friendly Focus"]}, {nofocus = L["No Focus"]} } },
flickerstreak@75 411 { "possess", false, { {possess = L["Mind Control"]} } },
flickerstreak@64 412 { "group", false, { {raid = L["Raid"]}, {party = L["Party"]}, {solo = L["Solo"]} } },
flickerstreak@64 413 { "combat", false, { {combat = L["In Combat"]}, {nocombat = L["Out of Combat"]} } },
flickerstreak@62 414 }
flickerstreak@62 415
flickerstreak@64 416 local ruleSelect = { }
flickerstreak@64 417 local ruleMap = { }
flickerstreak@64 418 local optionMap = setmetatable({},{__mode="k"})
flickerstreak@62 419
flickerstreak@68 420 local pointTable = {
flickerstreak@68 421 NONE = " ",
flickerstreak@68 422 CENTER = L["Center"],
flickerstreak@68 423 LEFT = L["Left"],
flickerstreak@68 424 RIGHT = L["Right"],
flickerstreak@68 425 TOP = L["Top"],
flickerstreak@68 426 BOTTOM = L["Bottom"],
flickerstreak@68 427 TOPLEFT = L["Top Left"],
flickerstreak@68 428 TOPRIGHT = L["Top Right"],
flickerstreak@68 429 BOTTOMLEFT = L["Bottom Left"],
flickerstreak@68 430 BOTTOMRIGHT = L["Bottom Right"],
flickerstreak@68 431 }
flickerstreak@68 432
flickerstreak@64 433 -- unpack rules table into ruleSelect and ruleMap
flickerstreak@64 434 for _, c in ipairs(rules) do
flickerstreak@64 435 local rule, hidden, fields = unpack(c)
flickerstreak@64 436 if not hidden then
flickerstreak@64 437 for _, field in ipairs(fields) do
flickerstreak@64 438 local key, label = next(field)
flickerstreak@64 439 table.insert(ruleSelect, label)
flickerstreak@64 440 table.insert(ruleMap, key)
flickerstreak@62 441 end
flickerstreak@62 442 end
flickerstreak@62 443 end
flickerstreak@62 444
flickerstreak@79 445 local stateOptions = {
flickerstreak@79 446 ordering = {
flickerstreak@79 447 name = L["Info"],
flickerstreak@79 448 order = 1,
flickerstreak@79 449 type = "group",
flickerstreak@79 450 args = {
flickerstreak@79 451 delete = {
flickerstreak@79 452 name = L["Delete this State"],
flickerstreak@79 453 order = -1,
flickerstreak@79 454 type = "execute",
flickerstreak@79 455 func = "DeleteState",
flickerstreak@79 456 },
flickerstreak@79 457 rename = {
flickerstreak@79 458 name = L["Name"],
flickerstreak@79 459 order = 1,
flickerstreak@79 460 type = "input",
flickerstreak@79 461 get = "GetName",
flickerstreak@79 462 set = "SetStateName",
flickerstreak@79 463 pattern = "^%w*$",
flickerstreak@79 464 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@79 465 },
flickerstreak@79 466 ordering = {
flickerstreak@79 467 name = L["Evaluation Order"],
flickerstreak@79 468 desc = L["State transitions are evaluated in the order listed:\nMove a state up or down to change the order"],
flickerstreak@79 469 order = 2,
flickerstreak@79 470 type = "group",
flickerstreak@79 471 inline = true,
flickerstreak@79 472 args = {
flickerstreak@79 473 up = {
flickerstreak@79 474 name = L["Up"],
flickerstreak@79 475 order = 1,
flickerstreak@79 476 type = "execute",
flickerstreak@79 477 width = "half",
flickerstreak@79 478 func = "MoveStateUp",
flickerstreak@79 479 },
flickerstreak@79 480 down = {
flickerstreak@79 481 name = L["Down"],
flickerstreak@79 482 order = 2,
flickerstreak@79 483 type = "execute",
flickerstreak@79 484 width = "half",
flickerstreak@79 485 func = "MoveStateDown",
flickerstreak@79 486 }
flickerstreak@79 487 }
flickerstreak@79 488 }
flickerstreak@79 489 }
flickerstreak@79 490 },
flickerstreak@79 491 properties = {
flickerstreak@79 492 name = L["Properties"],
flickerstreak@79 493 order = 2,
flickerstreak@79 494 type = "group",
flickerstreak@79 495 args = {
flickerstreak@79 496 desc = {
flickerstreak@79 497 name = L["Set the properties for the bar when in this state"],
flickerstreak@79 498 order = 1,
flickerstreak@79 499 type = "description"
flickerstreak@79 500 },
flickerstreak@79 501 hide = {
flickerstreak@79 502 name = L["Hide Bar"],
flickerstreak@81 503 order = 90,
flickerstreak@79 504 type = "toggle",
flickerstreak@79 505 set = "SetProp",
flickerstreak@79 506 get = "GetProp",
flickerstreak@79 507 },
flickerstreak@79 508 keybindstate = {
flickerstreak@79 509 name = L["Override Keybinds"],
flickerstreak@79 510 desc = L["Set this state to maintain its own set of keybinds which override the defaults when active"],
flickerstreak@81 511 order = 91,
flickerstreak@79 512 type = "toggle",
flickerstreak@79 513 set = "SetProp",
flickerstreak@79 514 get = "GetProp",
flickerstreak@79 515 },
flickerstreak@79 516 position = {
flickerstreak@79 517 name = L["Position"],
flickerstreak@81 518 order = 92,
flickerstreak@79 519 type = "group",
flickerstreak@79 520 inline = true,
flickerstreak@79 521 args = {
flickerstreak@79 522 enableAnchor = {
flickerstreak@79 523 name = L["Set New Position"],
flickerstreak@79 524 order = 1,
flickerstreak@79 525 type = "toggle",
flickerstreak@79 526 set = "SetProp",
flickerstreak@79 527 get = "GetProp",
flickerstreak@79 528 },
flickerstreak@79 529 anchorPoint = {
flickerstreak@79 530 name = L["Point"],
flickerstreak@79 531 order = 2,
flickerstreak@79 532 type = "select",
flickerstreak@79 533 values = pointTable,
flickerstreak@79 534 set = "SetAnchorPointProp",
flickerstreak@79 535 get = "GetAnchorPointProp",
flickerstreak@79 536 disabled = "GetAnchorDisabled",
flickerstreak@79 537 hidden = "GetAnchorDisabled",
flickerstreak@79 538 },
flickerstreak@79 539 anchorRelPoint = {
flickerstreak@79 540 name = L["Relative Point"],
flickerstreak@79 541 order = 3,
flickerstreak@79 542 type = "select",
flickerstreak@79 543 values = pointTable,
flickerstreak@79 544 set = "SetAnchorPointProp",
flickerstreak@79 545 get = "GetAnchorPointProp",
flickerstreak@79 546 disabled = "GetAnchorDisabled",
flickerstreak@79 547 hidden = "GetAnchorDisabled",
flickerstreak@79 548 },
flickerstreak@79 549 anchorX = {
flickerstreak@79 550 name = L["X Offset"],
flickerstreak@79 551 order = 4,
flickerstreak@79 552 type = "range",
flickerstreak@79 553 min = -100,
flickerstreak@79 554 max = 100,
flickerstreak@79 555 step = 1,
flickerstreak@79 556 set = "SetProp",
flickerstreak@79 557 get = "GetProp",
flickerstreak@79 558 disabled = "GetAnchorDisabled",
flickerstreak@79 559 hidden = "GetAnchorDisabled",
flickerstreak@79 560 },
flickerstreak@79 561 anchorY = {
flickerstreak@79 562 name = L["Y Offset"],
flickerstreak@79 563 order = 5,
flickerstreak@79 564 type = "range",
flickerstreak@79 565 min = -100,
flickerstreak@79 566 max = 100,
flickerstreak@79 567 step = 1,
flickerstreak@79 568 set = "SetProp",
flickerstreak@79 569 get = "GetProp",
flickerstreak@79 570 disabled = "GetAnchorDisabled",
flickerstreak@79 571 hidden = "GetAnchorDisabled",
flickerstreak@79 572 },
flickerstreak@79 573 },
flickerstreak@79 574 },
flickerstreak@79 575 scale = {
flickerstreak@79 576 name = L["Scale"],
flickerstreak@81 577 order = 93,
flickerstreak@79 578 type = "group",
flickerstreak@79 579 inline = true,
flickerstreak@79 580 args = {
flickerstreak@79 581 enableScale = {
flickerstreak@79 582 name = L["Set New Scale"],
flickerstreak@79 583 order = 1,
flickerstreak@79 584 type = "toggle",
flickerstreak@79 585 set = "SetProp",
flickerstreak@79 586 get = "GetProp",
flickerstreak@79 587 },
flickerstreak@79 588 scale = {
flickerstreak@79 589 name = L["Scale"],
flickerstreak@79 590 order = 2,
flickerstreak@79 591 type = "range",
flickerstreak@79 592 min = 0.1,
flickerstreak@79 593 max = 2.5,
flickerstreak@79 594 step = 0.05,
flickerstreak@79 595 isPercent = true,
flickerstreak@79 596 set = "SetProp",
flickerstreak@79 597 get = "GetProp",
flickerstreak@79 598 disabled = "GetScaleDisabled",
flickerstreak@79 599 hidden = "GetScaleDisabled",
flickerstreak@79 600 },
flickerstreak@79 601 },
flickerstreak@79 602 },
flickerstreak@79 603 },
flickerstreak@79 604 plugins = { }
flickerstreak@79 605 },
flickerstreak@79 606 rules = {
flickerstreak@79 607 name = L["Rule"],
flickerstreak@79 608 order = 3,
flickerstreak@79 609 type = "group",
flickerstreak@79 610 args = {
flickerstreak@79 611 mode = {
flickerstreak@79 612 name = L["Select this state"],
flickerstreak@79 613 order = 2,
flickerstreak@79 614 type = "select",
flickerstreak@79 615 style = "radio",
flickerstreak@79 616 values = {
flickerstreak@79 617 default = L["by default"],
flickerstreak@79 618 any = L["when ANY of these"],
flickerstreak@79 619 all = L["when ALL of these"],
flickerstreak@79 620 custom = L["via custom rule"],
flickerstreak@79 621 keybind = L["via keybinding"],
flickerstreak@79 622 },
flickerstreak@79 623 set = "SetType",
flickerstreak@79 624 get = "GetType",
flickerstreak@79 625 },
flickerstreak@79 626 clear = {
flickerstreak@79 627 name = L["Clear All"],
flickerstreak@79 628 order = 3,
flickerstreak@79 629 type = "execute",
flickerstreak@79 630 hidden = "GetClearAllDisabled",
flickerstreak@79 631 disabled = "GetClearAllDisabled",
flickerstreak@79 632 func = "ClearAllConditions",
flickerstreak@79 633 },
flickerstreak@79 634 inputs = {
flickerstreak@79 635 name = L["Conditions"],
flickerstreak@79 636 order = 4,
flickerstreak@79 637 type = "multiselect",
flickerstreak@79 638 hidden = "GetConditionsDisabled",
flickerstreak@79 639 disabled = "GetConditionsDisabled",
flickerstreak@79 640 values = ruleSelect,
flickerstreak@79 641 set = "SetCondition",
flickerstreak@79 642 get = "GetCondition",
flickerstreak@79 643 },
flickerstreak@79 644 custom = {
flickerstreak@79 645 name = L["Custom Rule"],
flickerstreak@79 646 order = 5,
flickerstreak@79 647 type = "input",
flickerstreak@79 648 multiline = true,
flickerstreak@79 649 hidden = "GetCustomDisabled",
flickerstreak@79 650 disabled = "GetCustomDisabled",
flickerstreak@79 651 desc = L["Syntax like macro rules: see preset rules for examples"],
flickerstreak@79 652 set = "SetCustomRule",
flickerstreak@79 653 get = "GetCustomRule",
flickerstreak@79 654 validate = "ValidateCustomRule",
flickerstreak@79 655 },
flickerstreak@79 656 keybind = {
flickerstreak@79 657 name = L["Keybinding"],
flickerstreak@79 658 order = 6,
flickerstreak@79 659 inline = true,
flickerstreak@79 660 hidden = "GetKeybindDisabled",
flickerstreak@79 661 disabled = "GetKeybindDisabled",
flickerstreak@79 662 type = "group",
flickerstreak@79 663 args = {
flickerstreak@79 664 desc = {
flickerstreak@79 665 name = L["Invoking a state keybind toggles an override of all other transition rules."],
flickerstreak@79 666 order = 1,
flickerstreak@79 667 type = "description",
flickerstreak@79 668 },
flickerstreak@79 669 keybind = {
flickerstreak@79 670 name = L["State Hotkey"],
flickerstreak@79 671 desc = L["Define an override toggle keybind"],
flickerstreak@79 672 order = 2,
flickerstreak@79 673 type = "keybinding",
flickerstreak@79 674 set = "SetKeybind",
flickerstreak@79 675 get = "GetKeybind",
flickerstreak@79 676 },
flickerstreak@79 677 },
flickerstreak@79 678 },
flickerstreak@79 679 },
flickerstreak@79 680 },
flickerstreak@79 681 }
flickerstreak@79 682
flickerstreak@79 683 local StateHandler = { }
flickerstreak@79 684
flickerstreak@79 685 function StateHandler:New( bar, opts )
flickerstreak@79 686 local self = setmetatable({ bar = bar }, { __index = StateHandler })
flickerstreak@79 687
flickerstreak@79 688 function self:GetName()
flickerstreak@79 689 return opts.name
flickerstreak@79 690 end
flickerstreak@79 691
flickerstreak@79 692 function self:SetName(name)
flickerstreak@79 693 opts.name = name
flickerstreak@79 694 end
flickerstreak@79 695
flickerstreak@79 696 function self:GetOrder()
flickerstreak@79 697 return opts.order
flickerstreak@79 698 end
flickerstreak@79 699
flickerstreak@79 700 -- get reference to states table: even if the bar
flickerstreak@79 701 -- name changes the states table ref won't
flickerstreak@79 702 self.states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@79 703
flickerstreak@79 704 tbuild(self.states, opts.name)
flickerstreak@79 705
flickerstreak@79 706 opts.order = self:GetRule("order")
flickerstreak@79 707 if opts.order == nil then
flickerstreak@79 708 -- add after the highest
flickerstreak@79 709 opts.order = 100
flickerstreak@79 710 for _, state in pairs(self.states) do
flickerstreak@79 711 local x = tonumber(tfetch(state, "rule", "order"))
flickerstreak@79 712 if x and x >= opts.order then
flickerstreak@79 713 opts.order = x + 1
flickerstreak@79 714 end
flickerstreak@79 715 end
flickerstreak@79 716 self:SetRule("order",opts.order)
flickerstreak@79 717 end
flickerstreak@79 718
flickerstreak@79 719 return self
flickerstreak@79 720 end
flickerstreak@79 721
flickerstreak@79 722 -- helper methods
flickerstreak@79 723
flickerstreak@79 724 function StateHandler:SetRule( key, value, ... )
flickerstreak@79 725 tbuild(self.states, self:GetName(), "rule", ...)[key] = value
flickerstreak@79 726 end
flickerstreak@79 727
flickerstreak@79 728 function StateHandler:GetRule( ... )
flickerstreak@79 729 return tfetch(self.states, self:GetName(), "rule", ...)
flickerstreak@79 730 end
flickerstreak@79 731
flickerstreak@79 732 function StateHandler:FixAll( setkey )
flickerstreak@79 733 -- if multiple selections in the same group are chosen when 'all' is selected,
flickerstreak@79 734 -- keep only one of them. If changing the mode, the first in the fields list will
flickerstreak@79 735 -- be chosen arbitrarily. Otherwise, if selecting a new checkbox from the field-set,
flickerstreak@79 736 -- it will be retained.
flickerstreak@79 737 local notified = false
flickerstreak@79 738 if self:GetRule("type") == "all" then
flickerstreak@79 739 for _, c in ipairs(rules) do
flickerstreak@79 740 local rule, hidden, fields = unpack(c)
flickerstreak@79 741 local once = false
flickerstreak@79 742 if setkey then
flickerstreak@79 743 for idx, field in ipairs(fields) do
flickerstreak@79 744 if next(field) == setkey then
flickerstreak@79 745 once = true
flickerstreak@79 746 end
flickerstreak@79 747 end
flickerstreak@79 748 end
flickerstreak@79 749 for idx, field in ipairs(fields) do
flickerstreak@79 750 local key = next(field)
flickerstreak@79 751 if self:GetRule("values",key) then
flickerstreak@79 752 if once and key ~= setkey then
flickerstreak@79 753 self:SetRule(key,false,"values")
flickerstreak@79 754 if not setkey and not notified then
flickerstreak@79 755 ReAction:UserError(L["Warning: one or more incompatible rules were turned off"])
flickerstreak@79 756 notified = true
flickerstreak@79 757 end
flickerstreak@79 758 end
flickerstreak@79 759 once = true
flickerstreak@79 760 end
flickerstreak@79 761 end
flickerstreak@79 762 end
flickerstreak@79 763 end
flickerstreak@79 764 end
flickerstreak@79 765
flickerstreak@79 766 function StateHandler:GetNeighbors()
flickerstreak@79 767 local before, after
flickerstreak@79 768 for k, v in pairs(self.states) do
flickerstreak@79 769 local o = tonumber(tfetch(v, "rule", "order"))
flickerstreak@79 770 if o and k ~= self:GetName() then
flickerstreak@79 771 local obefore = tfetch(self.states,before,"rule","order")
flickerstreak@79 772 local oafter = tfetch(self.states,after,"rule","order")
flickerstreak@79 773 if o < self:GetOrder() and (not obefore or obefore < o) then
flickerstreak@79 774 before = k
flickerstreak@79 775 end
flickerstreak@79 776 if o > self:GetOrder() and (not oafter or oafter > o) then
flickerstreak@79 777 after = k
flickerstreak@79 778 end
flickerstreak@79 779 end
flickerstreak@79 780 end
flickerstreak@79 781 return before, after
flickerstreak@79 782 end
flickerstreak@79 783
flickerstreak@79 784 function StateHandler:SwapOrder( a, b )
flickerstreak@79 785 -- do options table
flickerstreak@79 786 local args = optionMap[self.bar].args
flickerstreak@79 787 args[a].order, args[b].order = args[b].order, args[a].order
flickerstreak@79 788 -- do profile
flickerstreak@79 789 a = tbuild(self.states, a, "rule")
flickerstreak@79 790 b = tbuild(self.states, b, "rule")
flickerstreak@79 791 a.order, b.order = b.order, a.order
flickerstreak@79 792 end
flickerstreak@79 793
flickerstreak@79 794 -- handler methods
flickerstreak@79 795
flickerstreak@79 796 function StateHandler:GetProp( info )
flickerstreak@79 797 -- gets property of the same name as the options arg
flickerstreak@79 798 return GetProperty(self.bar, self:GetName(), info[#info])
flickerstreak@79 799 end
flickerstreak@79 800
flickerstreak@79 801 function StateHandler:SetProp( info, value )
flickerstreak@79 802 -- sets property of the same name as the options arg
flickerstreak@79 803 SetProperty(self.bar, self:GetName(), info[#info], value)
flickerstreak@79 804 end
flickerstreak@79 805
flickerstreak@79 806 function StateHandler:DeleteState()
flickerstreak@79 807 if self.states[self:GetName()] then
flickerstreak@79 808 self.states[self:GetName()] = nil
flickerstreak@79 809 ApplyStates(self.bar)
flickerstreak@79 810 end
flickerstreak@79 811 optionMap[self.bar].args[self:GetName()] = nil
flickerstreak@79 812 end
flickerstreak@79 813
flickerstreak@79 814 function StateHandler:SetStateName(info, value)
flickerstreak@79 815 -- check for existing state name
flickerstreak@79 816 if self.states[value] then
flickerstreak@79 817 ReAction:UserError(format(L["State named '%s' already exists"],value))
flickerstreak@79 818 return
flickerstreak@79 819 end
flickerstreak@79 820 local args = optionMap[self.bar].args
flickerstreak@79 821 local name = self:GetName()
flickerstreak@79 822 self.states[value], args[value], self.states[name], args[name] = self.states[name], args[name], nil, nil
flickerstreak@79 823 self:SetName(value)
flickerstreak@79 824 ApplyStates(self.bar)
flickerstreak@83 825 ReAction:ShowEditor(self.bar, moduleID, value)
flickerstreak@83 826 end
flickerstreak@79 827
flickerstreak@79 828 function StateHandler:MoveStateUp()
flickerstreak@79 829 local before, after = self:GetNeighbors()
flickerstreak@79 830 if before then
flickerstreak@79 831 self:SwapOrder(before, self:GetName())
flickerstreak@79 832 ApplyStates(self.bar)
flickerstreak@79 833 end
flickerstreak@79 834 end
flickerstreak@79 835
flickerstreak@79 836 function StateHandler:MoveStateDown()
flickerstreak@79 837 local before, after = self:GetNeighbors()
flickerstreak@79 838 if after then
flickerstreak@79 839 self:SwapOrder(self:GetName(), after)
flickerstreak@79 840 ApplyStates(self.bar)
flickerstreak@79 841 end
flickerstreak@79 842 end
flickerstreak@79 843
flickerstreak@79 844 function StateHandler:GetAnchorDisabled()
flickerstreak@79 845 return not GetProperty(self.bar, self:GetName(), "enableAnchor")
flickerstreak@79 846 end
flickerstreak@79 847
flickerstreak@79 848 function StateHandler:SetAnchorPointProp(info, value)
flickerstreak@79 849 self:SetProp(info, value ~= "NONE" and value or nil)
flickerstreak@79 850 end
flickerstreak@79 851
flickerstreak@79 852 function StateHandler:GetAnchorPointProp(info)
flickerstreak@79 853 return self:GetProp(info) or "NONE"
flickerstreak@79 854 end
flickerstreak@79 855
flickerstreak@79 856 function StateHandler:GetScaleDisabled()
flickerstreak@79 857 return not GetProperty(self.bar, self:GetName(), "enableScale")
flickerstreak@79 858 end
flickerstreak@79 859
flickerstreak@79 860 function StateHandler:SetType(info, value)
flickerstreak@79 861 self:SetRule("type", value)
flickerstreak@79 862 self:FixAll()
flickerstreak@79 863 ApplyStates(self.bar)
flickerstreak@79 864 end
flickerstreak@79 865
flickerstreak@79 866 function StateHandler:GetType()
flickerstreak@79 867 return self:GetRule("type")
flickerstreak@79 868 end
flickerstreak@79 869
flickerstreak@79 870 function StateHandler:GetClearAllDisabled()
flickerstreak@79 871 local t = self:GetRule("type")
flickerstreak@79 872 return not( t == "any" or t == "all" or t == "custom")
flickerstreak@79 873 end
flickerstreak@79 874
flickerstreak@79 875 function StateHandler:ClearAllConditions()
flickerstreak@79 876 local t = self:GetRule("type")
flickerstreak@79 877 if t == "custom" then
flickerstreak@79 878 self:SetRule("custom","")
flickerstreak@79 879 elseif t == "any" or t == "all" then
flickerstreak@79 880 self:SetRule("values", {})
flickerstreak@79 881 end
flickerstreak@79 882 ApplyStates(self.bar)
flickerstreak@79 883 end
flickerstreak@79 884
flickerstreak@79 885 function StateHandler:GetConditionsDisabled()
flickerstreak@79 886 local t = self:GetRule("type")
flickerstreak@79 887 return not( t == "any" or t == "all")
flickerstreak@79 888 end
flickerstreak@79 889
flickerstreak@79 890 function StateHandler:SetCondition(info, key, value)
flickerstreak@79 891 self:SetRule(ruleMap[key], value or nil, "values")
flickerstreak@79 892 if value then
flickerstreak@79 893 self:FixAll(ruleMap[key])
flickerstreak@79 894 end
flickerstreak@79 895 ApplyStates(self.bar)
flickerstreak@79 896 end
flickerstreak@79 897
flickerstreak@79 898 function StateHandler:GetCondition(info, key)
flickerstreak@79 899 return self:GetRule("values", ruleMap[key]) or false
flickerstreak@79 900 end
flickerstreak@79 901
flickerstreak@79 902 function StateHandler:GetCustomDisabled()
flickerstreak@79 903 return self:GetRule("type") ~= "custom"
flickerstreak@79 904 end
flickerstreak@79 905
flickerstreak@79 906 function StateHandler:SetCustomRule(info, value)
flickerstreak@79 907 self:SetRule("custom",value)
flickerstreak@79 908 ApplyStates(self.bar)
flickerstreak@79 909 end
flickerstreak@79 910
flickerstreak@79 911 function StateHandler:GetCustomRule()
flickerstreak@79 912 return self:GetRule("custom") or ""
flickerstreak@79 913 end
flickerstreak@79 914
flickerstreak@79 915 function StateHandler:ValidateCustomRule(info, value)
flickerstreak@79 916 local s = value:gsub("%s","") -- remove all spaces
flickerstreak@79 917 -- unfortunately %b and captures don't support the '+' notation, or this would be considerably simpler
flickerstreak@79 918 repeat
flickerstreak@79 919 if s == "" then
flickerstreak@79 920 return true
flickerstreak@79 921 end
flickerstreak@79 922 local c, r = s:match("(%b[])(.*)")
flickerstreak@79 923 if c == nil and s and #s > 0 then
flickerstreak@79 924 return format(L["Invalid custom rule '%s': each clause must appear within [brackets]"],value or "")
flickerstreak@79 925 end
flickerstreak@79 926 s = r
flickerstreak@79 927 until c == nil
flickerstreak@79 928 return true
flickerstreak@79 929 end
flickerstreak@79 930
flickerstreak@79 931 function StateHandler:GetKeybindDisabled()
flickerstreak@79 932 return self:GetRule("type") ~= "keybind"
flickerstreak@79 933 end
flickerstreak@79 934
flickerstreak@79 935 function StateHandler:GetKeybind()
flickerstreak@79 936 return self:GetRule("keybind")
flickerstreak@79 937 end
flickerstreak@79 938
flickerstreak@79 939 function StateHandler:SetKeybind(info, value)
flickerstreak@79 940 if value and #value == 0 then
flickerstreak@79 941 value = nil
flickerstreak@79 942 end
flickerstreak@79 943 self:SetRule("keybind",value)
flickerstreak@79 944 ApplyStates(self.bar)
flickerstreak@79 945 end
flickerstreak@79 946
flickerstreak@62 947 local function CreateStateOptions(bar, name)
flickerstreak@62 948 local opts = {
flickerstreak@62 949 type = "group",
flickerstreak@62 950 name = name,
flickerstreak@64 951 childGroups = "tab",
flickerstreak@79 952 args = stateOptions
flickerstreak@25 953 }
flickerstreak@62 954
flickerstreak@79 955 opts.handler = StateHandler:New(bar,opts)
flickerstreak@64 956
flickerstreak@62 957 return opts
flickerstreak@25 958 end
flickerstreak@62 959
flickerstreak@64 960
flickerstreak@79 961 function RegisterPropertyOptions( field, options, handler )
flickerstreak@79 962 stateOptions.properties.plugins[field] = options
flickerstreak@79 963 if handler then
flickerstreak@79 964 for k,v in pairs(handler) do
flickerstreak@79 965 StateHandler[k] = v
flickerstreak@79 966 end
flickerstreak@79 967 end
flickerstreak@79 968 end
flickerstreak@79 969
flickerstreak@79 970
flickerstreak@79 971 function module:GetBarOptions(bar)
flickerstreak@62 972 local private = { }
flickerstreak@75 973 local states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@62 974 local options = {
flickerstreak@77 975 name = L["Dynamic State"],
flickerstreak@62 976 type = "group",
flickerstreak@77 977 order = -1,
flickerstreak@64 978 childGroups = "tree",
flickerstreak@62 979 disabled = InCombatLockdown,
flickerstreak@62 980 args = {
flickerstreak@64 981 __desc__ = {
flickerstreak@68 982 name = L["States are evaluated in the order they are listed"],
flickerstreak@68 983 order = 1,
flickerstreak@64 984 type = "description",
flickerstreak@64 985 },
flickerstreak@64 986 __new__ = {
flickerstreak@64 987 name = L["New State..."],
flickerstreak@64 988 order = 2,
flickerstreak@68 989 type = "group",
flickerstreak@62 990 args = {
flickerstreak@64 991 name = {
flickerstreak@64 992 name = L["State Name"],
flickerstreak@64 993 desc = L["Set a name for the new state"],
flickerstreak@68 994 order = 1,
flickerstreak@68 995 type = "input",
flickerstreak@64 996 get = function() return private.newstatename or "" end,
flickerstreak@64 997 set = function(info,value) private.newstatename = value end,
flickerstreak@64 998 pattern = "^%w*$",
flickerstreak@64 999 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@64 1000 },
flickerstreak@64 1001 create = {
flickerstreak@68 1002 name = L["Create State"],
flickerstreak@68 1003 order = 2,
flickerstreak@64 1004 type = "execute",
flickerstreak@64 1005 func = function ()
flickerstreak@64 1006 local name = private.newstatename
flickerstreak@68 1007 if states[name] then
flickerstreak@75 1008 ReAction:UserError(format(L["State named '%s' already exists"],name))
flickerstreak@68 1009 else
flickerstreak@68 1010 -- TODO: select default state options and pass as final argument
flickerstreak@68 1011 states[name] = { }
flickerstreak@68 1012 optionMap[bar].args[name] = CreateStateOptions(bar,name)
flickerstreak@81 1013 ReAction:ShowEditor(bar, moduleID, name)
flickerstreak@68 1014 private.newstatename = ""
flickerstreak@68 1015 end
flickerstreak@64 1016 end,
flickerstreak@64 1017 disabled = function()
flickerstreak@64 1018 local name = private.newstatename or ""
flickerstreak@64 1019 return #name == 0 or name:find("%W")
flickerstreak@64 1020 end,
flickerstreak@62 1021 }
flickerstreak@62 1022 }
flickerstreak@64 1023 }
flickerstreak@62 1024 }
flickerstreak@62 1025 }
flickerstreak@79 1026 for name, config in pairs(states) do
flickerstreak@79 1027 options.args[name] = CreateStateOptions(bar,name)
flickerstreak@62 1028 end
flickerstreak@64 1029 optionMap[bar] = options
flickerstreak@62 1030 return options
flickerstreak@62 1031 end
flickerstreak@25 1032 end
flickerstreak@25 1033
flickerstreak@79 1034 -- Module API --
flickerstreak@79 1035
flickerstreak@79 1036 -- Pass in a property field-name, an implementation function, a static options table, and an
flickerstreak@79 1037 -- optional options handler method-table
flickerstreak@79 1038 --
flickerstreak@79 1039 -- propertyImplFunc prototype:
flickerstreak@79 1040 -- propertyImplFunc( bar, stateTable )
flickerstreak@79 1041 -- where stateTable is a { ["statename"] = { state config } } table.
flickerstreak@79 1042 --
flickerstreak@79 1043 -- The options table is static, i.e. not bar-specific and should only reference handler method
flickerstreak@79 1044 -- strings (either existing ones or those added via optHandler). The existing options are ordered
flickerstreak@81 1045 -- 90-99. Order #1 is reserved for the heading.
flickerstreak@79 1046 --
flickerstreak@79 1047 -- The contents of optHandler, if provided, will be added to the existing StateHandler metatable.
flickerstreak@79 1048 -- See above, for existing API. In particular see the properties set up in the New method: self.bar,
flickerstreak@79 1049 -- self.states, and self:GetName(), and the generic property handlers self:GetProp() and self:SetProp().
flickerstreak@79 1050 --
flickerstreak@79 1051 function module:RegisterStateProperty( field, propertyImplFunc, options, optHandler )
flickerstreak@79 1052 RegisterProperty(field, propertyImplFunc)
flickerstreak@79 1053 RegisterPropertyOptions(field, options, optHandler)
flickerstreak@25 1054 end
flickerstreak@79 1055