annotate classes/State.lua @ 240:98d7ad4a1158

move state.lua into classes for now, removed obsolete functions
author Flick
date Fri, 25 Mar 2011 16:42:21 -0700
parents modules/State.lua@2669f737d9d7
children 09c8e9baa35a
rev   line source
flickerstreak@109 1 --[[
flickerstreak@109 2 ReAction bar state driver interface
flickerstreak@109 3
flickerstreak@109 4 --]]
flickerstreak@109 5
flickerstreak@109 6 -- local imports
flickerstreak@175 7 local addonName, addonTable = ...
flickerstreak@175 8 local ReAction = addonTable.ReAction
flickerstreak@109 9 local L = ReAction.L
flickerstreak@109 10 local _G = _G
flickerstreak@109 11 local format = string.format
flickerstreak@109 12 local InCombatLockdown = InCombatLockdown
flickerstreak@109 13 local RegisterStateDriver = RegisterStateDriver
flickerstreak@109 14
flickerstreak@109 15 -- module declaration
flickerstreak@109 16 local moduleID = "State"
flickerstreak@109 17 local module = ReAction:NewModule( moduleID, "AceEvent-3.0" )
flickerstreak@109 18
flickerstreak@109 19 -- Utility --
flickerstreak@109 20
flickerstreak@109 21 -- traverse a table tree by key list and fetch the result or first nil
flickerstreak@109 22 local function tfetch(t, ...)
flickerstreak@109 23 for i = 1, select('#', ...) do
flickerstreak@109 24 t = t and t[select(i, ...)]
flickerstreak@109 25 end
flickerstreak@109 26 return t
flickerstreak@109 27 end
flickerstreak@109 28
flickerstreak@109 29 -- traverse a table tree by key list and build tree as necessary
flickerstreak@109 30 local function tbuild(t, ...)
flickerstreak@109 31 for i = 1, select('#', ...) do
flickerstreak@109 32 local key = select(i, ...)
flickerstreak@109 33 if not t[key] then t[key] = { } end
flickerstreak@109 34 t = t[key]
flickerstreak@109 35 end
flickerstreak@109 36 return t
flickerstreak@109 37 end
flickerstreak@109 38
flickerstreak@109 39 -- return a new array of keys of table 't', sorted by comparing
flickerstreak@109 40 -- sub-fields (obtained via tfetch) of the table values
flickerstreak@109 41 local function fieldsort( t, ... )
flickerstreak@109 42 local r = { }
flickerstreak@109 43 for k in pairs(t) do
flickerstreak@109 44 table.insert(r,k)
flickerstreak@109 45 end
flickerstreak@109 46 local path = { ... }
flickerstreak@109 47 table.sort(r, function(lhs, rhs)
flickerstreak@109 48 local olhs = tfetch(t[lhs], unpack(path)) or 0
flickerstreak@109 49 local orhs = tfetch(t[rhs], unpack(path)) or 0
flickerstreak@109 50 return olhs < orhs
flickerstreak@109 51 end)
flickerstreak@109 52 return r
flickerstreak@109 53 end
flickerstreak@109 54
flickerstreak@109 55
Flick@240 56 local ApplyStates, CleanupStates, SetProperty, GetProperty
flickerstreak@109 57
flickerstreak@109 58 -- PRIVATE --
flickerstreak@109 59 do
flickerstreak@109 60 function GetProperty( bar, state, propname )
Flick@228 61 return tfetch(bar:GetConfig(), "states", state, propname)
flickerstreak@109 62 end
flickerstreak@109 63
flickerstreak@109 64 function SetProperty( bar, state, propname, value )
Flick@228 65 local s = tbuild(bar:GetConfig(), "states", state)
flickerstreak@109 66 s[propname] = value
flickerstreak@155 67 bar:SetSecureStateData(state, propname, value)
flickerstreak@109 68 end
flickerstreak@109 69
flickerstreak@109 70 function ApplyStates( bar )
Flick@228 71 local states = tfetch(bar:GetConfig(), "states")
flickerstreak@109 72 if states then
flickerstreak@157 73 bar:SetStateDriver(states)
flickerstreak@109 74 end
flickerstreak@109 75 end
flickerstreak@109 76
flickerstreak@109 77 function CleanupStates( bar )
flickerstreak@155 78 bar:SetStateDriver(nil)
flickerstreak@109 79 end
flickerstreak@109 80 end
flickerstreak@109 81
flickerstreak@109 82
flickerstreak@109 83
flickerstreak@109 84 -- module event handlers --
flickerstreak@109 85
flickerstreak@109 86 function module:OnInitialize()
flickerstreak@109 87 self:RegisterEvent("UPDATE_SHAPESHIFT_FORMS")
flickerstreak@109 88
flickerstreak@109 89 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@109 90
flickerstreak@109 91 ReAction.RegisterCallback(self, "OnCreateBar","OnRefreshBar")
flickerstreak@109 92 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@109 93 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@109 94 end
flickerstreak@109 95
flickerstreak@109 96 function module:OnEnable()
flickerstreak@109 97 self:UPDATE_SHAPESHIFT_FORMS() -- it doesn't fire on a /reloadui
flickerstreak@109 98 end
flickerstreak@109 99
flickerstreak@109 100 function module:UPDATE_SHAPESHIFT_FORMS()
flickerstreak@109 101 -- Re-parse the rules table according to the new form list.
flickerstreak@109 102 -- This happens both at initial login (after PLAYER_ENTERING_WORLD)
flickerstreak@109 103 -- as well as when gaining new abilities.
flickerstreak@157 104 ReAction.Bar.InitRuleFormats()
flickerstreak@155 105 for _, bar in ReAction:IterateBars() do
flickerstreak@155 106 ApplyStates(bar)
flickerstreak@109 107 end
flickerstreak@109 108 end
flickerstreak@109 109
flickerstreak@109 110 function module:OnRefreshBar(event, bar, name)
Flick@228 111 ApplyStates(bar)
flickerstreak@109 112 end
flickerstreak@109 113
flickerstreak@109 114 function module:OnDestroyBar(event, bar, name)
flickerstreak@109 115 CleanupStates(bar)
flickerstreak@109 116 end
flickerstreak@109 117
flickerstreak@109 118
flickerstreak@109 119
flickerstreak@109 120
flickerstreak@109 121 -- Options --
flickerstreak@109 122
flickerstreak@109 123 do
flickerstreak@109 124 -- pre-sorted by the order they should appear in
flickerstreak@109 125 local rules = {
flickerstreak@118 126 -- rule fields
flickerstreak@118 127 { "stance", { {battle = L["Battle Stance"]}, {defensive = L["Defensive Stance"]}, {berserker = L["Berserker Stance"]} } },
flickerstreak@118 128 { "form", { {caster = L["Caster Form"]}, {bear = L["Bear Form"]}, {cat = L["Cat Form"]}, {tree = L["Tree of Life"]}, {moonkin = L["Moonkin Form"]} } },
flickerstreak@178 129 { "stealth", { {stealth = L["Stealth"]}, {nostealth = L["No Stealth"]}, {shadowdance = L["Shadow Dance"]} } },
flickerstreak@118 130 { "shadow", { {shadowform = L["Shadowform"]}, {noshadowform = L["No Shadowform"]} } },
flickerstreak@176 131 { "demon", { {demon = L["Demon Form"]}, {nodemon = L["No Demon Form"]} } },
flickerstreak@118 132 { "pet", { {pet = L["With Pet"]}, {nopet = L["Without Pet"]} } },
flickerstreak@118 133 { "target", { {harm = L["Hostile Target"]}, {help = L["Friendly Target"]}, {notarget = L["No Target"]} } },
flickerstreak@118 134 { "focus", { {focusharm = L["Hostile Focus"]}, {focushelp = L["Friendly Focus"]}, {nofocus = L["No Focus"]} } },
flickerstreak@118 135 { "possess", { {possess = L["Mind Control"]} } },
flickerstreak@118 136 { "vehicle", { {vehicle = L["In a Vehicle"]} } },
flickerstreak@118 137 { "group", { {raid = L["Raid"]}, {party = L["Party"]}, {solo = L["Solo"]} } },
flickerstreak@118 138 { "combat", { {combat = L["In Combat"]}, {nocombat = L["Out of Combat"]} } },
flickerstreak@109 139 }
flickerstreak@109 140
flickerstreak@109 141 local ruleSelect = { }
flickerstreak@109 142 local ruleMap = { }
flickerstreak@109 143 local optionMap = setmetatable({},{__mode="k"})
flickerstreak@109 144
flickerstreak@109 145 local pointTable = {
flickerstreak@109 146 NONE = " ",
flickerstreak@109 147 CENTER = L["Center"],
flickerstreak@109 148 LEFT = L["Left"],
flickerstreak@109 149 RIGHT = L["Right"],
flickerstreak@109 150 TOP = L["Top"],
flickerstreak@109 151 BOTTOM = L["Bottom"],
flickerstreak@109 152 TOPLEFT = L["Top Left"],
flickerstreak@109 153 TOPRIGHT = L["Top Right"],
flickerstreak@109 154 BOTTOMLEFT = L["Bottom Left"],
flickerstreak@109 155 BOTTOMRIGHT = L["Bottom Right"],
flickerstreak@109 156 }
flickerstreak@109 157
flickerstreak@109 158 -- unpack rules table into ruleSelect and ruleMap
flickerstreak@109 159 for _, c in ipairs(rules) do
flickerstreak@118 160 local rule, fields = unpack(c)
flickerstreak@118 161 for _, field in ipairs(fields) do
flickerstreak@118 162 local key, label = next(field)
flickerstreak@118 163 table.insert(ruleSelect, label)
flickerstreak@118 164 table.insert(ruleMap, key)
flickerstreak@109 165 end
flickerstreak@109 166 end
flickerstreak@109 167
flickerstreak@109 168 local stateOptions = {
flickerstreak@109 169 ordering = {
flickerstreak@109 170 name = L["Info"],
flickerstreak@109 171 order = 1,
flickerstreak@109 172 type = "group",
flickerstreak@109 173 args = {
flickerstreak@109 174 delete = {
flickerstreak@109 175 name = L["Delete this State"],
flickerstreak@109 176 order = -1,
flickerstreak@109 177 type = "execute",
flickerstreak@109 178 func = "DeleteState",
flickerstreak@109 179 },
flickerstreak@109 180 rename = {
flickerstreak@109 181 name = L["Name"],
flickerstreak@109 182 order = 1,
flickerstreak@109 183 type = "input",
flickerstreak@109 184 get = "GetName",
flickerstreak@109 185 set = "SetStateName",
flickerstreak@109 186 pattern = "^%w*$",
flickerstreak@109 187 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@109 188 },
flickerstreak@109 189 ordering = {
flickerstreak@109 190 name = L["Evaluation Order"],
flickerstreak@109 191 desc = L["State transitions are evaluated in the order listed:\nMove a state up or down to change the order"],
flickerstreak@109 192 order = 2,
flickerstreak@109 193 type = "group",
flickerstreak@109 194 inline = true,
flickerstreak@109 195 args = {
flickerstreak@109 196 up = {
flickerstreak@109 197 name = L["Up"],
flickerstreak@109 198 order = 1,
flickerstreak@109 199 type = "execute",
flickerstreak@109 200 width = "half",
flickerstreak@109 201 func = "MoveStateUp",
flickerstreak@109 202 },
flickerstreak@109 203 down = {
flickerstreak@109 204 name = L["Down"],
flickerstreak@109 205 order = 2,
flickerstreak@109 206 type = "execute",
flickerstreak@109 207 width = "half",
flickerstreak@109 208 func = "MoveStateDown",
flickerstreak@109 209 }
flickerstreak@109 210 }
flickerstreak@109 211 }
flickerstreak@109 212 }
flickerstreak@109 213 },
flickerstreak@109 214 properties = {
flickerstreak@109 215 name = L["Properties"],
flickerstreak@109 216 order = 2,
flickerstreak@109 217 type = "group",
flickerstreak@109 218 args = {
flickerstreak@109 219 desc = {
flickerstreak@109 220 name = L["Set the properties for the bar when in this state"],
flickerstreak@109 221 order = 1,
flickerstreak@109 222 type = "description"
flickerstreak@109 223 },
Flick@239 224 page = {
Flick@239 225 name = L["Show Page #"],
Flick@239 226 order = 11,
Flick@239 227 type = "select",
Flick@239 228 width = "half",
Flick@239 229 disabled = "IsPageDisabled",
Flick@239 230 hidden = "IsPageHidden",
Flick@239 231 values = "GetPageValues",
Flick@239 232 set = "SetProp",
Flick@239 233 get = "GetPage",
Flick@239 234 },
flickerstreak@109 235 hide = {
flickerstreak@109 236 name = L["Hide Bar"],
flickerstreak@109 237 order = 90,
flickerstreak@109 238 type = "toggle",
flickerstreak@109 239 set = "SetProp",
flickerstreak@109 240 get = "GetProp",
flickerstreak@109 241 },
flickerstreak@109 242 --[[ BROKEN
flickerstreak@109 243 keybindState = {
flickerstreak@109 244 name = L["Override Keybinds"],
flickerstreak@109 245 desc = L["Set this state to maintain its own set of keybinds which override the defaults when active"],
flickerstreak@109 246 order = 91,
flickerstreak@109 247 type = "toggle",
flickerstreak@109 248 set = "SetProp",
flickerstreak@109 249 get = "GetProp",
flickerstreak@109 250 }, ]]
flickerstreak@109 251 position = {
flickerstreak@109 252 name = L["Position"],
flickerstreak@109 253 order = 92,
flickerstreak@109 254 type = "group",
flickerstreak@109 255 inline = true,
flickerstreak@109 256 args = {
flickerstreak@109 257 anchorEnable = {
flickerstreak@109 258 name = L["Reposition"],
flickerstreak@109 259 order = 1,
flickerstreak@109 260 type = "toggle",
flickerstreak@109 261 set = "SetProp",
flickerstreak@109 262 get = "GetProp",
flickerstreak@109 263 },
flickerstreak@109 264 anchorFrame = {
flickerstreak@109 265 name = L["Anchor Frame"],
flickerstreak@109 266 order = 2,
flickerstreak@109 267 type = "select",
flickerstreak@109 268 values = "GetAnchorFrames",
flickerstreak@109 269 set = "SetAnchorFrame",
flickerstreak@109 270 get = "GetAnchorFrame",
flickerstreak@109 271 disabled = "GetAnchorDisabled",
flickerstreak@109 272 hidden = "GetAnchorDisabled",
flickerstreak@109 273 },
flickerstreak@109 274 anchorPoint = {
flickerstreak@109 275 name = L["Point"],
flickerstreak@109 276 order = 3,
flickerstreak@109 277 type = "select",
flickerstreak@109 278 values = pointTable,
flickerstreak@109 279 set = "SetAnchorPointProp",
flickerstreak@109 280 get = "GetAnchorPointProp",
flickerstreak@109 281 disabled = "GetAnchorDisabled",
flickerstreak@109 282 hidden = "GetAnchorDisabled",
flickerstreak@109 283 },
flickerstreak@109 284 anchorRelPoint = {
flickerstreak@109 285 name = L["Relative Point"],
flickerstreak@109 286 order = 4,
flickerstreak@109 287 type = "select",
flickerstreak@109 288 values = pointTable,
flickerstreak@109 289 set = "SetAnchorPointProp",
flickerstreak@109 290 get = "GetAnchorPointProp",
flickerstreak@109 291 disabled = "GetAnchorDisabled",
flickerstreak@109 292 hidden = "GetAnchorDisabled",
flickerstreak@109 293 },
flickerstreak@109 294 anchorX = {
flickerstreak@109 295 name = L["X Offset"],
flickerstreak@109 296 order = 5,
flickerstreak@109 297 type = "range",
flickerstreak@109 298 min = -100,
flickerstreak@109 299 max = 100,
flickerstreak@109 300 step = 1,
flickerstreak@109 301 set = "SetProp",
flickerstreak@109 302 get = "GetProp",
flickerstreak@109 303 disabled = "GetAnchorDisabled",
flickerstreak@109 304 hidden = "GetAnchorDisabled",
flickerstreak@109 305 },
flickerstreak@109 306 anchorY = {
flickerstreak@109 307 name = L["Y Offset"],
flickerstreak@109 308 order = 6,
flickerstreak@109 309 type = "range",
flickerstreak@109 310 min = -100,
flickerstreak@109 311 max = 100,
flickerstreak@109 312 step = 1,
flickerstreak@109 313 set = "SetProp",
flickerstreak@109 314 get = "GetProp",
flickerstreak@109 315 disabled = "GetAnchorDisabled",
flickerstreak@109 316 hidden = "GetAnchorDisabled",
flickerstreak@109 317 },
flickerstreak@109 318 },
flickerstreak@109 319 },
flickerstreak@109 320 scale = {
flickerstreak@109 321 name = L["Scale"],
flickerstreak@109 322 order = 93,
flickerstreak@109 323 type = "group",
flickerstreak@109 324 inline = true,
flickerstreak@109 325 args = {
flickerstreak@109 326 enableScale = {
flickerstreak@109 327 name = L["Set New Scale"],
flickerstreak@109 328 order = 1,
flickerstreak@109 329 type = "toggle",
flickerstreak@109 330 set = "SetProp",
flickerstreak@109 331 get = "GetProp",
flickerstreak@109 332 },
flickerstreak@109 333 scale = {
flickerstreak@109 334 name = L["Scale"],
flickerstreak@109 335 order = 2,
flickerstreak@109 336 type = "range",
flickerstreak@109 337 min = 0.25,
flickerstreak@109 338 max = 2.5,
flickerstreak@109 339 step = 0.05,
flickerstreak@109 340 isPercent = true,
flickerstreak@109 341 set = "SetProp",
flickerstreak@109 342 get = "GetScale",
flickerstreak@109 343 disabled = "GetScaleDisabled",
flickerstreak@109 344 hidden = "GetScaleDisabled",
flickerstreak@109 345 },
flickerstreak@109 346 },
flickerstreak@109 347 },
flickerstreak@109 348 alpha = {
flickerstreak@109 349 name = L["Transparency"],
flickerstreak@109 350 order = 94,
flickerstreak@109 351 type = "group",
flickerstreak@109 352 inline = true,
flickerstreak@109 353 args = {
flickerstreak@109 354 enableAlpha = {
flickerstreak@109 355 name = L["Set Transparency"],
flickerstreak@109 356 order = 1,
flickerstreak@109 357 type = "toggle",
flickerstreak@109 358 set = "SetProp",
flickerstreak@109 359 get = "GetProp",
flickerstreak@109 360 },
flickerstreak@109 361 alpha = {
flickerstreak@109 362 name = L["Transparency"],
flickerstreak@109 363 order = 2,
flickerstreak@109 364 type = "range",
flickerstreak@109 365 min = 0,
flickerstreak@109 366 max = 1,
flickerstreak@109 367 step = 0.01,
flickerstreak@109 368 bigStep = 0.05,
flickerstreak@109 369 isPercent = true,
flickerstreak@109 370 set = "SetProp",
flickerstreak@109 371 get = "GetAlpha",
flickerstreak@109 372 disabled = "GetAlphaDisabled",
flickerstreak@109 373 hidden = "GetAlphaDisabled",
flickerstreak@109 374 },
flickerstreak@109 375 },
flickerstreak@109 376 },
flickerstreak@109 377 },
flickerstreak@109 378 plugins = { }
flickerstreak@109 379 },
flickerstreak@109 380 rules = {
flickerstreak@109 381 name = L["Rule"],
flickerstreak@109 382 order = 3,
flickerstreak@109 383 type = "group",
flickerstreak@109 384 args = {
flickerstreak@109 385 mode = {
flickerstreak@109 386 name = L["Select this state"],
flickerstreak@109 387 order = 2,
flickerstreak@109 388 type = "select",
flickerstreak@109 389 style = "radio",
flickerstreak@109 390 values = {
flickerstreak@109 391 default = L["by default"],
flickerstreak@109 392 any = L["when ANY of these"],
flickerstreak@109 393 all = L["when ALL of these"],
flickerstreak@109 394 custom = L["via custom rule"],
flickerstreak@109 395 keybind = L["via keybinding"],
flickerstreak@109 396 },
flickerstreak@109 397 set = "SetType",
flickerstreak@109 398 get = "GetType",
flickerstreak@109 399 },
flickerstreak@109 400 clear = {
flickerstreak@109 401 name = L["Clear All"],
flickerstreak@109 402 order = 3,
flickerstreak@109 403 type = "execute",
flickerstreak@109 404 hidden = "GetClearAllDisabled",
flickerstreak@109 405 disabled = "GetClearAllDisabled",
flickerstreak@109 406 func = "ClearAllConditions",
flickerstreak@109 407 },
flickerstreak@109 408 inputs = {
flickerstreak@109 409 name = L["Conditions"],
flickerstreak@109 410 order = 4,
flickerstreak@109 411 type = "multiselect",
flickerstreak@109 412 hidden = "GetConditionsDisabled",
flickerstreak@109 413 disabled = "GetConditionsDisabled",
flickerstreak@109 414 values = ruleSelect,
flickerstreak@109 415 set = "SetCondition",
flickerstreak@109 416 get = "GetCondition",
flickerstreak@109 417 },
flickerstreak@109 418 custom = {
flickerstreak@109 419 name = L["Custom Rule"],
flickerstreak@109 420 order = 5,
flickerstreak@109 421 type = "input",
flickerstreak@109 422 multiline = true,
flickerstreak@109 423 hidden = "GetCustomDisabled",
flickerstreak@109 424 disabled = "GetCustomDisabled",
flickerstreak@109 425 desc = L["Syntax like macro rules: see preset rules for examples"],
flickerstreak@109 426 set = "SetCustomRule",
flickerstreak@109 427 get = "GetCustomRule",
flickerstreak@109 428 validate = "ValidateCustomRule",
flickerstreak@109 429 },
flickerstreak@109 430 keybind = {
flickerstreak@109 431 name = L["Keybinding"],
flickerstreak@109 432 order = 6,
flickerstreak@109 433 inline = true,
flickerstreak@109 434 hidden = "GetKeybindDisabled",
flickerstreak@109 435 disabled = "GetKeybindDisabled",
flickerstreak@109 436 type = "group",
flickerstreak@109 437 args = {
flickerstreak@109 438 desc = {
flickerstreak@109 439 name = L["Invoking a state keybind toggles an override of all other transition rules."],
flickerstreak@109 440 order = 1,
flickerstreak@109 441 type = "description",
flickerstreak@109 442 },
flickerstreak@109 443 keybind = {
flickerstreak@109 444 name = L["State Hotkey"],
flickerstreak@109 445 desc = L["Define an override toggle keybind"],
flickerstreak@109 446 order = 2,
flickerstreak@109 447 type = "keybinding",
flickerstreak@109 448 set = "SetKeybind",
flickerstreak@109 449 get = "GetKeybind",
flickerstreak@109 450 },
flickerstreak@109 451 },
flickerstreak@109 452 },
flickerstreak@109 453 },
flickerstreak@109 454 },
flickerstreak@109 455 }
flickerstreak@109 456
Flick@240 457 local StateHandler = { }
Flick@240 458 local meta = { __index = StateHandler }
flickerstreak@109 459
flickerstreak@109 460 function StateHandler:New( bar, opts )
flickerstreak@109 461 local self = setmetatable(
flickerstreak@109 462 {
flickerstreak@109 463 bar = bar
flickerstreak@109 464 },
Flick@240 465 meta )
flickerstreak@109 466
flickerstreak@109 467 function self:GetName()
flickerstreak@109 468 return opts.name
flickerstreak@109 469 end
flickerstreak@109 470
flickerstreak@109 471 function self:SetName(name)
flickerstreak@109 472 opts.name = name
flickerstreak@109 473 end
flickerstreak@109 474
flickerstreak@109 475 function self:GetOrder()
flickerstreak@109 476 return opts.order
flickerstreak@109 477 end
flickerstreak@109 478
flickerstreak@109 479 -- get reference to states table: even if the bar
flickerstreak@109 480 -- name changes the states table ref won't
Flick@228 481 self.states = tbuild(bar:GetConfig(), "states")
flickerstreak@109 482 self.state = tbuild(self.states, opts.name)
flickerstreak@109 483
flickerstreak@109 484 opts.order = self:GetRuleField("order")
flickerstreak@109 485 if opts.order == nil then
flickerstreak@109 486 -- add after the highest
flickerstreak@109 487 opts.order = 100
flickerstreak@109 488 for _, state in pairs(self.states) do
flickerstreak@109 489 local x = tonumber(tfetch(state, "rule", "order"))
flickerstreak@109 490 if x and x >= opts.order then
flickerstreak@109 491 opts.order = x + 1
flickerstreak@109 492 end
flickerstreak@109 493 end
flickerstreak@109 494 self:SetRuleField("order",opts.order)
flickerstreak@109 495 end
flickerstreak@109 496
flickerstreak@109 497 return self
flickerstreak@109 498 end
flickerstreak@109 499
flickerstreak@109 500 -- helper methods
flickerstreak@109 501
flickerstreak@109 502 function StateHandler:SetRuleField( key, value, ... )
flickerstreak@109 503 tbuild(self.state, "rule", ...)[key] = value
flickerstreak@109 504 end
flickerstreak@109 505
flickerstreak@109 506 function StateHandler:GetRuleField( ... )
flickerstreak@109 507 return tfetch(self.state, "rule", ...)
flickerstreak@109 508 end
flickerstreak@109 509
flickerstreak@109 510 function StateHandler:FixAll( setkey )
flickerstreak@109 511 -- if multiple selections in the same group are chosen when 'all' is selected,
flickerstreak@109 512 -- keep only one of them. If changing the mode, the first in the fields list will
flickerstreak@109 513 -- be chosen arbitrarily. Otherwise, if selecting a new checkbox from the field-set,
flickerstreak@109 514 -- it will be retained.
flickerstreak@109 515 local notified = false
flickerstreak@109 516 if self:GetRuleField("type") == "all" then
flickerstreak@109 517 for _, c in ipairs(rules) do
flickerstreak@118 518 local rule, fields = unpack(c)
flickerstreak@109 519 local once = false
flickerstreak@109 520 if setkey then
flickerstreak@109 521 for idx, field in ipairs(fields) do
flickerstreak@109 522 if next(field) == setkey then
flickerstreak@109 523 once = true
flickerstreak@109 524 end
flickerstreak@109 525 end
flickerstreak@109 526 end
flickerstreak@109 527 for idx, field in ipairs(fields) do
flickerstreak@109 528 local key = next(field)
flickerstreak@109 529 if self:GetRuleField("values",key) then
flickerstreak@109 530 if once and key ~= setkey then
flickerstreak@109 531 self:SetRuleField(key,false,"values")
flickerstreak@109 532 if not setkey and not notified then
flickerstreak@109 533 ReAction:UserError(L["Warning: one or more incompatible rules were turned off"])
flickerstreak@109 534 notified = true
flickerstreak@109 535 end
flickerstreak@109 536 end
flickerstreak@109 537 once = true
flickerstreak@109 538 end
flickerstreak@109 539 end
flickerstreak@109 540 end
flickerstreak@109 541 end
flickerstreak@109 542 end
flickerstreak@109 543
flickerstreak@109 544 function StateHandler:GetNeighbors()
flickerstreak@109 545 local before, after
flickerstreak@109 546 for k, v in pairs(self.states) do
flickerstreak@109 547 local o = tonumber(tfetch(v, "rule", "order"))
flickerstreak@109 548 if o and k ~= self:GetName() then
flickerstreak@109 549 local obefore = tfetch(self.states,before,"rule","order")
flickerstreak@109 550 local oafter = tfetch(self.states,after,"rule","order")
flickerstreak@109 551 if o < self:GetOrder() and (not obefore or obefore < o) then
flickerstreak@109 552 before = k
flickerstreak@109 553 end
flickerstreak@109 554 if o > self:GetOrder() and (not oafter or oafter > o) then
flickerstreak@109 555 after = k
flickerstreak@109 556 end
flickerstreak@109 557 end
flickerstreak@109 558 end
flickerstreak@109 559 return before, after
flickerstreak@109 560 end
flickerstreak@109 561
flickerstreak@109 562 function StateHandler:SwapOrder( a, b )
flickerstreak@109 563 -- do options table
flickerstreak@109 564 local args = optionMap[self.bar].args
flickerstreak@109 565 args[a].order, args[b].order = args[b].order, args[a].order
flickerstreak@109 566 -- do profile
flickerstreak@109 567 a = tbuild(self.states, a, "rule")
flickerstreak@109 568 b = tbuild(self.states, b, "rule")
flickerstreak@109 569 a.order, b.order = b.order, a.order
flickerstreak@109 570 end
flickerstreak@109 571
flickerstreak@109 572 -- handler methods
flickerstreak@109 573
flickerstreak@109 574 function StateHandler:GetProp( info )
flickerstreak@109 575 -- gets property of the same name as the options arg
flickerstreak@109 576 return GetProperty(self.bar, self:GetName(), info[#info])
flickerstreak@109 577 end
flickerstreak@109 578
flickerstreak@109 579 function StateHandler:SetProp( info, value )
flickerstreak@109 580 -- sets property of the same name as the options arg
flickerstreak@109 581 SetProperty(self.bar, self:GetName(), info[#info], value)
flickerstreak@109 582 end
flickerstreak@109 583
flickerstreak@109 584 function StateHandler:DeleteState()
flickerstreak@109 585 if self.states[self:GetName()] then
flickerstreak@109 586 self.states[self:GetName()] = nil
flickerstreak@109 587 ApplyStates(self.bar)
flickerstreak@109 588 end
flickerstreak@109 589 optionMap[self.bar].args[self:GetName()] = nil
flickerstreak@109 590 end
flickerstreak@109 591
flickerstreak@109 592 function StateHandler:SetStateName(info, value)
flickerstreak@109 593 -- check for existing state name
flickerstreak@109 594 if self.states[value] then
flickerstreak@109 595 ReAction:UserError(format(L["State named '%s' already exists"],value))
flickerstreak@109 596 return
flickerstreak@109 597 end
flickerstreak@109 598 local args = optionMap[self.bar].args
flickerstreak@109 599 local name = self:GetName()
flickerstreak@109 600 self.states[value], args[value], self.states[name], args[name] = self.states[name], args[name], nil, nil
flickerstreak@109 601 self:SetName(value)
flickerstreak@109 602 ApplyStates(self.bar)
flickerstreak@109 603 ReAction:ShowEditor(self.bar, moduleID, value)
flickerstreak@109 604 end
flickerstreak@109 605
flickerstreak@109 606 function StateHandler:MoveStateUp()
flickerstreak@109 607 local before, after = self:GetNeighbors()
flickerstreak@109 608 if before then
flickerstreak@109 609 self:SwapOrder(before, self:GetName())
flickerstreak@109 610 ApplyStates(self.bar)
flickerstreak@109 611 end
flickerstreak@109 612 end
flickerstreak@109 613
flickerstreak@109 614 function StateHandler:MoveStateDown()
flickerstreak@109 615 local before, after = self:GetNeighbors()
flickerstreak@109 616 if after then
flickerstreak@109 617 self:SwapOrder(self:GetName(), after)
flickerstreak@109 618 ApplyStates(self.bar)
flickerstreak@109 619 end
flickerstreak@109 620 end
flickerstreak@109 621
flickerstreak@109 622 function StateHandler:GetAnchorDisabled()
flickerstreak@109 623 return not GetProperty(self.bar, self:GetName(), "anchorEnable")
flickerstreak@109 624 end
flickerstreak@109 625
Flick@239 626 function StateHandler:IsPageDisabled()
Flick@239 627 local n = self.bar:GetConfig().nPages or 1
Flick@239 628 return not (n > 1)
Flick@239 629 end
Flick@239 630
Flick@239 631 function StateHandler:IsPageHidden()
Flick@239 632 return not self.bar:GetConfig().nPages
Flick@239 633 end
Flick@239 634
Flick@239 635 function StateHandler:GetPageValues()
Flick@239 636 if not self._pagevalues then
Flick@239 637 self._pagevalues = { }
Flick@239 638 end
Flick@239 639 local n = self.bar:GetConfig().nPages
Flick@239 640 -- cache the results
Flick@239 641 if self._npages ~= n then
Flick@239 642 self._npages = n
Flick@239 643 wipe(self._pagevalues)
Flick@239 644 for i = 1, n do
Flick@239 645 self._pagevalues["page"..i] = i
Flick@239 646 end
Flick@239 647 end
Flick@239 648 return self._pagevalues
Flick@239 649 end
Flick@239 650
Flick@239 651 function StateHandler:GetPage(info)
Flick@239 652 return self:GetProp(info) or 1
Flick@239 653 end
Flick@239 654
flickerstreak@109 655 function StateHandler:GetAnchorFrames(info)
flickerstreak@109 656 self._anchorframes = self._anchorframes or { }
flickerstreak@109 657 table.wipe(self._anchorframes)
flickerstreak@109 658
flickerstreak@109 659 table.insert(self._anchorframes, "UIParent")
flickerstreak@109 660 for name, bar in ReAction:IterateBars() do
flickerstreak@109 661 table.insert(self._anchorframes, bar:GetFrame():GetName())
flickerstreak@109 662 end
flickerstreak@109 663 return self._anchorframes
flickerstreak@109 664 end
flickerstreak@109 665
flickerstreak@109 666 function StateHandler:GetAnchorFrame(info)
flickerstreak@109 667 local value = self:GetProp(info)
flickerstreak@109 668 for k,v in pairs(self._anchorframes) do
flickerstreak@109 669 if v == value then
flickerstreak@109 670 return k
flickerstreak@109 671 end
flickerstreak@109 672 end
flickerstreak@109 673 end
flickerstreak@109 674
flickerstreak@109 675 function StateHandler:SetAnchorFrame(info, value)
flickerstreak@109 676 local f = _G[self._anchorframes[value]]
flickerstreak@109 677 if f then
flickerstreak@157 678 self.bar:SetFrameRef("anchor-"..self:GetName(), f)
flickerstreak@109 679 self:SetProp(info, f:GetName())
flickerstreak@109 680 end
flickerstreak@109 681 end
flickerstreak@109 682
flickerstreak@109 683 function StateHandler:SetAnchorPointProp(info, value)
flickerstreak@109 684 self:SetProp(info, value ~= "NONE" and value or nil)
flickerstreak@109 685 end
flickerstreak@109 686
flickerstreak@109 687 function StateHandler:GetAnchorPointProp(info)
flickerstreak@109 688 return self:GetProp(info) or "NONE"
flickerstreak@109 689 end
flickerstreak@109 690
flickerstreak@109 691 function StateHandler:GetScale(info)
flickerstreak@109 692 return self:GetProp(info) or 1.0
flickerstreak@109 693 end
flickerstreak@109 694
flickerstreak@109 695 function StateHandler:GetScaleDisabled()
flickerstreak@109 696 return not GetProperty(self.bar, self:GetName(), "enableScale")
flickerstreak@109 697 end
flickerstreak@109 698
flickerstreak@109 699 function StateHandler:GetAlpha(info)
flickerstreak@109 700 return self:GetProp(info) or 1.0
flickerstreak@109 701 end
flickerstreak@109 702
flickerstreak@109 703 function StateHandler:GetAlphaDisabled()
flickerstreak@109 704 return not GetProperty(self.bar, self:GetName(), "enableAlpha")
flickerstreak@109 705 end
flickerstreak@109 706
flickerstreak@109 707 function StateHandler:SetType(info, value)
flickerstreak@109 708 self:SetRuleField("type", value)
flickerstreak@109 709 self:FixAll()
flickerstreak@109 710 ApplyStates(self.bar)
flickerstreak@109 711 end
flickerstreak@109 712
flickerstreak@109 713 function StateHandler:GetType()
flickerstreak@109 714 return self:GetRuleField("type")
flickerstreak@109 715 end
flickerstreak@109 716
flickerstreak@109 717 function StateHandler:GetClearAllDisabled()
flickerstreak@109 718 local t = self:GetRuleField("type")
flickerstreak@109 719 return not( t == "any" or t == "all" or t == "custom")
flickerstreak@109 720 end
flickerstreak@109 721
flickerstreak@109 722 function StateHandler:ClearAllConditions()
flickerstreak@109 723 local t = self:GetRuleField("type")
flickerstreak@109 724 if t == "custom" then
flickerstreak@109 725 self:SetRuleField("custom","")
flickerstreak@109 726 elseif t == "any" or t == "all" then
flickerstreak@109 727 self:SetRuleField("values", {})
flickerstreak@109 728 end
flickerstreak@109 729 ApplyStates(self.bar)
flickerstreak@109 730 end
flickerstreak@109 731
flickerstreak@109 732 function StateHandler:GetConditionsDisabled()
flickerstreak@109 733 local t = self:GetRuleField("type")
flickerstreak@109 734 return not( t == "any" or t == "all")
flickerstreak@109 735 end
flickerstreak@109 736
flickerstreak@109 737 function StateHandler:SetCondition(info, key, value)
flickerstreak@109 738 self:SetRuleField(ruleMap[key], value or nil, "values")
flickerstreak@109 739 if value then
flickerstreak@109 740 self:FixAll(ruleMap[key])
flickerstreak@109 741 end
flickerstreak@109 742 ApplyStates(self.bar)
flickerstreak@109 743 end
flickerstreak@109 744
flickerstreak@109 745 function StateHandler:GetCondition(info, key)
flickerstreak@109 746 return self:GetRuleField("values", ruleMap[key]) or false
flickerstreak@109 747 end
flickerstreak@109 748
flickerstreak@109 749 function StateHandler:GetCustomDisabled()
flickerstreak@109 750 return self:GetRuleField("type") ~= "custom"
flickerstreak@109 751 end
flickerstreak@109 752
flickerstreak@109 753 function StateHandler:SetCustomRule(info, value)
flickerstreak@109 754 self:SetRuleField("custom",value)
flickerstreak@109 755 ApplyStates(self.bar)
flickerstreak@109 756 end
flickerstreak@109 757
flickerstreak@109 758 function StateHandler:GetCustomRule()
flickerstreak@109 759 return self:GetRuleField("custom") or ""
flickerstreak@109 760 end
flickerstreak@109 761
flickerstreak@109 762 function StateHandler:ValidateCustomRule(info, value)
flickerstreak@109 763 local s = value:gsub("%s","") -- remove all spaces
flickerstreak@109 764 -- unfortunately %b and captures don't support the '+' notation, or this would be considerably simpler
flickerstreak@109 765 repeat
flickerstreak@109 766 if s == "" then
flickerstreak@109 767 return true
flickerstreak@109 768 end
flickerstreak@109 769 local c, r = s:match("(%b[])(.*)")
flickerstreak@109 770 if c == nil and s and #s > 0 then
flickerstreak@109 771 return format(L["Invalid custom rule '%s': each clause must appear within [brackets]"],value or "")
flickerstreak@109 772 end
flickerstreak@109 773 s = r
flickerstreak@109 774 until c == nil
flickerstreak@109 775 return true
flickerstreak@109 776 end
flickerstreak@109 777
flickerstreak@109 778 function StateHandler:GetKeybindDisabled()
flickerstreak@109 779 return self:GetRuleField("type") ~= "keybind"
flickerstreak@109 780 end
flickerstreak@109 781
flickerstreak@109 782 function StateHandler:GetKeybind()
flickerstreak@109 783 return self:GetRuleField("keybind")
flickerstreak@109 784 end
flickerstreak@109 785
flickerstreak@109 786 function StateHandler:SetKeybind(info, value)
flickerstreak@109 787 if value and #value == 0 then
flickerstreak@109 788 value = nil
flickerstreak@109 789 end
flickerstreak@109 790 self:SetRuleField("keybind",value)
flickerstreak@109 791 ApplyStates(self.bar)
flickerstreak@109 792 end
flickerstreak@109 793
flickerstreak@109 794 local function CreateStateOptions(bar, name)
flickerstreak@109 795 local opts = {
flickerstreak@109 796 type = "group",
flickerstreak@109 797 name = name,
flickerstreak@109 798 childGroups = "tab",
flickerstreak@109 799 args = stateOptions
flickerstreak@109 800 }
flickerstreak@109 801
flickerstreak@109 802 opts.handler = StateHandler:New(bar,opts)
flickerstreak@109 803
flickerstreak@109 804 return opts
flickerstreak@109 805 end
flickerstreak@109 806
flickerstreak@109 807 function module:GetBarOptions(bar)
flickerstreak@109 808 local private = { }
Flick@228 809 local states = tbuild(bar:GetConfig(), "states")
flickerstreak@109 810 local options = {
flickerstreak@109 811 name = L["Dynamic State"],
flickerstreak@109 812 type = "group",
flickerstreak@109 813 order = -1,
flickerstreak@109 814 childGroups = "tree",
flickerstreak@109 815 disabled = InCombatLockdown,
flickerstreak@109 816 args = {
flickerstreak@109 817 __desc__ = {
flickerstreak@109 818 name = L["States are evaluated in the order they are listed"],
flickerstreak@109 819 order = 1,
flickerstreak@109 820 type = "description",
flickerstreak@109 821 },
flickerstreak@109 822 __new__ = {
flickerstreak@109 823 name = L["New State..."],
flickerstreak@109 824 order = 2,
flickerstreak@109 825 type = "group",
flickerstreak@109 826 args = {
flickerstreak@109 827 name = {
flickerstreak@109 828 name = L["State Name"],
flickerstreak@109 829 desc = L["Set a name for the new state"],
flickerstreak@109 830 order = 1,
flickerstreak@109 831 type = "input",
flickerstreak@109 832 get = function() return private.newstatename or "" end,
flickerstreak@109 833 set = function(info,value) private.newstatename = value end,
flickerstreak@109 834 pattern = "^%w*$",
flickerstreak@109 835 usage = L["State names must be alphanumeric without spaces"],
flickerstreak@109 836 },
flickerstreak@109 837 create = {
flickerstreak@109 838 name = L["Create State"],
flickerstreak@109 839 order = 2,
flickerstreak@109 840 type = "execute",
flickerstreak@109 841 func = function ()
flickerstreak@109 842 local name = private.newstatename
flickerstreak@109 843 if states[name] then
flickerstreak@109 844 ReAction:UserError(format(L["State named '%s' already exists"],name))
flickerstreak@109 845 else
flickerstreak@109 846 -- TODO: select default state options and pass as final argument
flickerstreak@109 847 states[name] = { }
flickerstreak@109 848 optionMap[bar].args[name] = CreateStateOptions(bar,name)
flickerstreak@109 849 ReAction:ShowEditor(bar, moduleID, name)
flickerstreak@109 850 private.newstatename = ""
flickerstreak@109 851 end
flickerstreak@109 852 end,
flickerstreak@109 853 disabled = function()
flickerstreak@109 854 local name = private.newstatename or ""
flickerstreak@109 855 return #name == 0 or name:find("%W")
flickerstreak@109 856 end,
flickerstreak@109 857 }
flickerstreak@109 858 }
flickerstreak@109 859 }
flickerstreak@109 860 }
flickerstreak@109 861 }
flickerstreak@109 862 for name, config in pairs(states) do
flickerstreak@109 863 options.args[name] = CreateStateOptions(bar,name)
flickerstreak@109 864 end
flickerstreak@109 865 optionMap[bar] = options
flickerstreak@109 866 return options
flickerstreak@109 867 end
flickerstreak@109 868 end
flickerstreak@109 869
flickerstreak@109 870
flickerstreak@109 871 -- Export methods to Bar class --
flickerstreak@109 872
flickerstreak@109 873 ReAction.Bar.GetStateProperty = GetProperty
flickerstreak@109 874 ReAction.Bar.SetStateProperty = SetProperty