annotate modules/State.lua @ 239:2669f737d9d7

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