annotate ReAction.lua @ 227:741c4f9b251e

Better handling of new profiles
author Flick
date Mon, 21 Mar 2011 11:10:23 -0700
parents 19fd228d9241
children 98114c158e62
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@223 34 ReAction.barTypes = { }
flickerstreak@223 35
flickerstreak@205 36 ------ Handlers ------
flickerstreak@28 37 function ReAction:OnInitialize()
flickerstreak@28 38 self.db = LibStub("AceDB-3.0"):New("ReAction_DB",
flickerstreak@213 39 self.defaultProfile,
flickerstreak@182 40 true -- use global 'Default' (locale-specific)
flickerstreak@28 41 )
flickerstreak@205 42
flickerstreak@213 43 self:UpgradeProfile()
flickerstreak@211 44
flickerstreak@205 45 self.bars = { }
flickerstreak@205 46
flickerstreak@213 47 self.LBF = LibStub("LibButtonFacade",true)
flickerstreak@213 48 if self.LBF then
flickerstreak@213 49 self.LBF:RegisterSkinCallback("ReAction", self.OnSkinChanged, self)
flickerstreak@213 50 end
flickerstreak@213 51
flickerstreak@211 52 -- It's fairly normal to use the Blizzard vehicle bar, and to have
flickerstreak@211 53 -- your regular buttons in the same location. If you do this, and don't
flickerstreak@211 54 -- bother to hide your buttons, they'll obscure some parts of the vehicle bar.
flickerstreak@211 55 VehicleMenuBar:SetFrameLevel(VehicleMenuBar:GetFrameLevel()+3)
flickerstreak@211 56
flickerstreak@205 57 self.callbacks = LibStub("CallbackHandler-1.0"):New(self)
Flick@227 58
flickerstreak@184 59 LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED")
flickerstreak@184 60 LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED")
flickerstreak@207 61 LKB.RegisterCallback(self, "LIBKEYBOUND_MODE_COLOR_CHANGED","LIBKEYBOUND_ENABLED")
Flick@227 62
Flick@227 63 -- see Profile.lua for these callback implementations
Flick@227 64 self.db.RegisterCallback(self,"OnProfileChanged")
Flick@227 65 self.db.RegisterCallback(self,"OnProfileCopied","OnProfileChanged")
Flick@227 66 self.db.RegisterCallback(self,"OnNewProfile")
Flick@227 67 self.db.RegisterCallback(self,"OnProfileReset", "OnNewProfile")
Flick@227 68
flickerstreak@182 69 self:RegisterEvent("PLAYER_REGEN_DISABLED")
Flick@227 70
flickerstreak@182 71 self:InitializeOptions()
flickerstreak@28 72 end
flickerstreak@28 73
flickerstreak@28 74 function ReAction:OnEnable()
flickerstreak@205 75 self:InitializeBars()
flickerstreak@28 76 end
flickerstreak@28 77
flickerstreak@28 78 function ReAction:OnDisable()
flickerstreak@205 79 self:TearDownBars()
flickerstreak@28 80 end
flickerstreak@28 81
flickerstreak@33 82 function ReAction:PLAYER_REGEN_DISABLED()
flickerstreak@205 83 if self.configMode == true then
flickerstreak@63 84 self:UserError(L["ReAction config mode disabled during combat."])
flickerstreak@33 85 self:SetConfigMode(false)
flickerstreak@88 86 self:SetKeybindMode(false)
flickerstreak@185 87 self:CloseEditor()
flickerstreak@33 88 end
flickerstreak@33 89 end
flickerstreak@33 90
flickerstreak@88 91 function ReAction:LIBKEYBOUND_ENABLED( evt )
flickerstreak@88 92 self:SetKeybindMode(true)
flickerstreak@88 93 end
flickerstreak@88 94
flickerstreak@88 95 function ReAction:LIBKEYBOUND_DISABLED( evt )
flickerstreak@88 96 return self:SetKeybindMode(false)
flickerstreak@88 97 end
flickerstreak@88 98
flickerstreak@213 99 function ReAction:OnSkinChanged( skinID, gloss, backdrop, group, button, colors )
flickerstreak@213 100 if group == nil then
flickerstreak@213 101 -- don't store global
flickerstreak@213 102 else
flickerstreak@213 103 -- 'group' is the bar-name
flickerstreak@213 104 local bar = self:GetBar(group)
flickerstreak@213 105 if bar then
flickerstreak@213 106 local c = bar:GetConfig().ButtonFacade
flickerstreak@213 107 if c then
flickerstreak@213 108 c.skinID = skinID
flickerstreak@213 109 c.gloss = gloss
flickerstreak@213 110 c.backdrop = backdrop
flickerstreak@213 111 c.colors = colors
flickerstreak@213 112 end
flickerstreak@213 113 end
flickerstreak@213 114 end
flickerstreak@213 115 end
flickerstreak@213 116
flickerstreak@33 117
flickerstreak@205 118 ------ Methods ------
flickerstreak@77 119
flickerstreak@61 120 function ReAction:UserError(msg)
flickerstreak@61 121 UIErrorsFrame:AddMessage(msg)
flickerstreak@61 122 end
flickerstreak@61 123
flickerstreak@205 124 function ReAction:GetBar(arg)
flickerstreak@205 125 if type(arg) == "string" then
flickerstreak@205 126 return self.bars[arg], arg
flickerstreak@205 127 elseif type(arg) == "table" then -- reverse lookup
flickerstreak@205 128 for name, bar in pairs(self.bars) do
flickerstreak@205 129 if arg == bar then
flickerstreak@205 130 return bar, name
flickerstreak@205 131 end
flickerstreak@205 132 end
flickerstreak@205 133 else
flickerstreak@205 134 error("ReAction:GetBar() requires either a name or a bar table arg")
flickerstreak@205 135 end
flickerstreak@184 136 end
flickerstreak@184 137
flickerstreak@205 138 function ReAction:IterateBars()
flickerstreak@205 139 return pairs(self.bars)
flickerstreak@205 140 end
flickerstreak@184 141
flickerstreak@63 142 -- usage:
flickerstreak@91 143 -- (1) ReAction:CreateBar(name, [cfgTable])
flickerstreak@63 144 -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing])
flickerstreak@91 145 function ReAction:CreateBar(name, config, ...)
flickerstreak@91 146 local profile = self.db.profile
flickerstreak@91 147
flickerstreak@217 148 name = tostring(name)
flickerstreak@217 149 if not name or name == "" then
flickerstreak@217 150 error("ReAction:CreateBar() - bar name string required")
flickerstreak@217 151 elseif self.bars[name] then
flickerstreak@217 152 self:UserError(format(L["ReAction: name '%s' already in use"],name))
flickerstreak@217 153 return nil
flickerstreak@91 154 end
flickerstreak@91 155
flickerstreak@223 156 local class
flickerstreak@91 157 if type(config) == "string" then
flickerstreak@223 158 class = self.barTypes[config]
flickerstreak@218 159 if not class then
flickerstreak@218 160 error(("ReAction:CreateBar() - unknown bar type '%s'"):format(config))
flickerstreak@48 161 end
flickerstreak@218 162 config = tcopy(class:GetDefaultBarConfig())
flickerstreak@218 163 config.btnRows = select(1,...) or config.btnRows
flickerstreak@218 164 config.btnColumns = select(2,...) or config.btnColumns
flickerstreak@218 165 config.btnWidth = select(3,...) or config.btnWidth
flickerstreak@218 166 config.btnHeight = select(3,...) or config.btnHeight
flickerstreak@218 167 config.spacing = select(4,...) or config.spacing
flickerstreak@48 168 config.width = config.width or config.btnColumns*(config.btnWidth + config.spacing) + 1
flickerstreak@48 169 config.height = config.height or config.btnRows*(config.btnHeight + config.spacing) + 1
flickerstreak@81 170 config.anchor = config.anchor or "UIParent"
flickerstreak@81 171 config.point = config.point or "BOTTOM"
flickerstreak@81 172 config.relpoint = config.relpoint or "BOTTOM"
flickerstreak@48 173 config.y = config.y or 200
flickerstreak@48 174 config.x = config.x or 0
flickerstreak@223 175 else
flickerstreak@223 176 config = config or profile.bars[name] or { }
flickerstreak@223 177 if not config or not config.type or not self.barTypes[config.type] then
flickerstreak@223 178 error(("ReAction: Unable to construct/fetch config table for bar '%s'"):format(name))
flickerstreak@223 179 end
flickerstreak@223 180 class = self.barTypes[config.type]
flickerstreak@48 181 end
flickerstreak@91 182
flickerstreak@91 183 profile.bars[name] = config
flickerstreak@91 184 local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua
flickerstreak@205 185 self.bars[name] = bar
flickerstreak@205 186 self.callbacks:Fire("OnCreateBar", bar, name)
flickerstreak@205 187 if self.configMode then
flickerstreak@33 188 bar:ShowControls(true)
flickerstreak@33 189 end
flickerstreak@33 190
flickerstreak@28 191 return bar
flickerstreak@28 192 end
flickerstreak@28 193
flickerstreak@205 194 function ReAction:DestroyBar(x)
flickerstreak@205 195 local bar, name = self:GetBar(x)
flickerstreak@63 196 if bar and name then
flickerstreak@205 197 self.bars[name] = nil
flickerstreak@205 198 self.callbacks:Fire("OnDestroyBar", bar, name)
flickerstreak@205 199 bar:Destroy()
flickerstreak@28 200 end
flickerstreak@28 201 end
flickerstreak@28 202
flickerstreak@205 203 function ReAction:RefreshBar(x)
flickerstreak@205 204 local bar, name = self:GetBar(x)
flickerstreak@205 205 if bar and name then
flickerstreak@205 206 self.callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@205 207 end
flickerstreak@63 208 end
flickerstreak@63 209
flickerstreak@205 210 function ReAction:InitializeBars()
flickerstreak@205 211 if not self.barsInitialized then
flickerstreak@211 212 self:ManageBlizzardBars()
flickerstreak@211 213
flickerstreak@205 214 for name, config in pairs(self.db.profile.bars) do
flickerstreak@205 215 if config then
flickerstreak@205 216 self:CreateBar(name, config)
flickerstreak@205 217 end
flickerstreak@205 218 end
flickerstreak@205 219 -- re-anchor and refresh in case anchor order does not match init order
flickerstreak@205 220 for name, bar in pairs(self.bars) do
flickerstreak@205 221 bar:ApplyAnchor()
flickerstreak@205 222 self.callbacks:Fire("OnRefreshBar", bar, name)
flickerstreak@205 223 end
flickerstreak@205 224 self.barsInitialized = true
flickerstreak@205 225 end
flickerstreak@205 226 end
flickerstreak@205 227
flickerstreak@205 228 function ReAction:TearDownBars()
flickerstreak@205 229 for name, bar in pairs(self.bars) do
flickerstreak@205 230 if bar then
flickerstreak@208 231 self.bars[name] = self:DestroyBar(bar)
flickerstreak@205 232 end
flickerstreak@205 233 end
flickerstreak@205 234 self.barsInitialized = false
flickerstreak@205 235 end
flickerstreak@205 236
flickerstreak@205 237 function ReAction:RebuildAll()
flickerstreak@205 238 self:TearDownBars()
flickerstreak@205 239 self:InitializeBars()
flickerstreak@28 240 end
flickerstreak@28 241
flickerstreak@28 242 function ReAction:RenameBar(x, newname)
flickerstreak@205 243 local bar, name = self:GetBar(x)
flickerstreak@63 244 if type(newname) ~= "string" then
flickerstreak@63 245 error("ReAction:RenameBar() - second argument must be a string")
flickerstreak@63 246 end
flickerstreak@63 247 if bar and name and #newname > 0 then
flickerstreak@127 248 if newname == name then
flickerstreak@127 249 return
flickerstreak@127 250 end
flickerstreak@205 251 if self.bars[newname] then
flickerstreak@127 252 self:UserError(format(L["ReAction: name '%s' already in use"],newname))
flickerstreak@47 253 else
flickerstreak@205 254 self.bars[newname], self.bars[name] = self.bars[name], nil
flickerstreak@47 255 bar:SetName(newname or "")
flickerstreak@47 256 local cfg = self.db.profile.bars
flickerstreak@47 257 cfg[newname], cfg[name] = cfg[name], nil
flickerstreak@205 258 self.callbacks:Fire("OnRenameBar", bar, name, newname)
flickerstreak@28 259 end
flickerstreak@28 260 end
flickerstreak@28 261 end
flickerstreak@28 262
flickerstreak@205 263 function ReAction:EraseBar(x)
flickerstreak@205 264 local bar, name = self:GetBar(x)
flickerstreak@63 265 if bar and name then
flickerstreak@205 266 self.callbacks:Fire("OnEraseBar", bar, name)
flickerstreak@208 267 self:DestroyBar(bar)
flickerstreak@205 268 self.db.profile.bars[name] = nil
flickerstreak@63 269 end
flickerstreak@63 270 end
flickerstreak@63 271
flickerstreak@211 272 local blizzFrames = {
flickerstreak@211 273 MainMenuBar,
flickerstreak@211 274 MultiBarLeft,
flickerstreak@211 275 MultiBarRight,
flickerstreak@211 276 MultiBarBottomLeft,
flickerstreak@211 277 MultiBarBottomRight,
flickerstreak@211 278 }
flickerstreak@211 279
flickerstreak@211 280 local hideFrame = CreateFrame("Frame")
flickerstreak@211 281 hideFrame:Hide()
flickerstreak@211 282 local hiddenParents = { }
flickerstreak@211 283 local function ManageBlizzFrame(f, hide)
flickerstreak@211 284 if hide and not hiddenParents[f] then
flickerstreak@211 285 hiddenParents[f] = f:GetParent()
flickerstreak@211 286 f:SetParent(hideFrame)
flickerstreak@211 287 elseif not hide and hiddenParents[f] then
flickerstreak@211 288 f:SetParent(hiddenParents[f])
flickerstreak@211 289 hiddenParents[f] = nil
flickerstreak@211 290 if f:IsShown() then
flickerstreak@211 291 f:Show() -- refresh
flickerstreak@211 292 end
flickerstreak@211 293 end
flickerstreak@211 294 end
flickerstreak@211 295
flickerstreak@211 296 function ReAction:ManageBlizzardBars()
flickerstreak@211 297 for _, f in pairs(blizzFrames) do
flickerstreak@211 298 ManageBlizzFrame(f, self.db.profile.options.hideBlizzardBars)
flickerstreak@211 299 end
flickerstreak@211 300 ManageBlizzFrame(VehicleMenuBar, self.db.profile.options.hideBlizzardVehicleBar)
flickerstreak@211 301 end
flickerstreak@211 302
flickerstreak@218 303 function ReAction:RegisterBarType( class, isDefault )
flickerstreak@218 304 local name = class:GetBarType()
flickerstreak@218 305 self.barTypes[name] = class
flickerstreak@218 306 if isDefault then
flickerstreak@218 307 self.defaultBarType = name
flickerstreak@48 308 end
flickerstreak@185 309 self:RefreshEditor()
flickerstreak@48 310 end
flickerstreak@48 311
flickerstreak@63 312 function ReAction:IterateBarTypes()
flickerstreak@218 313 return pairs(self.barTypes)
flickerstreak@63 314 end
flickerstreak@63 315
flickerstreak@218 316 function ReAction:GetDefaultBarConfig(barType)
flickerstreak@218 317 if barType and self.barTypes[barType] then
flickerstreak@218 318 return self.barTypes[barType]:GetDefaultBarConfig()
flickerstreak@63 319 end
flickerstreak@63 320 end
flickerstreak@63 321
flickerstreak@63 322 function ReAction:GetBarTypeOptions( fill )
flickerstreak@63 323 fill = fill or { }
flickerstreak@63 324 for k in self:IterateBarTypes() do
flickerstreak@63 325 fill[k] = k
flickerstreak@63 326 end
flickerstreak@63 327 return fill
flickerstreak@63 328 end
flickerstreak@63 329
flickerstreak@63 330 function ReAction:GetDefaultBarType()
flickerstreak@218 331 return self.defaultBarType
flickerstreak@63 332 end
flickerstreak@63 333
flickerstreak@33 334 function ReAction:SetConfigMode( mode )
flickerstreak@205 335 if mode ~= self.configMode then
flickerstreak@207 336 if mode then
flickerstreak@207 337 self:SetKeybindMode(false)
flickerstreak@207 338 end
flickerstreak@205 339 self.configMode = mode
flickerstreak@205 340 self.callbacks:Fire("OnConfigModeChanged", mode)
flickerstreak@77 341 end
flickerstreak@63 342 end
flickerstreak@63 343
flickerstreak@63 344 function ReAction:GetConfigMode()
flickerstreak@205 345 return self.configMode
flickerstreak@33 346 end
flickerstreak@38 347
flickerstreak@88 348 function ReAction:SetKeybindMode( mode )
flickerstreak@205 349 if mode ~= self.kbMode then
flickerstreak@88 350 if mode then
flickerstreak@207 351 self:SetConfigMode(false)
flickerstreak@184 352 LKB:Activate()
flickerstreak@88 353 else
flickerstreak@184 354 LKB:Deactivate()
flickerstreak@88 355 end
flickerstreak@207 356 for _, bar in self:IterateBars() do
flickerstreak@207 357 bar:SetKeybindMode(mode)
flickerstreak@207 358 end
flickerstreak@205 359 self.kbMode = LKB:IsShown() or false
flickerstreak@88 360 end
flickerstreak@88 361 end
flickerstreak@88 362
flickerstreak@88 363 function ReAction:GetKeybindMode( mode )
flickerstreak@205 364 return self.kbMode
flickerstreak@88 365 end
Flick@225 366
Flick@225 367
Flick@225 368 -- ConfigMode support
Flick@225 369 CONFIGMODE_CALLBACKS = CONFIGMODE_CALLBACKS or {}
Flick@225 370
Flick@225 371 function CONFIGMODE_CALLBACKS.ReAction( action, mode )
Flick@225 372 if action == "ON" then
Flick@225 373 ReAction:SetConfigMode(true)
Flick@225 374 elseif action == "OFF" then
Flick@225 375 ReAction:SetConfigMode(false)
Flick@225 376 elseif action == "LISTMODES" then
Flick@225 377 -- no modes
Flick@225 378 end
Flick@225 379 end
Flick@225 380