annotate ReAction.lua @ 182:55c2fc0c8d55

Collect options in one file clean up ReAction.lua a bit remove AceConsole-3.0
author Flick <flickerstreak@gmail.com>
date Thu, 21 Oct 2010 22:07:11 +0000
parents df68b5a40490
children 1ee86bbb05a0
rev   line source
flickerstreak@63 1 --[[
flickerstreak@63 2 ReAction.lua
flickerstreak@63 3
flickerstreak@182 4 The ReAction core manages several collections:
flickerstreak@63 5 - modules (via AceAddon)
flickerstreak@63 6 - bars
flickerstreak@182 7 - bar options
flickerstreak@63 8 - bar-type constructors
flickerstreak@63 9
flickerstreak@90 10 and publishes events when those collections change. It also implements a couple properties
flickerstreak@63 11 and has a couple convenience methods which drill down to particular modules.
flickerstreak@63 12
flickerstreak@90 13 Most of the "real work" of the addon happens in Bar.lua, Overlay.lua, State.lua, and the various modules.
flickerstreak@63 14
flickerstreak@63 15 Events (with handler arguments):
flickerstreak@63 16 --------------------------------
flickerstreak@63 17 "OnCreateBar" (bar, name) : after a bar object is created
flickerstreak@63 18 "OnDestroyBar" (bar, name) : before a bar object is destroyed
flickerstreak@63 19 "OnEraseBar" (bar, name) : before a bar config is removed from the profile db
flickerstreak@63 20 "OnRenameBar" (bar, oldname, newname) : after a bar is renamed
flickerstreak@63 21 "OnRefreshBar" (bar, name) : after a bar's state has been updated
flickerstreak@63 22 "OnOptionsRefreshed" () : after the global options tree is refreshed
flickerstreak@63 23 "OnConfigModeChanged" (mode) : after the config mode is changed
flickerstreak@63 24 "OnBarOptionGeneratorRegistered" (module, function) : after an options generator function is registered
flickerstreak@63 25
flickerstreak@63 26 ReAction is also an AceAddon-3.0 and contains an AceDB-3.0, which in turn publish more events.
flickerstreak@63 27 ]]--
flickerstreak@27 28
flickerstreak@182 29 local addonName, addonTable = ...
flickerstreak@182 30 local version = GetAddOnMetadata(addonName,"Version")
flickerstreak@182 31 local ReAction = LibStub("AceAddon-3.0"):NewAddon( addonName,
flickerstreak@33 32 "AceEvent-3.0"
flickerstreak@30 33 )
flickerstreak@175 34 addonTable.ReAction = ReAction
flickerstreak@27 35
flickerstreak@33 36 ------ LIBRARIES ------
flickerstreak@63 37 local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction)
flickerstreak@88 38 local KB = LibStub("LibKeyBound-1.0")
flickerstreak@33 39 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction")
flickerstreak@33 40 ReAction.L = L
flickerstreak@182 41 ReAction.KB = KB
flickerstreak@182 42 ReAction.callbacks = callbacks
flickerstreak@33 43
flickerstreak@28 44 ------ PRIVATE ------
flickerstreak@116 45 local private = { }
flickerstreak@63 46 local bars = {}
flickerstreak@63 47 local defaultBarConfig = {}
flickerstreak@63 48 local barOptionGenerators = { }
flickerstreak@63 49
flickerstreak@116 50
flickerstreak@182 51 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, CallModuleMethod
flickerstreak@28 52 do
flickerstreak@28 53 local pcall = pcall
flickerstreak@28 54 local geterrorhandler = geterrorhandler
flickerstreak@63 55 local self = ReAction
flickerstreak@63 56 local inited = false
flickerstreak@28 57
flickerstreak@63 58 function SelectBar(x)
flickerstreak@28 59 local bar, name
flickerstreak@28 60 if type(x) == "string" then
flickerstreak@28 61 name = x
flickerstreak@63 62 bar = self:GetBar(name)
flickerstreak@50 63 else
flickerstreak@63 64 for k,v in pairs(bars) do
flickerstreak@50 65 if v == x then
flickerstreak@28 66 name = k
flickerstreak@50 67 bar = x
flickerstreak@28 68 end
flickerstreak@28 69 end
flickerstreak@28 70 end
flickerstreak@28 71 return bar, name
flickerstreak@28 72 end
flickerstreak@28 73
flickerstreak@63 74 function DestroyBar(x)
flickerstreak@28 75 local bar, name = SelectBar(x)
flickerstreak@63 76 if bar and name then
flickerstreak@63 77 bars[name] = nil
flickerstreak@63 78 callbacks:Fire("OnDestroyBar", bar, name)
flickerstreak@28 79 bar:Destroy()
flickerstreak@28 80 end
flickerstreak@28 81 end
flickerstreak@28 82
flickerstreak@63 83 function InitializeBars()
flickerstreak@63 84 if not inited then
flickerstreak@63 85 for name, config in pairs(self.db.profile.bars) do
flickerstreak@28 86 if config then
flickerstreak@63 87 self:CreateBar(name, config)
flickerstreak@28 88 end
flickerstreak@28 89 end
flickerstreak@101 90 -- re-anchor and refresh in case anchor order does not match init order
flickerstreak@63 91 for name, bar in pairs(bars) do
flickerstreak@63 92 bar:ApplyAnchor()
flickerstreak@101 93 callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@63 94 end
flickerstreak@63 95 inited = true
flickerstreak@28 96 end
flickerstreak@28 97 end
flickerstreak@28 98
flickerstreak@63 99 function TearDownBars()
flickerstreak@63 100 for name, bar in pairs(bars) do
flickerstreak@28 101 if bar then
flickerstreak@63 102 bars[name] = DestroyBar(bar)
flickerstreak@28 103 end
flickerstreak@28 104 end
flickerstreak@63 105 inited = false
flickerstreak@28 106 end
flickerstreak@28 107
flickerstreak@63 108 function DeepCopy(x)
flickerstreak@28 109 if type(x) ~= "table" then
flickerstreak@28 110 return x
flickerstreak@28 111 end
flickerstreak@28 112 local r = {}
flickerstreak@28 113 for k,v in pairs(x) do
flickerstreak@28 114 r[k] = DeepCopy(v)
flickerstreak@28 115 end
flickerstreak@28 116 return r
flickerstreak@28 117 end
flickerstreak@28 118
flickerstreak@63 119 function CallModuleMethod(modulename, method, ...)
flickerstreak@63 120 local m = self:GetModule(modulename,true)
flickerstreak@63 121 if m then
flickerstreak@63 122 if type(m) == "table" and type(m[method]) == "function" then
flickerstreak@63 123 m[method](m,...)
flickerstreak@63 124 end
flickerstreak@30 125 end
flickerstreak@30 126 end
flickerstreak@92 127
flickerstreak@28 128 end
flickerstreak@28 129
flickerstreak@28 130
flickerstreak@28 131 ------ HANDLERS ------
flickerstreak@28 132 function ReAction:OnInitialize()
flickerstreak@28 133 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
flickerstreak@28 134 {
flickerstreak@28 135 profile = {
flickerstreak@28 136 bars = { },
flickerstreak@28 137 defaultBar = { }
flickerstreak@28 138 }
flickerstreak@111 139 },
flickerstreak@182 140 true -- use global 'Default' (locale-specific)
flickerstreak@28 141 )
flickerstreak@88 142 KB.RegisterCallback(self,"LIBKEYBOUND_ENABLED")
flickerstreak@88 143 KB.RegisterCallback(self,"LIBKEYBOUND_DISABLED")
flickerstreak@88 144
flickerstreak@182 145 self:RegisterEvent("PLAYER_REGEN_DISABLED")
flickerstreak@63 146
flickerstreak@182 147 self:InitializeOptions()
flickerstreak@28 148 end
flickerstreak@28 149
flickerstreak@28 150 function ReAction:OnEnable()
flickerstreak@28 151 InitializeBars()
flickerstreak@28 152 end
flickerstreak@28 153
flickerstreak@28 154 function ReAction:OnDisable()
flickerstreak@28 155 TearDownBars()
flickerstreak@28 156 end
flickerstreak@28 157
flickerstreak@33 158 function ReAction:PLAYER_REGEN_DISABLED()
flickerstreak@63 159 if private.configMode == true then
flickerstreak@63 160 self:UserError(L["ReAction config mode disabled during combat."])
flickerstreak@33 161 self:SetConfigMode(false)
flickerstreak@88 162 self:SetKeybindMode(false)
flickerstreak@33 163 end
flickerstreak@33 164 end
flickerstreak@33 165
flickerstreak@88 166 function ReAction:LIBKEYBOUND_ENABLED( evt )
flickerstreak@88 167 self:SetKeybindMode(true)
flickerstreak@88 168 end
flickerstreak@88 169
flickerstreak@88 170 function ReAction:LIBKEYBOUND_DISABLED( evt )
flickerstreak@88 171 return self:SetKeybindMode(false)
flickerstreak@88 172 end
flickerstreak@88 173
flickerstreak@33 174
flickerstreak@28 175
flickerstreak@28 176 ------ API ------
flickerstreak@77 177
flickerstreak@61 178 function ReAction:UserError(msg)
flickerstreak@61 179 -- any user errors should be flashed to the UIErrorsFrame
flickerstreak@61 180 UIErrorsFrame:AddMessage(msg)
flickerstreak@61 181 end
flickerstreak@61 182
flickerstreak@63 183 -- usage:
flickerstreak@91 184 -- (1) ReAction:CreateBar(name, [cfgTable])
flickerstreak@63 185 -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing])
flickerstreak@91 186 function ReAction:CreateBar(name, config, ...)
flickerstreak@91 187 local profile = self.db.profile
flickerstreak@91 188
flickerstreak@127 189 if name then
flickerstreak@127 190 if bars[name] then
flickerstreak@127 191 self:UserError(format(L["ReAction: name '%s' already in use"],name))
flickerstreak@127 192 return nil
flickerstreak@127 193 end
flickerstreak@127 194 else
flickerstreak@91 195 local prefix = L["Bar "]
flickerstreak@91 196 local i = 1
flickerstreak@91 197 repeat
flickerstreak@91 198 name = prefix..i
flickerstreak@91 199 i = i + 1
flickerstreak@91 200 until bars[name] == nil
flickerstreak@91 201 end
flickerstreak@91 202
flickerstreak@91 203 if type(config) == "string" then
flickerstreak@91 204 config = defaultBarConfig[config]
flickerstreak@48 205 if not config then
flickerstreak@91 206 error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...))))
flickerstreak@48 207 end
flickerstreak@48 208 config = DeepCopy(config)
flickerstreak@91 209 config.btnRows = select(1,...) or config.btnRows or 1
flickerstreak@91 210 config.btnColumns = select(2,...) or config.btnColumns or 12
flickerstreak@91 211 config.btnWidth = select(3,...) or config.btnWidth or 36
flickerstreak@91 212 config.btnHeight = select(3,...) or config.btnHeight or 36
flickerstreak@91 213 config.spacing = select(4,...) or config.spacing or 3
flickerstreak@48 214 config.width = config.width or config.btnColumns*(config.btnWidth + config.spacing) + 1
flickerstreak@48 215 config.height = config.height or config.btnRows*(config.btnHeight + config.spacing) + 1
flickerstreak@81 216 config.anchor = config.anchor or "UIParent"
flickerstreak@81 217 config.point = config.point or "BOTTOM"
flickerstreak@81 218 config.relpoint = config.relpoint or "BOTTOM"
flickerstreak@48 219 config.y = config.y or 200
flickerstreak@48 220 config.x = config.x or 0
flickerstreak@48 221 end
flickerstreak@91 222 config = config or profile.bars[name] or DeepCopy(profile.defaultBar)
flickerstreak@91 223
flickerstreak@91 224 profile.bars[name] = config
flickerstreak@91 225 local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua
flickerstreak@63 226 bars[name] = bar
flickerstreak@63 227 callbacks:Fire("OnCreateBar", bar, name)
flickerstreak@63 228 if private.configMode then
flickerstreak@33 229 bar:ShowControls(true)
flickerstreak@33 230 end
flickerstreak@33 231
flickerstreak@28 232 return bar
flickerstreak@28 233 end
flickerstreak@28 234
flickerstreak@28 235 function ReAction:EraseBar(x)
flickerstreak@28 236 local bar, name = SelectBar(x)
flickerstreak@63 237 if bar and name then
flickerstreak@63 238 callbacks:Fire("OnEraseBar", bar, name)
flickerstreak@28 239 DestroyBar(bar)
flickerstreak@28 240 self.db.profile.bars[name] = nil
flickerstreak@28 241 end
flickerstreak@28 242 end
flickerstreak@28 243
flickerstreak@28 244 function ReAction:GetBar(name)
flickerstreak@63 245 return bars[name]
flickerstreak@63 246 end
flickerstreak@63 247
flickerstreak@90 248 -- returns pairs of name, bar
flickerstreak@63 249 function ReAction:IterateBars()
flickerstreak@63 250 return pairs(bars)
flickerstreak@28 251 end
flickerstreak@28 252
flickerstreak@28 253 function ReAction:RenameBar(x, newname)
flickerstreak@28 254 local bar, name = SelectBar(x)
flickerstreak@63 255 if type(newname) ~= "string" then
flickerstreak@63 256 error("ReAction:RenameBar() - second argument must be a string")
flickerstreak@63 257 end
flickerstreak@63 258 if bar and name and #newname > 0 then
flickerstreak@127 259 if newname == name then
flickerstreak@127 260 return
flickerstreak@127 261 end
flickerstreak@63 262 if bars[newname] then
flickerstreak@127 263 self:UserError(format(L["ReAction: name '%s' already in use"],newname))
flickerstreak@47 264 else
flickerstreak@63 265 bars[newname], bars[name] = bars[name], nil
flickerstreak@47 266 bar:SetName(newname or "")
flickerstreak@47 267 local cfg = self.db.profile.bars
flickerstreak@47 268 cfg[newname], cfg[name] = cfg[name], nil
flickerstreak@63 269 callbacks:Fire("OnRenameBar", bar, name, newname)
flickerstreak@28 270 end
flickerstreak@28 271 end
flickerstreak@28 272 end
flickerstreak@28 273
flickerstreak@63 274 function ReAction:RefreshBar(x)
flickerstreak@63 275 local bar, name = SelectBar(x)
flickerstreak@63 276 if bar and name then
flickerstreak@63 277 callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@63 278 end
flickerstreak@63 279 end
flickerstreak@63 280
flickerstreak@53 281 function ReAction:RegisterBarType( name, config, isDefaultChoice )
flickerstreak@63 282 defaultBarConfig[name] = config
flickerstreak@48 283 if isDefaultChoice then
flickerstreak@81 284 private.defaultBarConfigChoice = name
flickerstreak@48 285 end
flickerstreak@48 286 self:RefreshOptions()
flickerstreak@48 287 end
flickerstreak@48 288
flickerstreak@53 289 function ReAction:UnregisterBarType( name )
flickerstreak@63 290 defaultBarConfig[name] = nil
flickerstreak@63 291 if private.defaultBarConfigChoice == name then
flickerstreak@63 292 private.defaultBarConfigChoice = nil
flickerstreak@48 293 end
flickerstreak@48 294 self:RefreshOptions()
flickerstreak@48 295 end
flickerstreak@48 296
flickerstreak@63 297 function ReAction:IterateBarTypes()
flickerstreak@63 298 return pairs(defaultBarConfig)
flickerstreak@63 299 end
flickerstreak@63 300
flickerstreak@63 301 function ReAction:GetBarTypeConfig(name)
flickerstreak@63 302 if name then
flickerstreak@63 303 return defaultBarConfig[name]
flickerstreak@63 304 end
flickerstreak@63 305 end
flickerstreak@63 306
flickerstreak@63 307 function ReAction:GetBarTypeOptions( fill )
flickerstreak@63 308 fill = fill or { }
flickerstreak@63 309 for k in self:IterateBarTypes() do
flickerstreak@63 310 fill[k] = k
flickerstreak@63 311 end
flickerstreak@63 312 return fill
flickerstreak@63 313 end
flickerstreak@63 314
flickerstreak@63 315 function ReAction:GetDefaultBarType()
flickerstreak@63 316 return private.defaultBarConfigChoice
flickerstreak@63 317 end
flickerstreak@63 318
flickerstreak@30 319 function ReAction:RefreshOptions()
flickerstreak@63 320 callbacks:Fire("OnOptionsRefreshed")
flickerstreak@63 321 end
flickerstreak@63 322
flickerstreak@63 323 --
flickerstreak@182 324 -- In addition to global options, options tables
flickerstreak@63 325 -- must be generated dynamically for each bar.
flickerstreak@63 326 --
flickerstreak@63 327 -- 'func' should be a function or a method string.
flickerstreak@63 328 -- The function or method will be passed the bar as its parameter.
flickerstreak@63 329 -- (methods will of course get the module as the first 'self' parameter)
flickerstreak@63 330 --
flickerstreak@63 331 -- A generator can be unregistered by passing a nil func.
flickerstreak@63 332 --
flickerstreak@63 333 function ReAction:RegisterBarOptionGenerator( module, func )
flickerstreak@63 334 if not module or type(module) ~= "table" then -- doesn't need to be a proper module, strictly
flickerstreak@63 335 error("ReAction:RegisterBarOptionGenerator() : Invalid module")
flickerstreak@63 336 end
flickerstreak@63 337 if type(func) == "string" then
flickerstreak@63 338 if not module[func] then
flickerstreak@63 339 error(("ReAction:RegisterBarOptionGenerator() : Invalid method '%s'"):format(func))
flickerstreak@63 340 end
flickerstreak@63 341 elseif func and type(func) ~= "function" then
flickerstreak@63 342 error("ReAction:RegisterBarOptionGenerator() : Invalid function")
flickerstreak@63 343 end
flickerstreak@63 344 barOptionGenerators[module] = func
flickerstreak@63 345 callbacks:Fire("OnBarOptionGeneratorRegistered", module, func)
flickerstreak@63 346 end
flickerstreak@63 347
flickerstreak@63 348 -- builds a table suitable for use as an AceConfig3 group 'plugins' sub-table
flickerstreak@63 349 function ReAction:GenerateBarOptionsTable( bar )
flickerstreak@63 350 local opts = { }
flickerstreak@63 351 for module, func in pairs(barOptionGenerators) do
flickerstreak@63 352 local success, r
flickerstreak@63 353 if type(func) == "string" then
flickerstreak@63 354 success, r = pcall(module[func], module, bar)
flickerstreak@63 355 else
flickerstreak@63 356 success, r = pcall(func, bar)
flickerstreak@63 357 end
flickerstreak@63 358 if success then
flickerstreak@90 359 if r then
flickerstreak@90 360 opts[module:GetName()] = { [module:GetName()] = r }
flickerstreak@90 361 end
flickerstreak@63 362 else
flickerstreak@63 363 geterrorhandler()(r)
flickerstreak@63 364 end
flickerstreak@63 365 end
flickerstreak@63 366 return opts
flickerstreak@30 367 end
flickerstreak@33 368
flickerstreak@33 369 function ReAction:SetConfigMode( mode )
flickerstreak@77 370 if mode ~= private.configMode then
flickerstreak@77 371 private.configMode = mode
flickerstreak@77 372 callbacks:Fire("OnConfigModeChanged", mode)
flickerstreak@77 373 end
flickerstreak@63 374 end
flickerstreak@63 375
flickerstreak@63 376 function ReAction:GetConfigMode()
flickerstreak@63 377 return private.configMode
flickerstreak@33 378 end
flickerstreak@38 379
flickerstreak@81 380 function ReAction:ShowEditor(bar, ...)
flickerstreak@81 381 CallModuleMethod("ConfigUI","LaunchBarEditor",bar, ...)
flickerstreak@47 382 end
flickerstreak@88 383
flickerstreak@88 384 function ReAction:SetKeybindMode( mode )
flickerstreak@88 385 if mode ~= private.kbMode then
flickerstreak@88 386 if mode then
flickerstreak@88 387 KB:Activate()
flickerstreak@88 388 else
flickerstreak@88 389 KB:Deactivate()
flickerstreak@88 390 end
flickerstreak@88 391 private.kbMode = KB:IsShown() or false
flickerstreak@88 392 end
flickerstreak@88 393 end
flickerstreak@88 394
flickerstreak@88 395 function ReAction:GetKeybindMode( mode )
flickerstreak@88 396 return private.kbMode
flickerstreak@88 397 end