annotate ReAction.lua @ 194:55af1ebbec65

Remove silly 'close this window when you open the other one' checkboxes
author Flick <flickerstreak@gmail.com>
date Mon, 15 Nov 2010 10:30:19 -0800
parents 2e7a322e0195
children c7be637a47bc
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@63 7 - bar-type constructors
flickerstreak@63 8
flickerstreak@90 9 and publishes events when those collections change. It also implements a couple properties
flickerstreak@63 10 and has a couple convenience methods which drill down to particular modules.
flickerstreak@63 11
flickerstreak@90 12 Most of the "real work" of the addon happens in Bar.lua, Overlay.lua, State.lua, and the various modules.
flickerstreak@63 13
flickerstreak@63 14 Events (with handler arguments):
flickerstreak@63 15 --------------------------------
flickerstreak@63 16 "OnCreateBar" (bar, name) : after a bar object is created
flickerstreak@63 17 "OnDestroyBar" (bar, name) : before a bar object is destroyed
flickerstreak@63 18 "OnEraseBar" (bar, name) : before a bar config is removed from the profile db
flickerstreak@63 19 "OnRenameBar" (bar, oldname, newname) : after a bar is renamed
flickerstreak@63 20 "OnRefreshBar" (bar, name) : after a bar's state has been updated
flickerstreak@63 21 "OnConfigModeChanged" (mode) : after the config mode is changed
flickerstreak@63 22
flickerstreak@63 23 ReAction is also an AceAddon-3.0 and contains an AceDB-3.0, which in turn publish more events.
flickerstreak@63 24 ]]--
flickerstreak@185 25 local _, addonTable = ...
flickerstreak@185 26 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction",
flickerstreak@33 27 "AceEvent-3.0"
flickerstreak@30 28 )
flickerstreak@185 29 ReAction.version = "1.0"
flickerstreak@175 30 addonTable.ReAction = ReAction
flickerstreak@27 31
flickerstreak@33 32 ------ LIBRARIES ------
flickerstreak@63 33 local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction)
flickerstreak@184 34 local LKB = LibStub("LibKeyBound-1.0")
flickerstreak@33 35 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction")
flickerstreak@33 36 ReAction.L = L
flickerstreak@184 37 ReAction.LKB = LKB
flickerstreak@182 38 ReAction.callbacks = callbacks
flickerstreak@33 39
flickerstreak@28 40 ------ PRIVATE ------
flickerstreak@116 41 local private = { }
flickerstreak@63 42 local bars = {}
flickerstreak@63 43 local defaultBarConfig = {}
flickerstreak@63 44
flickerstreak@116 45
flickerstreak@185 46 local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy
flickerstreak@28 47 do
flickerstreak@28 48 local pcall = pcall
flickerstreak@28 49 local geterrorhandler = geterrorhandler
flickerstreak@63 50 local self = ReAction
flickerstreak@63 51 local inited = false
flickerstreak@28 52
flickerstreak@63 53 function SelectBar(x)
flickerstreak@28 54 local bar, name
flickerstreak@28 55 if type(x) == "string" then
flickerstreak@28 56 name = x
flickerstreak@63 57 bar = self:GetBar(name)
flickerstreak@50 58 else
flickerstreak@63 59 for k,v in pairs(bars) do
flickerstreak@50 60 if v == x then
flickerstreak@28 61 name = k
flickerstreak@50 62 bar = x
flickerstreak@28 63 end
flickerstreak@28 64 end
flickerstreak@28 65 end
flickerstreak@28 66 return bar, name
flickerstreak@28 67 end
flickerstreak@28 68
flickerstreak@63 69 function DestroyBar(x)
flickerstreak@28 70 local bar, name = SelectBar(x)
flickerstreak@63 71 if bar and name then
flickerstreak@63 72 bars[name] = nil
flickerstreak@63 73 callbacks:Fire("OnDestroyBar", bar, name)
flickerstreak@28 74 bar:Destroy()
flickerstreak@28 75 end
flickerstreak@28 76 end
flickerstreak@28 77
flickerstreak@63 78 function InitializeBars()
flickerstreak@63 79 if not inited then
flickerstreak@63 80 for name, config in pairs(self.db.profile.bars) do
flickerstreak@28 81 if config then
flickerstreak@63 82 self:CreateBar(name, config)
flickerstreak@28 83 end
flickerstreak@28 84 end
flickerstreak@101 85 -- re-anchor and refresh in case anchor order does not match init order
flickerstreak@63 86 for name, bar in pairs(bars) do
flickerstreak@63 87 bar:ApplyAnchor()
flickerstreak@101 88 callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@63 89 end
flickerstreak@63 90 inited = true
flickerstreak@28 91 end
flickerstreak@28 92 end
flickerstreak@28 93
flickerstreak@63 94 function TearDownBars()
flickerstreak@63 95 for name, bar in pairs(bars) do
flickerstreak@28 96 if bar then
flickerstreak@63 97 bars[name] = DestroyBar(bar)
flickerstreak@28 98 end
flickerstreak@28 99 end
flickerstreak@63 100 inited = false
flickerstreak@28 101 end
flickerstreak@28 102
flickerstreak@63 103 function DeepCopy(x)
flickerstreak@28 104 if type(x) ~= "table" then
flickerstreak@28 105 return x
flickerstreak@28 106 end
flickerstreak@28 107 local r = {}
flickerstreak@28 108 for k,v in pairs(x) do
flickerstreak@28 109 r[k] = DeepCopy(v)
flickerstreak@28 110 end
flickerstreak@28 111 return r
flickerstreak@28 112 end
flickerstreak@28 113
flickerstreak@28 114 end
flickerstreak@28 115
flickerstreak@28 116
flickerstreak@28 117 ------ HANDLERS ------
flickerstreak@28 118 function ReAction:OnInitialize()
flickerstreak@28 119 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
flickerstreak@28 120 {
flickerstreak@28 121 profile = {
flickerstreak@28 122 bars = { },
flickerstreak@184 123 defaultBar = { },
flickerstreak@28 124 }
flickerstreak@111 125 },
flickerstreak@182 126 true -- use global 'Default' (locale-specific)
flickerstreak@28 127 )
flickerstreak@184 128 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED")
flickerstreak@184 129 LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED")
flickerstreak@88 130
flickerstreak@182 131 self:RegisterEvent("PLAYER_REGEN_DISABLED")
flickerstreak@63 132
flickerstreak@182 133 self:InitializeOptions()
flickerstreak@28 134 end
flickerstreak@28 135
flickerstreak@28 136 function ReAction:OnEnable()
flickerstreak@28 137 InitializeBars()
flickerstreak@28 138 end
flickerstreak@28 139
flickerstreak@28 140 function ReAction:OnDisable()
flickerstreak@28 141 TearDownBars()
flickerstreak@28 142 end
flickerstreak@28 143
flickerstreak@33 144 function ReAction:PLAYER_REGEN_DISABLED()
flickerstreak@63 145 if private.configMode == true then
flickerstreak@63 146 self:UserError(L["ReAction config mode disabled during combat."])
flickerstreak@33 147 self:SetConfigMode(false)
flickerstreak@88 148 self:SetKeybindMode(false)
flickerstreak@185 149 self:CloseEditor()
flickerstreak@33 150 end
flickerstreak@33 151 end
flickerstreak@33 152
flickerstreak@88 153 function ReAction:LIBKEYBOUND_ENABLED( evt )
flickerstreak@88 154 self:SetKeybindMode(true)
flickerstreak@88 155 end
flickerstreak@88 156
flickerstreak@88 157 function ReAction:LIBKEYBOUND_DISABLED( evt )
flickerstreak@88 158 return self:SetKeybindMode(false)
flickerstreak@88 159 end
flickerstreak@88 160
flickerstreak@33 161
flickerstreak@28 162
flickerstreak@28 163 ------ API ------
flickerstreak@77 164
flickerstreak@61 165 function ReAction:UserError(msg)
flickerstreak@61 166 -- any user errors should be flashed to the UIErrorsFrame
flickerstreak@61 167 UIErrorsFrame:AddMessage(msg)
flickerstreak@61 168 end
flickerstreak@61 169
flickerstreak@184 170 function ReAction:RebuildAll()
flickerstreak@184 171 TearDownBars()
flickerstreak@184 172 InitializeBars()
flickerstreak@184 173 end
flickerstreak@184 174
flickerstreak@184 175
flickerstreak@63 176 -- usage:
flickerstreak@91 177 -- (1) ReAction:CreateBar(name, [cfgTable])
flickerstreak@63 178 -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing])
flickerstreak@91 179 function ReAction:CreateBar(name, config, ...)
flickerstreak@91 180 local profile = self.db.profile
flickerstreak@91 181
flickerstreak@127 182 if name then
flickerstreak@127 183 if bars[name] then
flickerstreak@127 184 self:UserError(format(L["ReAction: name '%s' already in use"],name))
flickerstreak@127 185 return nil
flickerstreak@127 186 end
flickerstreak@127 187 else
flickerstreak@91 188 local prefix = L["Bar "]
flickerstreak@91 189 local i = 1
flickerstreak@91 190 repeat
flickerstreak@91 191 name = prefix..i
flickerstreak@91 192 i = i + 1
flickerstreak@91 193 until bars[name] == nil
flickerstreak@91 194 end
flickerstreak@91 195
flickerstreak@91 196 if type(config) == "string" then
flickerstreak@91 197 config = defaultBarConfig[config]
flickerstreak@48 198 if not config then
flickerstreak@91 199 error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...))))
flickerstreak@48 200 end
flickerstreak@48 201 config = DeepCopy(config)
flickerstreak@91 202 config.btnRows = select(1,...) or config.btnRows or 1
flickerstreak@91 203 config.btnColumns = select(2,...) or config.btnColumns or 12
flickerstreak@91 204 config.btnWidth = select(3,...) or config.btnWidth or 36
flickerstreak@91 205 config.btnHeight = select(3,...) or config.btnHeight or 36
flickerstreak@91 206 config.spacing = select(4,...) or config.spacing or 3
flickerstreak@48 207 config.width = config.width or config.btnColumns*(config.btnWidth + config.spacing) + 1
flickerstreak@48 208 config.height = config.height or config.btnRows*(config.btnHeight + config.spacing) + 1
flickerstreak@81 209 config.anchor = config.anchor or "UIParent"
flickerstreak@81 210 config.point = config.point or "BOTTOM"
flickerstreak@81 211 config.relpoint = config.relpoint or "BOTTOM"
flickerstreak@48 212 config.y = config.y or 200
flickerstreak@48 213 config.x = config.x or 0
flickerstreak@48 214 end
flickerstreak@91 215 config = config or profile.bars[name] or DeepCopy(profile.defaultBar)
flickerstreak@91 216
flickerstreak@91 217 profile.bars[name] = config
flickerstreak@91 218 local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua
flickerstreak@63 219 bars[name] = bar
flickerstreak@63 220 callbacks:Fire("OnCreateBar", bar, name)
flickerstreak@63 221 if private.configMode then
flickerstreak@33 222 bar:ShowControls(true)
flickerstreak@33 223 end
flickerstreak@33 224
flickerstreak@28 225 return bar
flickerstreak@28 226 end
flickerstreak@28 227
flickerstreak@28 228 function ReAction:EraseBar(x)
flickerstreak@28 229 local bar, name = SelectBar(x)
flickerstreak@63 230 if bar and name then
flickerstreak@63 231 callbacks:Fire("OnEraseBar", bar, name)
flickerstreak@28 232 DestroyBar(bar)
flickerstreak@28 233 self.db.profile.bars[name] = nil
flickerstreak@28 234 end
flickerstreak@28 235 end
flickerstreak@28 236
flickerstreak@28 237 function ReAction:GetBar(name)
flickerstreak@63 238 return bars[name]
flickerstreak@63 239 end
flickerstreak@63 240
flickerstreak@90 241 -- returns pairs of name, bar
flickerstreak@63 242 function ReAction:IterateBars()
flickerstreak@63 243 return pairs(bars)
flickerstreak@28 244 end
flickerstreak@28 245
flickerstreak@28 246 function ReAction:RenameBar(x, newname)
flickerstreak@28 247 local bar, name = SelectBar(x)
flickerstreak@63 248 if type(newname) ~= "string" then
flickerstreak@63 249 error("ReAction:RenameBar() - second argument must be a string")
flickerstreak@63 250 end
flickerstreak@63 251 if bar and name and #newname > 0 then
flickerstreak@127 252 if newname == name then
flickerstreak@127 253 return
flickerstreak@127 254 end
flickerstreak@63 255 if bars[newname] then
flickerstreak@127 256 self:UserError(format(L["ReAction: name '%s' already in use"],newname))
flickerstreak@47 257 else
flickerstreak@63 258 bars[newname], bars[name] = bars[name], nil
flickerstreak@47 259 bar:SetName(newname or "")
flickerstreak@47 260 local cfg = self.db.profile.bars
flickerstreak@47 261 cfg[newname], cfg[name] = cfg[name], nil
flickerstreak@63 262 callbacks:Fire("OnRenameBar", bar, name, newname)
flickerstreak@28 263 end
flickerstreak@28 264 end
flickerstreak@28 265 end
flickerstreak@28 266
flickerstreak@63 267 function ReAction:RefreshBar(x)
flickerstreak@63 268 local bar, name = SelectBar(x)
flickerstreak@63 269 if bar and name then
flickerstreak@63 270 callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@63 271 end
flickerstreak@63 272 end
flickerstreak@63 273
flickerstreak@53 274 function ReAction:RegisterBarType( name, config, isDefaultChoice )
flickerstreak@63 275 defaultBarConfig[name] = config
flickerstreak@48 276 if isDefaultChoice then
flickerstreak@81 277 private.defaultBarConfigChoice = name
flickerstreak@48 278 end
flickerstreak@185 279 self:RefreshEditor()
flickerstreak@48 280 end
flickerstreak@48 281
flickerstreak@53 282 function ReAction:UnregisterBarType( name )
flickerstreak@63 283 defaultBarConfig[name] = nil
flickerstreak@63 284 if private.defaultBarConfigChoice == name then
flickerstreak@63 285 private.defaultBarConfigChoice = nil
flickerstreak@48 286 end
flickerstreak@185 287 self:RefreshEditor()
flickerstreak@48 288 end
flickerstreak@48 289
flickerstreak@63 290 function ReAction:IterateBarTypes()
flickerstreak@63 291 return pairs(defaultBarConfig)
flickerstreak@63 292 end
flickerstreak@63 293
flickerstreak@63 294 function ReAction:GetBarTypeConfig(name)
flickerstreak@63 295 if name then
flickerstreak@63 296 return defaultBarConfig[name]
flickerstreak@63 297 end
flickerstreak@63 298 end
flickerstreak@63 299
flickerstreak@63 300 function ReAction:GetBarTypeOptions( fill )
flickerstreak@63 301 fill = fill or { }
flickerstreak@63 302 for k in self:IterateBarTypes() do
flickerstreak@63 303 fill[k] = k
flickerstreak@63 304 end
flickerstreak@63 305 return fill
flickerstreak@63 306 end
flickerstreak@63 307
flickerstreak@63 308 function ReAction:GetDefaultBarType()
flickerstreak@63 309 return private.defaultBarConfigChoice
flickerstreak@63 310 end
flickerstreak@63 311
flickerstreak@33 312 function ReAction:SetConfigMode( mode )
flickerstreak@77 313 if mode ~= private.configMode then
flickerstreak@77 314 private.configMode = mode
flickerstreak@77 315 callbacks:Fire("OnConfigModeChanged", mode)
flickerstreak@77 316 end
flickerstreak@63 317 end
flickerstreak@63 318
flickerstreak@63 319 function ReAction:GetConfigMode()
flickerstreak@63 320 return private.configMode
flickerstreak@33 321 end
flickerstreak@38 322
flickerstreak@88 323 function ReAction:SetKeybindMode( mode )
flickerstreak@88 324 if mode ~= private.kbMode then
flickerstreak@88 325 if mode then
flickerstreak@184 326 LKB:Activate()
flickerstreak@88 327 else
flickerstreak@184 328 LKB:Deactivate()
flickerstreak@88 329 end
flickerstreak@184 330 private.kbMode = LKB:IsShown() or false
flickerstreak@88 331 end
flickerstreak@88 332 end
flickerstreak@88 333
flickerstreak@88 334 function ReAction:GetKeybindMode( mode )
flickerstreak@88 335 return private.kbMode
flickerstreak@88 336 end