Mercurial > wow > buffalo2
comparison Init.lua @ 0:3dbcad2b387d
initial push
author | Nenue |
---|---|
date | Wed, 30 Mar 2016 02:24:56 -0400 |
parents | |
children | 3397aae1f44d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dbcad2b387d |
---|---|
1 --- Modulizer framework | |
2 -- OnInitialize | |
3 -- OnUpdate | |
4 -- OnEnable -- run when GetSpecialization() returns true | |
5 | |
6 local ADDON, A = ... | |
7 Veneer = Veneer or CreateFrame('Frame', 'Veneer', UIParent) | |
8 local B = Veneer | |
9 local wipe, min, max, random, tinsert = table.wipe, math.min, math.max, math.random, table.insert | |
10 local pairs, ipairs, select, unpack, _G = pairs, ipairs, select, unpack, _G | |
11 local type, tostring, format = type, tostring, string.format | |
12 A.frame = B | |
13 | |
14 --- Cache tables | |
15 local initOnced | |
16 local modules = {} | |
17 local queuedModules = {} | |
18 local moduleStack = { | |
19 } | |
20 | |
21 --- Various region categories | |
22 B.displays = {} | |
23 B.configLayers = {} | |
24 B.configLayersRef = {} | |
25 | |
26 --@debug@ | |
27 --- Generates a print handler pointing to a static channel signature | |
28 -- @usage func = B.print(sig) | |
29 -- @param sig channel name or number | |
30 local printfuncs = {} | |
31 B.print = function(pref, ...) | |
32 if Devian and Devian.InWorkspace() then | |
33 printfuncs[pref] = printfuncs[pref] or function(...) print(pref, ...) end | |
34 | |
35 return printfuncs[pref] | |
36 else | |
37 return function () end | |
38 end | |
39 end | |
40 | |
41 local rgb = {} | |
42 local getcolor = function() | |
43 local n, p = 0, 4 | |
44 for i = 1, 3 do | |
45 rgb[i] = min(random(n,p) * 64, 255) | |
46 if rgb[i] == 255 then | |
47 p = 4 | |
48 elseif rgb[i] > 0 then | |
49 n = 2 | |
50 end | |
51 end | |
52 return unpack(rgb) | |
53 end | |
54 | |
55 local color = {} | |
56 local fprints = {} | |
57 B.fprint = function() | |
58 if not (Devian and Devian.InWorkspace()) then | |
59 return function() end | |
60 end | |
61 | |
62 | |
63 local sig = debugstack(2,1) | |
64 if fprints[sig] then | |
65 return fprints[sig] | |
66 end | |
67 | |
68 local func = sig:match("%`(%a+)%'") | |
69 if not func then | |
70 func = sig:match("<(.-)>") | |
71 end | |
72 func = func:gsub('(%l+)(%u)', function(a, b) return a:sub(0,2) .. b end, 1) | |
73 func = func:gsub('^.+%\\', '') | |
74 if not func then | |
75 func = 'noname' | |
76 end | |
77 | |
78 local r, g, b = getcolor() | |
79 color[sig] = color[sig] or format('|cFF%02X%02X%02X%s|r', r, g, b, func) | |
80 | |
81 --print(color[func] .. ' ( ' .. table.concat(args, ', ')..' )' ) | |
82 func = B.print(func) | |
83 fprints[sig] = func | |
84 return func | |
85 end | |
86 | |
87 --@end-debug@ | |
88 --[=[@non-debug@ | |
89 B.print = function() end | |
90 --@end-non-debug@]=] | |
91 | |
92 -- for the Mikk script | |
93 -- GLOBALS: NUM_LE_RAID_BUFF_TYPES | |
94 -- GLOBALS: BUFF_FLASH_TIME_ON, BUFF_FLASH_TIME_OFF, BUFF_MIN_ALPHA, BUFF_WARNING_TIME, BUFF_DURATION_WARNING_TIME | |
95 -- GLOBALS: BUFFS_PER_ROW, BUFF_MAX_DISPLAY, BUFF_ACTUAL_DISPLAY, DEBUFF_MAX_DISPLAY, DEBUFF_ACTUAL_DISPLAY, BUFF_ROW_SPACING | |
96 -- GLOBALS: CONSOLIDATED_BUFFS_PER_ROW, CONSOLIDATED_BUFF_ROW_HEIGHT, NUM_TEMP_ENCHANT_FRAMES | |
97 -- GLOBALS: BUFF_BUTTON_HEIGHT, BUFF_FRAME_BASE_EXTENT, BUFF_HORIZ_SPACING | |
98 | |
99 local print = B.print('Bfl') | |
100 | |
101 --- Template for making perpendicular traversals of the displays structure; also makes sure the table is there | |
102 B.Abstract = function(dest, key, table) | |
103 if table then | |
104 for _, v in pairs(dest) do | |
105 v[key] = {} | |
106 end | |
107 end | |
108 B[key] = setmetatable({}, { | |
109 __index = function(t, k) | |
110 return dest[k][key] | |
111 end, | |
112 __newindex = function(_, k, v) | |
113 print('abstract write ('..key..'):', k) | |
114 dest[k][key] = v | |
115 end, | |
116 __tostring = function() return 'Abstract:'..key..'' end | |
117 }) | |
118 | |
119 | |
120 return B[key] | |
121 end | |
122 | |
123 | |
124 --- localize for speed | |
125 local layers, refs, displays = B.configLayers, B.configLayersRef, B.displays | |
126 | |
127 local ModulesCall = function(func) | |
128 | |
129 local n = 0 | |
130 for i = 1, #moduleStack do | |
131 print('calling level '..i) | |
132 local stackset = moduleStack[i] | |
133 | |
134 for name, module in pairs(stackset) do | |
135 n = n + 1 | |
136 print(n..' '..name..'.'..func..'()') | |
137 | |
138 | |
139 if module[func] then | |
140 module[func](module) | |
141 end | |
142 end | |
143 end | |
144 end | |
145 | |
146 | |
147 local Enable = function() | |
148 end | |
149 | |
150 --- The things that happen repeatedly | |
151 local Init = function () | |
152 end | |
153 | |
154 | |
155 --- Things that happen immediately upon entering world | |
156 local InitOnce = function() | |
157 print('entering world first time') | |
158 local defaults = B.ConfDefaults | |
159 print('|cFFFFFF00Veneer|r') | |
160 if not VeneerData then | |
161 VeneerData = {} | |
162 for k,v in pairs(defaults) do | |
163 VeneerData[k] = v | |
164 end | |
165 print('Veneer defaults being used.') | |
166 end | |
167 | |
168 B.Conf = setmetatable(VeneerData, {__index = function(_, k) return defaults[k] end}) | |
169 | |
170 -- suffix tables | |
171 for name, display in pairs(displays) do | |
172 display.conf = setmetatable({}, { | |
173 __index = function(_, k) | |
174 --print('config check '.. name .. k) | |
175 return B.Conf[name .. k] or B.Conf['BuffButton' .. k] | |
176 end, | |
177 __newindex = function(_, k , v) | |
178 B.Conf[name..k] = v | |
179 end, | |
180 }) | |
181 end | |
182 | |
183 -- To ensure that modules are run in controlled order, walk the dependency list; if the dep shows up | |
184 -- in the loaded manifest, remove the value. If the dep list isn't empty, move that module to the next | |
185 -- layer. | |
186 local loaded = {} | |
187 local stackLevels = #moduleStack | |
188 local i = 1 | |
189 moduleStack[1] = modules | |
190 repeat | |
191 print('setting init level '.. i) | |
192 local queue = moduleStack[i] | |
193 for name, module in pairs(queue) do | |
194 | |
195 if queuedModules[name] and #queuedModules[name] > 0 then | |
196 local p = #queuedModules[name] | |
197 for j = 1, p do | |
198 local dep = queuedModules[name][j] | |
199 | |
200 if loaded[dep] then | |
201 print( ' ' .. dep .. ' OK') | |
202 queuedModules[name][j] = nil | |
203 for k = j, p do | |
204 print(' shift ' .. (k+1) .. ' ('..tostring(queuedModules[name][k+1])..') to ' .. k ..'') | |
205 queuedModules[name][k] = queuedModules[name][k+1] | |
206 end | |
207 end | |
208 end | |
209 | |
210 if #queuedModules[name] == 0 then | |
211 queuedModules[name] = nil | |
212 print(' |cFF00FFFF'.. name ..'|r deps OK') | |
213 loaded[name] = true | |
214 else | |
215 | |
216 print(' |cFFFF8800' .. name ..'|r pending') | |
217 local next = i+1 | |
218 if not moduleStack[next] then | |
219 moduleStack[next] = {} | |
220 end | |
221 stackLevels = next | |
222 moduleStack[next][name] = module | |
223 queue[name] = nil | |
224 end | |
225 | |
226 else | |
227 print(' |cFF00FF00'.. name ..'|r no deps') | |
228 loaded[name] = true | |
229 end | |
230 end | |
231 i = i + 1 | |
232 until i > stackLevels | |
233 | |
234 | |
235 for level, batch in ipairs(moduleStack) do | |
236 print('config level', level) | |
237 for name, module in pairs(batch) do | |
238 print('integrity check', name) | |
239 | |
240 --[===[@non-debug@ | |
241 if module.defaults and not VeneerData[name] then | |
242 --@end-non-debug@]===] | |
243 print('Adding defaults from module ', name) | |
244 VeneerData[name] = module.default | |
245 --[===[@non-debug@ | |
246 end | |
247 --@end-non-debug@]===] | |
248 end | |
249 end | |
250 -- remove from existing | |
251 end | |
252 | |
253 --- Fires an update to all modules | |
254 local lastUpdate | |
255 function B.UpdateAll(...) | |
256 lastUpdate = GetTime() | |
257 ModulesCall('OnUpdate', lastUpdate) | |
258 end | |
259 | |
260 B:RegisterEvent('PLAYER_ENTERING_WORLD') | |
261 B:SetScript('OnEvent', function(self, event) | |
262 if event == 'PLAYER_ENTERING_WORLD' then | |
263 if not initOnced then | |
264 InitOnce() | |
265 ModulesCall('OnInitialize') | |
266 initOnced = true | |
267 C_Timer.After(1, function() | |
268 if GetSpecialization() then | |
269 print(GetSpecialization(), 'enabling') | |
270 ModulesCall('OnEnable') | |
271 B:SetScript('OnUpdate', nil) | |
272 end | |
273 | |
274 end) | |
275 end | |
276 | |
277 end | |
278 | |
279 B.UpdateAll() | |
280 end) | |
281 | |
282 --- Modulizer method | |
283 -- | |
284 function B:RegisterModule (name, module, ...) | |
285 if modules[name] then | |
286 print('pulling modules[|cFFFF8800'.. tostring(name) ..'|r]') | |
287 return modules[name] | |
288 end | |
289 | |
290 print('new module |cFF00BBFF'.. tostring(name) ..'|r') | |
291 if module then | |
292 if modules[name] then | |
293 error("Module table for '"..tostring(name).."' already exists.") | |
294 end | |
295 else | |
296 module = CreateFrame('Frame', 'Veneer' .. tostring(name) .. 'Handler', B, 'VeneerHandlerTemplate') | |
297 end | |
298 modules[name] = module | |
299 if select('#', ...) >= 1 then | |
300 local numDeps = select('#', ...) | |
301 print(' '..numDeps..' deps detected') | |
302 for i = 1, numDeps do | |
303 local dep = select(i, ...) | |
304 -- means that init/enable funcs are ordered to run after deps do their things | |
305 queuedModules[name] = queuedModules[name] or {} | |
306 tinsert(queuedModules[name], dep) | |
307 print(' needs '..dep) | |
308 end | |
309 end | |
310 return module | |
311 end | |
312 | |
313 | |
314 B.SetConfigLayers = function(frame) | |
315 local print = B.fprint() | |
316 if not frame.config then | |
317 print(frame:GetName(), 'has no config layers') | |
318 return | |
319 end | |
320 print('Registering config layers from', frame:GetName()) | |
321 | |
322 for i, subframe in ipairs(frame.config) do | |
323 -- make sure there are no duplicates | |
324 if not refs[subframe] then | |
325 local key = #layers+1 | |
326 layers[key] = subframe | |
327 refs[subframe] = key | |
328 end | |
329 print(' ', i, subframe:GetName()) | |
330 end | |
331 end | |
332 | |
333 B.RemoveConfigLayers = function(frame) | |
334 local print = B.fprint() | |
335 print('|cFFFF0000RemoveConfigLayers', frame:GetName()) | |
336 for i, subframe in pairs(layers) do | |
337 if subframe:GetParent() == frame then | |
338 print('|cFFFF8800 ', subframe:GetParent():GetName(), '|cFFFFFF00', subframe:GetName()) | |
339 layers[i]:Hide() | |
340 layers[i] = nil | |
341 refs[subframe] = nil | |
342 end | |
343 end | |
344 end | |
345 | |
346 B.UpdateConfigLayers = function() | |
347 local print = B.fprint() | |
348 local func = B.Conf.GuidesMode and 'Show' or 'Hide' | |
349 local numAnchors = 0 | |
350 for name, display in pairs(displays) do | |
351 numAnchors = numAnchors + 1 | |
352 display.anchor:EnableMouse(B.Conf.GuidesMode) | |
353 if B.Conf.GuidesMode then | |
354 display.anchor:SetScript('OnUpdate', display.anchor.OnUpdate) | |
355 else | |
356 display.anchor:SetScript('OnUpdate', nil) | |
357 | |
358 for i, anchorButton in ipairs(display.anchor.anchorButton) do | |
359 anchorButton:Hide() | |
360 end | |
361 | |
362 end | |
363 | |
364 end | |
365 for id, region in pairs(layers) do | |
366 print(id, region:GetName(), func) | |
367 region[func](region) | |
368 end | |
369 | |
370 print('['..func..'] updated', #layers, 'regions,', numAnchors, 'frames') | |
371 end |