annotate State.lua @ 74:00e28094e1a3

Some README updates
author Flick <flickerstreak@gmail.com>
date Tue, 10 Jun 2008 22:25:15 +0000
parents aa88aed52124
children 06cd74bdc7da
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@68 37
flickerstreak@68 38 local InitRules, ApplyStates, SetProperty, GetProperty
flickerstreak@64 39 do
flickerstreak@67 40 -- As far as I can tell the macro clauses are NOT locale-specific.
flickerstreak@67 41 local ruleformats = {
flickerstreak@67 42 stealth = "stealth",
flickerstreak@67 43 nostealth = "nostealth",
flickerstreak@67 44 shadowform = "form:1",
flickerstreak@67 45 noshadowform = "noform",
flickerstreak@67 46 pet = "pet",
flickerstreak@67 47 nopet = "nopet",
flickerstreak@67 48 harm = "target=target,harm",
flickerstreak@67 49 help = "target=target,help",
flickerstreak@67 50 notarget = "target=target,noexists",
flickerstreak@67 51 focusharm = "target=focus,harm",
flickerstreak@67 52 focushelp = "target=focus,help",
flickerstreak@67 53 nofocus = "target=focus,noexists",
flickerstreak@67 54 raid = "group:raid",
flickerstreak@67 55 party = "group:party",
flickerstreak@67 56 solo = "nogroup",
flickerstreak@67 57 combat = "combat",
flickerstreak@67 58 nocombat = "nocombat",
flickerstreak@67 59 }
flickerstreak@65 60
flickerstreak@67 61 -- Have to do these shenanigans instead of hardcoding the stances/forms because
flickerstreak@67 62 -- the ordering varies if the character is missing a form. For warriors
flickerstreak@67 63 -- this is rarely a problem (c'mon, who actually skips the level 10 def stance quest?)
flickerstreak@67 64 -- but for druids it can be. Some people never bother to do the aquatic form quest
flickerstreak@67 65 -- until well past when they get cat form, and stance 5 can be flight, tree, or moonkin
flickerstreak@67 66 -- depending on talents.
flickerstreak@65 67 function InitRules()
flickerstreak@65 68 local forms = { }
flickerstreak@67 69 -- sort by icon since it's locale-independent
flickerstreak@65 70 for i = 1, GetNumShapeshiftForms() do
flickerstreak@65 71 local icon = GetShapeshiftFormInfo(i)
flickerstreak@65 72 forms[icon] = i;
flickerstreak@65 73 end
flickerstreak@65 74 -- use 9 if not found since 9 is never a valid stance/form
flickerstreak@65 75 local defensive = forms["Interface\\Icons\\Ability_Warrior_DefensiveStance"] or 9
flickerstreak@65 76 local berserker = forms["Interface\\Icons\\Ability_Racial_Avatar"] or 9
flickerstreak@65 77 local bear = forms["Interface\\Icons\\Ability_Racial_BearForm"] or 9 -- bear and dire bear share the same icon
flickerstreak@65 78 local aquatic = forms["Interface\\Icons\\Ability_Druid_AquaticForm"] or 9
flickerstreak@65 79 local cat = forms["Interface\\Icons\\Ability_Druid_CatForm"] or 9
flickerstreak@65 80 local travel = forms["Interface\\Icons\\Ability_Druid_TravelForm"] or 9
flickerstreak@65 81 local treekin = forms["Interface\\Icons\\Ability_Druid_TreeofLife"] or forms["Interface\\Icons\\Spell_Nature_ForceOfNature"] or 9
flickerstreak@67 82 local flight = forms["Interface\\Icons\\Ability_Druid_FlightForm"] or 9 -- flight and swift flight share the same icon
flickerstreak@65 83
flickerstreak@65 84 ruleformats.battle = "stance:1"
flickerstreak@65 85 ruleformats.defensive = ("stance:%d"):format(defensive)
flickerstreak@65 86 ruleformats.berserker = ("stance:%d"):format(berserker)
flickerstreak@65 87 ruleformats.caster = ("form:0/%d/%d/%d"):format(aquatic, travel, flight)
flickerstreak@65 88 ruleformats.bear = ("form:%d"):format(bear)
flickerstreak@65 89 ruleformats.cat = ("form:%d"):format(cat)
flickerstreak@65 90 ruleformats.treeOrMoonkin = ("form:%d"):format(treekin)
flickerstreak@64 91 end
flickerstreak@62 92
flickerstreak@68 93 -- return a new array of keys of table 't', sorted by comparing
flickerstreak@68 94 -- sub-fields (obtained via tfetch) of the table values
flickerstreak@68 95 local function fieldsort( t, ... )
flickerstreak@68 96 local r = { }
flickerstreak@68 97 for k in pairs(t) do
flickerstreak@68 98 table.insert(r,k)
flickerstreak@68 99 end
flickerstreak@68 100 local dotdotdot = { ... }
flickerstreak@68 101 table.sort(r, function(lhs, rhs)
flickerstreak@68 102 local olhs = tfetch(t[lhs], unpack(dotdotdot)) or 0
flickerstreak@68 103 local orhs = tfetch(t[rhs], unpack(dotdotdot)) or 0
flickerstreak@68 104 return olhs < orhs
flickerstreak@68 105 end)
flickerstreak@68 106 return r
flickerstreak@68 107 end
flickerstreak@68 108
flickerstreak@72 109 local function BuildRules(states)
flickerstreak@72 110 local rules = { }
flickerstreak@72 111 local keybinds = { }
flickerstreak@64 112 local default
flickerstreak@72 113 local fmt = "%s %s"
flickerstreak@72 114 for idx, state in ipairs(fieldsort(states, "rule", "order")) do
flickerstreak@72 115 local c = states[state].rule
flickerstreak@72 116 local type = tfetch(c,"type")
flickerstreak@72 117 if type == "default" then
flickerstreak@72 118 default = default or state
flickerstreak@72 119 elseif type == "keybind" then
flickerstreak@72 120 keybinds[state] = c.keybind or false
flickerstreak@72 121 elseif type == "custom" then
flickerstreak@72 122 if c.custom then
flickerstreak@64 123 -- strip out all spaces from the custom rule
flickerstreak@72 124 table.insert(rules, fmt:format(c.custom:gsub("%s",""), state))
flickerstreak@64 125 end
flickerstreak@72 126 elseif type == "any" then
flickerstreak@72 127 if c.values then
flickerstreak@72 128 local clauses = { }
flickerstreak@72 129 for key, value in pairs(c.values) do
flickerstreak@72 130 table.insert(clauses, ("[%s]"):format(ruleformats[key]))
flickerstreak@64 131 end
flickerstreak@72 132 if #clauses > 0 then
flickerstreak@72 133 table.insert(rules, fmt:format(table.concat(clauses), state))
flickerstreak@64 134 end
flickerstreak@64 135 end
flickerstreak@72 136 elseif type == "all" then
flickerstreak@72 137 if c.values then
flickerstreak@72 138 local clauses = { }
flickerstreak@72 139 for key, value in pairs(c.values) do
flickerstreak@72 140 table.insert(clauses, ruleformats[key])
flickerstreak@64 141 end
flickerstreak@72 142 if #clauses > 0 then
flickerstreak@72 143 table.insert(rules, fmt:format(("[%s]"):format(table.concat(clauses, ",")), state))
flickerstreak@64 144 end
flickerstreak@64 145 end
flickerstreak@64 146 end
flickerstreak@64 147 end
flickerstreak@64 148 if default then
flickerstreak@72 149 table.insert(rules, default)
flickerstreak@64 150 end
flickerstreak@72 151 return rules, keybinds
flickerstreak@64 152 end
flickerstreak@64 153
flickerstreak@68 154 local propertyFuncs = { }
flickerstreak@64 155
flickerstreak@64 156 function ApplyStates( bar )
flickerstreak@64 157 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@64 158 if states then
flickerstreak@72 159 local rules, keybinds = BuildRules(states)
flickerstreak@72 160 bar:SetStateDriver(table.concat(rules,";"), states, keybinds)
flickerstreak@68 161 for k, f in pairs(propertyFuncs) do
flickerstreak@68 162 f(bar, states)
flickerstreak@68 163 end
flickerstreak@64 164 end
flickerstreak@64 165 end
flickerstreak@68 166
flickerstreak@68 167 function GetProperty( bar, state, propname )
flickerstreak@68 168 return tfetch(module.db.profile.bars, bar:GetName(), "states", state, propname)
flickerstreak@68 169 end
flickerstreak@68 170
flickerstreak@68 171 function SetProperty( bar, state, propname, value )
flickerstreak@68 172 local states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@68 173 tbuild(states, state)[propname] = value
flickerstreak@68 174 local f = propertyFuncs[propname]
flickerstreak@68 175 if f then
flickerstreak@68 176 f(bar, states)
flickerstreak@68 177 end
flickerstreak@68 178 end
flickerstreak@68 179
flickerstreak@68 180 -- state property functions
flickerstreak@68 181 function propertyFuncs.hide( bar, states )
flickerstreak@68 182 local tmp = { }
flickerstreak@68 183 for state, config in pairs(states) do
flickerstreak@68 184 if config.hide then
flickerstreak@68 185 table.insert(tmp, state)
flickerstreak@68 186 end
flickerstreak@68 187 end
flickerstreak@68 188 local s = table.concat(tmp,",")
flickerstreak@68 189 bar:SetHideStates(s)
flickerstreak@68 190 end
flickerstreak@68 191
flickerstreak@68 192 function propertyFuncs.page( bar, states )
flickerstreak@68 193 local map = { }
flickerstreak@68 194 for state, config in pairs(states) do
flickerstreak@68 195 map[state] = config.page
flickerstreak@68 196 end
flickerstreak@68 197 bar:SetStatePageMap(state, map)
flickerstreak@68 198 end
flickerstreak@68 199
flickerstreak@68 200 function propertyFuncs.keybindstate( bar, states )
flickerstreak@68 201 local map = { }
flickerstreak@68 202 for state, config in pairs(states) do
flickerstreak@68 203 if config.keybindstate then
flickerstreak@68 204 table.insert(map,state)
flickerstreak@68 205 end
flickerstreak@68 206 end
flickerstreak@68 207 bar:SetStateKeybindOverrideMap(map)
flickerstreak@68 208 end
flickerstreak@68 209
flickerstreak@71 210 local function updateAnchor(bar, states)
flickerstreak@71 211 local map = { }
flickerstreak@71 212 for state, c in pairs(states) do
flickerstreak@71 213 if c.enableAnchor then
flickerstreak@71 214 map[state] = { point = c.anchorPoint, relpoint = c.anchorRelPoint, x = c.anchorX, y = c.anchorY }
flickerstreak@71 215 end
flickerstreak@71 216 end
flickerstreak@71 217 bar:SetStateAnchorMap(map)
flickerstreak@68 218 end
flickerstreak@68 219
flickerstreak@71 220 propertyFuncs.enableAnchor = updateAnchor
flickerstreak@71 221 propertyFuncs.anchorPoint = updateAnchor
flickerstreak@71 222 propertyFuncs.anchorRelPoint = updateAnchor
flickerstreak@71 223 propertyFuncs.anchorX = updateAnchor
flickerstreak@71 224 propertyFuncs.anchorY = updateAnchor
flickerstreak@68 225
flickerstreak@71 226 local function updateScale( bar, states )
flickerstreak@71 227 local map = { }
flickerstreak@71 228 for state, c in pairs(states) do
flickerstreak@71 229 if c.enablescale then
flickerstreak@71 230 map[state] = c.scale
flickerstreak@71 231 end
flickerstreak@71 232 end
flickerstreak@71 233 bar:SetStateScaleMap(map)
flickerstreak@68 234 end
flickerstreak@68 235
flickerstreak@71 236 propertyFuncs.enablescale = updateScale
flickerstreak@71 237 propertyFuncs.scale = updateScale
flickerstreak@68 238
flickerstreak@64 239 end
flickerstreak@64 240
flickerstreak@64 241
flickerstreak@68 242
flickerstreak@68 243 -- module event handlers --
flickerstreak@68 244
flickerstreak@65 245 function module:OnInitialize()
flickerstreak@65 246 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@65 247 {
flickerstreak@65 248 profile = {
flickerstreak@65 249 bars = { },
flickerstreak@65 250 }
flickerstreak@65 251 }
flickerstreak@65 252 )
flickerstreak@65 253
flickerstreak@65 254 InitRules()
flickerstreak@65 255 self:RegisterEvent("PLAYER_AURAS_CHANGED")
flickerstreak@65 256
flickerstreak@65 257 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@65 258
flickerstreak@65 259 ReAction.RegisterCallback(self, "OnCreateBar","OnRefreshBar")
flickerstreak@65 260 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@65 261 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@65 262 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@65 263 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@65 264 end
flickerstreak@65 265
flickerstreak@65 266 function module:PLAYER_AURAS_CHANGED()
flickerstreak@65 267 self:UnregisterEvent("PLAYER_AURAS_CHANGED")
flickerstreak@66 268 -- on login the number of stances is 0 until this event fires during the init sequence.
flickerstreak@66 269 -- however if you reload just the UI the number of stances is correct immediately
flickerstreak@66 270 -- and this event won't fire until you gain/lose buffs/debuffs, at which point you might
flickerstreak@66 271 -- be in combat.
flickerstreak@66 272 if not InCombatLockdown() then
flickerstreak@66 273 InitRules()
flickerstreak@66 274 for name, bar in ReAction:IterateBars() do
flickerstreak@67 275 self:OnRefreshBar(nil,bar,name)
flickerstreak@66 276 end
flickerstreak@66 277 end
flickerstreak@65 278 end
flickerstreak@65 279
flickerstreak@65 280 function module:OnRefreshBar(event, bar, name)
flickerstreak@65 281 local c = self.db.profile.bars[name]
flickerstreak@65 282 if c then
flickerstreak@68 283 ApplyStates(bar)
flickerstreak@65 284 end
flickerstreak@65 285 end
flickerstreak@65 286
flickerstreak@65 287 function module:OnEraseBar(event, bar, name)
flickerstreak@65 288 self.db.profile.bars[name] = nil
flickerstreak@65 289 end
flickerstreak@65 290
flickerstreak@65 291 function module:OnRenameBar(event, bar, oldname, newname)
flickerstreak@65 292 local b = self.db.profile.bars
flickerstreak@65 293 bars[newname], bars[oldname] = bars[oldname], nil
flickerstreak@65 294 end
flickerstreak@65 295
flickerstreak@65 296 function module:OnConfigModeChanged(event, mode)
flickerstreak@65 297 -- TODO: unregister all state drivers (temporarily) and hidestates
flickerstreak@65 298 end
flickerstreak@65 299
flickerstreak@64 300
flickerstreak@64 301
flickerstreak@64 302 -- Options --
flickerstreak@64 303
flickerstreak@64 304 local CreateBarOptions
flickerstreak@62 305 do
flickerstreak@62 306 local function ClassCheck(...)
flickerstreak@62 307 for i = 1, select('#',...) do
flickerstreak@62 308 local _, c = UnitClass("player")
flickerstreak@62 309 if c == select(i,...) then
flickerstreak@62 310 return false
flickerstreak@62 311 end
flickerstreak@62 312 end
flickerstreak@62 313 return true
flickerstreak@62 314 end
flickerstreak@62 315
flickerstreak@64 316 -- pre-sorted by the order they should appear in
flickerstreak@64 317 local rules = {
flickerstreak@64 318 -- rule hidden fields
flickerstreak@64 319 { "stance", ClassCheck("WARRIOR"), { {battle = L["Battle Stance"]}, {defensive = L["Defensive Stance"]}, {berserker = L["Berserker Stance"]} } },
flickerstreak@64 320 { "form", ClassCheck("DRUID"), { {caster = L["Caster Form"]}, {bear = L["Bear Form"]}, {cat = L["Cat Form"]}, {treeOrMoonkin = L["Tree/Moonkin"]} } },
flickerstreak@64 321 { "stealth", ClassCheck("ROGUE","DRUID"), { {stealth = L["Stealth"]}, {nostealth = L["No Stealth"]} } },
flickerstreak@64 322 { "shadow", ClassCheck("PRIEST"), { {shadowform = L["Shadowform"]}, {noshadowform = L["No Shadowform"]} } },
flickerstreak@64 323 { "pet", ClassCheck("HUNTER","WARLOCK"), { {pet = L["With Pet"]}, {nopet = L["Without Pet"]} } },
flickerstreak@64 324 { "target", false, { {harm = L["Hostile Target"]}, {help = L["Friendly Target"]}, {notarget = L["No Target"]} } },
flickerstreak@64 325 { "focus", false, { {focusharm = L["Hostile Focus"]}, {focushelp = L["Friendly Focus"]}, {nofocus = L["No Focus"]} } },
flickerstreak@64 326 { "group", false, { {raid = L["Raid"]}, {party = L["Party"]}, {solo = L["Solo"]} } },
flickerstreak@64 327 { "combat", false, { {combat = L["In Combat"]}, {nocombat = L["Out of Combat"]} } },
flickerstreak@62 328 }
flickerstreak@62 329
flickerstreak@64 330 local ruleSelect = { }
flickerstreak@64 331 local ruleMap = { }
flickerstreak@64 332 local optionMap = setmetatable({},{__mode="k"})
flickerstreak@62 333
flickerstreak@68 334 local pointTable = {
flickerstreak@68 335 NONE = " ",
flickerstreak@68 336 CENTER = L["Center"],
flickerstreak@68 337 LEFT = L["Left"],
flickerstreak@68 338 RIGHT = L["Right"],
flickerstreak@68 339 TOP = L["Top"],
flickerstreak@68 340 BOTTOM = L["Bottom"],
flickerstreak@68 341 TOPLEFT = L["Top Left"],
flickerstreak@68 342 TOPRIGHT = L["Top Right"],
flickerstreak@68 343 BOTTOMLEFT = L["Bottom Left"],
flickerstreak@68 344 BOTTOMRIGHT = L["Bottom Right"],
flickerstreak@68 345 }
flickerstreak@68 346
flickerstreak@64 347 -- unpack rules table into ruleSelect and ruleMap
flickerstreak@64 348 for _, c in ipairs(rules) do
flickerstreak@64 349 local rule, hidden, fields = unpack(c)
flickerstreak@64 350 if not hidden then
flickerstreak@64 351 for _, field in ipairs(fields) do
flickerstreak@64 352 local key, label = next(field)
flickerstreak@64 353 table.insert(ruleSelect, label)
flickerstreak@64 354 table.insert(ruleMap, key)
flickerstreak@62 355 end
flickerstreak@62 356 end
flickerstreak@62 357 end
flickerstreak@62 358
flickerstreak@62 359 local function CreateStateOptions(bar, name)
flickerstreak@62 360 local opts = {
flickerstreak@62 361 type = "group",
flickerstreak@62 362 name = name,
flickerstreak@64 363 childGroups = "tab",
flickerstreak@25 364 }
flickerstreak@62 365
flickerstreak@64 366 local states = tbuild(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@64 367
flickerstreak@68 368 local function update()
flickerstreak@68 369 ApplyStates(bar)
flickerstreak@68 370 end
flickerstreak@68 371
flickerstreak@68 372 local function setrule( key, value, ... )
flickerstreak@64 373 tbuild(states, opts.name, "rule", ...)[key] = value
flickerstreak@64 374 end
flickerstreak@64 375
flickerstreak@68 376 local function getrule( ... )
flickerstreak@64 377 return tfetch(states, opts.name, "rule", ...)
flickerstreak@64 378 end
flickerstreak@64 379
flickerstreak@68 380 local function setprop(info, value)
flickerstreak@68 381 SetProperty(bar, opts.name, info[#info], value)
flickerstreak@68 382 end
flickerstreak@68 383
flickerstreak@68 384 local function getprop(info)
flickerstreak@68 385 return GetProperty(bar, opts.name, info[#info])
flickerstreak@68 386 end
flickerstreak@68 387
flickerstreak@64 388 local function fixall(setkey)
flickerstreak@64 389 -- if multiple selections in the same group are chosen when 'all' is selected,
flickerstreak@64 390 -- keep only one of them. If changing the mode, the first in the fields list will
flickerstreak@64 391 -- be chosen arbitrarily. Otherwise, if selecting a new checkbox from the field-set,
flickerstreak@64 392 -- it will be retained.
flickerstreak@64 393 local notified = false
flickerstreak@64 394 for _, c in ipairs(rules) do
flickerstreak@64 395 local rule, hidden, fields = unpack(c)
flickerstreak@64 396 local found = false
flickerstreak@64 397 for key in ipairs(fields) do
flickerstreak@68 398 if getrule("values",key) then
flickerstreak@64 399 if (found or setkey) and key ~= setkey then
flickerstreak@68 400 setrule(key,false,"values")
flickerstreak@64 401 if not setkey and not notified then
flickerstreak@64 402 ReAction:UserError(L["Warning: one or more incompatible rules were turned off"])
flickerstreak@64 403 notified = true
flickerstreak@64 404 end
flickerstreak@64 405 end
flickerstreak@64 406 found = true
flickerstreak@64 407 end
flickerstreak@62 408 end
flickerstreak@62 409 end
flickerstreak@62 410 end
flickerstreak@62 411
flickerstreak@64 412 local function getNeighbors()
flickerstreak@64 413 local before, after
flickerstreak@64 414 for k, v in pairs(states) do
flickerstreak@64 415 local o = tonumber(tfetch(v, "rule", "order"))
flickerstreak@64 416 if o and k ~= opts.name then
flickerstreak@64 417 local obefore = tfetch(states,before,"rule","order")
flickerstreak@64 418 local oafter = tfetch(states,after,"rule","order")
flickerstreak@64 419 if o < opts.order and (not obefore or obefore < o) then
flickerstreak@64 420 before = k
flickerstreak@64 421 end
flickerstreak@64 422 if o > opts.order and (not oafter or oafter > o) then
flickerstreak@64 423 after = k
flickerstreak@64 424 end
flickerstreak@64 425 end
flickerstreak@64 426 end
flickerstreak@64 427 return before, after
flickerstreak@64 428 end
flickerstreak@64 429
flickerstreak@64 430 local function swapOrder( a, b )
flickerstreak@64 431 -- do options table
flickerstreak@64 432 local args = optionMap[bar].args
flickerstreak@64 433 args[a].order, args[b].order = args[b].order, args[a].order
flickerstreak@64 434 -- do profile
flickerstreak@64 435 a = tbuild(states, a, "rule")
flickerstreak@64 436 b = tbuild(states, b, "rule")
flickerstreak@64 437 a.order, b.order = b.order, a.order
flickerstreak@64 438 end
flickerstreak@64 439
flickerstreak@68 440 local function anchordisable()
flickerstreak@71 441 return not GetProperty(bar, opts.name, "enableAnchor")
flickerstreak@64 442 end
flickerstreak@64 443
flickerstreak@68 444 tbuild(states, name)
flickerstreak@64 445
flickerstreak@68 446 opts.order = getrule("order")
flickerstreak@64 447 if opts.order == nil then
flickerstreak@64 448 -- add after the highest
flickerstreak@64 449 opts.order = 100
flickerstreak@64 450 for _, state in pairs(states) do
flickerstreak@64 451 local x = tonumber(tfetch(state, "rule", "order"))
flickerstreak@64 452 if x and x >= opts.order then
flickerstreak@64 453 opts.order = x + 1
flickerstreak@64 454 end
flickerstreak@64 455 end
flickerstreak@68 456 setrule("order",opts.order)
flickerstreak@64 457 end
flickerstreak@64 458
flickerstreak@64 459 opts.args = {
flickerstreak@68 460 ordering = {
flickerstreak@68 461 name = L["Info"],
flickerstreak@68 462 order = 1,
flickerstreak@64 463 type = "group",
flickerstreak@64 464 args = {
flickerstreak@64 465 delete = {
flickerstreak@68 466 name = L["Delete this State"],
flickerstreak@68 467 order = -1,
flickerstreak@64 468 type = "execute",
flickerstreak@64 469 func = function(info)
flickerstreak@68 470 if states[opts.name] then
flickerstreak@68 471 states[opts.name] = nil
flickerstreak@68 472 ApplyStates(bar)
flickerstreak@68 473 end
flickerstreak@64 474 optionMap[bar].args[opts.name] = nil
flickerstreak@64 475 end,
flickerstreak@64 476 },
flickerstreak@64 477 rename = {
flickerstreak@64 478 name = L["Name"],
flickerstreak@64 479 order = 1,
flickerstreak@68 480 type = "input",
flickerstreak@64 481 get = function() return opts.name end,
flickerstreak@64 482 set = function(info, value)
flickerstreak@64 483 -- check for existing state name
flickerstreak@64 484 if states[value] then
flickerstreak@64 485 L["State named '%s' already exists"]:format(value)
flickerstreak@64 486 end
flickerstreak@64 487 local args = optionMap[bar].args
flickerstreak@64 488 states[value], args[value], states[opts.name], args[opts.name] = states[opts.name], args[opts.name], nil, nil
flickerstreak@64 489 opts.name = value
flickerstreak@64 490 update()
flickerstreak@64 491 end,
flickerstreak@64 492 pattern = "^%w*$",
flickerstreak@64 493 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@64 494 },
flickerstreak@64 495 ordering = {
flickerstreak@64 496 name = L["Evaluation Order"],
flickerstreak@64 497 desc = L["State transitions are evaluated in the order listed:\nMove a state up or down to change the order"],
flickerstreak@64 498 order = 2,
flickerstreak@68 499 type = "group",
flickerstreak@68 500 inline = true,
flickerstreak@64 501 args = {
flickerstreak@64 502 up = {
flickerstreak@68 503 name = L["Up"],
flickerstreak@68 504 order = 1,
flickerstreak@64 505 type = "execute",
flickerstreak@64 506 width = "half",
flickerstreak@64 507 func = function()
flickerstreak@64 508 local before, after = getNeighbors()
flickerstreak@64 509 if before then
flickerstreak@64 510 swapOrder(before, opts.name)
flickerstreak@64 511 update()
flickerstreak@64 512 end
flickerstreak@64 513 end,
flickerstreak@64 514 },
flickerstreak@64 515 down = {
flickerstreak@68 516 name = L["Down"],
flickerstreak@68 517 order = 2,
flickerstreak@64 518 type = "execute",
flickerstreak@64 519 width = "half",
flickerstreak@64 520 func = function()
flickerstreak@64 521 local before, after = getNeighbors()
flickerstreak@64 522 if after then
flickerstreak@64 523 swapOrder(opts.name, after)
flickerstreak@64 524 update()
flickerstreak@64 525 end
flickerstreak@64 526 end,
flickerstreak@64 527 }
flickerstreak@64 528 }
flickerstreak@68 529 }
flickerstreak@64 530 }
flickerstreak@64 531 },
flickerstreak@68 532 properties = {
flickerstreak@68 533 name = L["Properties"],
flickerstreak@68 534 order = 2,
flickerstreak@68 535 type = "group",
flickerstreak@68 536 args = {
flickerstreak@68 537 desc = {
flickerstreak@68 538 name = L["Set the properties for the bar when in this state"],
flickerstreak@68 539 order = 1,
flickerstreak@68 540 type = "description"
flickerstreak@68 541 },
flickerstreak@68 542 hide = {
flickerstreak@68 543 name = L["Hide Bar"],
flickerstreak@68 544 order = 2,
flickerstreak@68 545 type = "toggle",
flickerstreak@68 546 set = setprop,
flickerstreak@68 547 get = getprop,
flickerstreak@68 548 },
flickerstreak@68 549 page = {
flickerstreak@68 550 name = L["Show Page #"],
flickerstreak@68 551 order = 3,
flickerstreak@68 552 type = "select",
flickerstreak@68 553 disabled = function()
flickerstreak@68 554 return bar:GetNumPages() < 2
flickerstreak@68 555 end,
flickerstreak@68 556 hidden = function()
flickerstreak@68 557 return bar:GetNumPages() < 2
flickerstreak@68 558 end,
flickerstreak@68 559 values = function()
flickerstreak@68 560 local pages = { none = " " }
flickerstreak@68 561 for i = 1, bar:GetNumPages() do
flickerstreak@68 562 pages[i] = i
flickerstreak@68 563 end
flickerstreak@68 564 return pages
flickerstreak@68 565 end,
flickerstreak@68 566 set = function(info, value)
flickerstreak@68 567 if value == "none" then
flickerstreak@68 568 setprop(info, nil)
flickerstreak@68 569 else
flickerstreak@68 570 setprop(info, value)
flickerstreak@68 571 end
flickerstreak@68 572 end,
flickerstreak@68 573 get = function(info)
flickerstreak@68 574 return getprop(info) or "none"
flickerstreak@68 575 end,
flickerstreak@68 576 },
flickerstreak@68 577 keybindstate = {
flickerstreak@68 578 name = L["Override Keybinds"],
flickerstreak@68 579 desc = L["Set this state to maintain its own set of keybinds which override the defaults when active"],
flickerstreak@68 580 order = 4,
flickerstreak@68 581 type = "toggle",
flickerstreak@68 582 set = setprop,
flickerstreak@68 583 get = getprop,
flickerstreak@68 584 },
flickerstreak@68 585 position = {
flickerstreak@68 586 name = L["Position"],
flickerstreak@68 587 order = 5,
flickerstreak@68 588 type = "group",
flickerstreak@68 589 inline = true,
flickerstreak@68 590 args = {
flickerstreak@71 591 enableAnchor = {
flickerstreak@68 592 name = L["Set New Position"],
flickerstreak@68 593 order = 1,
flickerstreak@68 594 type = "toggle",
flickerstreak@68 595 set = setprop,
flickerstreak@68 596 get = getprop,
flickerstreak@68 597 },
flickerstreak@68 598 anchorPoint = {
flickerstreak@68 599 name = L["Point"],
flickerstreak@68 600 order = 2,
flickerstreak@68 601 type = "select",
flickerstreak@68 602 values = pointTable,
flickerstreak@68 603 set = function(info, value) setprop(info, value ~= "NONE" and value or nil) end,
flickerstreak@68 604 get = function(info) return getprop(info) or "NONE" end,
flickerstreak@68 605 disabled = anchordisable,
flickerstreak@68 606 hidden = anchordisable,
flickerstreak@68 607 },
flickerstreak@68 608 anchorRelPoint = {
flickerstreak@68 609 name = L["Relative Point"],
flickerstreak@68 610 order = 3,
flickerstreak@68 611 type = "select",
flickerstreak@68 612 values = pointTable,
flickerstreak@68 613 set = function(info, value) setprop(info, value ~= "NONE" and value or nil) end,
flickerstreak@68 614 get = function(info) return getprop(info) or "NONE" end,
flickerstreak@68 615 disabled = anchordisable,
flickerstreak@68 616 hidden = anchordisable,
flickerstreak@68 617 },
flickerstreak@68 618 anchorX = {
flickerstreak@68 619 name = L["X Offset"],
flickerstreak@68 620 order = 4,
flickerstreak@68 621 type = "range",
flickerstreak@68 622 min = -100,
flickerstreak@68 623 max = 100,
flickerstreak@68 624 step = 1,
flickerstreak@68 625 set = setprop,
flickerstreak@68 626 get = getprop,
flickerstreak@68 627 disabled = anchordisable,
flickerstreak@68 628 hidden = anchordisable,
flickerstreak@68 629 },
flickerstreak@68 630 anchorY = {
flickerstreak@68 631 name = L["Y Offset"],
flickerstreak@68 632 order = 5,
flickerstreak@68 633 type = "range",
flickerstreak@68 634 min = -100,
flickerstreak@68 635 max = 100,
flickerstreak@68 636 step = 1,
flickerstreak@68 637 set = setprop,
flickerstreak@68 638 get = getprop,
flickerstreak@68 639 disabled = anchordisable,
flickerstreak@68 640 hidden = anchordisable,
flickerstreak@68 641 },
flickerstreak@68 642 },
flickerstreak@68 643 },
flickerstreak@68 644 scale = {
flickerstreak@68 645 name = L["Scale"],
flickerstreak@68 646 order = 6,
flickerstreak@68 647 type = "group",
flickerstreak@68 648 inline = true,
flickerstreak@68 649 args = {
flickerstreak@68 650 enablescale = {
flickerstreak@68 651 name = L["Set New Scale"],
flickerstreak@68 652 order = 1,
flickerstreak@68 653 type = "toggle",
flickerstreak@68 654 set = setprop,
flickerstreak@68 655 get = getprop,
flickerstreak@68 656 },
flickerstreak@68 657 scale = {
flickerstreak@68 658 name = L["Scale"],
flickerstreak@68 659 order = 2,
flickerstreak@68 660 type = "range",
flickerstreak@68 661 min = 0.1,
flickerstreak@68 662 max = 2.5,
flickerstreak@68 663 step = 0.05,
flickerstreak@68 664 isPercent = true,
flickerstreak@68 665 set = setprop,
flickerstreak@68 666 get = function(info) return getprop(info) or 1 end,
flickerstreak@68 667 disabled = function() return not GetProperty(bar, opts.name, "enablescale") end,
flickerstreak@68 668 hidden = function() return not GetProperty(bar, opts.name, "enablescale") end,
flickerstreak@68 669 },
flickerstreak@68 670 },
flickerstreak@68 671 },
flickerstreak@68 672 },
flickerstreak@68 673 },
flickerstreak@64 674 rules = {
flickerstreak@70 675 name = L["Rule"],
flickerstreak@68 676 order = 3,
flickerstreak@64 677 type = "group",
flickerstreak@64 678 args = {
flickerstreak@64 679 mode = {
flickerstreak@68 680 name = L["Select this state"],
flickerstreak@68 681 order = 2,
flickerstreak@64 682 type = "select",
flickerstreak@64 683 style = "radio",
flickerstreak@64 684 values = {
flickerstreak@64 685 default = L["by default"],
flickerstreak@64 686 any = L["when ANY of these"],
flickerstreak@64 687 all = L["when ALL of these"],
flickerstreak@68 688 custom = L["via custom rule"],
flickerstreak@68 689 keybind = L["via keybinding"],
flickerstreak@64 690 },
flickerstreak@64 691 set = function( info, value )
flickerstreak@68 692 setrule("type", value)
flickerstreak@64 693 fixall()
flickerstreak@64 694 update()
flickerstreak@64 695 end,
flickerstreak@64 696 get = function( info )
flickerstreak@68 697 return getrule("type")
flickerstreak@64 698 end,
flickerstreak@64 699 },
flickerstreak@64 700 clear = {
flickerstreak@68 701 name = L["Clear All"],
flickerstreak@68 702 order = 3,
flickerstreak@64 703 type = "execute",
flickerstreak@68 704 hidden = function()
flickerstreak@68 705 local t = getrule("type")
flickerstreak@68 706 return t ~= "any" and t ~= "all"
flickerstreak@68 707 end,
flickerstreak@68 708 disabled = function()
flickerstreak@68 709 local t = getrule("type")
flickerstreak@68 710 return t ~= "any" and t ~= "all"
flickerstreak@68 711 end,
flickerstreak@64 712 func = function()
flickerstreak@68 713 local type = getrule("type")
flickerstreak@64 714 if type == "custom" then
flickerstreak@68 715 setrule("custom","")
flickerstreak@64 716 elseif type == "any" or type == "all" then
flickerstreak@68 717 setrule("values", {})
flickerstreak@64 718 end
flickerstreak@64 719 update()
flickerstreak@64 720 end,
flickerstreak@64 721 },
flickerstreak@64 722 inputs = {
flickerstreak@68 723 name = L["Conditions"],
flickerstreak@68 724 order = 4,
flickerstreak@64 725 type = "multiselect",
flickerstreak@64 726 hidden = function()
flickerstreak@68 727 local t = getrule("type")
flickerstreak@68 728 return t ~= "any" and t ~= "all"
flickerstreak@64 729 end,
flickerstreak@64 730 disabled = function()
flickerstreak@68 731 local t = getrule("type")
flickerstreak@68 732 return t ~= "any" and t ~= "all"
flickerstreak@64 733 end,
flickerstreak@64 734 values = ruleSelect,
flickerstreak@64 735 set = function(info, key, value )
flickerstreak@68 736 setrule(ruleMap[key], value or nil, "values")
flickerstreak@64 737 if value then
flickerstreak@64 738 fixall(ruleMap[key])
flickerstreak@64 739 end
flickerstreak@64 740 update()
flickerstreak@64 741 end,
flickerstreak@64 742 get = function(info, key)
flickerstreak@68 743 return getrule("values", ruleMap[key]) or false
flickerstreak@64 744 end,
flickerstreak@64 745 },
flickerstreak@64 746 custom = {
flickerstreak@68 747 name = L["Custom Rule"],
flickerstreak@68 748 order = 5,
flickerstreak@64 749 type = "input",
flickerstreak@64 750 multiline = true,
flickerstreak@64 751 hidden = function()
flickerstreak@68 752 return getrule("type") ~= "custom"
flickerstreak@64 753 end,
flickerstreak@64 754 disabled = function()
flickerstreak@68 755 return getrule("type") ~= "custom"
flickerstreak@64 756 end,
flickerstreak@64 757 desc = L["Syntax like macro rules: see preset rules for examples"],
flickerstreak@64 758 set = function(info, value)
flickerstreak@68 759 setrule("custom",value)
flickerstreak@64 760 update()
flickerstreak@64 761 end,
flickerstreak@64 762 get = function(info)
flickerstreak@68 763 return getrule("custom") or ""
flickerstreak@64 764 end,
flickerstreak@64 765 validate = function (info, rule)
flickerstreak@64 766 local s = rule:gsub("%s","") -- remove all spaces
flickerstreak@64 767 -- unfortunately %b and captures don't support the '+' notation, or this would be considerably simpler
flickerstreak@64 768 repeat
flickerstreak@64 769 if s == "" then
flickerstreak@64 770 return true
flickerstreak@64 771 end
flickerstreak@64 772 local c, r = s:match("(%b[])(.*)")
flickerstreak@64 773 if c == nil and s and #s > 0 then
flickerstreak@64 774 return L["Invalid custom rule '%s': each clause must appear within [brackets]"]:format(rule)
flickerstreak@64 775 end
flickerstreak@64 776 s = r
flickerstreak@64 777 until c == nil
flickerstreak@64 778 return true
flickerstreak@64 779 end,
flickerstreak@68 780 },
flickerstreak@68 781 keybind = {
flickerstreak@68 782 name = L["Keybinding"],
flickerstreak@68 783 order = 6,
flickerstreak@68 784 inline = true,
flickerstreak@68 785 hidden = function() return getrule("type") ~= "keybind" end,
flickerstreak@68 786 disabled = function() return getrule("type") ~= "keybind" end,
flickerstreak@68 787 type = "group",
flickerstreak@68 788 args = {
flickerstreak@68 789 desc = {
flickerstreak@72 790 name = L["Invoking a state keybind toggles an override of all other transition rules."],
flickerstreak@68 791 order = 1,
flickerstreak@68 792 type = "description",
flickerstreak@68 793 },
flickerstreak@68 794 keybind = {
flickerstreak@68 795 name = L["State Hotkey"],
flickerstreak@68 796 desc = L["Define an override toggle keybind"],
flickerstreak@68 797 order = 2,
flickerstreak@68 798 type = "keybinding",
flickerstreak@68 799 set = function(info, value)
flickerstreak@70 800 if value and #value == 0 then
flickerstreak@70 801 value = nil
flickerstreak@70 802 end
flickerstreak@68 803 setrule("keybind",value)
flickerstreak@68 804 update()
flickerstreak@68 805 end,
flickerstreak@68 806 get = function() return getrule("keybind") end,
flickerstreak@68 807 },
flickerstreak@68 808 },
flickerstreak@68 809 },
flickerstreak@68 810 },
flickerstreak@68 811 },
flickerstreak@62 812 }
flickerstreak@62 813 return opts
flickerstreak@25 814 end
flickerstreak@62 815
flickerstreak@64 816
flickerstreak@62 817 CreateBarOptions = function(bar)
flickerstreak@62 818 local private = { }
flickerstreak@62 819 local options = {
flickerstreak@62 820 type = "group",
flickerstreak@62 821 name = L["Dynamic State"],
flickerstreak@64 822 childGroups = "tree",
flickerstreak@62 823 disabled = InCombatLockdown,
flickerstreak@62 824 args = {
flickerstreak@64 825 __desc__ = {
flickerstreak@68 826 name = L["States are evaluated in the order they are listed"],
flickerstreak@68 827 order = 1,
flickerstreak@64 828 type = "description",
flickerstreak@64 829 },
flickerstreak@64 830 __new__ = {
flickerstreak@64 831 name = L["New State..."],
flickerstreak@64 832 order = 2,
flickerstreak@68 833 type = "group",
flickerstreak@62 834 args = {
flickerstreak@64 835 name = {
flickerstreak@64 836 name = L["State Name"],
flickerstreak@64 837 desc = L["Set a name for the new state"],
flickerstreak@68 838 order = 1,
flickerstreak@68 839 type = "input",
flickerstreak@64 840 get = function() return private.newstatename or "" end,
flickerstreak@64 841 set = function(info,value) private.newstatename = value end,
flickerstreak@64 842 pattern = "^%w*$",
flickerstreak@64 843 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@64 844 },
flickerstreak@64 845 create = {
flickerstreak@68 846 name = L["Create State"],
flickerstreak@68 847 order = 2,
flickerstreak@64 848 type = "execute",
flickerstreak@64 849 func = function ()
flickerstreak@64 850 local name = private.newstatename
flickerstreak@68 851 if states[name] then
flickerstreak@68 852 ReAction:UserError(L["State named '%s' already exists"]:format(name))
flickerstreak@68 853 else
flickerstreak@68 854 -- TODO: select default state options and pass as final argument
flickerstreak@68 855 states[name] = { }
flickerstreak@68 856 optionMap[bar].args[name] = CreateStateOptions(bar,name)
flickerstreak@68 857 private.newstatename = ""
flickerstreak@68 858 end
flickerstreak@64 859 end,
flickerstreak@64 860 disabled = function()
flickerstreak@64 861 local name = private.newstatename or ""
flickerstreak@64 862 return #name == 0 or name:find("%W")
flickerstreak@64 863 end,
flickerstreak@62 864 }
flickerstreak@62 865 }
flickerstreak@64 866 }
flickerstreak@62 867 }
flickerstreak@62 868 }
flickerstreak@62 869 local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
flickerstreak@62 870 if states then
flickerstreak@62 871 for name, config in pairs(states) do
flickerstreak@64 872 options.args[name] = CreateStateOptions(bar,name)
flickerstreak@62 873 end
flickerstreak@62 874 end
flickerstreak@64 875 optionMap[bar] = options
flickerstreak@62 876 return options
flickerstreak@62 877 end
flickerstreak@25 878 end
flickerstreak@25 879
flickerstreak@62 880 function module:GetBarOptions(bar)
flickerstreak@64 881 return CreateBarOptions(bar)
flickerstreak@25 882 end