Mercurial > wow > reaction
comparison ReAction.lua @ 28:21bcaf8215ff
- converted to Ace3
- rearranged file layout
- configGUI menus not working right now
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Mon, 17 Mar 2008 18:24:53 +0000 |
parents | f1e838841ce1 |
children | 0d95ce7a9ec2 |
comparison
equal
deleted
inserted
replaced
27:f1e838841ce1 | 28:21bcaf8215ff |
---|---|
1 --[[ | 1 -- ReAction.lua |
2 ReAction Add-On main file. | 2 -- See modules/ReAction_ModuleTemplate for Module API listing |
3 Performs add-on and library initialization and setup. | 3 -- See Bar.lua for Bar object listing |
4 --]] | 4 |
5 | 5 ------ LIBRARIES ------ |
6 ------ LOCALIZATION ---------- | 6 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction") |
7 local L = AceLibrary("AceLocale-2.2"):new("ReAction") | |
8 | |
9 | |
10 ------ GLOBAL VARIABLES ------ | |
11 -- 'ReAction' is exported as a global. | |
12 ReAction = AceLibrary("AceAddon-2.0"):new( | |
13 "AceModuleCore-2.0", | |
14 "AceEvent-2.0", | |
15 "AceDB-2.0" | |
16 ) | |
17 -- global variable strings for integration with WoW keybindings dialog (see bindings.xml) | |
18 BINDING_HEADER_REACTION = L["ReAction"] | |
19 BINDING_NAME_REACTION_TOGGLELOCK = L["Toggle ReAction Bar Lock"] | |
20 BINDING_NAME_REACTION_TOGGLEKEYBIND = L["ReAction Keybinding Mode"] | |
21 | |
22 | 7 |
23 ------ CORE ------ | 8 ------ CORE ------ |
24 local ReAction = ReAction | 9 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction" ) |
25 ReAction.revision = tonumber(("$Revision: 1 $"):match("%d+")) | 10 ReAction.revision = tonumber(("$Revision: 1 $"):match("%d+")) |
26 ReAction.L = L | 11 ReAction.L = L |
27 | 12 |
28 | 13 ------ GLOBALS ------ |
29 | 14 _G["ReAction"] = ReAction |
30 -- from AceAddon-2.0 | 15 |
31 function ReAction:OnInitialize() | 16 ------ DEBUGGING ------ |
32 self:RegisterDB("ReActionDB") | |
33 end | |
34 | |
35 -- from AceAddon-2.0 | |
36 function ReAction:OnEnable() | |
37 | |
38 end | |
39 | |
40 -- from AceAddon-2.0 | |
41 function ReAction:OnDisable() | |
42 | |
43 end | |
44 | |
45 -- from AceDB-2.0 | |
46 function ReAction:OnProfileEnable() | |
47 | |
48 end | |
49 | |
50 -- from AceDB-2.0 | |
51 function ReAction:OnProfileDisable() | |
52 | |
53 end | |
54 | |
55 -- from AceModuleCore-2.0 | |
56 function ReAction:OnModuleEnable(module) | |
57 -- this handles initialization ordering issues with ReAction_Bar | |
58 local barMod = self:GetModule("Bar") | |
59 if barMod and module.ApplyToBar then | |
60 for _, b in pairs(barMod.bars) do | |
61 if b then | |
62 module:ApplyToBar(b) | |
63 end | |
64 end | |
65 end | |
66 end | |
67 | |
68 -- from AceModuleCore-2.0 | |
69 function ReAction:OnModuleDisable(module) | |
70 local barMod = self:GetModule("Bar") | |
71 if barMod and module.RemoveFromBar then | |
72 for _, b in pairs(barMod.bars) do | |
73 if b then | |
74 module:RemoveFromBar(b) | |
75 end | |
76 end | |
77 end | |
78 end | |
79 | |
80 --[[ | |
81 Module API (see bar.lua for usage) | |
82 | |
83 module:ApplyToBar(bar) | |
84 module:RemoveFromBar(bar) | |
85 module:RefreshBar(bar) | |
86 module:ApplyConfigMode(mode,listOfBars) | |
87 module:GetBarNameModifier(bar) | |
88 module:EraseBarConfig(barName) | |
89 ]]-- | |
90 | |
91 | |
92 -- debugging | |
93 ReAction.debug = true | 17 ReAction.debug = true |
94 if ReAction.debug then | 18 if ReAction.debug then |
95 ReAction.print = function(msg) | 19 ReAction.print = function(msg) |
96 DEFAULT_CHAT_FRAME:AddMessage(msg) | 20 DEFAULT_CHAT_FRAME:AddMessage(msg) |
97 end | 21 end |
98 --seterrorhandler(ReAction.print) | 22 --seterrorhandler(ReAction.print) |
99 else | 23 else |
100 ReAction.print = function() end | 24 ReAction.print = function() end |
101 end | 25 end |
102 | 26 |
27 ------ PRIVATE ------ | |
28 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, SafeCall, CheckMethod | |
29 do | |
30 local pcall = pcall | |
31 local geterrorhandler = geterrorhandler | |
32 | |
33 SelectBar = function(x) | |
34 local bar, name | |
35 if type(x) == "string" then | |
36 name = x | |
37 bar = ReAction:GetBar(name) | |
38 elseif ReAction.Bar:IsInstance(x) then | |
39 bar = x | |
40 for k,v in pairs(ReAction.bars) do | |
41 if v == bar then | |
42 name = k | |
43 end | |
44 end | |
45 else | |
46 error("bad argument to SelectBar") | |
47 end | |
48 return bar, name | |
49 end | |
50 | |
51 DestroyBar = function(x) | |
52 local bar, name = SelectBar(x) | |
53 if name and bar then | |
54 ReAction.bars[name] = nil | |
55 ReAction:CallMethodOnAllModules("RemoveFromBar", bar) | |
56 bar:Destroy() | |
57 end | |
58 end | |
59 | |
60 InitializeBars = function () | |
61 if not(ReAction.inited) then | |
62 for name, config in pairs(ReAction.db.profile.bars) do | |
63 if config then | |
64 ReAction:CreateBar(name, config) | |
65 end | |
66 end | |
67 ReAction:CallMethodOnAllBars("ApplyAnchor") -- re-anchor in the case of oddball ordering | |
68 ReAction.inited = true | |
69 end | |
70 end | |
71 | |
72 TearDownBars = function() | |
73 for name, bar in pairs(ReAction.bars) do | |
74 if bar then | |
75 ReAction.bars[name] = DestroyBar(bar) | |
76 end | |
77 end | |
78 ReAction.inited = false | |
79 end | |
80 | |
81 DeepCopy = function(x) | |
82 if type(x) ~= "table" then | |
83 return x | |
84 end | |
85 local r = {} | |
86 for k,v in pairs(x) do | |
87 r[k] = DeepCopy(v) | |
88 end | |
89 return r | |
90 end | |
91 | |
92 SafeCall = function(f, ...) | |
93 if f then | |
94 local success, err = pcall(f,...) | |
95 if not success then | |
96 geterrorhandler()(err) | |
97 end | |
98 end | |
99 end | |
100 | |
101 CheckMethod = function(m) | |
102 if type(m) == "function" then | |
103 return m | |
104 end | |
105 if type(m) ~= "string" then | |
106 error("Invalid method") | |
107 end | |
108 end | |
109 end | |
110 | |
111 | |
112 ------ HANDLERS ------ | |
113 function ReAction:OnInitialize() | |
114 self.db = LibStub("AceDB-3.0"):New("ReAction_DB", | |
115 { | |
116 profile = { | |
117 bars = { }, | |
118 defaultBar = { } | |
119 } | |
120 } | |
121 -- default profile is character-specific | |
122 ) | |
123 self.db.RegisterCallback(self,"OnProfileChanged") | |
124 | |
125 self.bars = {} | |
126 end | |
127 | |
128 function ReAction:OnEnable() | |
129 InitializeBars() | |
130 end | |
131 | |
132 function ReAction:OnDisable() | |
133 TearDownBars() | |
134 end | |
135 | |
136 function ReAction:OnProfileChanged() | |
137 self.TearDownBars() | |
138 self.InitializeBars() | |
139 end | |
140 | |
141 function ReAction:OnModuleEnable(module) | |
142 if module.ApplyToBar then | |
143 for _, b in pairs(bars) do | |
144 if b then | |
145 module:ApplyToBar(b) | |
146 end | |
147 end | |
148 end | |
149 end | |
150 | |
151 function ReAction:OnModuleDisable(module) | |
152 if module.RemoveFromBar then | |
153 for _, b in pairs(bars) do | |
154 if b then | |
155 module:RemoveFromBar(b) | |
156 end | |
157 end | |
158 end | |
159 end | |
160 | |
161 | |
162 ------ API ------ | |
163 function ReAction:CallMethodOnAllModules(method, ...) | |
164 local m = CheckMethod(method) | |
165 for _, x in self:IterateModules() do | |
166 if x then | |
167 SafeCall(m or x[method], x, ...) | |
168 end | |
169 end | |
170 end | |
171 | |
172 function ReAction:CallMethodOnAllBars(method,...) | |
173 local m = CheckMethod(method) | |
174 for _, x in pairs(self.bars) do | |
175 if x then | |
176 SafeCall(m or x[method], x, ...) | |
177 end | |
178 end | |
179 end | |
180 | |
181 function ReAction:CreateBar(name, defaultConfig, prefix) | |
182 local profile = self.db.profile | |
183 defaultConfig = defaultConfig or profile.defaultBar | |
184 prefix = prefix or L["Bar "] | |
185 if not name then | |
186 i = 1 | |
187 repeat | |
188 name = prefix..i | |
189 i = i + 1 | |
190 until self.bars[name] == nil | |
191 end | |
192 profile.bars[name] = profile.bars[name] or DeepCopy(defaultConfig) | |
193 local bar = self.Bar:new( name, profile.bars[name] ) -- ReAction.Bar defined in Bar.lua | |
194 self:CallMethodOnAllModules("ApplyToBar", bar) | |
195 self.bars[name] = bar | |
196 return bar | |
197 end | |
198 | |
199 function ReAction:EraseBar(x) | |
200 local bar, name = SelectBar(x) | |
201 if name and bar then | |
202 DestroyBar(bar) | |
203 self.db.profile.bars[name] = nil | |
204 self:CallMethodOnAllModules("EraseBarConfig", name) | |
205 end | |
206 end | |
207 | |
208 function ReAction:GetBar(name) | |
209 return self.bars[name] | |
210 end | |
211 | |
212 function ReAction:RenameBar(x, newname) | |
213 local bar, name = SelectBar(x) | |
214 if bar and name and newname then | |
215 if self.bars[newname] then | |
216 error(L["ReAction: name already in use"]) | |
217 end | |
218 self.bars[newname] = self.bars[name] | |
219 self.bars[name] = nil | |
220 bar.name = newname or "" | |
221 local cfg = self.db.profile.bars | |
222 cfg[newname], cfg[name] = cfg[name], nil | |
223 self:CallMethodOnAllModules("RenameBarConfig", name, newname) | |
224 end | |
225 end | |
226 | |
227 |