annotate ReAction.lua @ 30:0d95ce7a9ec2

- added Ace3 externs - converted ReAction_ConfigUI to use blizzard interface addons panel via AceConfigDialog-3.0 - partially converted FuBar module to LibRock, deprecated it (going to remove it entirely later) - cleaned up a couple other tidbits
author Flick <flickerstreak@gmail.com>
date Wed, 02 Apr 2008 23:31:13 +0000
parents 21bcaf8215ff
children c54c481ad0ed
rev   line source
flickerstreak@28 1 -- ReAction.lua
flickerstreak@28 2 -- See modules/ReAction_ModuleTemplate for Module API listing
flickerstreak@28 3 -- See Bar.lua for Bar object listing
flickerstreak@30 4 local MAJOR_VERSION = GetAddOnMetadata("ReAction","Version")
flickerstreak@25 5
flickerstreak@28 6 ------ LIBRARIES ------
flickerstreak@28 7 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction")
flickerstreak@27 8
flickerstreak@27 9 ------ CORE ------
flickerstreak@30 10 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction",
flickerstreak@30 11 "AceConsole-3.0"
flickerstreak@30 12 )
flickerstreak@27 13 ReAction.revision = tonumber(("$Revision: 1 $"):match("%d+"))
flickerstreak@30 14 ReAction.version = MAJOR_VERSION
flickerstreak@27 15 ReAction.L = L
flickerstreak@27 16
flickerstreak@28 17 ------ GLOBALS ------
flickerstreak@28 18 _G["ReAction"] = ReAction
flickerstreak@27 19
flickerstreak@28 20 ------ DEBUGGING ------
flickerstreak@25 21 ReAction.debug = true
flickerstreak@25 22 if ReAction.debug then
flickerstreak@25 23 ReAction.print = function(msg)
flickerstreak@25 24 DEFAULT_CHAT_FRAME:AddMessage(msg)
flickerstreak@25 25 end
flickerstreak@25 26 --seterrorhandler(ReAction.print)
flickerstreak@25 27 else
flickerstreak@25 28 ReAction.print = function() end
flickerstreak@25 29 end
flickerstreak@25 30
flickerstreak@28 31 ------ PRIVATE ------
flickerstreak@30 32 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, SafeCall, CheckMethod, SlashHandler
flickerstreak@28 33 do
flickerstreak@28 34 local pcall = pcall
flickerstreak@28 35 local geterrorhandler = geterrorhandler
flickerstreak@28 36
flickerstreak@28 37 SelectBar = function(x)
flickerstreak@28 38 local bar, name
flickerstreak@28 39 if type(x) == "string" then
flickerstreak@28 40 name = x
flickerstreak@28 41 bar = ReAction:GetBar(name)
flickerstreak@28 42 elseif ReAction.Bar:IsInstance(x) then
flickerstreak@28 43 bar = x
flickerstreak@28 44 for k,v in pairs(ReAction.bars) do
flickerstreak@28 45 if v == bar then
flickerstreak@28 46 name = k
flickerstreak@28 47 end
flickerstreak@28 48 end
flickerstreak@28 49 else
flickerstreak@28 50 error("bad argument to SelectBar")
flickerstreak@28 51 end
flickerstreak@28 52 return bar, name
flickerstreak@28 53 end
flickerstreak@28 54
flickerstreak@28 55 DestroyBar = function(x)
flickerstreak@28 56 local bar, name = SelectBar(x)
flickerstreak@28 57 if name and bar then
flickerstreak@28 58 ReAction.bars[name] = nil
flickerstreak@28 59 ReAction:CallMethodOnAllModules("RemoveFromBar", bar)
flickerstreak@28 60 bar:Destroy()
flickerstreak@28 61 end
flickerstreak@28 62 end
flickerstreak@28 63
flickerstreak@28 64 InitializeBars = function ()
flickerstreak@28 65 if not(ReAction.inited) then
flickerstreak@28 66 for name, config in pairs(ReAction.db.profile.bars) do
flickerstreak@28 67 if config then
flickerstreak@28 68 ReAction:CreateBar(name, config)
flickerstreak@28 69 end
flickerstreak@28 70 end
flickerstreak@28 71 ReAction:CallMethodOnAllBars("ApplyAnchor") -- re-anchor in the case of oddball ordering
flickerstreak@28 72 ReAction.inited = true
flickerstreak@28 73 end
flickerstreak@28 74 end
flickerstreak@28 75
flickerstreak@28 76 TearDownBars = function()
flickerstreak@28 77 for name, bar in pairs(ReAction.bars) do
flickerstreak@28 78 if bar then
flickerstreak@28 79 ReAction.bars[name] = DestroyBar(bar)
flickerstreak@28 80 end
flickerstreak@28 81 end
flickerstreak@28 82 ReAction.inited = false
flickerstreak@28 83 end
flickerstreak@28 84
flickerstreak@28 85 DeepCopy = function(x)
flickerstreak@28 86 if type(x) ~= "table" then
flickerstreak@28 87 return x
flickerstreak@28 88 end
flickerstreak@28 89 local r = {}
flickerstreak@28 90 for k,v in pairs(x) do
flickerstreak@28 91 r[k] = DeepCopy(v)
flickerstreak@28 92 end
flickerstreak@28 93 return r
flickerstreak@28 94 end
flickerstreak@28 95
flickerstreak@28 96 SafeCall = function(f, ...)
flickerstreak@28 97 if f then
flickerstreak@28 98 local success, err = pcall(f,...)
flickerstreak@28 99 if not success then
flickerstreak@28 100 geterrorhandler()(err)
flickerstreak@28 101 end
flickerstreak@28 102 end
flickerstreak@28 103 end
flickerstreak@28 104
flickerstreak@28 105 CheckMethod = function(m)
flickerstreak@28 106 if type(m) == "function" then
flickerstreak@28 107 return m
flickerstreak@28 108 end
flickerstreak@28 109 if type(m) ~= "string" then
flickerstreak@28 110 error("Invalid method")
flickerstreak@28 111 end
flickerstreak@28 112 end
flickerstreak@30 113
flickerstreak@30 114 local function CallOptsModule(method,...)
flickerstreak@30 115 local m = ReAction:GetModule("ConfigUI",true)
flickerstreak@30 116 if not m then
flickerstreak@30 117 LoadAddOn("ReAction_ConfigUI")
flickerstreak@30 118 m = ReAction:GetModule("ConfigUI")
flickerstreak@30 119 end
flickerstreak@30 120 if m and type(m) == "table" and type(m[method]) == "function" then
flickerstreak@30 121 m[method](m,...)
flickerstreak@30 122 else
flickerstreak@30 123 ReAction:Print("Options module not found")
flickerstreak@30 124 end
flickerstreak@30 125 end
flickerstreak@30 126
flickerstreak@30 127 SlashHandler = function(option)
flickerstreak@30 128 if option == "config" then
flickerstreak@30 129 CallOptsModule("OpenConfig",true)
flickerstreak@30 130 elseif option == "unlock" then
flickerstreak@30 131 CallOptsModule("SetConfigMode",true)
flickerstreak@30 132 elseif option == "lock" then
flickerstreak@30 133 CallOptsModule("SetConfigMode",false)
flickerstreak@30 134 else
flickerstreak@30 135 ReAction:Print(ReAction.version)
flickerstreak@30 136 ReAction:Print("/reaction config")
flickerstreak@30 137 ReAction:Print("/reaction unlock")
flickerstreak@30 138 ReAction:Print("/reaction lock")
flickerstreak@30 139 end
flickerstreak@30 140 end
flickerstreak@28 141 end
flickerstreak@28 142
flickerstreak@28 143
flickerstreak@28 144 ------ HANDLERS ------
flickerstreak@28 145 function ReAction:OnInitialize()
flickerstreak@28 146 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
flickerstreak@28 147 {
flickerstreak@28 148 profile = {
flickerstreak@28 149 bars = { },
flickerstreak@28 150 defaultBar = { }
flickerstreak@28 151 }
flickerstreak@28 152 }
flickerstreak@28 153 -- default profile is character-specific
flickerstreak@28 154 )
flickerstreak@28 155 self.db.RegisterCallback(self,"OnProfileChanged")
flickerstreak@30 156 self.callbacks = LibStub("CallbackHandler-1.0"):New(self)
flickerstreak@30 157 self:RegisterChatCommand("reaction", SlashHandler)
flickerstreak@30 158 self:RegisterChatCommand("rxn", SlashHandler)
flickerstreak@28 159
flickerstreak@28 160 self.bars = {}
flickerstreak@28 161 end
flickerstreak@28 162
flickerstreak@28 163 function ReAction:OnEnable()
flickerstreak@28 164 InitializeBars()
flickerstreak@28 165 end
flickerstreak@28 166
flickerstreak@28 167 function ReAction:OnDisable()
flickerstreak@28 168 TearDownBars()
flickerstreak@28 169 end
flickerstreak@28 170
flickerstreak@28 171 function ReAction:OnProfileChanged()
flickerstreak@28 172 self.TearDownBars()
flickerstreak@28 173 self.InitializeBars()
flickerstreak@28 174 end
flickerstreak@28 175
flickerstreak@28 176 function ReAction:OnModuleEnable(module)
flickerstreak@28 177 if module.ApplyToBar then
flickerstreak@28 178 for _, b in pairs(bars) do
flickerstreak@28 179 if b then
flickerstreak@28 180 module:ApplyToBar(b)
flickerstreak@28 181 end
flickerstreak@28 182 end
flickerstreak@28 183 end
flickerstreak@28 184 end
flickerstreak@28 185
flickerstreak@28 186 function ReAction:OnModuleDisable(module)
flickerstreak@28 187 if module.RemoveFromBar then
flickerstreak@28 188 for _, b in pairs(bars) do
flickerstreak@28 189 if b then
flickerstreak@28 190 module:RemoveFromBar(b)
flickerstreak@28 191 end
flickerstreak@28 192 end
flickerstreak@28 193 end
flickerstreak@28 194 end
flickerstreak@28 195
flickerstreak@28 196
flickerstreak@28 197 ------ API ------
flickerstreak@28 198 function ReAction:CallMethodOnAllModules(method, ...)
flickerstreak@28 199 local m = CheckMethod(method)
flickerstreak@28 200 for _, x in self:IterateModules() do
flickerstreak@28 201 if x then
flickerstreak@28 202 SafeCall(m or x[method], x, ...)
flickerstreak@28 203 end
flickerstreak@28 204 end
flickerstreak@28 205 end
flickerstreak@28 206
flickerstreak@28 207 function ReAction:CallMethodOnAllBars(method,...)
flickerstreak@28 208 local m = CheckMethod(method)
flickerstreak@28 209 for _, x in pairs(self.bars) do
flickerstreak@28 210 if x then
flickerstreak@28 211 SafeCall(m or x[method], x, ...)
flickerstreak@28 212 end
flickerstreak@28 213 end
flickerstreak@28 214 end
flickerstreak@28 215
flickerstreak@28 216 function ReAction:CreateBar(name, defaultConfig, prefix)
flickerstreak@28 217 local profile = self.db.profile
flickerstreak@28 218 defaultConfig = defaultConfig or profile.defaultBar
flickerstreak@28 219 prefix = prefix or L["Bar "]
flickerstreak@28 220 if not name then
flickerstreak@28 221 i = 1
flickerstreak@28 222 repeat
flickerstreak@28 223 name = prefix..i
flickerstreak@28 224 i = i + 1
flickerstreak@28 225 until self.bars[name] == nil
flickerstreak@28 226 end
flickerstreak@28 227 profile.bars[name] = profile.bars[name] or DeepCopy(defaultConfig)
flickerstreak@28 228 local bar = self.Bar:new( name, profile.bars[name] ) -- ReAction.Bar defined in Bar.lua
flickerstreak@28 229 self:CallMethodOnAllModules("ApplyToBar", bar)
flickerstreak@28 230 self.bars[name] = bar
flickerstreak@30 231 self.callbacks:Fire("OnCreateBar", bar)
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@28 237 if name and bar then
flickerstreak@28 238 DestroyBar(bar)
flickerstreak@28 239 self.db.profile.bars[name] = nil
flickerstreak@28 240 self:CallMethodOnAllModules("EraseBarConfig", name)
flickerstreak@30 241 self.callbacks:Fire("OnEraseBar",name)
flickerstreak@28 242 end
flickerstreak@28 243 end
flickerstreak@28 244
flickerstreak@28 245 function ReAction:GetBar(name)
flickerstreak@28 246 return self.bars[name]
flickerstreak@28 247 end
flickerstreak@28 248
flickerstreak@28 249 function ReAction:RenameBar(x, newname)
flickerstreak@28 250 local bar, name = SelectBar(x)
flickerstreak@28 251 if bar and name and newname then
flickerstreak@28 252 if self.bars[newname] then
flickerstreak@28 253 error(L["ReAction: name already in use"])
flickerstreak@28 254 end
flickerstreak@28 255 self.bars[newname] = self.bars[name]
flickerstreak@28 256 self.bars[name] = nil
flickerstreak@28 257 bar.name = newname or ""
flickerstreak@28 258 local cfg = self.db.profile.bars
flickerstreak@28 259 cfg[newname], cfg[name] = cfg[name], nil
flickerstreak@28 260 self:CallMethodOnAllModules("RenameBarConfig", name, newname)
flickerstreak@30 261 self.callbacks:Fire("OnRenameBar", name, newname)
flickerstreak@28 262 end
flickerstreak@28 263 end
flickerstreak@28 264
flickerstreak@30 265 ReAction.options = {}
flickerstreak@30 266 -- See modules/ReAction_ConfigUI for valid options contexts.
flickerstreak@30 267 function ReAction:RegisterOptions(context, module, opts)
flickerstreak@30 268 if module == nil or context == nil then
flickerstreak@30 269 error("ReAction:RegisterOptions requires a module object and context ID")
flickerstreak@30 270 end
flickerstreak@30 271 if not self.options[context] then
flickerstreak@30 272 self.options[context] = {}
flickerstreak@30 273 end
flickerstreak@30 274 self.options[context][module] = opts
flickerstreak@30 275 self.callbacks:Fire("OnOptionsRegistered", context, module, opts)
flickerstreak@30 276 end
flickerstreak@28 277
flickerstreak@30 278 function ReAction:GetOptions(context)
flickerstreak@30 279 if context then
flickerstreak@30 280 if not self.options[context] then
flickerstreak@30 281 self.options[context] = { }
flickerstreak@30 282 end
flickerstreak@30 283 return self.options[context]
flickerstreak@30 284 end
flickerstreak@30 285 end
flickerstreak@30 286
flickerstreak@30 287 function ReAction:GetOptionContextList()
flickerstreak@30 288 local c = {}
flickerstreak@30 289 for k in self.options do
flickerstreak@30 290 tinsert(c,k)
flickerstreak@30 291 end
flickerstreak@30 292 return c
flickerstreak@30 293 end
flickerstreak@30 294
flickerstreak@30 295 function ReAction:RefreshOptions()
flickerstreak@30 296 self.callbacks:Fire("OnOptionsRefreshed")
flickerstreak@30 297 end