# HG changeset patch # User Flick # Date 1205778293 0 # Node ID 21bcaf8215ff61f28e63c58ca1cb52954b85f398 # Parent f1e838841ce1e932236c41c8a85061067e8475be - converted to Ace3 - rearranged file layout - configGUI menus not working right now diff -r f1e838841ce1 -r 21bcaf8215ff Bar.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bar.lua Mon Mar 17 18:24:53 2008 +0000 @@ -0,0 +1,146 @@ +local ReAction = ReAction +local L = ReAction.L +local _G = _G +local CreateFrame = CreateFrame + +-- update ReAction revision if this file is newer +local revision = tonumber(("$Revision: 1 $"):match("%d+")) +if revision > ReAction.revision then + Reaction.revision = revision +end + +------ BAR CLASS ------ +local Bar = { _classID = {} } + +local function Constructor( self, name, config ) + self.name, self.config = name, config + + if type(config) ~= "table" then + error("ReAction.Bar: config table required") + end + + local f = CreateFrame("Frame",nil,config.parent or UIParent,"SecureStateDriverTemplate") + f:SetFrameStrata("MEDIUM") + config.width = config.width or 400 + config.height = config.height or 80 + f:SetWidth(config.width) + f:SetWidth(config.height) + + self.frame = f + self:RefreshLayout() + self:ApplyAnchor() + f:Show() +end + +function Bar:Destroy() + local f = self.frame + f:UnregisterAllEvents() + f:Hide() + f:SetParent(UIParent) + f:ClearAllPoints() + self.labelString = nil + self.controlFrame = nil + self.frame = nil + self.config = nil +end + +function Bar:RefreshLayout() + ReAction:CallMethodOnAllModules("RefreshBar", self) +end + +function Bar:ApplyAnchor() + local f, config = self.frame, self.config + f:SetWidth(config.width) + f:SetHeight(config.height) + local anchor = config.anchor + if anchor then + local anchorTo + if config.anchorTo then + anchorTo = ReAction:GetBar(config.anchorTo) or _G[config.anchorTo] + end + f:SetPoint(anchor, anchorTo, config.relativePoint, config.x or 0, config.y or 0) + else + f:SetPoint("CENTER") + end +end + +function Bar:GetFrame() + return self.frame +end + +function Bar:GetSize() + return self.frame:GetWidth() or 200, self.frame:GetHeight() or 200 +end + +function Bar:SetSize(w,h) + self.config.width = w + self.config.height = h +end + +function Bar:GetButtonSize() + local w = self.config.btnWidth or 32 + local h = self.config.btnHeight or 32 + -- TODO: get from modules? + return w,h +end + +function Bar:SetButtonSize(w,h) + if w > 0 and h > 0 then + self.config.btnWidth = w + self.config.btnHeight = h + end +end + +function Bar:GetButtonGrid() + local cfg = self.config + local r = cfg.btnRows or 1 + local c = cfg.btnColumns or 1 + local s = cfg.spacing or 4 + return r,c,s +end + +function Bar:SetButtonGrid(r,c,s) + if r > 0 and c > 0 and s > 0 then + local cfg = self.config + cfg.btnRows = r + cfg.btnColumns = c + cfg.spacing = s + end +end + +function Bar:GetName() + return self.name +end + +function Bar:PlaceButton(f, idx, baseW, baseH) + local r, c, s = self:GetButtonGrid() + local bh, bw = self:GetButtonSize() + local row, col = floor((idx-1)/c), mod((idx-1),c) -- zero-based + local x, y = col*bw + (col+0.5)*s, row*bh + (row+0.5)*s + local scale = bw/baseW + + f:ClearAllPoints() + f:SetPoint("TOPLEFT",x/scale,-y/scale) + f:SetScale(scale) +-- f:Show() +end + + + +------ Export as a class-factory ------ +ReAction.Bar = { + prototype = Bar, + + IsInstance = function(self, x) + return type(x) == "table" and x._classID == Bar._classID + end, + + new = function(self, ...) + local x = { } + for k,v in pairs(Bar) do + x[k] = v + end + Constructor(x, ...) + return x + end +} diff -r f1e838841ce1 -r 21bcaf8215ff ReAction.lua --- a/ReAction.lua Tue Mar 11 21:39:34 2008 +0000 +++ b/ReAction.lua Mon Mar 17 18:24:53 2008 +0000 @@ -1,95 +1,19 @@ ---[[ - ReAction Add-On main file. - Performs add-on and library initialization and setup. ---]] +-- ReAction.lua +-- See modules/ReAction_ModuleTemplate for Module API listing +-- See Bar.lua for Bar object listing ------- LOCALIZATION ---------- -local L = AceLibrary("AceLocale-2.2"):new("ReAction") - - ------- GLOBAL VARIABLES ------ --- 'ReAction' is exported as a global. -ReAction = AceLibrary("AceAddon-2.0"):new( - "AceModuleCore-2.0", - "AceEvent-2.0", - "AceDB-2.0" -) --- global variable strings for integration with WoW keybindings dialog (see bindings.xml) -BINDING_HEADER_REACTION = L["ReAction"] -BINDING_NAME_REACTION_TOGGLELOCK = L["Toggle ReAction Bar Lock"] -BINDING_NAME_REACTION_TOGGLEKEYBIND = L["ReAction Keybinding Mode"] - +------ LIBRARIES ------ +local L = LibStub("AceLocale-3.0"):GetLocale("ReAction") ------ CORE ------ -local ReAction = ReAction +local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction" ) ReAction.revision = tonumber(("$Revision: 1 $"):match("%d+")) ReAction.L = L +------ GLOBALS ------ +_G["ReAction"] = ReAction - --- from AceAddon-2.0 -function ReAction:OnInitialize() - self:RegisterDB("ReActionDB") -end - --- from AceAddon-2.0 -function ReAction:OnEnable() - -end - --- from AceAddon-2.0 -function ReAction:OnDisable() - -end - --- from AceDB-2.0 -function ReAction:OnProfileEnable() - -end - --- from AceDB-2.0 -function ReAction:OnProfileDisable() - -end - --- from AceModuleCore-2.0 -function ReAction:OnModuleEnable(module) - -- this handles initialization ordering issues with ReAction_Bar - local barMod = self:GetModule("Bar") - if barMod and module.ApplyToBar then - for _, b in pairs(barMod.bars) do - if b then - module:ApplyToBar(b) - end - end - end -end - --- from AceModuleCore-2.0 -function ReAction:OnModuleDisable(module) - local barMod = self:GetModule("Bar") - if barMod and module.RemoveFromBar then - for _, b in pairs(barMod.bars) do - if b then - module:RemoveFromBar(b) - end - end - end -end - ---[[ - Module API (see bar.lua for usage) - - module:ApplyToBar(bar) - module:RemoveFromBar(bar) - module:RefreshBar(bar) - module:ApplyConfigMode(mode,listOfBars) - module:GetBarNameModifier(bar) - module:EraseBarConfig(barName) -]]-- - - --- debugging +------ DEBUGGING ------ ReAction.debug = true if ReAction.debug then ReAction.print = function(msg) @@ -100,3 +24,204 @@ ReAction.print = function() end end +------ PRIVATE ------ +local SelectBar, DestroyBar, InitializeBars, TearDownBars, DeepCopy, SafeCall, CheckMethod +do + local pcall = pcall + local geterrorhandler = geterrorhandler + + SelectBar = function(x) + local bar, name + if type(x) == "string" then + name = x + bar = ReAction:GetBar(name) + elseif ReAction.Bar:IsInstance(x) then + bar = x + for k,v in pairs(ReAction.bars) do + if v == bar then + name = k + end + end + else + error("bad argument to SelectBar") + end + return bar, name + end + + DestroyBar = function(x) + local bar, name = SelectBar(x) + if name and bar then + ReAction.bars[name] = nil + ReAction:CallMethodOnAllModules("RemoveFromBar", bar) + bar:Destroy() + end + end + + InitializeBars = function () + if not(ReAction.inited) then + for name, config in pairs(ReAction.db.profile.bars) do + if config then + ReAction:CreateBar(name, config) + end + end + ReAction:CallMethodOnAllBars("ApplyAnchor") -- re-anchor in the case of oddball ordering + ReAction.inited = true + end + end + + TearDownBars = function() + for name, bar in pairs(ReAction.bars) do + if bar then + ReAction.bars[name] = DestroyBar(bar) + end + end + ReAction.inited = false + end + + DeepCopy = function(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 + + SafeCall = function(f, ...) + if f then + local success, err = pcall(f,...) + if not success then + geterrorhandler()(err) + end + end + end + + CheckMethod = function(m) + if type(m) == "function" then + return m + end + if type(m) ~= "string" then + error("Invalid method") + end + end +end + + +------ HANDLERS ------ +function ReAction:OnInitialize() + self.db = LibStub("AceDB-3.0"):New("ReAction_DB", + { + profile = { + bars = { }, + defaultBar = { } + } + } + -- default profile is character-specific + ) + self.db.RegisterCallback(self,"OnProfileChanged") + + self.bars = {} +end + +function ReAction:OnEnable() + InitializeBars() +end + +function ReAction:OnDisable() + TearDownBars() +end + +function ReAction:OnProfileChanged() + self.TearDownBars() + self.InitializeBars() +end + +function ReAction:OnModuleEnable(module) + if module.ApplyToBar then + for _, b in pairs(bars) do + if b then + module:ApplyToBar(b) + end + end + end +end + +function ReAction:OnModuleDisable(module) + if module.RemoveFromBar then + for _, b in pairs(bars) do + if b then + module:RemoveFromBar(b) + end + end + end +end + + +------ API ------ +function ReAction:CallMethodOnAllModules(method, ...) + local m = CheckMethod(method) + for _, x in self:IterateModules() do + if x then + SafeCall(m or x[method], x, ...) + end + end +end + +function ReAction:CallMethodOnAllBars(method,...) + local m = CheckMethod(method) + for _, x in pairs(self.bars) do + if x then + SafeCall(m or x[method], x, ...) + end + end +end + +function ReAction:CreateBar(name, defaultConfig, prefix) + local profile = self.db.profile + defaultConfig = defaultConfig or profile.defaultBar + prefix = prefix or L["Bar "] + if not name then + i = 1 + repeat + name = prefix..i + i = i + 1 + until self.bars[name] == nil + end + profile.bars[name] = profile.bars[name] or DeepCopy(defaultConfig) + local bar = self.Bar:new( name, profile.bars[name] ) -- ReAction.Bar defined in Bar.lua + self:CallMethodOnAllModules("ApplyToBar", bar) + self.bars[name] = bar + return bar +end + +function ReAction:EraseBar(x) + local bar, name = SelectBar(x) + if name and bar then + DestroyBar(bar) + self.db.profile.bars[name] = nil + self:CallMethodOnAllModules("EraseBarConfig", name) + end +end + +function ReAction:GetBar(name) + return self.bars[name] +end + +function ReAction:RenameBar(x, newname) + local bar, name = SelectBar(x) + if bar and name and newname then + if self.bars[newname] then + error(L["ReAction: name already in use"]) + end + self.bars[newname] = self.bars[name] + self.bars[name] = nil + bar.name = newname or "" + local cfg = self.db.profile.bars + cfg[newname], cfg[name] = cfg[name], nil + self:CallMethodOnAllModules("RenameBarConfig", name, newname) + end +end + + diff -r f1e838841ce1 -r 21bcaf8215ff ReAction.toc --- a/ReAction.toc Tue Mar 11 21:39:34 2008 +0000 +++ b/ReAction.toc Mon Mar 17 18:24:53 2008 +0000 @@ -5,8 +5,7 @@ ## LoadOnDemand: 0 ## Author: Flick ## Version: 1.0 -## SavedVariables: ReActionDB +## SavedVariables: ReAction_DB ## X-Category: Action Bars -## X-Embeds: AceAddon-2.0, AceConsole-2.0, AceDB-2.0, AceEvent-2.0, AceHook-2.1, AceLibrary, AceLocale-2.2, AceModuleCore-2.0, AceOO-2.0, Dewdrop-2.0, Waterfall-1.0 ReAction.xml diff -r f1e838841ce1 -r 21bcaf8215ff ReAction.xml --- a/ReAction.xml Tue Mar 11 21:39:34 2008 +0000 +++ b/ReAction.xml Mon Mar 17 18:24:53 2008 +0000 @@ -6,8 +6,9 @@