ESOUI SVN ZAMStats

[/] [trunk/] [ZAM_Stats/] [ZAM_Stats.lua] - Rev 10

Compare with Previous | Blame | View Log

-- ZAM_Stats © ZAM Network LLC
-- All Rights Reserved

-- API for Modules --
---------------------

-- Creating your module --
-- Usage: control, label = ZAM_Stats:CreateModule(key)
        -- key = string; a unique identifier/name of your module
        -- control = reference to your module's frame
        -- label = reference to your module's text

-- Knowing when your module is ready --
-- Usage: CALLBACK_MANAGER:RegisterCallback(event, method)
        -- event = string; the event fired by ZAM_Stats - it is called "ZAM_Stats_Modules_Ready"
        -- method = function; the function to call when the event fires 

-- Setting the text --
-- Usage: ZAM_Stats:SetModuleText(label, prefix, suffix)
        -- label = reference to the module's label
        -- prefix = string; the value/data to display in the module
        -- suffix = string; any descriptor text after the data (ex. "MB")

-- Letting the options panel force a refresh of the text --
-- Usage: CALLBACK_MANAGER:RegisterCallback(event, method)
        -- event = string; the event fired by ZAM_Stats - it is called "ZAM_Stats_Force_Refresh"
        -- method = function; the function to call when the event fires (should be your ZAM_Stats:SetModuleText function call)

---------------------------------------------------------------------------

ZAM_Stats = {}

-- UPVALUES --
local ZAM_Stats = ZAM_Stats
local LMP = LibStub("LibMediaProvider-1.0")
local db
local wm = GetWindowManager()
ZAM_Stats.modules = {}
local modules = ZAM_Stats.modules


function ZAM_Stats.ConfigModule(frame)
        local name = frame:GetName()
        frame:ClearAnchors()
        if not db[name].point then
                db[name].point = {["a"]=CENTER, ["b"]=CENTER, ["x"]=0, ["y"]=0}
        end
        local anchor = db[name].point
        frame:SetAnchor(anchor.a, GuiRoot, anchor.b or anchor.a, anchor.x, anchor.y)
        frame:SetMovable(not db.locked)
        frame.bg:SetCenterColor(0,0,0, db.locked and 0 or .4)
        frame.text:SetFont(("%s|%s|%s"):format(LMP:Fetch("font", db.font), db.fsize, db.fstyle))
        frame.text:SetHorizontalAlignment(_G["TEXT_ALIGN_"..db.align])
end

function ZAM_Stats:CreateModule(module)
        local f = wm:CreateTopLevelWindow("ZAM_Stats_"..module)
                f:SetDimensions(100, 30)
                f:SetMouseEnabled(true)
                f:SetHandler("OnReceiveDrag", function(self)
                                if not db.locked then
                                        self:StartMoving()
                                end
                        end)
                f:SetHandler("OnMouseUp", function(self)
                                self:StopMovingOrResizing()
                                local _,a,_,b,x,y = self:GetAnchor()
                                db[self:GetName()].point = {["a"]=a, ["b"]=b, ["x"]=x, ["y"]=y}
                        end)
                f:SetClampedToScreen(true)
                f:SetDrawLayer(DL_BACKGROUND)
        f.bg = wm:CreateControlFromVirtual("ZAM_Stats_"..module.."BG", f, "ZAM_StatsBGTemplate")
        local fbg = f.bg
                fbg:SetCenterColor(0, 0, 0, .4)
                fbg:SetEdgeColor(0, 0, 0, 0)
                fbg:SetDimensions(100, 30)
                fbg:SetAnchorFill(f)
        f.text = wm:CreateControlFromVirtual("ZAM_Stats_"..module.."Text", f, "ZAM_StatsTextTemplate")
        local ftext = f.text
                ftext:SetDimensions(100, 30)
                ftext:SetAnchorFill(f)
                ftext:SetVerticalAlignment(TEXT_ALIGN_CENTER)
                ftext:SetText("test")
                
        table.insert(modules, f)
        
        --maybe change later to only return module (module.bg and module.text are there now anyway)
        return f, ftext
end

function ZAM_Stats.RGBPercToHex(r,g,b)
        r = r <= 1 and r >= 0 and r or 0
        g = g <= 1 and g >= 0 and g or 0
        b = b <= 1 and b >= 0 and b or 0
        return string.format("%02x%02x%02x", r*255, g*255, b*255)
end

--maybe change this later so you just need to pass in the module, not its text object
function ZAM_Stats:SetModuleText(ftext, value, suffix)
        ftext:SetText("|c"..ZAM_Stats.preHex..value.."|c"..ZAM_Stats.sufHex..suffix)
end

local function AddonLoaded()
        db = ZAM_Stats.SetUpSavedVars()
                
        ZAM_Stats.preHex = ZAM_Stats.RGBPercToHex(db.preColor.r,db.preColor.g,db.preColor.b)
        ZAM_Stats.sufHex = ZAM_Stats.RGBPercToHex(db.sufColor.r,db.sufColor.g,db.sufColor.b)
        
        --setup each module
        for k,v in pairs(modules) do
                ZAM_Stats.ConfigModule(v)
        end
        --send a callback to each module
        CALLBACK_MANAGER:FireCallbacks("ZAM_Stats_Modules_Ready")       --maybe switch to a per-module callback?
end


EVENT_MANAGER:RegisterForEvent("ZAM_Stats", EVENT_ADD_ON_LOADED, function(event, addon)
--EVENT_MANAGER:RegisterForEvent("ZAM_Stats", EVENT_PLAYER_ACTIVATED, function(event, addon)
                if addon == "ZAM_Stats" then
                        AddonLoaded()
                        ZAM_Stats.RegisterOptions()
                end
        end)

Compare with Previous | Blame