flickerstreak@28
|
1 -- ReAction.lua
|
flickerstreak@28
|
2 -- See modules/ReAction_ModuleTemplate for Module API listing
|
flickerstreak@28
|
3 -- See Bar.lua for Bar object listing
|
flickerstreak@27
|
4
|
flickerstreak@27
|
5 ------ CORE ------
|
flickerstreak@30
|
6 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction",
|
flickerstreak@33
|
7 "AceConsole-3.0",
|
flickerstreak@33
|
8 "AceEvent-3.0"
|
flickerstreak@30
|
9 )
|
flickerstreak@33
|
10 ReAction.version = GetAddOnMetadata("ReAction","Version")
|
flickerstreak@33
|
11 ReAction.revision = tonumber(("$Revision$"):match("%d+"))
|
flickerstreak@27
|
12
|
flickerstreak@28
|
13 ------ GLOBALS ------
|
flickerstreak@28
|
14 _G["ReAction"] = ReAction
|
flickerstreak@27
|
15
|
flickerstreak@28
|
16 ------ DEBUGGING ------
|
flickerstreak@25
|
17 ReAction.debug = true
|
flickerstreak@25
|
18 if ReAction.debug then
|
flickerstreak@25
|
19 ReAction.print = function(msg)
|
flickerstreak@25
|
20 DEFAULT_CHAT_FRAME:AddMessage(msg)
|
flickerstreak@25
|
21 end
|
flickerstreak@25
|
22 --seterrorhandler(ReAction.print)
|
flickerstreak@25
|
23 else
|
flickerstreak@25
|
24 ReAction.print = function() end
|
flickerstreak@25
|
25 end
|
flickerstreak@25
|
26
|
flickerstreak@33
|
27 ------ LIBRARIES ------
|
flickerstreak@33
|
28 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction")
|
flickerstreak@33
|
29 ReAction.L = L
|
flickerstreak@33
|
30
|
flickerstreak@28
|
31 ------ PRIVATE ------
|
flickerstreak@30
|
32 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, SafeCall, CheckMethod, SlashHandler
|
flickerstreak@28
|
33 do
|
flickerstreak@28
|
34 local pcall = pcall
|
flickerstreak@28
|
35 local geterrorhandler = geterrorhandler
|
flickerstreak@28
|
36
|
flickerstreak@28
|
37 SelectBar = function(x)
|
flickerstreak@28
|
38 local bar, name
|
flickerstreak@28
|
39 if type(x) == "string" then
|
flickerstreak@28
|
40 name = x
|
flickerstreak@28
|
41 bar = ReAction:GetBar(name)
|
flickerstreak@28
|
42 elseif ReAction.Bar:IsInstance(x) then
|
flickerstreak@28
|
43 bar = x
|
flickerstreak@28
|
44 for k,v in pairs(ReAction.bars) do
|
flickerstreak@28
|
45 if v == bar then
|
flickerstreak@28
|
46 name = k
|
flickerstreak@28
|
47 end
|
flickerstreak@28
|
48 end
|
flickerstreak@28
|
49 else
|
flickerstreak@28
|
50 error("bad argument to SelectBar")
|
flickerstreak@28
|
51 end
|
flickerstreak@28
|
52 return bar, name
|
flickerstreak@28
|
53 end
|
flickerstreak@28
|
54
|
flickerstreak@28
|
55 DestroyBar = function(x)
|
flickerstreak@28
|
56 local bar, name = SelectBar(x)
|
flickerstreak@28
|
57 if name and bar then
|
flickerstreak@28
|
58 ReAction.bars[name] = nil
|
flickerstreak@28
|
59 ReAction:CallMethodOnAllModules("RemoveFromBar", bar)
|
flickerstreak@28
|
60 bar:Destroy()
|
flickerstreak@28
|
61 end
|
flickerstreak@28
|
62 end
|
flickerstreak@28
|
63
|
flickerstreak@28
|
64 InitializeBars = function ()
|
flickerstreak@28
|
65 if not(ReAction.inited) then
|
flickerstreak@28
|
66 for name, config in pairs(ReAction.db.profile.bars) do
|
flickerstreak@28
|
67 if config then
|
flickerstreak@28
|
68 ReAction:CreateBar(name, config)
|
flickerstreak@28
|
69 end
|
flickerstreak@28
|
70 end
|
flickerstreak@28
|
71 ReAction:CallMethodOnAllBars("ApplyAnchor") -- re-anchor in the case of oddball ordering
|
flickerstreak@28
|
72 ReAction.inited = true
|
flickerstreak@28
|
73 end
|
flickerstreak@28
|
74 end
|
flickerstreak@28
|
75
|
flickerstreak@28
|
76 TearDownBars = function()
|
flickerstreak@28
|
77 for name, bar in pairs(ReAction.bars) do
|
flickerstreak@28
|
78 if bar then
|
flickerstreak@28
|
79 ReAction.bars[name] = DestroyBar(bar)
|
flickerstreak@28
|
80 end
|
flickerstreak@28
|
81 end
|
flickerstreak@28
|
82 ReAction.inited = false
|
flickerstreak@28
|
83 end
|
flickerstreak@28
|
84
|
flickerstreak@28
|
85 DeepCopy = function(x)
|
flickerstreak@28
|
86 if type(x) ~= "table" then
|
flickerstreak@28
|
87 return x
|
flickerstreak@28
|
88 end
|
flickerstreak@28
|
89 local r = {}
|
flickerstreak@28
|
90 for k,v in pairs(x) do
|
flickerstreak@28
|
91 r[k] = DeepCopy(v)
|
flickerstreak@28
|
92 end
|
flickerstreak@28
|
93 return r
|
flickerstreak@28
|
94 end
|
flickerstreak@28
|
95
|
flickerstreak@28
|
96 SafeCall = function(f, ...)
|
flickerstreak@28
|
97 if f then
|
flickerstreak@28
|
98 local success, err = pcall(f,...)
|
flickerstreak@28
|
99 if not success then
|
flickerstreak@28
|
100 geterrorhandler()(err)
|
flickerstreak@28
|
101 end
|
flickerstreak@28
|
102 end
|
flickerstreak@28
|
103 end
|
flickerstreak@28
|
104
|
flickerstreak@28
|
105 CheckMethod = function(m)
|
flickerstreak@28
|
106 if type(m) == "function" then
|
flickerstreak@28
|
107 return m
|
flickerstreak@28
|
108 end
|
flickerstreak@28
|
109 if type(m) ~= "string" then
|
flickerstreak@28
|
110 error("Invalid method")
|
flickerstreak@28
|
111 end
|
flickerstreak@28
|
112 end
|
flickerstreak@30
|
113
|
flickerstreak@30
|
114 local function CallOptsModule(method,...)
|
flickerstreak@33
|
115 local m = ReAction:GetModule("ConfigUI",true)
|
flickerstreak@33
|
116 if not m then
|
flickerstreak@33
|
117 LoadAddOn("ReAction_ConfigUI")
|
flickerstreak@33
|
118 m = ReAction:GetModule("ConfigUI")
|
flickerstreak@33
|
119 end
|
flickerstreak@33
|
120 if m and type(m) == "table" and type(m[method]) == "function" then
|
flickerstreak@33
|
121 m[method](m,...)
|
flickerstreak@33
|
122 else
|
flickerstreak@33
|
123 ReAction:Print("Options module not found")
|
flickerstreak@33
|
124 end
|
flickerstreak@30
|
125 end
|
flickerstreak@30
|
126
|
flickerstreak@30
|
127 SlashHandler = function(option)
|
flickerstreak@30
|
128 if option == "config" then
|
flickerstreak@30
|
129 CallOptsModule("OpenConfig",true)
|
flickerstreak@30
|
130 elseif option == "unlock" then
|
flickerstreak@33
|
131 ReAction:SetConfigMode(true)
|
flickerstreak@30
|
132 elseif option == "lock" then
|
flickerstreak@33
|
133 ReAction:SetConfigMode(false)
|
flickerstreak@30
|
134 else
|
flickerstreak@30
|
135 ReAction:Print(ReAction.version)
|
flickerstreak@30
|
136 ReAction:Print("/reaction config")
|
flickerstreak@30
|
137 ReAction:Print("/reaction unlock")
|
flickerstreak@30
|
138 ReAction:Print("/reaction lock")
|
flickerstreak@30
|
139 end
|
flickerstreak@30
|
140 end
|
flickerstreak@28
|
141 end
|
flickerstreak@28
|
142
|
flickerstreak@28
|
143
|
flickerstreak@28
|
144 ------ HANDLERS ------
|
flickerstreak@28
|
145 function ReAction:OnInitialize()
|
flickerstreak@28
|
146 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
|
flickerstreak@28
|
147 {
|
flickerstreak@28
|
148 profile = {
|
flickerstreak@28
|
149 bars = { },
|
flickerstreak@28
|
150 defaultBar = { }
|
flickerstreak@28
|
151 }
|
flickerstreak@28
|
152 }
|
flickerstreak@28
|
153 -- default profile is character-specific
|
flickerstreak@28
|
154 )
|
flickerstreak@28
|
155 self.db.RegisterCallback(self,"OnProfileChanged")
|
flickerstreak@30
|
156 self.callbacks = LibStub("CallbackHandler-1.0"):New(self)
|
flickerstreak@30
|
157 self:RegisterChatCommand("reaction", SlashHandler)
|
flickerstreak@30
|
158 self:RegisterChatCommand("rxn", SlashHandler)
|
flickerstreak@33
|
159 self:RegisterEvent("PLAYER_REGEN_DISABLED")
|
flickerstreak@28
|
160
|
flickerstreak@28
|
161 self.bars = {}
|
flickerstreak@28
|
162 end
|
flickerstreak@28
|
163
|
flickerstreak@28
|
164 function ReAction:OnEnable()
|
flickerstreak@28
|
165 InitializeBars()
|
flickerstreak@28
|
166 end
|
flickerstreak@28
|
167
|
flickerstreak@28
|
168 function ReAction:OnDisable()
|
flickerstreak@28
|
169 TearDownBars()
|
flickerstreak@28
|
170 end
|
flickerstreak@28
|
171
|
flickerstreak@28
|
172 function ReAction:OnProfileChanged()
|
flickerstreak@33
|
173 TearDownBars()
|
flickerstreak@33
|
174 InitializeBars()
|
flickerstreak@28
|
175 end
|
flickerstreak@28
|
176
|
flickerstreak@28
|
177 function ReAction:OnModuleEnable(module)
|
flickerstreak@28
|
178 if module.ApplyToBar then
|
flickerstreak@28
|
179 for _, b in pairs(bars) do
|
flickerstreak@28
|
180 if b then
|
flickerstreak@28
|
181 module:ApplyToBar(b)
|
flickerstreak@28
|
182 end
|
flickerstreak@28
|
183 end
|
flickerstreak@28
|
184 end
|
flickerstreak@28
|
185 end
|
flickerstreak@28
|
186
|
flickerstreak@28
|
187 function ReAction:OnModuleDisable(module)
|
flickerstreak@28
|
188 if module.RemoveFromBar then
|
flickerstreak@28
|
189 for _, b in pairs(bars) do
|
flickerstreak@28
|
190 if b then
|
flickerstreak@28
|
191 module:RemoveFromBar(b)
|
flickerstreak@28
|
192 end
|
flickerstreak@28
|
193 end
|
flickerstreak@28
|
194 end
|
flickerstreak@28
|
195 end
|
flickerstreak@28
|
196
|
flickerstreak@33
|
197 function ReAction:PLAYER_REGEN_DISABLED()
|
flickerstreak@33
|
198 if self.configMode == true then
|
flickerstreak@33
|
199 UIErrorsFrame:AddMessage(L["ReAction config mode disabled during combat."])
|
flickerstreak@33
|
200 self:SetConfigMode(false)
|
flickerstreak@33
|
201 end
|
flickerstreak@33
|
202 end
|
flickerstreak@33
|
203
|
flickerstreak@33
|
204
|
flickerstreak@28
|
205
|
flickerstreak@28
|
206 ------ API ------
|
flickerstreak@28
|
207 function ReAction:CallMethodOnAllModules(method, ...)
|
flickerstreak@28
|
208 local m = CheckMethod(method)
|
flickerstreak@28
|
209 for _, x in self:IterateModules() do
|
flickerstreak@28
|
210 if x then
|
flickerstreak@28
|
211 SafeCall(m or x[method], x, ...)
|
flickerstreak@28
|
212 end
|
flickerstreak@28
|
213 end
|
flickerstreak@28
|
214 end
|
flickerstreak@28
|
215
|
flickerstreak@28
|
216 function ReAction:CallMethodOnAllBars(method,...)
|
flickerstreak@28
|
217 local m = CheckMethod(method)
|
flickerstreak@28
|
218 for _, x in pairs(self.bars) do
|
flickerstreak@28
|
219 if x then
|
flickerstreak@28
|
220 SafeCall(m or x[method], x, ...)
|
flickerstreak@28
|
221 end
|
flickerstreak@28
|
222 end
|
flickerstreak@28
|
223 end
|
flickerstreak@28
|
224
|
flickerstreak@28
|
225 function ReAction:CreateBar(name, defaultConfig, prefix)
|
flickerstreak@28
|
226 local profile = self.db.profile
|
flickerstreak@28
|
227 defaultConfig = defaultConfig or profile.defaultBar
|
flickerstreak@28
|
228 prefix = prefix or L["Bar "]
|
flickerstreak@28
|
229 if not name then
|
flickerstreak@28
|
230 i = 1
|
flickerstreak@28
|
231 repeat
|
flickerstreak@28
|
232 name = prefix..i
|
flickerstreak@28
|
233 i = i + 1
|
flickerstreak@28
|
234 until self.bars[name] == nil
|
flickerstreak@28
|
235 end
|
flickerstreak@28
|
236 profile.bars[name] = profile.bars[name] or DeepCopy(defaultConfig)
|
flickerstreak@28
|
237 local bar = self.Bar:new( name, profile.bars[name] ) -- ReAction.Bar defined in Bar.lua
|
flickerstreak@28
|
238 self:CallMethodOnAllModules("ApplyToBar", bar)
|
flickerstreak@28
|
239 self.bars[name] = bar
|
flickerstreak@30
|
240 self.callbacks:Fire("OnCreateBar", bar)
|
flickerstreak@33
|
241 if self.configMode then
|
flickerstreak@33
|
242 bar:ShowControls(true)
|
flickerstreak@33
|
243 end
|
flickerstreak@33
|
244
|
flickerstreak@28
|
245 return bar
|
flickerstreak@28
|
246 end
|
flickerstreak@28
|
247
|
flickerstreak@28
|
248 function ReAction:EraseBar(x)
|
flickerstreak@28
|
249 local bar, name = SelectBar(x)
|
flickerstreak@28
|
250 if name and bar then
|
flickerstreak@28
|
251 DestroyBar(bar)
|
flickerstreak@28
|
252 self.db.profile.bars[name] = nil
|
flickerstreak@28
|
253 self:CallMethodOnAllModules("EraseBarConfig", name)
|
flickerstreak@30
|
254 self.callbacks:Fire("OnEraseBar",name)
|
flickerstreak@28
|
255 end
|
flickerstreak@28
|
256 end
|
flickerstreak@28
|
257
|
flickerstreak@28
|
258 function ReAction:GetBar(name)
|
flickerstreak@28
|
259 return self.bars[name]
|
flickerstreak@28
|
260 end
|
flickerstreak@28
|
261
|
flickerstreak@28
|
262 function ReAction:RenameBar(x, newname)
|
flickerstreak@28
|
263 local bar, name = SelectBar(x)
|
flickerstreak@28
|
264 if bar and name and newname then
|
flickerstreak@28
|
265 if self.bars[newname] then
|
flickerstreak@28
|
266 error(L["ReAction: name already in use"])
|
flickerstreak@28
|
267 end
|
flickerstreak@28
|
268 self.bars[newname] = self.bars[name]
|
flickerstreak@28
|
269 self.bars[name] = nil
|
flickerstreak@28
|
270 bar.name = newname or ""
|
flickerstreak@28
|
271 local cfg = self.db.profile.bars
|
flickerstreak@28
|
272 cfg[newname], cfg[name] = cfg[name], nil
|
flickerstreak@28
|
273 self:CallMethodOnAllModules("RenameBarConfig", name, newname)
|
flickerstreak@30
|
274 self.callbacks:Fire("OnRenameBar", name, newname)
|
flickerstreak@28
|
275 end
|
flickerstreak@28
|
276 end
|
flickerstreak@28
|
277
|
flickerstreak@30
|
278 ReAction.options = {}
|
flickerstreak@30
|
279 -- See modules/ReAction_ConfigUI for valid options contexts.
|
flickerstreak@30
|
280 function ReAction:RegisterOptions(context, module, opts)
|
flickerstreak@30
|
281 if module == nil or context == nil then
|
flickerstreak@30
|
282 error("ReAction:RegisterOptions requires a module object and context ID")
|
flickerstreak@30
|
283 end
|
flickerstreak@30
|
284 if not self.options[context] then
|
flickerstreak@30
|
285 self.options[context] = {}
|
flickerstreak@30
|
286 end
|
flickerstreak@30
|
287 self.options[context][module] = opts
|
flickerstreak@30
|
288 self.callbacks:Fire("OnOptionsRegistered", context, module, opts)
|
flickerstreak@30
|
289 end
|
flickerstreak@28
|
290
|
flickerstreak@30
|
291 function ReAction:GetOptions(context)
|
flickerstreak@30
|
292 if context then
|
flickerstreak@30
|
293 if not self.options[context] then
|
flickerstreak@30
|
294 self.options[context] = { }
|
flickerstreak@30
|
295 end
|
flickerstreak@30
|
296 return self.options[context]
|
flickerstreak@30
|
297 end
|
flickerstreak@30
|
298 end
|
flickerstreak@30
|
299
|
flickerstreak@30
|
300 function ReAction:GetOptionContextList()
|
flickerstreak@30
|
301 local c = {}
|
flickerstreak@30
|
302 for k in self.options do
|
flickerstreak@30
|
303 tinsert(c,k)
|
flickerstreak@30
|
304 end
|
flickerstreak@30
|
305 return c
|
flickerstreak@30
|
306 end
|
flickerstreak@30
|
307
|
flickerstreak@30
|
308 function ReAction:RefreshOptions()
|
flickerstreak@30
|
309 self.callbacks:Fire("OnOptionsRefreshed")
|
flickerstreak@30
|
310 end
|
flickerstreak@33
|
311
|
flickerstreak@33
|
312 function ReAction:SetConfigMode( mode )
|
flickerstreak@33
|
313 self:CallMethodOnAllBars("ShowControls",mode)
|
flickerstreak@33
|
314 self:CallMethodOnAllModules("ApplyConfigMode",mode,self.bars)
|
flickerstreak@33
|
315 self.configMode = mode
|
flickerstreak@33
|
316 end
|