Mercurial > wow > reaction
view ReAction.lua @ 205:42fd93f19291
Code cleanup of ReAction.lua
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Tue, 16 Nov 2010 21:17:22 -0800 |
parents | 85f7c7857617 |
children | c24ac8ee1e45 443d2ea4be86 |
line wrap: on
line source
local addonName, addonTable = ... 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") ------ Utility ------ local tcopy do function tcopy(x) if type(x) ~= "table" then return x end local r = {} for k,v in pairs(x) do 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 ------ function ReAction:OnInitialize() self.db = LibStub("AceDB-3.0"):New("ReAction_DB", { profile = { bars = { }, defaultBar = { }, } }, 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() self:InitializeBars() end function ReAction:OnDisable() self:TearDownBars() end function ReAction:PLAYER_REGEN_DISABLED() if self.configMode == true then self:UserError(L["ReAction config mode disabled during combat."]) self:SetConfigMode(false) self:SetKeybindMode(false) self:CloseEditor() end end function ReAction:LIBKEYBOUND_ENABLED( evt ) self:SetKeybindMode(true) end function ReAction:LIBKEYBOUND_DISABLED( evt ) return self:SetKeybindMode(false) end ------ Methods ------ function ReAction:UserError(msg) UIErrorsFrame:AddMessage(msg) end 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]) -- (2) ReAction:CreateBar(name, "barType", [nRows], [nCols], [btnSize], [btnSpacing]) function ReAction:CreateBar(name, config, ...) local profile = self.db.profile if name then if self.bars[name] then self:UserError(format(L["ReAction: name '%s' already in use"],name)) return nil end else local prefix = L["Bar "] local i = 1 repeat name = prefix..i i = i + 1 until self.bars[name] == nil end if type(config) == "string" then config = self.defaultBarConfig[config] if not config then error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...)))) end 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 config.btnHeight = select(3,...) or config.btnHeight or 36 config.spacing = select(4,...) or config.spacing or 3 config.width = config.width or config.btnColumns*(config.btnWidth + config.spacing) + 1 config.height = config.height or config.btnRows*(config.btnHeight + config.spacing) + 1 config.anchor = config.anchor or "UIParent" config.point = config.point or "BOTTOM" config.relpoint = config.relpoint or "BOTTOM" config.y = config.y or 200 config.x = config.x or 0 end 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 self.bars[name] = bar self.callbacks:Fire("OnCreateBar", bar, name) if self.configMode then bar:ShowControls(true) end return bar end function ReAction:DestroyBar(x) local bar, name = self:GetBar(x) if bar and name then self.bars[name] = nil self.callbacks:Fire("OnDestroyBar", bar, name) bar:Destroy() end end function ReAction:RefreshBar(x) local bar, name = self:GetBar(x) if bar and name then self.callbacks:Fire("OnRefreshBar", bar, name) end end 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 = self:GetBar(x) if type(newname) ~= "string" then error("ReAction:RenameBar() - second argument must be a string") end if bar and name and #newname > 0 then if newname == name then return end if self.bars[newname] then self:UserError(format(L["ReAction: name '%s' already in use"],newname)) else 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 self.callbacks:Fire("OnRenameBar", bar, name, newname) end end end function ReAction:EraseBar(x) local bar, name = self:GetBar(x) if bar and name then self.callbacks:Fire("OnEraseBar", bar, name) DestroyBar(bar) self.db.profile.bars[name] = nil end end function ReAction:RegisterBarType( name, config, isDefaultChoice ) self.defaultBarConfig[name] = config if isDefaultChoice then self.defaultBarConfigChoice = name end self:RefreshEditor() end function ReAction:UnregisterBarType( name ) self.defaultBarConfig[name] = nil if self.defaultBarConfigChoice == name then self.defaultBarConfigChoice = nil end self:RefreshEditor() end function ReAction:IterateBarTypes() return pairs(self.defaultBarConfig) end function ReAction:GetBarTypeConfig(name) if name then return self.defaultBarConfig[name] end end function ReAction:GetBarTypeOptions( fill ) fill = fill or { } for k in self:IterateBarTypes() do fill[k] = k end return fill end function ReAction:GetDefaultBarType() return self.defaultBarConfigChoice end function ReAction:SetConfigMode( mode ) if mode ~= self.configMode then self.configMode = mode self.callbacks:Fire("OnConfigModeChanged", mode) end end function ReAction:GetConfigMode() return self.configMode end function ReAction:SetKeybindMode( mode ) if mode ~= self.kbMode then if mode then LKB:Activate() else LKB:Deactivate() end self.kbMode = LKB:IsShown() or false end end function ReAction:GetKeybindMode( mode ) return self.kbMode end