annotate ReAction.lua @ 211:97949dbe987f

Demodularize HideBlizzard - added framework for updating profile database - removed unused profile.defaultBars[]
author Flick <flickerstreak@gmail.com>
date Thu, 18 Nov 2010 12:59:00 -0800
parents c24ac8ee1e45
children e275a8663a16
rev   line source
flickerstreak@201 1 local addonName, addonTable = ...
flickerstreak@205 2 local pcall = pcall
flickerstreak@205 3 local pairs = pairs
flickerstreak@205 4 local type = type
flickerstreak@205 5 local geterrorhandler = geterrorhandler
flickerstreak@184 6 local LKB = LibStub("LibKeyBound-1.0")
flickerstreak@33 7 local L = LibStub("AceLocale-3.0"):GetLocale("ReAction")
flickerstreak@33 8
flickerstreak@205 9 ------ Utility ------
flickerstreak@205 10 local tcopy
flickerstreak@28 11 do
flickerstreak@205 12 function tcopy(x)
flickerstreak@28 13 if type(x) ~= "table" then
flickerstreak@28 14 return x
flickerstreak@28 15 end
flickerstreak@28 16 local r = {}
flickerstreak@28 17 for k,v in pairs(x) do
flickerstreak@205 18 r[k] = tcopy(v)
flickerstreak@28 19 end
flickerstreak@28 20 return r
flickerstreak@28 21 end
flickerstreak@28 22 end
flickerstreak@28 23
flickerstreak@205 24 ------ Core ------
flickerstreak@205 25 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction",
flickerstreak@205 26 "AceEvent-3.0"
flickerstreak@205 27 )
flickerstreak@205 28 addonTable.ReAction = ReAction
flickerstreak@205 29 ReAction.version = "1.1"
flickerstreak@205 30 ReAction.L = L
flickerstreak@205 31 ReAction.LKB = LKB
flickerstreak@28 32
flickerstreak@205 33
flickerstreak@205 34 ------ Handlers ------
flickerstreak@28 35 function ReAction:OnInitialize()
flickerstreak@28 36 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
flickerstreak@28 37 {
flickerstreak@28 38 profile = {
flickerstreak@211 39 dbversion = nil,
flickerstreak@28 40 bars = { },
flickerstreak@211 41 options = {
flickerstreak@211 42 hideBlizzardBars = false,
flickerstreak@211 43 hideBlizzardVehicleBar = false,
flickerstreak@211 44 },
flickerstreak@211 45 },
flickerstreak@211 46 global = {
flickerstreak@211 47 skipKeybindWarning = false,
flickerstreak@28 48 }
flickerstreak@111 49 },
flickerstreak@182 50 true -- use global 'Default' (locale-specific)
flickerstreak@28 51 )
flickerstreak@205 52
flickerstreak@211 53 self:UpdateDB()
flickerstreak@211 54
flickerstreak@205 55 self.bars = { }
flickerstreak@205 56 self.defaultBarConfig = { }
flickerstreak@205 57
flickerstreak@211 58 -- It's fairly normal to use the Blizzard vehicle bar, and to have
flickerstreak@211 59 -- your regular buttons in the same location. If you do this, and don't
flickerstreak@211 60 -- bother to hide your buttons, they'll obscure some parts of the vehicle bar.
flickerstreak@211 61 VehicleMenuBar:SetFrameLevel(VehicleMenuBar:GetFrameLevel()+3)
flickerstreak@211 62
flickerstreak@205 63 self.callbacks = LibStub("CallbackHandler-1.0"):New(self)
flickerstreak@184 64 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED")
flickerstreak@184 65 LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED")
flickerstreak@207 66 LKB.RegisterCallback(self, "LIBKEYBOUND_MODE_COLOR_CHANGED","LIBKEYBOUND_ENABLED")
flickerstreak@182 67 self:RegisterEvent("PLAYER_REGEN_DISABLED")
flickerstreak@182 68 self:InitializeOptions()
flickerstreak@28 69 end
flickerstreak@28 70
flickerstreak@28 71 function ReAction:OnEnable()
flickerstreak@205 72 self:InitializeBars()
flickerstreak@28 73 end
flickerstreak@28 74
flickerstreak@28 75 function ReAction:OnDisable()
flickerstreak@205 76 self:TearDownBars()
flickerstreak@28 77 end
flickerstreak@28 78
flickerstreak@33 79 function ReAction:PLAYER_REGEN_DISABLED()
flickerstreak@205 80 if self.configMode == true then
flickerstreak@63 81 self:UserError(L["ReAction config mode disabled during combat."])
flickerstreak@33 82 self:SetConfigMode(false)
flickerstreak@88 83 self:SetKeybindMode(false)
flickerstreak@185 84 self:CloseEditor()
flickerstreak@33 85 end
flickerstreak@33 86 end
flickerstreak@33 87
flickerstreak@88 88 function ReAction:LIBKEYBOUND_ENABLED( evt )
flickerstreak@88 89 self:SetKeybindMode(true)
flickerstreak@88 90 end
flickerstreak@88 91
flickerstreak@88 92 function ReAction:LIBKEYBOUND_DISABLED( evt )
flickerstreak@88 93 return self:SetKeybindMode(false)
flickerstreak@88 94 end
flickerstreak@88 95
flickerstreak@33 96
flickerstreak@205 97 ------ Methods ------
flickerstreak@77 98
flickerstreak@61 99 function ReAction:UserError(msg)
flickerstreak@61 100 UIErrorsFrame:AddMessage(msg)
flickerstreak@61 101 end
flickerstreak@61 102
flickerstreak@205 103 function ReAction:GetBar(arg)
flickerstreak@205 104 if type(arg) == "string" then
flickerstreak@205 105 return self.bars[arg], arg
flickerstreak@205 106 elseif type(arg) == "table" then -- reverse lookup
flickerstreak@205 107 for name, bar in pairs(self.bars) do
flickerstreak@205 108 if arg == bar then
flickerstreak@205 109 return bar, name
flickerstreak@205 110 end
flickerstreak@205 111 end
flickerstreak@205 112 else
flickerstreak@205 113 error("ReAction:GetBar() requires either a name or a bar table arg")
flickerstreak@205 114 end
flickerstreak@184 115 end
flickerstreak@184 116
flickerstreak@205 117 function ReAction:IterateBars()
flickerstreak@205 118 return pairs(self.bars)
flickerstreak@205 119 end
flickerstreak@184 120
flickerstreak@63 121 -- usage:
flickerstreak@91 122 -- (1) ReAction:CreateBar(name, [cfgTable])
flickerstreak@63 123 -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing])
flickerstreak@91 124 function ReAction:CreateBar(name, config, ...)
flickerstreak@91 125 local profile = self.db.profile
flickerstreak@91 126
flickerstreak@127 127 if name then
flickerstreak@205 128 if self.bars[name] then
flickerstreak@127 129 self:UserError(format(L["ReAction: name '%s' already in use"],name))
flickerstreak@127 130 return nil
flickerstreak@127 131 end
flickerstreak@127 132 else
flickerstreak@91 133 local prefix = L["Bar "]
flickerstreak@91 134 local i = 1
flickerstreak@91 135 repeat
flickerstreak@91 136 name = prefix..i
flickerstreak@91 137 i = i + 1
flickerstreak@205 138 until self.bars[name] == nil
flickerstreak@91 139 end
flickerstreak@91 140
flickerstreak@91 141 if type(config) == "string" then
flickerstreak@205 142 config = self.defaultBarConfig[config]
flickerstreak@48 143 if not config then
flickerstreak@91 144 error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...))))
flickerstreak@48 145 end
flickerstreak@205 146 config = tcopy(config)
flickerstreak@91 147 config.btnRows = select(1,...) or config.btnRows or 1
flickerstreak@91 148 config.btnColumns = select(2,...) or config.btnColumns or 12
flickerstreak@91 149 config.btnWidth = select(3,...) or config.btnWidth or 36
flickerstreak@91 150 config.btnHeight = select(3,...) or config.btnHeight or 36
flickerstreak@91 151 config.spacing = select(4,...) or config.spacing or 3
flickerstreak@48 152 config.width = config.width or config.btnColumns*(config.btnWidth + config.spacing) + 1
flickerstreak@48 153 config.height = config.height or config.btnRows*(config.btnHeight + config.spacing) + 1
flickerstreak@81 154 config.anchor = config.anchor or "UIParent"
flickerstreak@81 155 config.point = config.point or "BOTTOM"
flickerstreak@81 156 config.relpoint = config.relpoint or "BOTTOM"
flickerstreak@48 157 config.y = config.y or 200
flickerstreak@48 158 config.x = config.x or 0
flickerstreak@48 159 end
flickerstreak@211 160 config = config or profile.bars[name] or { }
flickerstreak@91 161
flickerstreak@91 162 profile.bars[name] = config
flickerstreak@91 163 local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua
flickerstreak@205 164 self.bars[name] = bar
flickerstreak@205 165 self.callbacks:Fire("OnCreateBar", bar, name)
flickerstreak@205 166 if self.configMode then
flickerstreak@33 167 bar:ShowControls(true)
flickerstreak@33 168 end
flickerstreak@33 169
flickerstreak@28 170 return bar
flickerstreak@28 171 end
flickerstreak@28 172
flickerstreak@205 173 function ReAction:DestroyBar(x)
flickerstreak@205 174 local bar, name = self:GetBar(x)
flickerstreak@63 175 if bar and name then
flickerstreak@205 176 self.bars[name] = nil
flickerstreak@205 177 self.callbacks:Fire("OnDestroyBar", bar, name)
flickerstreak@205 178 bar:Destroy()
flickerstreak@28 179 end
flickerstreak@28 180 end
flickerstreak@28 181
flickerstreak@205 182 function ReAction:RefreshBar(x)
flickerstreak@205 183 local bar, name = self:GetBar(x)
flickerstreak@205 184 if bar and name then
flickerstreak@205 185 self.callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@205 186 end
flickerstreak@63 187 end
flickerstreak@63 188
flickerstreak@205 189 function ReAction:InitializeBars()
flickerstreak@205 190 if not self.barsInitialized then
flickerstreak@211 191 self:ManageBlizzardBars()
flickerstreak@211 192
flickerstreak@205 193 for name, config in pairs(self.db.profile.bars) do
flickerstreak@205 194 if config then
flickerstreak@205 195 self:CreateBar(name, config)
flickerstreak@205 196 end
flickerstreak@205 197 end
flickerstreak@205 198 -- re-anchor and refresh in case anchor order does not match init order
flickerstreak@205 199 for name, bar in pairs(self.bars) do
flickerstreak@205 200 bar:ApplyAnchor()
flickerstreak@205 201 self.callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@205 202 end
flickerstreak@205 203 self.barsInitialized = true
flickerstreak@205 204 end
flickerstreak@205 205 end
flickerstreak@205 206
flickerstreak@205 207 function ReAction:TearDownBars()
flickerstreak@205 208 for name, bar in pairs(self.bars) do
flickerstreak@205 209 if bar then
flickerstreak@205 210 self.bars[name] = DestroyBar(bar)
flickerstreak@205 211 end
flickerstreak@205 212 end
flickerstreak@205 213 self.barsInitialized = false
flickerstreak@205 214 end
flickerstreak@205 215
flickerstreak@205 216 function ReAction:RebuildAll()
flickerstreak@205 217 self:TearDownBars()
flickerstreak@205 218 self:InitializeBars()
flickerstreak@28 219 end
flickerstreak@28 220
flickerstreak@28 221 function ReAction:RenameBar(x, newname)
flickerstreak@205 222 local bar, name = self:GetBar(x)
flickerstreak@63 223 if type(newname) ~= "string" then
flickerstreak@63 224 error("ReAction:RenameBar() - second argument must be a string")
flickerstreak@63 225 end
flickerstreak@63 226 if bar and name and #newname > 0 then
flickerstreak@127 227 if newname == name then
flickerstreak@127 228 return
flickerstreak@127 229 end
flickerstreak@205 230 if self.bars[newname] then
flickerstreak@127 231 self:UserError(format(L["ReAction: name '%s' already in use"],newname))
flickerstreak@47 232 else
flickerstreak@205 233 self.bars[newname], self.bars[name] = self.bars[name], nil
flickerstreak@47 234 bar:SetName(newname or "")
flickerstreak@47 235 local cfg = self.db.profile.bars
flickerstreak@47 236 cfg[newname], cfg[name] = cfg[name], nil
flickerstreak@205 237 self.callbacks:Fire("OnRenameBar", bar, name, newname)
flickerstreak@28 238 end
flickerstreak@28 239 end
flickerstreak@28 240 end
flickerstreak@28 241
flickerstreak@205 242 function ReAction:EraseBar(x)
flickerstreak@205 243 local bar, name = self:GetBar(x)
flickerstreak@63 244 if bar and name then
flickerstreak@205 245 self.callbacks:Fire("OnEraseBar", bar, name)
flickerstreak@205 246 DestroyBar(bar)
flickerstreak@205 247 self.db.profile.bars[name] = nil
flickerstreak@63 248 end
flickerstreak@63 249 end
flickerstreak@63 250
flickerstreak@211 251 local blizzFrames = {
flickerstreak@211 252 MainMenuBar,
flickerstreak@211 253 MultiBarLeft,
flickerstreak@211 254 MultiBarRight,
flickerstreak@211 255 MultiBarBottomLeft,
flickerstreak@211 256 MultiBarBottomRight,
flickerstreak@211 257 }
flickerstreak@211 258
flickerstreak@211 259 local hideFrame = CreateFrame("Frame")
flickerstreak@211 260 hideFrame:Hide()
flickerstreak@211 261 local hiddenParents = { }
flickerstreak@211 262 local function ManageBlizzFrame(f, hide)
flickerstreak@211 263 if hide and not hiddenParents[f] then
flickerstreak@211 264 hiddenParents[f] = f:GetParent()
flickerstreak@211 265 f:SetParent(hideFrame)
flickerstreak@211 266 elseif not hide and hiddenParents[f] then
flickerstreak@211 267 f:SetParent(hiddenParents[f])
flickerstreak@211 268 hiddenParents[f] = nil
flickerstreak@211 269 if f:IsShown() then
flickerstreak@211 270 f:Show() -- refresh
flickerstreak@211 271 end
flickerstreak@211 272 end
flickerstreak@211 273 end
flickerstreak@211 274
flickerstreak@211 275 function ReAction:ManageBlizzardBars()
flickerstreak@211 276 for _, f in pairs(blizzFrames) do
flickerstreak@211 277 ManageBlizzFrame(f, self.db.profile.options.hideBlizzardBars)
flickerstreak@211 278 end
flickerstreak@211 279 ManageBlizzFrame(VehicleMenuBar, self.db.profile.options.hideBlizzardVehicleBar)
flickerstreak@211 280 end
flickerstreak@211 281
flickerstreak@53 282 function ReAction:RegisterBarType( name, config, isDefaultChoice )
flickerstreak@205 283 self.defaultBarConfig[name] = config
flickerstreak@48 284 if isDefaultChoice then
flickerstreak@205 285 self.defaultBarConfigChoice = name
flickerstreak@48 286 end
flickerstreak@185 287 self:RefreshEditor()
flickerstreak@48 288 end
flickerstreak@48 289
flickerstreak@53 290 function ReAction:UnregisterBarType( name )
flickerstreak@205 291 self.defaultBarConfig[name] = nil
flickerstreak@205 292 if self.defaultBarConfigChoice == name then
flickerstreak@205 293 self.defaultBarConfigChoice = nil
flickerstreak@48 294 end
flickerstreak@185 295 self:RefreshEditor()
flickerstreak@48 296 end
flickerstreak@48 297
flickerstreak@63 298 function ReAction:IterateBarTypes()
flickerstreak@205 299 return pairs(self.defaultBarConfig)
flickerstreak@63 300 end
flickerstreak@63 301
flickerstreak@63 302 function ReAction:GetBarTypeConfig(name)
flickerstreak@63 303 if name then
flickerstreak@205 304 return self.defaultBarConfig[name]
flickerstreak@63 305 end
flickerstreak@63 306 end
flickerstreak@63 307
flickerstreak@63 308 function ReAction:GetBarTypeOptions( fill )
flickerstreak@63 309 fill = fill or { }
flickerstreak@63 310 for k in self:IterateBarTypes() do
flickerstreak@63 311 fill[k] = k
flickerstreak@63 312 end
flickerstreak@63 313 return fill
flickerstreak@63 314 end
flickerstreak@63 315
flickerstreak@63 316 function ReAction:GetDefaultBarType()
flickerstreak@205 317 return self.defaultBarConfigChoice
flickerstreak@63 318 end
flickerstreak@63 319
flickerstreak@33 320 function ReAction:SetConfigMode( mode )
flickerstreak@205 321 if mode ~= self.configMode then
flickerstreak@207 322 if mode then
flickerstreak@207 323 self:SetKeybindMode(false)
flickerstreak@207 324 end
flickerstreak@205 325 self.configMode = mode
flickerstreak@205 326 self.callbacks:Fire("OnConfigModeChanged", mode)
flickerstreak@77 327 end
flickerstreak@63 328 end
flickerstreak@63 329
flickerstreak@63 330 function ReAction:GetConfigMode()
flickerstreak@205 331 return self.configMode
flickerstreak@33 332 end
flickerstreak@38 333
flickerstreak@88 334 function ReAction:SetKeybindMode( mode )
flickerstreak@205 335 if mode ~= self.kbMode then
flickerstreak@88 336 if mode then
flickerstreak@207 337 self:SetConfigMode(false)
flickerstreak@184 338 LKB:Activate()
flickerstreak@88 339 else
flickerstreak@184 340 LKB:Deactivate()
flickerstreak@88 341 end
flickerstreak@207 342 for _, bar in self:IterateBars() do
flickerstreak@207 343 bar:SetKeybindMode(mode)
flickerstreak@207 344 end
flickerstreak@205 345 self.kbMode = LKB:IsShown() or false
flickerstreak@88 346 end
flickerstreak@88 347 end
flickerstreak@88 348
flickerstreak@88 349 function ReAction:GetKeybindMode( mode )
flickerstreak@205 350 return self.kbMode
flickerstreak@88 351 end