annotate ReAction.lua @ 213:8ba8ab8bf6dd

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