flickerstreak@25: local ReAction = ReAction flickerstreak@25: local L = ReAction.L flickerstreak@25: local _G = _G flickerstreak@25: local CreateFrame = CreateFrame flickerstreak@25: flickerstreak@25: -- update ReAction revision if this file is newer flickerstreak@25: local revision = tonumber(("$Revision: 1 $"):match("%d+")) flickerstreak@25: if revision > ReAction.revision then flickerstreak@25: Reaction.revision = revision flickerstreak@25: end flickerstreak@25: flickerstreak@28: ------ BAR CLASS ------ flickerstreak@28: local Bar = { _classID = {} } flickerstreak@25: flickerstreak@28: local function Constructor( self, name, config ) flickerstreak@25: self.name, self.config = name, config flickerstreak@25: flickerstreak@25: if type(config) ~= "table" then flickerstreak@28: error("ReAction.Bar: config table required") flickerstreak@25: end flickerstreak@25: flickerstreak@25: local f = CreateFrame("Frame",nil,config.parent or UIParent,"SecureStateDriverTemplate") flickerstreak@25: f:SetFrameStrata("MEDIUM") flickerstreak@25: config.width = config.width or 400 flickerstreak@25: config.height = config.height or 80 flickerstreak@25: f:SetWidth(config.width) flickerstreak@25: f:SetWidth(config.height) flickerstreak@25: flickerstreak@25: self.frame = f flickerstreak@25: self:RefreshLayout() flickerstreak@25: self:ApplyAnchor() flickerstreak@25: f:Show() flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:Destroy() flickerstreak@25: local f = self.frame flickerstreak@25: f:UnregisterAllEvents() flickerstreak@25: f:Hide() flickerstreak@25: f:SetParent(UIParent) flickerstreak@25: f:ClearAllPoints() flickerstreak@25: self.labelString = nil flickerstreak@25: self.controlFrame = nil flickerstreak@25: self.frame = nil flickerstreak@25: self.config = nil flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:RefreshLayout() flickerstreak@25: ReAction:CallMethodOnAllModules("RefreshBar", self) flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:ApplyAnchor() flickerstreak@25: local f, config = self.frame, self.config flickerstreak@25: f:SetWidth(config.width) flickerstreak@25: f:SetHeight(config.height) flickerstreak@25: local anchor = config.anchor flickerstreak@25: if anchor then flickerstreak@25: local anchorTo flickerstreak@25: if config.anchorTo then flickerstreak@28: anchorTo = ReAction:GetBar(config.anchorTo) or _G[config.anchorTo] flickerstreak@25: end flickerstreak@25: f:SetPoint(anchor, anchorTo, config.relativePoint, config.x or 0, config.y or 0) flickerstreak@25: else flickerstreak@25: f:SetPoint("CENTER") flickerstreak@25: end flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:GetFrame() flickerstreak@25: return self.frame flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:GetSize() flickerstreak@25: return self.frame:GetWidth() or 200, self.frame:GetHeight() or 200 flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:SetSize(w,h) flickerstreak@25: self.config.width = w flickerstreak@25: self.config.height = h flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:GetButtonSize() flickerstreak@25: local w = self.config.btnWidth or 32 flickerstreak@25: local h = self.config.btnHeight or 32 flickerstreak@25: -- TODO: get from modules? flickerstreak@25: return w,h flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:SetButtonSize(w,h) flickerstreak@25: if w > 0 and h > 0 then flickerstreak@25: self.config.btnWidth = w flickerstreak@25: self.config.btnHeight = h flickerstreak@25: end flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:GetButtonGrid() flickerstreak@25: local cfg = self.config flickerstreak@25: local r = cfg.btnRows or 1 flickerstreak@25: local c = cfg.btnColumns or 1 flickerstreak@25: local s = cfg.spacing or 4 flickerstreak@25: return r,c,s flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:SetButtonGrid(r,c,s) flickerstreak@25: if r > 0 and c > 0 and s > 0 then flickerstreak@25: local cfg = self.config flickerstreak@25: cfg.btnRows = r flickerstreak@25: cfg.btnColumns = c flickerstreak@25: cfg.spacing = s flickerstreak@25: end flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:GetName() flickerstreak@25: return self.name flickerstreak@25: end flickerstreak@25: flickerstreak@25: function Bar:PlaceButton(f, idx, baseW, baseH) flickerstreak@25: local r, c, s = self:GetButtonGrid() flickerstreak@25: local bh, bw = self:GetButtonSize() flickerstreak@25: local row, col = floor((idx-1)/c), mod((idx-1),c) -- zero-based flickerstreak@25: local x, y = col*bw + (col+0.5)*s, row*bh + (row+0.5)*s flickerstreak@25: local scale = bw/baseW flickerstreak@25: flickerstreak@25: f:ClearAllPoints() flickerstreak@25: f:SetPoint("TOPLEFT",x/scale,-y/scale) flickerstreak@25: f:SetScale(scale) flickerstreak@25: -- f:Show() flickerstreak@25: end flickerstreak@25: flickerstreak@28: flickerstreak@28: flickerstreak@28: ------ Export as a class-factory ------ flickerstreak@28: ReAction.Bar = { flickerstreak@28: prototype = Bar, flickerstreak@28: flickerstreak@28: IsInstance = function(self, x) flickerstreak@28: return type(x) == "table" and x._classID == Bar._classID flickerstreak@28: end, flickerstreak@28: flickerstreak@28: new = function(self, ...) flickerstreak@28: local x = { } flickerstreak@28: for k,v in pairs(Bar) do flickerstreak@28: x[k] = v flickerstreak@28: end flickerstreak@28: Constructor(x, ...) flickerstreak@28: return x flickerstreak@28: end flickerstreak@28: }