annotate ReAction.lua @ 223:c4b134512c50

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