Mercurial > wow > reaction
view ReAction.lua @ 195:85213d045acb
Fix editor panel title
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Mon, 15 Nov 2010 11:01:42 -0800 |
parents | 55af1ebbec65 |
children | c7be637a47bc |
line wrap: on
line source
--[[ ReAction.lua The ReAction core manages several collections: - modules (via AceAddon) - bars - bar-type constructors and publishes events when those collections change. It also implements a couple properties and has a couple convenience methods which drill down to particular modules. Most of the "real work" of the addon happens in Bar.lua, Overlay.lua, State.lua, and the various modules. Events (with handler arguments): -------------------------------- "OnCreateBar" (bar, name) : after a bar object is created "OnDestroyBar" (bar, name) : before a bar object is destroyed "OnEraseBar" (bar, name) : before a bar config is removed from the profile db "OnRenameBar" (bar, oldname, newname) : after a bar is renamed "OnRefreshBar" (bar, name) : after a bar's state has been updated "OnConfigModeChanged" (mode) : after the config mode is changed ReAction is also an AceAddon-3.0 and contains an AceDB-3.0, which in turn publish more events. ]]-- local _, addonTable = ... local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", "AceEvent-3.0" ) ReAction.version = "1.0" addonTable.ReAction = ReAction ------ LIBRARIES ------ local callbacks = LibStub("CallbackHandler-1.0"):New(ReAction) 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 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) if type(x) ~= "table" then return x end local r = {} for k,v in pairs(x) do r[k] = DeepCopy(v) end return r end end ------ HANDLERS ------ function ReAction:OnInitialize() self.db = LibStub("AceDB-3.0"):New("ReAction_DB", { profile = { bars = { }, defaultBar = { }, } }, true -- use global 'Default' (locale-specific) ) LKB.RegisterCallback(self,"LIBKEYBOUND_ENABLED") LKB.RegisterCallback(self,"LIBKEYBOUND_DISABLED") self:RegisterEvent("PLAYER_REGEN_DISABLED") self:InitializeOptions() end function ReAction:OnEnable() InitializeBars() end function ReAction:OnDisable() TearDownBars() end function ReAction:PLAYER_REGEN_DISABLED() if private.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 ------ API ------ function ReAction:UserError(msg) -- any user errors should be flashed to the UIErrorsFrame UIErrorsFrame:AddMessage(msg) end function ReAction:RebuildAll() TearDownBars() InitializeBars() 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 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 bars[name] == nil end if type(config) == "string" then config = defaultBarConfig[config] if not config then error(("ReAction:CreateBar() - unknown bar type '%s'"):format(tostring(select(1,...)))) end config = DeepCopy(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 DeepCopy(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 bar:ShowControls(true) end return bar end function ReAction:EraseBar(x) local bar, name = SelectBar(x) if bar and name then callbacks:Fire("OnEraseBar", bar, name) DestroyBar(bar) self.db.profile.bars[name] = nil end end function ReAction:GetBar(name) return bars[name] end -- returns pairs of name, bar function ReAction:IterateBars() return pairs(bars) end function ReAction:RenameBar(x, newname) local bar, name = SelectBar(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 bars[newname] then self:UserError(format(L["ReAction: name '%s' already in use"],newname)) else bars[newname], bars[name] = 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) end end end function ReAction:RefreshBar(x) local bar, name = SelectBar(x) if bar and name then callbacks:Fire("OnRefreshBar", bar, name) end end function ReAction:RegisterBarType( name, config, isDefaultChoice ) defaultBarConfig[name] = config if isDefaultChoice then private.defaultBarConfigChoice = name end self:RefreshEditor() end function ReAction:UnregisterBarType( name ) defaultBarConfig[name] = nil if private.defaultBarConfigChoice == name then private.defaultBarConfigChoice = nil end self:RefreshEditor() end function ReAction:IterateBarTypes() return pairs(defaultBarConfig) end function ReAction:GetBarTypeConfig(name) if name then return 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 private.defaultBarConfigChoice end function ReAction:SetConfigMode( mode ) if mode ~= private.configMode then private.configMode = mode callbacks:Fire("OnConfigModeChanged", mode) end end function ReAction:GetConfigMode() return private.configMode end function ReAction:SetKeybindMode( mode ) if mode ~= private.kbMode then if mode then LKB:Activate() else LKB:Deactivate() end private.kbMode = LKB:IsShown() or false end end function ReAction:GetKeybindMode( mode ) return private.kbMode end