annotate modules/ReAction_State/ReAction_State.lua @ 65:5ea65ec7d162

Fixed stance initializers for locale-independent behavior as well as proper initialization for druids (after PLAYER_AURAS_CHANGED)
author Flick <flickerstreak@gmail.com>
date Wed, 28 May 2008 17:26:27 +0000
parents 2000f4f4c6af
children 44d3716aba8d
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@25 11
flickerstreak@25 12 -- module declaration
flickerstreak@25 13 local moduleID = "State"
flickerstreak@65 14 local module = ReAction:NewModule( moduleID, "AceEvent-3.0" )
flickerstreak@62 15
flickerstreak@64 16 -- Utility --
flickerstreak@62 17
flickerstreak@62 18 -- traverse a table tree by key list and fetch the result or first nil
flickerstreak@62 19 local function tfetch(t, ...)
flickerstreak@62 20 for i = 1, select('#', ...) do
flickerstreak@62 21 t = t and t[select(i, ...)]
flickerstreak@62 22 end
flickerstreak@62 23 return t
flickerstreak@62 24 end
flickerstreak@62 25
flickerstreak@62 26 -- traverse a table tree by key list and build tree as necessary
flickerstreak@62 27 local function tbuild(t, ...)
flickerstreak@62 28 for i = 1, select('#', ...) do
flickerstreak@62 29 local key = select(i, ...)
flickerstreak@62 30 if not t[key] then t[key] = { } end
flickerstreak@62 31 t = t[key]
flickerstreak@62 32 end
flickerstreak@62 33 return t
flickerstreak@62 34 end
flickerstreak@62 35
flickerstreak@64 36 -- PRIVATE --
flickerstreak@65 37 local InitRules, ApplyStates
flickerstreak@64 38 do
flickerstreak@65 39 local ruleformats = { }
flickerstreak@65 40
flickerstreak@65 41 function InitRules()
flickerstreak@65 42 local forms = { }
flickerstreak@65 43 for i = 1, GetNumShapeshiftForms() do
flickerstreak@65 44 local icon = GetShapeshiftFormInfo(i)
flickerstreak@65 45 forms[icon] = i;
flickerstreak@65 46 end
flickerstreak@65 47 -- sort by icon since it's locale-independent
flickerstreak@65 48 -- use 9 if not found since 9 is never a valid stance/form
flickerstreak@65 49 -- we do these shenanigans instead of hardcoding the stances because
flickerstreak@65 50 -- the ordering varies if the character is missing a form. For warriors
flickerstreak@65 51 -- this is rarely a problem but for druids it can be. Some people never bother
flickerstreak@65 52 -- to do the aquatic form quest, and stance 5 can be either flight or tree/boomkin
flickerstreak@65 53 -- depending on talents.
flickerstreak@65 54 local defensive = forms["Interface\\Icons\\Ability_Warrior_DefensiveStance"] or 9
flickerstreak@65 55 local berserker = forms["Interface\\Icons\\Ability_Racial_Avatar"] or 9
flickerstreak@65 56 local bear = forms["Interface\\Icons\\Ability_Racial_BearForm"] or 9 -- bear and dire bear share the same icon
flickerstreak@65 57 local aquatic = forms["Interface\\Icons\\Ability_Druid_AquaticForm"] or 9
flickerstreak@65 58 local cat = forms["Interface\\Icons\\Ability_Druid_CatForm"] or 9
flickerstreak@65 59 local travel = forms["Interface\\Icons\\Ability_Druid_TravelForm"] or 9
flickerstreak@65 60 local treekin = forms["Interface\\Icons\\Ability_Druid_TreeofLife"] or forms["Interface\\Icons\\Spell_Nature_ForceOfNature"] or 9
flickerstreak@65 61 local flight = forms["Interface\\Icons\\Ability_Druid_FlightForm"] or 9 -- both flight and swift flight share the same icon
flickerstreak@65 62
flickerstreak@65 63 -- As far as I can tell the macro clauses are NOT locale-specific.
flickerstreak@65 64 ruleformats.battle = "stance:1"
flickerstreak@65 65 ruleformats.defensive = ("stance:%d"):format(defensive)
flickerstreak@65 66 ruleformats.berserker = ("stance:%d"):format(berserker)
flickerstreak@65 67 ruleformats.caster = ("form:0/%d/%d/%d"):format(aquatic, travel, flight)
flickerstreak@65 68 ruleformats.bear = ("form:%d"):format(bear)
flickerstreak@65 69 ruleformats.cat = ("form:%d"):format(cat)
flickerstreak@65 70 ruleformats.treeOrMoonkin = ("form:%d"):format(treekin)
flickerstreak@65 71 ruleformats.stealth = "stealth"
flickerstreak@65 72 ruleformats.nostealth = "nostealth"
flickerstreak@65 73 ruleformats.shadowform = "form:1"
flickerstreak@65 74 ruleformats.noshadowform = "noform"
flickerstreak@65 75 ruleformats.pet = "pet"
flickerstreak@65 76 ruleformats.nopet = "nopet"
flickerstreak@65 77 ruleformats.harm = "target=target,harm"
flickerstreak@65 78 ruleformats.help = "target=target,help"
flickerstreak@65 79 ruleformats.notarget = "target=target,noexists"
flickerstreak@65 80 ruleformats.focusharm = "target=focus,harm"
flickerstreak@65 81 ruleformats.focushelp = "target=focus,help"
flickerstreak@65 82 ruleformats.nofocus = "target=focus,noexists"
flickerstreak@65 83 ruleformats.raid = "group:raid"
flickerstreak@65 84 ruleformats.party = "group:party"
flickerstreak@65 85 ruleformats.solo = "nogroup"
flickerstreak@65 86 ruleformats.combat = "combat"
flickerstreak@65 87 ruleformats.nocombat = "nocombat"
flickerstreak@64 88 end
flickerstreak@62 89
flickerstreak@65 90 local function BuildRuleString(states)
flickerstreak@64 91 local s = ""
flickerstreak@64 92 local default
flickerstreak@64 93 local sorted = { }
flickerstreak@64 94 for name in pairs(states) do
flickerstreak@64 95 table.insert(sorted,name)
flickerstreak@64 96 end
flickerstreak@64 97 table.sort(sorted, function(lhs, rhs)
flickerstreak@64 98 local olhs = tfetch(states[lhs],"rule","order") or 0
flickerstreak@64 99 local orhs = tfetch(states[rhs],"rule","order") or 0
flickerstreak@64 100 return olhs < orhs
flickerstreak@64 101 end)
flickerstreak@64 102 for idx, name in ipairs(sorted) do
flickerstreak@64 103 local state = states[name]
flickerstreak@64 104 local semi = #s > 0 and "; " or ""
flickerstreak@64 105 local mode = tfetch(state,"rule","type")
flickerstreak@64 106 if mode == "default" then
flickerstreak@64 107 default = name
flickerstreak@64 108 elseif mode == "custom" then
flickerstreak@64 109 if state.rule.custom then
flickerstreak@64 110 -- strip out all spaces from the custom rule
flickerstreak@64 111 s = ("%s%s%s %s"):format(s, semi, state.rule.custom:gsub("%s",""), name)
flickerstreak@64 112 end
flickerstreak@64 113 elseif mode == "any" then
flickerstreak@64 114 if state.rule.values then
flickerstreak@64 115 local clause = ""
flickerstreak@64 116 for key, value in pairs(state.rule.values) do
flickerstreak@64 117 clause = ("%s[%s]"):format(clause,ruleformats[key])
flickerstreak@64 118 end
flickerstreak@64 119 if #clause > 0 then
flickerstreak@64 120 s = ("%s%s%s %s"):format(s, semi, clause, name)
flickerstreak@64 121 end
flickerstreak@64 122 end
flickerstreak@64 123 elseif mode == "all" then
flickerstreak@64 124 if state.rule.values then
flickerstreak@64 125 local clause = ""
flickerstreak@64 126 for key, value in pairs(state.rule.values) do
flickerstreak@64 127 clause = ("%s%s%s"):format(clause,#clause > 0 and "," or "", ruleformats[key])
flickerstreak@64 128 end
flickerstreak@64 129 if #clause > 0 then
flickerstreak@64 130 s = ("%s%s[%s] %s"):format(s, semi, clause, name)
flickerstreak@64 131 end
flickerstreak@64 132 end
flickerstreak@64 133 end
flickerstreak@64 134 end
flickerstreak@64 135 if default then
flickerstreak@64 136 s = ("%s%s%s"):format(s, #s > 0 and "; " or "", default)
flickerstreak@64 137 end
flickerstreak@64 138 return s
flickerstreak@64 139 end
flickerstreak@64 140
flickerstreak@64 141 local drivers = setmetatable({},{__mode="k"})
flickerstreak@64 142
flickerstreak@64 143 function ApplyStates( bar )
flickerstreak@64 144 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@64 145 if states then
flickerstreak@64 146 local frame = bar:GetFrame()
flickerstreak@64 147 local string = BuildRuleString(states)
flickerstreak@64 148 ReAction:Print("'"..string.."'")
flickerstreak@64 149 if string and #string > 0 then
flickerstreak@64 150 -- register a handler to set the value of attribute "state-reaction"
flickerstreak@64 151 -- in response to events as per the rule string
flickerstreak@64 152 RegisterStateDriver(frame, "reaction", string)
flickerstreak@64 153 drivers[bar] = true
flickerstreak@64 154 -- register a trivial map for each "statemap-reaction-XXX" to set 'state' to 'XXX'
flickerstreak@64 155 for state in pairs(states) do
flickerstreak@64 156 frame:SetAttribute(("statemap-reaction-%s"):format(state), state)
flickerstreak@64 157 end
flickerstreak@64 158 elseif drivers[bar] then
flickerstreak@64 159 UnregisterStateDriver(frame, "reaction")
flickerstreak@64 160 drivers[bar] = nil
flickerstreak@64 161 end
flickerstreak@64 162 end
flickerstreak@64 163 end
flickerstreak@64 164 end
flickerstreak@64 165
flickerstreak@64 166
flickerstreak@65 167 -- module event handlers
flickerstreak@65 168 function module:OnInitialize()
flickerstreak@65 169 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@65 170 {
flickerstreak@65 171 profile = {
flickerstreak@65 172 bars = { },
flickerstreak@65 173 }
flickerstreak@65 174 }
flickerstreak@65 175 )
flickerstreak@65 176
flickerstreak@65 177 InitRules()
flickerstreak@65 178 self:RegisterEvent("PLAYER_AURAS_CHANGED")
flickerstreak@65 179
flickerstreak@65 180 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@65 181
flickerstreak@65 182 ReAction.RegisterCallback(self, "OnCreateBar","OnRefreshBar")
flickerstreak@65 183 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@65 184 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@65 185 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@65 186 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@65 187 end
flickerstreak@65 188
flickerstreak@65 189 function module:PLAYER_AURAS_CHANGED()
flickerstreak@65 190 self:UnregisterEvent("PLAYER_AURAS_CHANGED")
flickerstreak@65 191 -- for some classes the number of stances is 0 until this event fires
flickerstreak@65 192 InitRules()
flickerstreak@65 193 end
flickerstreak@65 194
flickerstreak@65 195 function module:OnRefreshBar(event, bar, name)
flickerstreak@65 196 local c = self.db.profile.bars[name]
flickerstreak@65 197 if c then
flickerstreak@65 198 self:UpdateStates(bar)
flickerstreak@65 199 end
flickerstreak@65 200 end
flickerstreak@65 201
flickerstreak@65 202 function module:OnEraseBar(event, bar, name)
flickerstreak@65 203 self.db.profile.bars[name] = nil
flickerstreak@65 204 end
flickerstreak@65 205
flickerstreak@65 206 function module:OnRenameBar(event, bar, oldname, newname)
flickerstreak@65 207 local b = self.db.profile.bars
flickerstreak@65 208 bars[newname], bars[oldname] = bars[oldname], nil
flickerstreak@65 209 end
flickerstreak@65 210
flickerstreak@65 211 function module:OnConfigModeChanged(event, mode)
flickerstreak@65 212 -- TODO: unregister all state drivers (temporarily) and hidestates
flickerstreak@65 213 end
flickerstreak@65 214
flickerstreak@64 215
flickerstreak@64 216 -- API --
flickerstreak@64 217
flickerstreak@64 218 function module:UpdateStates( bar )
flickerstreak@64 219 ApplyStates(bar)
flickerstreak@64 220 end
flickerstreak@64 221
flickerstreak@64 222 function module:CreateState( bar, name )
flickerstreak@64 223 local states = tbuild(self.db.profile.bars, bar:GetName(), "states")
flickerstreak@64 224 if states[name] then
flickerstreak@64 225 ReAction:UserError(L["State named '%s' already exists"]:format(name))
flickerstreak@64 226 else
flickerstreak@64 227 states[name] = { }
flickerstreak@64 228 end
flickerstreak@64 229 end
flickerstreak@64 230
flickerstreak@64 231 function module:DeleteState( bar, name )
flickerstreak@64 232 local states = tfetch(self.db.profile.bars, bar:GetName(), "states")
flickerstreak@64 233 if states[name] then
flickerstreak@64 234 states[name] = nil
flickerstreak@64 235 ApplyStates(bar)
flickerstreak@64 236 end
flickerstreak@64 237 end
flickerstreak@64 238
flickerstreak@64 239 -- Options --
flickerstreak@64 240
flickerstreak@64 241 local CreateBarOptions
flickerstreak@62 242 do
flickerstreak@62 243 local function ClassCheck(...)
flickerstreak@62 244 for i = 1, select('#',...) do
flickerstreak@62 245 local _, c = UnitClass("player")
flickerstreak@62 246 if c == select(i,...) then
flickerstreak@62 247 return false
flickerstreak@62 248 end
flickerstreak@62 249 end
flickerstreak@62 250 return true
flickerstreak@62 251 end
flickerstreak@62 252
flickerstreak@64 253 -- pre-sorted by the order they should appear in
flickerstreak@64 254 local rules = {
flickerstreak@64 255 -- rule hidden fields
flickerstreak@64 256 { "stance", ClassCheck("WARRIOR"), { {battle = L["Battle Stance"]}, {defensive = L["Defensive Stance"]}, {berserker = L["Berserker Stance"]} } },
flickerstreak@64 257 { "form", ClassCheck("DRUID"), { {caster = L["Caster Form"]}, {bear = L["Bear Form"]}, {cat = L["Cat Form"]}, {treeOrMoonkin = L["Tree/Moonkin"]} } },
flickerstreak@64 258 { "stealth", ClassCheck("ROGUE","DRUID"), { {stealth = L["Stealth"]}, {nostealth = L["No Stealth"]} } },
flickerstreak@64 259 { "shadow", ClassCheck("PRIEST"), { {shadowform = L["Shadowform"]}, {noshadowform = L["No Shadowform"]} } },
flickerstreak@64 260 { "pet", ClassCheck("HUNTER","WARLOCK"), { {pet = L["With Pet"]}, {nopet = L["Without Pet"]} } },
flickerstreak@64 261 { "target", false, { {harm = L["Hostile Target"]}, {help = L["Friendly Target"]}, {notarget = L["No Target"]} } },
flickerstreak@64 262 { "focus", false, { {focusharm = L["Hostile Focus"]}, {focushelp = L["Friendly Focus"]}, {nofocus = L["No Focus"]} } },
flickerstreak@64 263 { "group", false, { {raid = L["Raid"]}, {party = L["Party"]}, {solo = L["Solo"]} } },
flickerstreak@64 264 { "combat", false, { {combat = L["In Combat"]}, {nocombat = L["Out of Combat"]} } },
flickerstreak@62 265 }
flickerstreak@62 266
flickerstreak@64 267 local ruleSelect = { }
flickerstreak@64 268 local ruleMap = { }
flickerstreak@64 269 local optionMap = setmetatable({},{__mode="k"})
flickerstreak@62 270
flickerstreak@64 271 -- unpack rules table into ruleSelect and ruleMap
flickerstreak@64 272 for _, c in ipairs(rules) do
flickerstreak@64 273 local rule, hidden, fields = unpack(c)
flickerstreak@64 274 if not hidden then
flickerstreak@64 275 for _, field in ipairs(fields) do
flickerstreak@64 276 local key, label = next(field)
flickerstreak@64 277 table.insert(ruleSelect, label)
flickerstreak@64 278 table.insert(ruleMap, key)
flickerstreak@62 279 end
flickerstreak@62 280 end
flickerstreak@62 281 end
flickerstreak@62 282
flickerstreak@62 283 local function CreateStateOptions(bar, name)
flickerstreak@62 284 local opts = {
flickerstreak@62 285 type = "group",
flickerstreak@62 286 name = name,
flickerstreak@64 287 childGroups = "tab",
flickerstreak@25 288 }
flickerstreak@62 289
flickerstreak@64 290 local states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@64 291
flickerstreak@64 292 local function put( key, value, ... )
flickerstreak@64 293 tbuild(states, opts.name, "rule", ...)[key] = value
flickerstreak@64 294 end
flickerstreak@64 295
flickerstreak@64 296 local function fetch( ... )
flickerstreak@64 297 return tfetch(states, opts.name, "rule", ...)
flickerstreak@64 298 end
flickerstreak@64 299
flickerstreak@64 300 local function fixall(setkey)
flickerstreak@64 301 -- if multiple selections in the same group are chosen when 'all' is selected,
flickerstreak@64 302 -- keep only one of them. If changing the mode, the first in the fields list will
flickerstreak@64 303 -- be chosen arbitrarily. Otherwise, if selecting a new checkbox from the field-set,
flickerstreak@64 304 -- it will be retained.
flickerstreak@64 305 local notified = false
flickerstreak@64 306 for _, c in ipairs(rules) do
flickerstreak@64 307 local rule, hidden, fields = unpack(c)
flickerstreak@64 308 local found = false
flickerstreak@64 309 for key in ipairs(fields) do
flickerstreak@64 310 if fetch("values",key) then
flickerstreak@64 311 if (found or setkey) and key ~= setkey then
flickerstreak@64 312 put(key,false,"values")
flickerstreak@64 313 if not setkey and not notified then
flickerstreak@64 314 ReAction:UserError(L["Warning: one or more incompatible rules were turned off"])
flickerstreak@64 315 notified = true
flickerstreak@64 316 end
flickerstreak@64 317 end
flickerstreak@64 318 found = true
flickerstreak@64 319 end
flickerstreak@62 320 end
flickerstreak@62 321 end
flickerstreak@62 322 end
flickerstreak@62 323
flickerstreak@64 324 local function getNeighbors()
flickerstreak@64 325 local before, after
flickerstreak@64 326 for k, v in pairs(states) do
flickerstreak@64 327 local o = tonumber(tfetch(v, "rule", "order"))
flickerstreak@64 328 if o and k ~= opts.name then
flickerstreak@64 329 local obefore = tfetch(states,before,"rule","order")
flickerstreak@64 330 local oafter = tfetch(states,after,"rule","order")
flickerstreak@64 331 if o < opts.order and (not obefore or obefore < o) then
flickerstreak@64 332 before = k
flickerstreak@64 333 end
flickerstreak@64 334 if o > opts.order and (not oafter or oafter > o) then
flickerstreak@64 335 after = k
flickerstreak@64 336 end
flickerstreak@64 337 end
flickerstreak@64 338 end
flickerstreak@64 339 return before, after
flickerstreak@64 340 end
flickerstreak@64 341
flickerstreak@64 342 local function swapOrder( a, b )
flickerstreak@64 343 -- do options table
flickerstreak@64 344 local args = optionMap[bar].args
flickerstreak@64 345 args[a].order, args[b].order = args[b].order, args[a].order
flickerstreak@64 346 -- do profile
flickerstreak@64 347 a = tbuild(states, a, "rule")
flickerstreak@64 348 b = tbuild(states, b, "rule")
flickerstreak@64 349 a.order, b.order = b.order, a.order
flickerstreak@64 350 end
flickerstreak@64 351
flickerstreak@64 352 local function update()
flickerstreak@64 353 module:UpdateStates(bar)
flickerstreak@64 354 end
flickerstreak@64 355
flickerstreak@64 356
flickerstreak@64 357 opts.order = fetch("order")
flickerstreak@64 358 if opts.order == nil then
flickerstreak@64 359 -- add after the highest
flickerstreak@64 360 opts.order = 100
flickerstreak@64 361 for _, state in pairs(states) do
flickerstreak@64 362 local x = tonumber(tfetch(state, "rule", "order"))
flickerstreak@64 363 if x and x >= opts.order then
flickerstreak@64 364 opts.order = x + 1
flickerstreak@64 365 end
flickerstreak@64 366 end
flickerstreak@64 367 put("order",opts.order)
flickerstreak@64 368 end
flickerstreak@64 369
flickerstreak@64 370 opts.args = {
flickerstreak@64 371 properties = {
flickerstreak@64 372 type = "group",
flickerstreak@64 373 name = L["Properties"],
flickerstreak@64 374 order = 1,
flickerstreak@64 375 args = {
flickerstreak@64 376 delete = {
flickerstreak@64 377 type = "execute",
flickerstreak@64 378 name = L["Delete this State"],
flickerstreak@64 379 func = function(info)
flickerstreak@64 380 module:DeleteState(bar,opts.name)
flickerstreak@64 381 optionMap[bar].args[opts.name] = nil
flickerstreak@64 382 end,
flickerstreak@64 383 order = -1
flickerstreak@64 384 },
flickerstreak@64 385 rename = {
flickerstreak@64 386 type = "input",
flickerstreak@64 387 name = L["Name"],
flickerstreak@64 388 order = 1,
flickerstreak@64 389 get = function() return opts.name end,
flickerstreak@64 390 set = function(info, value)
flickerstreak@64 391 -- check for existing state name
flickerstreak@64 392 if states[value] then
flickerstreak@64 393 L["State named '%s' already exists"]:format(value)
flickerstreak@64 394 end
flickerstreak@64 395 local args = optionMap[bar].args
flickerstreak@64 396 states[value], args[value], states[opts.name], args[opts.name] = states[opts.name], args[opts.name], nil, nil
flickerstreak@64 397 opts.name = value
flickerstreak@64 398 update()
flickerstreak@64 399 end,
flickerstreak@64 400 pattern = "^%w*$",
flickerstreak@64 401 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@64 402 },
flickerstreak@64 403 ordering = {
flickerstreak@64 404 type = "group",
flickerstreak@64 405 inline = true,
flickerstreak@64 406 name = L["Evaluation Order"],
flickerstreak@64 407 desc = L["State transitions are evaluated in the order listed:\nMove a state up or down to change the order"],
flickerstreak@64 408 order = 2,
flickerstreak@64 409 args = {
flickerstreak@64 410 up = {
flickerstreak@64 411 type = "execute",
flickerstreak@64 412 name = L["Up"],
flickerstreak@64 413 width = "half",
flickerstreak@64 414 order = 1,
flickerstreak@64 415 func = function()
flickerstreak@64 416 local before, after = getNeighbors()
flickerstreak@64 417 if before then
flickerstreak@64 418 swapOrder(before, opts.name)
flickerstreak@64 419 update()
flickerstreak@64 420 end
flickerstreak@64 421 end,
flickerstreak@64 422 },
flickerstreak@64 423 down = {
flickerstreak@64 424 type = "execute",
flickerstreak@64 425 name = L["Down"],
flickerstreak@64 426 width = "half",
flickerstreak@64 427 order = 2,
flickerstreak@64 428 func = function()
flickerstreak@64 429 local before, after = getNeighbors()
flickerstreak@64 430 if after then
flickerstreak@64 431 ReAction:Print(opts.name, after)
flickerstreak@64 432 swapOrder(opts.name, after)
flickerstreak@64 433 update()
flickerstreak@64 434 end
flickerstreak@64 435 end,
flickerstreak@64 436 }
flickerstreak@64 437 }
flickerstreak@64 438 },
flickerstreak@64 439 -- keybinding for show-this-state would go here
flickerstreak@64 440 -- show/hide would go here
flickerstreak@64 441 -- page # would go here
flickerstreak@64 442 -- anchoring would go here
flickerstreak@64 443 }
flickerstreak@64 444 },
flickerstreak@64 445 rules = {
flickerstreak@64 446 type = "group",
flickerstreak@64 447 name = L["Rules"],
flickerstreak@64 448 order = 2,
flickerstreak@64 449 args = {
flickerstreak@64 450 mode = {
flickerstreak@64 451 type = "select",
flickerstreak@64 452 style = "radio",
flickerstreak@64 453 name = L["Select this state"],
flickerstreak@64 454 values = {
flickerstreak@64 455 default = L["by default"],
flickerstreak@64 456 any = L["when ANY of these"],
flickerstreak@64 457 all = L["when ALL of these"],
flickerstreak@64 458 custom = L["via custom rule"]
flickerstreak@64 459 },
flickerstreak@64 460 set = function( info, value )
flickerstreak@64 461 put("type", value)
flickerstreak@64 462 fixall()
flickerstreak@64 463 update()
flickerstreak@64 464 end,
flickerstreak@64 465 get = function( info )
flickerstreak@64 466 return fetch("type")
flickerstreak@64 467 end,
flickerstreak@64 468 order = 2
flickerstreak@64 469 },
flickerstreak@64 470 clear = {
flickerstreak@64 471 type = "execute",
flickerstreak@64 472 name = L["Clear All"],
flickerstreak@64 473 func = function()
flickerstreak@64 474 local type = fetch("type")
flickerstreak@64 475 if type == "custom" then
flickerstreak@64 476 put("custom","")
flickerstreak@64 477 elseif type == "any" or type == "all" then
flickerstreak@64 478 put("values", {})
flickerstreak@64 479 end
flickerstreak@64 480 update()
flickerstreak@64 481 end,
flickerstreak@64 482 order = 3
flickerstreak@64 483 },
flickerstreak@64 484 inputs = {
flickerstreak@64 485 type = "multiselect",
flickerstreak@64 486 name = L["Rules"],
flickerstreak@64 487 hidden = function()
flickerstreak@64 488 return fetch("type") == "custom"
flickerstreak@64 489 end,
flickerstreak@64 490 disabled = function()
flickerstreak@64 491 return fetch("type") == "default"
flickerstreak@64 492 end,
flickerstreak@64 493 values = ruleSelect,
flickerstreak@64 494 set = function(info, key, value )
flickerstreak@64 495 put(ruleMap[key], value or nil, "values")
flickerstreak@64 496 if value then
flickerstreak@64 497 fixall(ruleMap[key])
flickerstreak@64 498 end
flickerstreak@64 499 update()
flickerstreak@64 500 end,
flickerstreak@64 501 get = function(info, key)
flickerstreak@64 502 return fetch("values", ruleMap[key]) or false
flickerstreak@64 503 end,
flickerstreak@64 504 order = 4
flickerstreak@64 505 },
flickerstreak@64 506 custom = {
flickerstreak@64 507 type = "input",
flickerstreak@64 508 multiline = true,
flickerstreak@64 509 hidden = function()
flickerstreak@64 510 return fetch("type") ~= "custom"
flickerstreak@64 511 end,
flickerstreak@64 512 disabled = function()
flickerstreak@64 513 return fetch("type") == "default"
flickerstreak@64 514 end,
flickerstreak@64 515 name = L["Custom Rule"],
flickerstreak@64 516 desc = L["Syntax like macro rules: see preset rules for examples"],
flickerstreak@64 517 set = function(info, value)
flickerstreak@64 518 put("custom",value)
flickerstreak@64 519 update()
flickerstreak@64 520 end,
flickerstreak@64 521 get = function(info)
flickerstreak@64 522 return fetch("custom") or ""
flickerstreak@64 523 end,
flickerstreak@64 524 validate = function (info, rule)
flickerstreak@64 525 local s = rule:gsub("%s","") -- remove all spaces
flickerstreak@64 526 -- unfortunately %b and captures don't support the '+' notation, or this would be considerably simpler
flickerstreak@64 527 repeat
flickerstreak@64 528 if s == "" then
flickerstreak@64 529 return true
flickerstreak@64 530 end
flickerstreak@64 531 local c, r = s:match("(%b[])(.*)")
flickerstreak@64 532 if c == nil and s and #s > 0 then
flickerstreak@64 533 return L["Invalid custom rule '%s': each clause must appear within [brackets]"]:format(rule)
flickerstreak@64 534 end
flickerstreak@64 535 s = r
flickerstreak@64 536 until c == nil
flickerstreak@64 537 return true
flickerstreak@64 538 end,
flickerstreak@64 539 order = 5,
flickerstreak@64 540 }
flickerstreak@64 541 }
flickerstreak@64 542 }
flickerstreak@62 543 }
flickerstreak@62 544 return opts
flickerstreak@25 545 end
flickerstreak@62 546
flickerstreak@64 547
flickerstreak@62 548 CreateBarOptions = function(bar)
flickerstreak@62 549 local private = { }
flickerstreak@62 550 local options = {
flickerstreak@62 551 type = "group",
flickerstreak@62 552 name = L["Dynamic State"],
flickerstreak@64 553 childGroups = "tree",
flickerstreak@62 554 disabled = InCombatLockdown,
flickerstreak@62 555 args = {
flickerstreak@64 556 __desc__ = {
flickerstreak@64 557 type = "description",
flickerstreak@64 558 name = L["States are evaluated in the order they are listed"],
flickerstreak@64 559 order = 1
flickerstreak@64 560 },
flickerstreak@64 561 __new__ = {
flickerstreak@62 562 type = "group",
flickerstreak@64 563 name = L["New State..."],
flickerstreak@64 564 order = 2,
flickerstreak@62 565 args = {
flickerstreak@64 566 name = {
flickerstreak@64 567 type = "input",
flickerstreak@64 568 name = L["State Name"],
flickerstreak@64 569 desc = L["Set a name for the new state"],
flickerstreak@64 570 get = function() return private.newstatename or "" end,
flickerstreak@64 571 set = function(info,value) private.newstatename = value end,
flickerstreak@64 572 pattern = "^%w*$",
flickerstreak@64 573 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@64 574 order = 1
flickerstreak@64 575 },
flickerstreak@64 576 create = {
flickerstreak@64 577 type = "execute",
flickerstreak@64 578 name = L["Create State"],
flickerstreak@64 579 func = function ()
flickerstreak@64 580 local name = private.newstatename
flickerstreak@64 581 module:CreateState(bar,name) -- TODO: select default state options and pass as final argument
flickerstreak@64 582 optionMap[bar].args[name] = CreateStateOptions(bar,name)
flickerstreak@64 583 private.newstatename = ""
flickerstreak@64 584 end,
flickerstreak@64 585 disabled = function()
flickerstreak@64 586 local name = private.newstatename or ""
flickerstreak@64 587 return #name == 0 or name:find("%W")
flickerstreak@64 588 end,
flickerstreak@64 589 order = 2,
flickerstreak@62 590 }
flickerstreak@62 591 }
flickerstreak@64 592 }
flickerstreak@62 593 }
flickerstreak@62 594 }
flickerstreak@62 595 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@62 596 if states then
flickerstreak@62 597 for name, config in pairs(states) do
flickerstreak@64 598 options.args[name] = CreateStateOptions(bar,name)
flickerstreak@62 599 end
flickerstreak@62 600 end
flickerstreak@64 601 optionMap[bar] = options
flickerstreak@62 602 return options
flickerstreak@62 603 end
flickerstreak@25 604 end
flickerstreak@25 605
flickerstreak@62 606 function module:GetBarOptions(bar)
flickerstreak@64 607 return CreateBarOptions(bar)
flickerstreak@25 608 end