# HG changeset patch # User Flick # Date 1289971042 28800 # Node ID 42fd93f1929184546439d873f9826390fe1d6604 # Parent 85f7c7857617caea037bfb5eca5924220985a6d0 Code cleanup of ReAction.lua diff -r 85f7c7857617 -r 42fd93f19291 ReAction.lua --- a/ReAction.lua Tue Nov 16 17:01:43 2010 -0800 +++ b/ReAction.lua Tue Nov 16 21:17:22 2010 -0800 @@ -1,96 +1,37 @@ local addonName, addonTable = ... -local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", - "AceEvent-3.0" -) -ReAction.version = "1.1" -addonTable.ReAction = ReAction - ------- LIBRARIES ------ -local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction) +local pcall = pcall +local pairs = pairs +local type = type +local geterrorhandler = geterrorhandler local LKB = LibStub("LibKeyBound-1.0") local L = LibStub("AceLocale-3.0"):GetLocale("ReAction") -ReAction.L = L -ReAction.LKB = LKB -ReAction.callbacks = callbacks ------- PRIVATE ------ -local private = { } -local bars = {} -local defaultBarConfig = {} - - -local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy +------ Utility ------ +local tcopy do - local pcall = pcall - local geterrorhandler = geterrorhandler - local self = ReAction - local inited = false - - function SelectBar(x) - local bar, name - if type(x) == "string" then - name = x - bar = self:GetBar(name) - else - for k,v in pairs(bars) do - if v == x then - name = k - bar = x - end - end - end - return bar, name - end - - function DestroyBar(x) - local bar, name = SelectBar(x) - if bar and name then - bars[name] = nil - callbacks:Fire("OnDestroyBar", bar, name) - bar:Destroy() - end - end - - function InitializeBars() - if not inited then - for name, config in pairs(self.db.profile.bars) do - if config then - self:CreateBar(name, config) - end - end - -- re-anchor and refresh in case anchor order does not match init order - for name, bar in pairs(bars) do - bar:ApplyAnchor() - callbacks:Fire("OnRefreshBar", bar, name) - end - inited = true - end - end - - function TearDownBars() - for name, bar in pairs(bars) do - if bar then - bars[name] = DestroyBar(bar) - end - end - inited = false - end - - function DeepCopy(x) + function tcopy(x) if type(x) ~= "table" then return x end local r = {} for k,v in pairs(x) do - r[k] = DeepCopy(v) + r[k] = tcopy(v) end return r end - end +------ Core ------ +local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", + "AceEvent-3.0" +) +addonTable.ReAction = ReAction +ReAction.version = "1.1" +ReAction.L = L +ReAction.LKB = LKB ------- HANDLERS ------ + +------ Handlers ------ function ReAction:OnInitialize() self.db = LibStub("AceDB-3.0"):New("ReAction_DB", { @@ -101,24 +42,27 @@ }, true -- use global 'Default' (locale-specific) ) + + self.bars = { } + self.defaultBarConfig = { } + + self.callbacks = LibStub("CallbackHandler-1.0"):New(self) LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED") LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED") - self:RegisterEvent("PLAYER_REGEN_DISABLED") - self:InitializeOptions() end function ReAction:OnEnable() - InitializeBars() + self:InitializeBars() end function ReAction:OnDisable() - TearDownBars() + self:TearDownBars() end function ReAction:PLAYER_REGEN_DISABLED() - if private.configMode == true then + if self.configMode == true then self:UserError(L["ReAction config mode disabled during combat."]) self:SetConfigMode(false) self:SetKeybindMode(false) @@ -135,19 +79,29 @@ end - ------- API ------ +------ Methods ------ function ReAction:UserError(msg) - -- any user errors should be flashed to the UIErrorsFrame UIErrorsFrame:AddMessage(msg) end -function ReAction:RebuildAll() - TearDownBars() - InitializeBars() +function ReAction:GetBar(arg) + if type(arg) == "string" then + return self.bars[arg], arg + elseif type(arg) == "table" then -- reverse lookup + for name, bar in pairs(self.bars) do + if arg == bar then + return bar, name + end + end + else + error("ReAction:GetBar() requires either a name or a bar table arg") + end end +function ReAction:IterateBars() + return pairs(self.bars) +end -- usage: -- (1) ReAction:CreateBar(name, [cfgTable]) @@ -156,7 +110,7 @@ local profile = self.db.profile if name then - if bars[name] then + if self.bars[name] then self:UserError(format(L["ReAction: name '%s' already in use"],name)) return nil end @@ -166,15 +120,15 @@ repeat name = prefix..i i = i + 1 - until bars[name] == nil + until self.bars[name] == nil end if type(config) == "string" then - config = defaultBarConfig[config] + config = self.defaultBarConfig[config] if not config then error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...)))) end - config = DeepCopy(config) + config = tcopy(config) config.btnRows = select(1,...) or config.btnRows or 1 config.btnColumns = select(2,...) or config.btnColumns or 12 config.btnWidth = select(3,...) or config.btnWidth or 36 @@ -188,39 +142,67 @@ config.y = config.y or 200 config.x = config.x or 0 end - config = config or profile.bars[name] or DeepCopy(profile.defaultBar) + config = config or profile.bars[name] or tcopy(profile.defaultBar) profile.bars[name] = config local bar = self.Bar:New( name, config ) -- ReAction.Bar defined in Bar.lua - bars[name] = bar - callbacks:Fire("OnCreateBar", bar, name) - if private.configMode then + self.bars[name] = bar + self.callbacks:Fire("OnCreateBar", bar, name) + if self.configMode then bar:ShowControls(true) end return bar end -function ReAction:EraseBar(x) - local bar, name = SelectBar(x) +function ReAction:DestroyBar(x) + local bar, name = self:GetBar(x) if bar and name then - callbacks:Fire("OnEraseBar", bar, name) - DestroyBar(bar) - self.db.profile.bars[name] = nil + self.bars[name] = nil + self.callbacks:Fire("OnDestroyBar", bar, name) + bar:Destroy() end end -function ReAction:GetBar(name) - return bars[name] +function ReAction:RefreshBar(x) + local bar, name = self:GetBar(x) + if bar and name then + self.callbacks:Fire("OnRefreshBar", bar, name) + end end --- returns pairs of name, bar -function ReAction:IterateBars() - return pairs(bars) +function ReAction:InitializeBars() + if not self.barsInitialized then + for name, config in pairs(self.db.profile.bars) do + if config then + self:CreateBar(name, config) + end + end + -- re-anchor and refresh in case anchor order does not match init order + for name, bar in pairs(self.bars) do + bar:ApplyAnchor() + self.callbacks:Fire("OnRefreshBar", bar, name) + end + self.barsInitialized = true + end +end + +function ReAction:TearDownBars() + for name, bar in pairs(self.bars) do + if bar then + self.bars[name] = DestroyBar(bar) + end + end + self.barsInitialized = false +end + +function ReAction:RebuildAll() + self:TearDownBars() + self:InitializeBars() end function ReAction:RenameBar(x, newname) - local bar, name = SelectBar(x) + local bar, name = self:GetBar(x) if type(newname) ~= "string" then error("ReAction:RenameBar() - second argument must be a string") end @@ -228,48 +210,50 @@ if newname == name then return end - if bars[newname] then + if self.bars[newname] then self:UserError(format(L["ReAction: name '%s' already in use"],newname)) else - bars[newname], bars[name] = bars[name], nil + self.bars[newname], self.bars[name] = self.bars[name], nil bar:SetName(newname or "") local cfg = self.db.profile.bars cfg[newname], cfg[name] = cfg[name], nil - callbacks:Fire("OnRenameBar", bar, name, newname) + self.callbacks:Fire("OnRenameBar", bar, name, newname) end end end -function ReAction:RefreshBar(x) - local bar, name = SelectBar(x) +function ReAction:EraseBar(x) + local bar, name = self:GetBar(x) if bar and name then - callbacks:Fire("OnRefreshBar", bar, name) + self.callbacks:Fire("OnEraseBar", bar, name) + DestroyBar(bar) + self.db.profile.bars[name] = nil end end function ReAction:RegisterBarType( name, config, isDefaultChoice ) - defaultBarConfig[name] = config + self.defaultBarConfig[name] = config if isDefaultChoice then - private.defaultBarConfigChoice = name + self.defaultBarConfigChoice = name end self:RefreshEditor() end function ReAction:UnregisterBarType( name ) - defaultBarConfig[name] = nil - if private.defaultBarConfigChoice == name then - private.defaultBarConfigChoice = nil + self.defaultBarConfig[name] = nil + if self.defaultBarConfigChoice == name then + self.defaultBarConfigChoice = nil end self:RefreshEditor() end function ReAction:IterateBarTypes() - return pairs(defaultBarConfig) + return pairs(self.defaultBarConfig) end function ReAction:GetBarTypeConfig(name) if name then - return defaultBarConfig[name] + return self.defaultBarConfig[name] end end @@ -282,31 +266,31 @@ end function ReAction:GetDefaultBarType() - return private.defaultBarConfigChoice + return self.defaultBarConfigChoice end function ReAction:SetConfigMode( mode ) - if mode ~= private.configMode then - private.configMode = mode - callbacks:Fire("OnConfigModeChanged", mode) + if mode ~= self.configMode then + self.configMode = mode + self.callbacks:Fire("OnConfigModeChanged", mode) end end function ReAction:GetConfigMode() - return private.configMode + return self.configMode end function ReAction:SetKeybindMode( mode ) - if mode ~= private.kbMode then + if mode ~= self.kbMode then if mode then LKB:Activate() else LKB:Deactivate() end - private.kbMode = LKB:IsShown() or false + self.kbMode = LKB:IsShown() or false end end function ReAction:GetKeybindMode( mode ) - return private.kbMode + return self.kbMode end