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