comparison ReAction.lua @ 185:2e7a322e0195

move ConfigUI module to Editor non-module, make it more self-contained
author Flick <flickerstreak@gmail.com>
date Fri, 22 Oct 2010 23:48:02 +0000
parents 1ee86bbb05a0
children 55af1ebbec65
comparison
equal deleted inserted replaced
184:1ee86bbb05a0 185:2e7a322e0195
2 ReAction.lua 2 ReAction.lua
3 3
4 The ReAction core manages several collections: 4 The ReAction core manages several collections:
5 - modules (via AceAddon) 5 - modules (via AceAddon)
6 - bars 6 - bars
7 - bar options
8 - bar-type constructors 7 - bar-type constructors
9 8
10 and publishes events when those collections change. It also implements a couple properties 9 and publishes events when those collections change. It also implements a couple properties
11 and has a couple convenience methods which drill down to particular modules. 10 and has a couple convenience methods which drill down to particular modules.
12 11
17 "OnCreateBar" (bar, name) : after a bar object is created 16 "OnCreateBar" (bar, name) : after a bar object is created
18 "OnDestroyBar" (bar, name) : before a bar object is destroyed 17 "OnDestroyBar" (bar, name) : before a bar object is destroyed
19 "OnEraseBar" (bar, name) : before a bar config is removed from the profile db 18 "OnEraseBar" (bar, name) : before a bar config is removed from the profile db
20 "OnRenameBar" (bar, oldname, newname) : after a bar is renamed 19 "OnRenameBar" (bar, oldname, newname) : after a bar is renamed
21 "OnRefreshBar" (bar, name) : after a bar's state has been updated 20 "OnRefreshBar" (bar, name) : after a bar's state has been updated
22 "OnOptionsRefreshed" () : after the global options tree is refreshed
23 "OnConfigModeChanged" (mode) : after the config mode is changed 21 "OnConfigModeChanged" (mode) : after the config mode is changed
24 "OnBarOptionGeneratorRegistered" (module, function) : after an options generator function is registered
25 22
26 ReAction is also an AceAddon-3.0 and contains an AceDB-3.0, which in turn publish more events. 23 ReAction is also an AceAddon-3.0 and contains an AceDB-3.0, which in turn publish more events.
27 ]]-- 24 ]]--
28 local addonName, addonTable = ... 25 local _, addonTable = ...
29 local ReAction = LibStub("AceAddon-3.0"):NewAddon( addonName, 26 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction",
30 "AceEvent-3.0" 27 "AceEvent-3.0"
31 ) 28 )
32 ReAction.version = GetAddOnMetadata(addonName,"Version") 29 ReAction.version = "1.0"
33 addonTable.ReAction = ReAction 30 addonTable.ReAction = ReAction
34 31
35 ------ LIBRARIES ------ 32 ------ LIBRARIES ------
36 local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction) 33 local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction)
37 local LKB = LibStub("LibKeyBound-1.0") 34 local LKB = LibStub("LibKeyBound-1.0")
42 39
43 ------ PRIVATE ------ 40 ------ PRIVATE ------
44 local private = { } 41 local private = { }
45 local bars = {} 42 local bars = {}
46 local defaultBarConfig = {} 43 local defaultBarConfig = {}
47 local barOptionGenerators = { } 44
48 45
49 46 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy
50 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, CallModuleMethod
51 do 47 do
52 local pcall = pcall 48 local pcall = pcall
53 local geterrorhandler = geterrorhandler 49 local geterrorhandler = geterrorhandler
54 local self = ReAction 50 local self = ReAction
55 local inited = false 51 local inited = false
113 r[k] = DeepCopy(v) 109 r[k] = DeepCopy(v)
114 end 110 end
115 return r 111 return r
116 end 112 end
117 113
118 function CallModuleMethod(modulename, method, ...)
119 local m = self:GetModule(modulename,true)
120 if m then
121 if type(m) == "table" and type(m[method]) == "function" then
122 m[method](m,...)
123 end
124 end
125 end
126
127 end 114 end
128 115
129 116
130 ------ HANDLERS ------ 117 ------ HANDLERS ------
131 function ReAction:OnInitialize() 118 function ReAction:OnInitialize()
133 { 120 {
134 profile = { 121 profile = {
135 bars = { }, 122 bars = { },
136 defaultBar = { }, 123 defaultBar = { },
137 closeOptionsOnEditorLaunch = true, 124 closeOptionsOnEditorLaunch = true,
125 editorCloseOnLaunch = true,
138 } 126 }
139 }, 127 },
140 true -- use global 'Default' (locale-specific) 128 true -- use global 'Default' (locale-specific)
141 ) 129 )
142 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED") 130 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED")
158 function ReAction:PLAYER_REGEN_DISABLED() 146 function ReAction:PLAYER_REGEN_DISABLED()
159 if private.configMode == true then 147 if private.configMode == true then
160 self:UserError(L["ReAction config mode disabled during combat."]) 148 self:UserError(L["ReAction config mode disabled during combat."])
161 self:SetConfigMode(false) 149 self:SetConfigMode(false)
162 self:SetKeybindMode(false) 150 self:SetKeybindMode(false)
151 self:CloseEditor()
163 end 152 end
164 end 153 end
165 154
166 function ReAction:LIBKEYBOUND_ENABLED( evt ) 155 function ReAction:LIBKEYBOUND_ENABLED( evt )
167 self:SetKeybindMode(true) 156 self:SetKeybindMode(true)
287 function ReAction:RegisterBarType( name, config, isDefaultChoice ) 276 function ReAction:RegisterBarType( name, config, isDefaultChoice )
288 defaultBarConfig[name] = config 277 defaultBarConfig[name] = config
289 if isDefaultChoice then 278 if isDefaultChoice then
290 private.defaultBarConfigChoice = name 279 private.defaultBarConfigChoice = name
291 end 280 end
292 self:RefreshOptions() 281 self:RefreshEditor()
293 end 282 end
294 283
295 function ReAction:UnregisterBarType( name ) 284 function ReAction:UnregisterBarType( name )
296 defaultBarConfig[name] = nil 285 defaultBarConfig[name] = nil
297 if private.defaultBarConfigChoice == name then 286 if private.defaultBarConfigChoice == name then
298 private.defaultBarConfigChoice = nil 287 private.defaultBarConfigChoice = nil
299 end 288 end
300 self:RefreshOptions() 289 self:RefreshEditor()
301 end 290 end
302 291
303 function ReAction:IterateBarTypes() 292 function ReAction:IterateBarTypes()
304 return pairs(defaultBarConfig) 293 return pairs(defaultBarConfig)
305 end 294 end
320 309
321 function ReAction:GetDefaultBarType() 310 function ReAction:GetDefaultBarType()
322 return private.defaultBarConfigChoice 311 return private.defaultBarConfigChoice
323 end 312 end
324 313
325 function ReAction:RefreshOptions()
326 callbacks:Fire("OnOptionsRefreshed")
327 end
328
329 --
330 -- In addition to global options, options tables
331 -- must be generated dynamically for each bar.
332 --
333 -- 'func' should be a function or a method string.
334 -- The function or method will be passed the bar as its parameter.
335 -- (methods will of course get the module as the first 'self' parameter)
336 --
337 -- A generator can be unregistered by passing a nil func.
338 --
339 function ReAction:RegisterBarOptionGenerator( module, func )
340 if not module or type(module) ~= "table" then -- doesn't need to be a proper module, strictly
341 error("ReAction:RegisterBarOptionGenerator() : Invalid module")
342 end
343 if type(func) == "string" then
344 if not module[func] then
345 error(("ReAction:RegisterBarOptionGenerator() : Invalid method '%s'"):format(func))
346 end
347 elseif func and type(func) ~= "function" then
348 error("ReAction:RegisterBarOptionGenerator() : Invalid function")
349 end
350 barOptionGenerators[module] = func
351 callbacks:Fire("OnBarOptionGeneratorRegistered", module, func)
352 end
353
354 -- builds a table suitable for use as an AceConfig3 group 'plugins' sub-table
355 function ReAction:GenerateBarOptionsTable( bar )
356 local opts = { }
357 for module, func in pairs(barOptionGenerators) do
358 local success, r
359 if type(func) == "string" then
360 success, r = pcall(module[func], module, bar)
361 else
362 success, r = pcall(func, bar)
363 end
364 if success then
365 if r then
366 opts[module:GetName()] = { [module:GetName()] = r }
367 end
368 else
369 geterrorhandler()(r)
370 end
371 end
372 return opts
373 end
374
375 function ReAction:SetConfigMode( mode ) 314 function ReAction:SetConfigMode( mode )
376 if mode ~= private.configMode then 315 if mode ~= private.configMode then
377 private.configMode = mode 316 private.configMode = mode
378 callbacks:Fire("OnConfigModeChanged", mode) 317 callbacks:Fire("OnConfigModeChanged", mode)
379 end 318 end
380 end 319 end
381 320
382 function ReAction:GetConfigMode() 321 function ReAction:GetConfigMode()
383 return private.configMode 322 return private.configMode
384 end
385
386 function ReAction:ShowEditor(bar, ...)
387 CallModuleMethod("ConfigUI","LaunchBarEditor",bar, ...)
388 end 323 end
389 324
390 function ReAction:SetKeybindMode( mode ) 325 function ReAction:SetKeybindMode( mode )
391 if mode ~= private.kbMode then 326 if mode ~= private.kbMode then
392 if mode then 327 if mode then