Mercurial > wow > reaction
comparison ReAction.lua @ 205:42fd93f19291
Code cleanup of ReAction.lua
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Tue, 16 Nov 2010 21:17:22 -0800 |
parents | 85f7c7857617 |
children | c24ac8ee1e45 443d2ea4be86 |
comparison
equal
deleted
inserted
replaced
204:85f7c7857617 | 205:42fd93f19291 |
---|---|
1 local addonName, addonTable = ... | 1 local addonName, addonTable = ... |
2 local pcall = pcall | |
3 local pairs = pairs | |
4 local type = type | |
5 local geterrorhandler = geterrorhandler | |
6 local LKB = LibStub("LibKeyBound-1.0") | |
7 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction") | |
8 | |
9 ------ Utility ------ | |
10 local tcopy | |
11 do | |
12 function tcopy(x) | |
13 if type(x) ~= "table" then | |
14 return x | |
15 end | |
16 local r = {} | |
17 for k,v in pairs(x) do | |
18 r[k] = tcopy(v) | |
19 end | |
20 return r | |
21 end | |
22 end | |
23 | |
24 ------ Core ------ | |
2 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", | 25 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", |
3 "AceEvent-3.0" | 26 "AceEvent-3.0" |
4 ) | 27 ) |
28 addonTable.ReAction = ReAction | |
5 ReAction.version = "1.1" | 29 ReAction.version = "1.1" |
6 addonTable.ReAction = ReAction | |
7 | |
8 ------ LIBRARIES ------ | |
9 local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction) | |
10 local LKB = LibStub("LibKeyBound-1.0") | |
11 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction") | |
12 ReAction.L = L | 30 ReAction.L = L |
13 ReAction.LKB = LKB | 31 ReAction.LKB = LKB |
14 ReAction.callbacks = callbacks | 32 |
15 | 33 |
16 ------ PRIVATE ------ | 34 ------ Handlers ------ |
17 local private = { } | |
18 local bars = {} | |
19 local defaultBarConfig = {} | |
20 | |
21 | |
22 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy | |
23 do | |
24 local pcall = pcall | |
25 local geterrorhandler = geterrorhandler | |
26 local self = ReAction | |
27 local inited = false | |
28 | |
29 function SelectBar(x) | |
30 local bar, name | |
31 if type(x) == "string" then | |
32 name = x | |
33 bar = self:GetBar(name) | |
34 else | |
35 for k,v in pairs(bars) do | |
36 if v == x then | |
37 name = k | |
38 bar = x | |
39 end | |
40 end | |
41 end | |
42 return bar, name | |
43 end | |
44 | |
45 function DestroyBar(x) | |
46 local bar, name = SelectBar(x) | |
47 if bar and name then | |
48 bars[name] = nil | |
49 callbacks:Fire("OnDestroyBar", bar, name) | |
50 bar:Destroy() | |
51 end | |
52 end | |
53 | |
54 function InitializeBars() | |
55 if not inited then | |
56 for name, config in pairs(self.db.profile.bars) do | |
57 if config then | |
58 self:CreateBar(name, config) | |
59 end | |
60 end | |
61 -- re-anchor and refresh in case anchor order does not match init order | |
62 for name, bar in pairs(bars) do | |
63 bar:ApplyAnchor() | |
64 callbacks:Fire("OnRefreshBar", bar, name) | |
65 end | |
66 inited = true | |
67 end | |
68 end | |
69 | |
70 function TearDownBars() | |
71 for name, bar in pairs(bars) do | |
72 if bar then | |
73 bars[name] = DestroyBar(bar) | |
74 end | |
75 end | |
76 inited = false | |
77 end | |
78 | |
79 function DeepCopy(x) | |
80 if type(x) ~= "table" then | |
81 return x | |
82 end | |
83 local r = {} | |
84 for k,v in pairs(x) do | |
85 r[k] = DeepCopy(v) | |
86 end | |
87 return r | |
88 end | |
89 | |
90 end | |
91 | |
92 | |
93 ------ HANDLERS ------ | |
94 function ReAction:OnInitialize() | 35 function ReAction:OnInitialize() |
95 self.db = LibStub("AceDB-3.0"):New("ReAction_DB", | 36 self.db = LibStub("AceDB-3.0"):New("ReAction_DB", |
96 { | 37 { |
97 profile = { | 38 profile = { |
98 bars = { }, | 39 bars = { }, |
99 defaultBar = { }, | 40 defaultBar = { }, |
100 } | 41 } |
101 }, | 42 }, |
102 true -- use global 'Default' (locale-specific) | 43 true -- use global 'Default' (locale-specific) |
103 ) | 44 ) |
45 | |
46 self.bars = { } | |
47 self.defaultBarConfig = { } | |
48 | |
49 self.callbacks = LibStub("CallbackHandler-1.0"):New(self) | |
104 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED") | 50 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED") |
105 LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED") | 51 LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED") |
106 | |
107 self:RegisterEvent("PLAYER_REGEN_DISABLED") | 52 self:RegisterEvent("PLAYER_REGEN_DISABLED") |
108 | |
109 self:InitializeOptions() | 53 self:InitializeOptions() |
110 end | 54 end |
111 | 55 |
112 function ReAction:OnEnable() | 56 function ReAction:OnEnable() |
113 InitializeBars() | 57 self:InitializeBars() |
114 end | 58 end |
115 | 59 |
116 function ReAction:OnDisable() | 60 function ReAction:OnDisable() |
117 TearDownBars() | 61 self:TearDownBars() |
118 end | 62 end |
119 | 63 |
120 function ReAction:PLAYER_REGEN_DISABLED() | 64 function ReAction:PLAYER_REGEN_DISABLED() |
121 if private.configMode == true then | 65 if self.configMode == true then |
122 self:UserError(L["ReAction config mode disabled during combat."]) | 66 self:UserError(L["ReAction config mode disabled during combat."]) |
123 self:SetConfigMode(false) | 67 self:SetConfigMode(false) |
124 self:SetKeybindMode(false) | 68 self:SetKeybindMode(false) |
125 self:CloseEditor() | 69 self:CloseEditor() |
126 end | 70 end |
133 function ReAction:LIBKEYBOUND_DISABLED( evt ) | 77 function ReAction:LIBKEYBOUND_DISABLED( evt ) |
134 return self:SetKeybindMode(false) | 78 return self:SetKeybindMode(false) |
135 end | 79 end |
136 | 80 |
137 | 81 |
138 | 82 ------ Methods ------ |
139 ------ API ------ | |
140 | 83 |
141 function ReAction:UserError(msg) | 84 function ReAction:UserError(msg) |
142 -- any user errors should be flashed to the UIErrorsFrame | |
143 UIErrorsFrame:AddMessage(msg) | 85 UIErrorsFrame:AddMessage(msg) |
144 end | 86 end |
145 | 87 |
146 function ReAction:RebuildAll() | 88 function ReAction:GetBar(arg) |
147 TearDownBars() | 89 if type(arg) == "string" then |
148 InitializeBars() | 90 return self.bars[arg], arg |
149 end | 91 elseif type(arg) == "table" then -- reverse lookup |
150 | 92 for name, bar in pairs(self.bars) do |
93 if arg == bar then | |
94 return bar, name | |
95 end | |
96 end | |
97 else | |
98 error("ReAction:GetBar() requires either a name or a bar table arg") | |
99 end | |
100 end | |
101 | |
102 function ReAction:IterateBars() | |
103 return pairs(self.bars) | |
104 end | |
151 | 105 |
152 -- usage: | 106 -- usage: |
153 -- (1) ReAction:CreateBar(name, [cfgTable]) | 107 -- (1) ReAction:CreateBar(name, [cfgTable]) |
154 -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing]) | 108 -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing]) |
155 function ReAction:CreateBar(name, config, ...) | 109 function ReAction:CreateBar(name, config, ...) |
156 local profile = self.db.profile | 110 local profile = self.db.profile |
157 | 111 |
158 if name then | 112 if name then |
159 if bars[name] then | 113 if self.bars[name] then |
160 self:UserError(format(L["ReAction: name '%s' already in use"],name)) | 114 self:UserError(format(L["ReAction: name '%s' already in use"],name)) |
161 return nil | 115 return nil |
162 end | 116 end |
163 else | 117 else |
164 local prefix = L["Bar "] | 118 local prefix = L["Bar "] |
165 local i = 1 | 119 local i = 1 |
166 repeat | 120 repeat |
167 name = prefix..i | 121 name = prefix..i |
168 i = i + 1 | 122 i = i + 1 |
169 until bars[name] == nil | 123 until self.bars[name] == nil |
170 end | 124 end |
171 | 125 |
172 if type(config) == "string" then | 126 if type(config) == "string" then |
173 config = defaultBarConfig[config] | 127 config = self.defaultBarConfig[config] |
174 if not config then | 128 if not config then |
175 error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...)))) | 129 error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...)))) |
176 end | 130 end |
177 config = DeepCopy(config) | 131 config = tcopy(config) |
178 config.btnRows = select(1,...) or config.btnRows or 1 | 132 config.btnRows = select(1,...) or config.btnRows or 1 |
179 config.btnColumns = select(2,...) or config.btnColumns or 12 | 133 config.btnColumns = select(2,...) or config.btnColumns or 12 |
180 config.btnWidth = select(3,...) or config.btnWidth or 36 | 134 config.btnWidth = select(3,...) or config.btnWidth or 36 |
181 config.btnHeight = select(3,...) or config.btnHeight or 36 | 135 config.btnHeight = select(3,...) or config.btnHeight or 36 |
182 config.spacing = select(4,...) or config.spacing or 3 | 136 config.spacing = select(4,...) or config.spacing or 3 |
186 config.point = config.point or "BOTTOM" | 140 config.point = config.point or "BOTTOM" |
187 config.relpoint = config.relpoint or "BOTTOM" | 141 config.relpoint = config.relpoint or "BOTTOM" |
188 config.y = config.y or 200 | 142 config.y = config.y or 200 |
189 config.x = config.x or 0 | 143 config.x = config.x or 0 |
190 end | 144 end |
191 config = config or profile.bars[name] or DeepCopy(profile.defaultBar) | 145 config = config or profile.bars[name] or tcopy(profile.defaultBar) |
192 | 146 |
193 profile.bars[name] = config | 147 profile.bars[name] = config |
194 local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua | 148 local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua |
195 bars[name] = bar | 149 self.bars[name] = bar |
196 callbacks:Fire("OnCreateBar", bar, name) | 150 self.callbacks:Fire("OnCreateBar", bar, name) |
197 if private.configMode then | 151 if self.configMode then |
198 bar:ShowControls(true) | 152 bar:ShowControls(true) |
199 end | 153 end |
200 | 154 |
201 return bar | 155 return bar |
202 end | 156 end |
203 | 157 |
204 function ReAction:EraseBar(x) | 158 function ReAction:DestroyBar(x) |
205 local bar, name = SelectBar(x) | 159 local bar, name = self:GetBar(x) |
206 if bar and name then | 160 if bar and name then |
207 callbacks:Fire("OnEraseBar", bar, name) | 161 self.bars[name] = nil |
208 DestroyBar(bar) | 162 self.callbacks:Fire("OnDestroyBar", bar, name) |
209 self.db.profile.bars[name] = nil | 163 bar:Destroy() |
210 end | 164 end |
211 end | 165 end |
212 | 166 |
213 function ReAction:GetBar(name) | 167 function ReAction:RefreshBar(x) |
214 return bars[name] | 168 local bar, name = self:GetBar(x) |
215 end | 169 if bar and name then |
216 | 170 self.callbacks:Fire("OnRefreshBar", bar, name) |
217 -- returns pairs of name, bar | 171 end |
218 function ReAction:IterateBars() | 172 end |
219 return pairs(bars) | 173 |
174 function ReAction:InitializeBars() | |
175 if not self.barsInitialized then | |
176 for name, config in pairs(self.db.profile.bars) do | |
177 if config then | |
178 self:CreateBar(name, config) | |
179 end | |
180 end | |
181 -- re-anchor and refresh in case anchor order does not match init order | |
182 for name, bar in pairs(self.bars) do | |
183 bar:ApplyAnchor() | |
184 self.callbacks:Fire("OnRefreshBar", bar, name) | |
185 end | |
186 self.barsInitialized = true | |
187 end | |
188 end | |
189 | |
190 function ReAction:TearDownBars() | |
191 for name, bar in pairs(self.bars) do | |
192 if bar then | |
193 self.bars[name] = DestroyBar(bar) | |
194 end | |
195 end | |
196 self.barsInitialized = false | |
197 end | |
198 | |
199 function ReAction:RebuildAll() | |
200 self:TearDownBars() | |
201 self:InitializeBars() | |
220 end | 202 end |
221 | 203 |
222 function ReAction:RenameBar(x, newname) | 204 function ReAction:RenameBar(x, newname) |
223 local bar, name = SelectBar(x) | 205 local bar, name = self:GetBar(x) |
224 if type(newname) ~= "string" then | 206 if type(newname) ~= "string" then |
225 error("ReAction:RenameBar() - second argument must be a string") | 207 error("ReAction:RenameBar() - second argument must be a string") |
226 end | 208 end |
227 if bar and name and #newname > 0 then | 209 if bar and name and #newname > 0 then |
228 if newname == name then | 210 if newname == name then |
229 return | 211 return |
230 end | 212 end |
231 if bars[newname] then | 213 if self.bars[newname] then |
232 self:UserError(format(L["ReAction: name '%s' already in use"],newname)) | 214 self:UserError(format(L["ReAction: name '%s' already in use"],newname)) |
233 else | 215 else |
234 bars[newname], bars[name] = bars[name], nil | 216 self.bars[newname], self.bars[name] = self.bars[name], nil |
235 bar:SetName(newname or "") | 217 bar:SetName(newname or "") |
236 local cfg = self.db.profile.bars | 218 local cfg = self.db.profile.bars |
237 cfg[newname], cfg[name] = cfg[name], nil | 219 cfg[newname], cfg[name] = cfg[name], nil |
238 callbacks:Fire("OnRenameBar", bar, name, newname) | 220 self.callbacks:Fire("OnRenameBar", bar, name, newname) |
239 end | 221 end |
240 end | 222 end |
241 end | 223 end |
242 | 224 |
243 function ReAction:RefreshBar(x) | 225 function ReAction:EraseBar(x) |
244 local bar, name = SelectBar(x) | 226 local bar, name = self:GetBar(x) |
245 if bar and name then | 227 if bar and name then |
246 callbacks:Fire("OnRefreshBar", bar, name) | 228 self.callbacks:Fire("OnEraseBar", bar, name) |
229 DestroyBar(bar) | |
230 self.db.profile.bars[name] = nil | |
247 end | 231 end |
248 end | 232 end |
249 | 233 |
250 function ReAction:RegisterBarType( name, config, isDefaultChoice ) | 234 function ReAction:RegisterBarType( name, config, isDefaultChoice ) |
251 defaultBarConfig[name] = config | 235 self.defaultBarConfig[name] = config |
252 if isDefaultChoice then | 236 if isDefaultChoice then |
253 private.defaultBarConfigChoice = name | 237 self.defaultBarConfigChoice = name |
254 end | 238 end |
255 self:RefreshEditor() | 239 self:RefreshEditor() |
256 end | 240 end |
257 | 241 |
258 function ReAction:UnregisterBarType( name ) | 242 function ReAction:UnregisterBarType( name ) |
259 defaultBarConfig[name] = nil | 243 self.defaultBarConfig[name] = nil |
260 if private.defaultBarConfigChoice == name then | 244 if self.defaultBarConfigChoice == name then |
261 private.defaultBarConfigChoice = nil | 245 self.defaultBarConfigChoice = nil |
262 end | 246 end |
263 self:RefreshEditor() | 247 self:RefreshEditor() |
264 end | 248 end |
265 | 249 |
266 function ReAction:IterateBarTypes() | 250 function ReAction:IterateBarTypes() |
267 return pairs(defaultBarConfig) | 251 return pairs(self.defaultBarConfig) |
268 end | 252 end |
269 | 253 |
270 function ReAction:GetBarTypeConfig(name) | 254 function ReAction:GetBarTypeConfig(name) |
271 if name then | 255 if name then |
272 return defaultBarConfig[name] | 256 return self.defaultBarConfig[name] |
273 end | 257 end |
274 end | 258 end |
275 | 259 |
276 function ReAction:GetBarTypeOptions( fill ) | 260 function ReAction:GetBarTypeOptions( fill ) |
277 fill = fill or { } | 261 fill = fill or { } |
280 end | 264 end |
281 return fill | 265 return fill |
282 end | 266 end |
283 | 267 |
284 function ReAction:GetDefaultBarType() | 268 function ReAction:GetDefaultBarType() |
285 return private.defaultBarConfigChoice | 269 return self.defaultBarConfigChoice |
286 end | 270 end |
287 | 271 |
288 function ReAction:SetConfigMode( mode ) | 272 function ReAction:SetConfigMode( mode ) |
289 if mode ~= private.configMode then | 273 if mode ~= self.configMode then |
290 private.configMode = mode | 274 self.configMode = mode |
291 callbacks:Fire("OnConfigModeChanged", mode) | 275 self.callbacks:Fire("OnConfigModeChanged", mode) |
292 end | 276 end |
293 end | 277 end |
294 | 278 |
295 function ReAction:GetConfigMode() | 279 function ReAction:GetConfigMode() |
296 return private.configMode | 280 return self.configMode |
297 end | 281 end |
298 | 282 |
299 function ReAction:SetKeybindMode( mode ) | 283 function ReAction:SetKeybindMode( mode ) |
300 if mode ~= private.kbMode then | 284 if mode ~= self.kbMode then |
301 if mode then | 285 if mode then |
302 LKB:Activate() | 286 LKB:Activate() |
303 else | 287 else |
304 LKB:Deactivate() | 288 LKB:Deactivate() |
305 end | 289 end |
306 private.kbMode = LKB:IsShown() or false | 290 self.kbMode = LKB:IsShown() or false |
307 end | 291 end |
308 end | 292 end |
309 | 293 |
310 function ReAction:GetKeybindMode( mode ) | 294 function ReAction:GetKeybindMode( mode ) |
311 return private.kbMode | 295 return self.kbMode |
312 end | 296 end |