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@25
|
4
|
flickerstreak@28
|
5 ------ LIBRARIES ------
|
flickerstreak@28
|
6 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction")
|
flickerstreak@27
|
7
|
flickerstreak@27
|
8 ------ CORE ------
|
flickerstreak@28
|
9 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction" )
|
flickerstreak@27
|
10 ReAction.revision = tonumber(("$Revision: 1 $"):match("%d+"))
|
flickerstreak@27
|
11 ReAction.L = L
|
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@28
|
27 ------ PRIVATE ------
|
flickerstreak@28
|
28 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, SafeCall, CheckMethod
|
flickerstreak@28
|
29 do
|
flickerstreak@28
|
30 local pcall = pcall
|
flickerstreak@28
|
31 local geterrorhandler = geterrorhandler
|
flickerstreak@28
|
32
|
flickerstreak@28
|
33 SelectBar = function(x)
|
flickerstreak@28
|
34 local bar, name
|
flickerstreak@28
|
35 if type(x) == "string" then
|
flickerstreak@28
|
36 name = x
|
flickerstreak@28
|
37 bar = ReAction:GetBar(name)
|
flickerstreak@28
|
38 elseif ReAction.Bar:IsInstance(x) then
|
flickerstreak@28
|
39 bar = x
|
flickerstreak@28
|
40 for k,v in pairs(ReAction.bars) do
|
flickerstreak@28
|
41 if v == bar then
|
flickerstreak@28
|
42 name = k
|
flickerstreak@28
|
43 end
|
flickerstreak@28
|
44 end
|
flickerstreak@28
|
45 else
|
flickerstreak@28
|
46 error("bad argument to SelectBar")
|
flickerstreak@28
|
47 end
|
flickerstreak@28
|
48 return bar, name
|
flickerstreak@28
|
49 end
|
flickerstreak@28
|
50
|
flickerstreak@28
|
51 DestroyBar = function(x)
|
flickerstreak@28
|
52 local bar, name = SelectBar(x)
|
flickerstreak@28
|
53 if name and bar then
|
flickerstreak@28
|
54 ReAction.bars[name] = nil
|
flickerstreak@28
|
55 ReAction:CallMethodOnAllModules("RemoveFromBar", bar)
|
flickerstreak@28
|
56 bar:Destroy()
|
flickerstreak@28
|
57 end
|
flickerstreak@28
|
58 end
|
flickerstreak@28
|
59
|
flickerstreak@28
|
60 InitializeBars = function ()
|
flickerstreak@28
|
61 if not(ReAction.inited) then
|
flickerstreak@28
|
62 for name, config in pairs(ReAction.db.profile.bars) do
|
flickerstreak@28
|
63 if config then
|
flickerstreak@28
|
64 ReAction:CreateBar(name, config)
|
flickerstreak@28
|
65 end
|
flickerstreak@28
|
66 end
|
flickerstreak@28
|
67 ReAction:CallMethodOnAllBars("ApplyAnchor") -- re-anchor in the case of oddball ordering
|
flickerstreak@28
|
68 ReAction.inited = true
|
flickerstreak@28
|
69 end
|
flickerstreak@28
|
70 end
|
flickerstreak@28
|
71
|
flickerstreak@28
|
72 TearDownBars = function()
|
flickerstreak@28
|
73 for name, bar in pairs(ReAction.bars) do
|
flickerstreak@28
|
74 if bar then
|
flickerstreak@28
|
75 ReAction.bars[name] = DestroyBar(bar)
|
flickerstreak@28
|
76 end
|
flickerstreak@28
|
77 end
|
flickerstreak@28
|
78 ReAction.inited = false
|
flickerstreak@28
|
79 end
|
flickerstreak@28
|
80
|
flickerstreak@28
|
81 DeepCopy = function(x)
|
flickerstreak@28
|
82 if type(x) ~= "table" then
|
flickerstreak@28
|
83 return x
|
flickerstreak@28
|
84 end
|
flickerstreak@28
|
85 local r = {}
|
flickerstreak@28
|
86 for k,v in pairs(x) do
|
flickerstreak@28
|
87 r[k] = DeepCopy(v)
|
flickerstreak@28
|
88 end
|
flickerstreak@28
|
89 return r
|
flickerstreak@28
|
90 end
|
flickerstreak@28
|
91
|
flickerstreak@28
|
92 SafeCall = function(f, ...)
|
flickerstreak@28
|
93 if f then
|
flickerstreak@28
|
94 local success, err = pcall(f,...)
|
flickerstreak@28
|
95 if not success then
|
flickerstreak@28
|
96 geterrorhandler()(err)
|
flickerstreak@28
|
97 end
|
flickerstreak@28
|
98 end
|
flickerstreak@28
|
99 end
|
flickerstreak@28
|
100
|
flickerstreak@28
|
101 CheckMethod = function(m)
|
flickerstreak@28
|
102 if type(m) == "function" then
|
flickerstreak@28
|
103 return m
|
flickerstreak@28
|
104 end
|
flickerstreak@28
|
105 if type(m) ~= "string" then
|
flickerstreak@28
|
106 error("Invalid method")
|
flickerstreak@28
|
107 end
|
flickerstreak@28
|
108 end
|
flickerstreak@28
|
109 end
|
flickerstreak@28
|
110
|
flickerstreak@28
|
111
|
flickerstreak@28
|
112 ------ HANDLERS ------
|
flickerstreak@28
|
113 function ReAction:OnInitialize()
|
flickerstreak@28
|
114 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
|
flickerstreak@28
|
115 {
|
flickerstreak@28
|
116 profile = {
|
flickerstreak@28
|
117 bars = { },
|
flickerstreak@28
|
118 defaultBar = { }
|
flickerstreak@28
|
119 }
|
flickerstreak@28
|
120 }
|
flickerstreak@28
|
121 -- default profile is character-specific
|
flickerstreak@28
|
122 )
|
flickerstreak@28
|
123 self.db.RegisterCallback(self,"OnProfileChanged")
|
flickerstreak@28
|
124
|
flickerstreak@28
|
125 self.bars = {}
|
flickerstreak@28
|
126 end
|
flickerstreak@28
|
127
|
flickerstreak@28
|
128 function ReAction:OnEnable()
|
flickerstreak@28
|
129 InitializeBars()
|
flickerstreak@28
|
130 end
|
flickerstreak@28
|
131
|
flickerstreak@28
|
132 function ReAction:OnDisable()
|
flickerstreak@28
|
133 TearDownBars()
|
flickerstreak@28
|
134 end
|
flickerstreak@28
|
135
|
flickerstreak@28
|
136 function ReAction:OnProfileChanged()
|
flickerstreak@28
|
137 self.TearDownBars()
|
flickerstreak@28
|
138 self.InitializeBars()
|
flickerstreak@28
|
139 end
|
flickerstreak@28
|
140
|
flickerstreak@28
|
141 function ReAction:OnModuleEnable(module)
|
flickerstreak@28
|
142 if module.ApplyToBar then
|
flickerstreak@28
|
143 for _, b in pairs(bars) do
|
flickerstreak@28
|
144 if b then
|
flickerstreak@28
|
145 module:ApplyToBar(b)
|
flickerstreak@28
|
146 end
|
flickerstreak@28
|
147 end
|
flickerstreak@28
|
148 end
|
flickerstreak@28
|
149 end
|
flickerstreak@28
|
150
|
flickerstreak@28
|
151 function ReAction:OnModuleDisable(module)
|
flickerstreak@28
|
152 if module.RemoveFromBar then
|
flickerstreak@28
|
153 for _, b in pairs(bars) do
|
flickerstreak@28
|
154 if b then
|
flickerstreak@28
|
155 module:RemoveFromBar(b)
|
flickerstreak@28
|
156 end
|
flickerstreak@28
|
157 end
|
flickerstreak@28
|
158 end
|
flickerstreak@28
|
159 end
|
flickerstreak@28
|
160
|
flickerstreak@28
|
161
|
flickerstreak@28
|
162 ------ API ------
|
flickerstreak@28
|
163 function ReAction:CallMethodOnAllModules(method, ...)
|
flickerstreak@28
|
164 local m = CheckMethod(method)
|
flickerstreak@28
|
165 for _, x in self:IterateModules() do
|
flickerstreak@28
|
166 if x then
|
flickerstreak@28
|
167 SafeCall(m or x[method], x, ...)
|
flickerstreak@28
|
168 end
|
flickerstreak@28
|
169 end
|
flickerstreak@28
|
170 end
|
flickerstreak@28
|
171
|
flickerstreak@28
|
172 function ReAction:CallMethodOnAllBars(method,...)
|
flickerstreak@28
|
173 local m = CheckMethod(method)
|
flickerstreak@28
|
174 for _, x in pairs(self.bars) do
|
flickerstreak@28
|
175 if x then
|
flickerstreak@28
|
176 SafeCall(m or x[method], x, ...)
|
flickerstreak@28
|
177 end
|
flickerstreak@28
|
178 end
|
flickerstreak@28
|
179 end
|
flickerstreak@28
|
180
|
flickerstreak@28
|
181 function ReAction:CreateBar(name, defaultConfig, prefix)
|
flickerstreak@28
|
182 local profile = self.db.profile
|
flickerstreak@28
|
183 defaultConfig = defaultConfig or profile.defaultBar
|
flickerstreak@28
|
184 prefix = prefix or L["Bar "]
|
flickerstreak@28
|
185 if not name then
|
flickerstreak@28
|
186 i = 1
|
flickerstreak@28
|
187 repeat
|
flickerstreak@28
|
188 name = prefix..i
|
flickerstreak@28
|
189 i = i + 1
|
flickerstreak@28
|
190 until self.bars[name] == nil
|
flickerstreak@28
|
191 end
|
flickerstreak@28
|
192 profile.bars[name] = profile.bars[name] or DeepCopy(defaultConfig)
|
flickerstreak@28
|
193 local bar = self.Bar:new( name, profile.bars[name] ) -- ReAction.Bar defined in Bar.lua
|
flickerstreak@28
|
194 self:CallMethodOnAllModules("ApplyToBar", bar)
|
flickerstreak@28
|
195 self.bars[name] = bar
|
flickerstreak@28
|
196 return bar
|
flickerstreak@28
|
197 end
|
flickerstreak@28
|
198
|
flickerstreak@28
|
199 function ReAction:EraseBar(x)
|
flickerstreak@28
|
200 local bar, name = SelectBar(x)
|
flickerstreak@28
|
201 if name and bar then
|
flickerstreak@28
|
202 DestroyBar(bar)
|
flickerstreak@28
|
203 self.db.profile.bars[name] = nil
|
flickerstreak@28
|
204 self:CallMethodOnAllModules("EraseBarConfig", name)
|
flickerstreak@28
|
205 end
|
flickerstreak@28
|
206 end
|
flickerstreak@28
|
207
|
flickerstreak@28
|
208 function ReAction:GetBar(name)
|
flickerstreak@28
|
209 return self.bars[name]
|
flickerstreak@28
|
210 end
|
flickerstreak@28
|
211
|
flickerstreak@28
|
212 function ReAction:RenameBar(x, newname)
|
flickerstreak@28
|
213 local bar, name = SelectBar(x)
|
flickerstreak@28
|
214 if bar and name and newname then
|
flickerstreak@28
|
215 if self.bars[newname] then
|
flickerstreak@28
|
216 error(L["ReAction: name already in use"])
|
flickerstreak@28
|
217 end
|
flickerstreak@28
|
218 self.bars[newname] = self.bars[name]
|
flickerstreak@28
|
219 self.bars[name] = nil
|
flickerstreak@28
|
220 bar.name = newname or ""
|
flickerstreak@28
|
221 local cfg = self.db.profile.bars
|
flickerstreak@28
|
222 cfg[newname], cfg[name] = cfg[name], nil
|
flickerstreak@28
|
223 self:CallMethodOnAllModules("RenameBarConfig", name, newname)
|
flickerstreak@28
|
224 end
|
flickerstreak@28
|
225 end
|
flickerstreak@28
|
226
|
flickerstreak@28
|
227
|