/trunk/NaturCombat/embed/GTB
local major = "GTB-1.0" |
local major = "NaturGTB" |
local minor = tonumber(string.match("$Revision: 752 $", "(%d+)") or 1) |
assert(LibStub, string.format("%s requires LibStub.", major)) |
["MUST_CALL"] = "You must call '%s' from a registered GTB object.", |
["GROUP_EXISTS"] = "The group '%s' already exists.", |
["BAD_FUNC"] = "You must pass an actual handler and function to '%s'.", |
["ALT_DRAG"] = "ALT + Drag to move the frame anchor.", |
["ALT_DRAG"] = "Drag to move the frame anchor.", |
} |
-- Validation for passed arguments |
local framePool = GTB.framePool |
local groups = GTB.groups |
local methods = {"RegisterOnMove", "SetAnchorVisible", "GetReversed", "GetGrowth", "GetMaxBars", "GetScale", "GetFontSize", "GetWidth", "GetHeight", "SetMaxBars", "SetBaseColor", "EnableGradient", "SetPoint", "SetScale", "SetWidth", "SetHeight", "SetTexture", "SetBarGrowth", "SetIconPosition", "SetTextColor", "HideBar", "ShowBar", "SortBars", |
"SetTimerColor", "SetFadeTime", "RegisterOnFade", "RegisterOnClick", "SetGroupFontSize", "SetInvert", "SetDisplayGroup", "GetDisplayGroup", "RegisterBar", "UnregisterBar", "SetRepeatingTimer", "UnregisterAllBars", "SetBarIcon", "SetTime", "SetBarText"} |
local methods = {"RegisterOnMove", "SetAnchorVisible", "SetAlpha", "GetAlpha", "SetFlashTime", "GetFlashTime", "GetReversed", "GetGrowth", "GetMaxBars", "GetScale", "GetFontSize", "GetWidth", "GetHeight", "SetMaxBars", "SetBaseColor", "EnableGradient", "SetPoint", "SetScale", "SetWidth", "SetHeight", "SetTexture", "SetBarGrowth", "SetIconPosition", "SetTextColor", "HideBar", "ShowBar", "SortBars", |
"SetTimerColor", "SetFadeTime", "RegisterOnFade", "RegisterOnClick", "GetVertical", "SetVerticalSpacing", "SetGroupFontSize", "SetInvert", "SetDisplayGroup", "GetDisplayGroup", "RegisterBar", "UnregisterBar", "SetRepeatingTimer", "UnregisterAllBars", "SetBarIcon", "SetTime", "SetBarText"} |
-- Internal functions for managing bars |
local function getFrame() |
-- Create icon |
frame.icon = frame:CreateTexture(nil, "ARTWORK") |
-- Create Focus/Target Icon |
frame.ticon = frame:CreateTexture(nil, "ARTWORK") |
-- Sparky |
frame.spark = frame:CreateTexture(nil, "OVERLAY") |
frame.spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark") |
end |
end |
-- Fadeout OnUpdate |
local function fadeOnUpdate(self, elapsed) |
local time = GetTime() |
-- Reposition the group |
-- edited for NaturCombat |
local function sortBars(a, b) |
local group = groups["NaturCombat"] |
for id, bar in pairs(group.bars) do |
if (not bar:IsShown()) then -- this specific bar exists.. but it's hidden |
return false |
else |
return a.endTime < b.endTime |
end |
end |
return a.endTime < b.endTime |
end |
local function repositionFrames(group) |
if( i > 1 ) then |
if( group.barGrowth == "DOWN" ) then |
bar:SetPoint("TOPLEFT", group.usedBars[i - 1], "BOTTOMLEFT", 0, 0) |
bar:SetPoint("TOPLEFT", group.usedBars[i - 1], "BOTTOMLEFT", 0, group.vertical) |
else |
bar:SetPoint("BOTTOMLEFT", group.usedBars[i - 1], "TOPLEFT", 0, 0) |
bar:SetPoint("BOTTOMLEFT", group.usedBars[i - 1], "TOPLEFT", 0, group.vertical) |
end |
elseif( group.barGrowth == "UP" ) then |
bar:SetPoint("BOTTOMLEFT", group.frame, "TOPLEFT", offset, 0) |
fadeoutBar(self) |
return |
end |
--flashTime = 5 |
self:SetAlpha(NaturDB.Alpha) |
-- Timer text, need to see if this can be optimized a bit later |
local hour = floor(self.secondsLeft / 3600) |
local minutes = self.secondsLeft - (hour * 3600) |
-- Dragging functions |
local function OnDragStart(self) |
if( IsAltKeyDown() ) then |
self.isMoving = true |
self:StartMoving() |
end |
self.isMoving = true |
self:StartMoving() |
end |
local function OnDragStop(self) |
GameTooltip:Hide() |
end |
function GTB:UnregisterAllBarGroups() |
for a, _ in pairs(groups) do |
groups[a] = nil; |
end |
end |
function GTB:UnregisterGroup(name) |
if groups[name] then |
groups[name] = nil; |
end |
end |
function GTB:RegisterGroup(name, texture) |
argcheck(name, 1, "string") |
argcheck(texture, 2, "string") |
assert(3, not groups[name], string.format(L["GROUP_EXISTS"], name)) |
local obj = {name = name, frame = CreateFrame("Frame", name, UIParent), fontSize = 11, height = 16, obj = obj, bars = {}, usedBars = {}, reversed = false} |
local obj = {name = name, frame = CreateFrame("Frame", name, UIParent), fontSize = 11, height = 16, obj = obj, bars = {}, usedBars = {}, vertical = 0} |
-- Inject our methods |
for _, func in pairs(methods) do |
obj:SetScale(1.0) |
obj:SetWidth(200) |
obj:SetFadeTime(0.25) |
--obj:SetFlashTime(5) |
obj:SetMaxBars(20) |
obj:EnableGradient(true) |
obj:SetAnchorVisible(true) |
--Edit the timer value of a bar |
function GTB.SetTime(group, id, value) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
if not group.bars[id] then return end |
group.bars[id].timer:SetText(value) |
group.bars[id].secondsLeft = value |
-- Edit the text on an active bar |
local origBarID = {} |
function GTB.SetBarText(group, id, text) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
if not group.bars[id] then return end |
group.bars[id].text:SetText(text) |
group.bars[id].barText = text |
end |
function GTB.HideBar(group, id) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
if not group.bars[id] then return end |
--Keep record of this timer |
origBarID[id] = {update=group.bars[id].lastUpdate,text=group.bars[id].barText,timer=group.bars[id].secondsLeft,icon=group.bars[id].iconPath,color={r=group.bars[id].r,g=group.bars[id].g,b=group.bars[id].b}} |
origBarID[id] = {update=group.bars[id].lastUpdate,text=group.bars[id].barText,timer=group.bars[id].secondsLeft,icon=group.bars[id].iconPath,ticon=group.bars[id].ticonPath,color={r=group.bars[id].r,g=group.bars[id].g,b=group.bars[id].b}} |
GTB.UnregisterBar(group, id) |
end |
function GTB.ShowBar(group, id, timer) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
if not origBarID[id] then return end |
local time = origBarID[id].timer - (GetTime() - origBarID[id].update) |
-- group, id, text, 60, 54, icon, r, g, b |
GTB.RegisterBar(group, id, origBarID[id].text, time, timer, origBarID[id].icon, origBarID[id].color.r, origBarID[id].color.g, origBarID[id].color.b) |
GTB.RegisterBar(group, id, origBarID[id].text, time, timer, origBarID[id].icon, origBarID[id].ticon, origBarID[id].color.r, origBarID[id].color.g, origBarID[id].color.b) |
end |
function GTB.SetInvert(group, value) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
group.reversed = value |
end |
function GTB.GetReversed(group) |
function GTB.GetScale(group) |
return group.scale |
end |
function GTB.GetFontSize(group) |
return group.fontSize |
end |
function GTB.GetMaxBars(group) |
return group.maxBars |
end |
function GTB.GetGrowth(group) |
return group.barGrowth |
end |
function GTB.SetVerticalSpacing(group, val) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
group.vertical = val |
end |
function GTB.GetVertical(group) |
return group.vertical |
end |
function GTB.SetAlpha(group, val) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
for k, v in pairs(group.bars) do |
group.bars[k]:SetAlpha(val); |
end |
end |
-- Change the font size of the group |
function GTB.SetGroupFontSize(group, size) |
if( group.redirectTo and groups[group.redirectTo] ) then |
group = groups[group.redirectTo] |
end |
group.fontSize = size |
end |
group.fadeTime = seconds |
end |
-- When should we start flashing the bar |
function GTB.SetFlashTime(group, seconds) |
argcheck(seconds, 2, "number") |
assert(3, group.name and groups[group.name], string.format(L["MUST_CALL"], "SetFadeTime")) |
group.flasherTime = seconds |
end |
function GTB.GetFlashTime(group) |
return group.flasherTime |
end |
-- Redirect everything to the specified group |
function GTB.SetDisplayGroup(group, name) |
argcheck(name, 2, "string", "nil") |
-------------------- |
-- Register |
function GTB.RegisterBar(group, id, text, seconds, startSeconds, icon, r, g, b) |
function GTB.RegisterBar(group, id, text, seconds, startSeconds, icon, ticon, r, g, b) |
argcheck(id, 2, "string", "number") |
argcheck(text, 3, "string") |
argcheck(seconds, 4, "number") |
argcheck(icon, 5, "string", "nil") |
argcheck(r, 6, "number", "nil") |
argcheck(g, 7, "number", "nil") |
argcheck(b, 8, "number", "nil") |
argcheck(icon, 5, "string", "number", "nil") |
argcheck(ticon, 6, "string", "nil") |
argcheck(r, 7, "number", "nil") |
argcheck(g, 8, "number", "nil") |
argcheck(b, 9, "number", "nil") |
assert(3, group.name and groups[group.name], string.format(L["MUST_CALL"], "RegisterBar")) |
local originalOwner = group.name |
-- Update icon |
if( icon ) then |
if (type(icon) == "number") then |
icon = select(3, GetSpellInfo(icon)); |
end |
frame.icon:SetTexture(icon) |
if( frame.icon:GetTexture() ) then |
frame.icon:Hide() |
end |
-- Update Target/Focus Icon |
if ( ticon ~= type("string") ) then |
frame.ticon:SetTexture(ticon) |
if (frame.ticon:GetTexture()) then |
frame.ticon:SetWidth(group.height) |
frame.ticon:SetHeight(group.height) |
frame.ticon:SetTexCoord(0.07, 0.93, 0.07, 0.93) |
frame.ticon:SetPoint("TOPLEFT", frame, "TOPRIGHT", offset, 0) |
frame.ticon:Show() |
else |
frame.ticon:Hide() |
end |
end |
-- Set info the bar needs to know |
frame.r = r or group.baseColor.r |
frame.g = g or group.baseColor.g |
frame.gradients = group.gradients |
frame.groupName = group.name |
frame.iconPath = icon |
frame.ticonPath = ticon |
frame.barText = text |
frame.barID = id |
frame.group = group |
frame.reversed = group.reversed |
frame.vertical = group.vertical |
-- Setup background |
frame.bg:SetStatusBarTexture(group.texture) |
frame.bg:SetStatusBarColor(0.0, 0.5, 0.5, 0.5) |
-- Start it up |
frame:SetStatusBarTexture(group.texture) |
frame:SetStatusBarColor(frame.r, frame.g, frame.b) |
frame:SetStatusBarColor(r, g, b) |
frame:SetWidth(group.width) |
frame:SetHeight(group.height) |
frame:SetScale(group.scale) |
frame:SetScript("OnUpdate", barOnUpdate) |
--[[frame.button:SetScript("OnClick", function(self, button) |
frame.button:SetScript("OnClick", function(self, button) |
if (button == "RightButton") then |
-- delete bar |
GTB.UnregisterBar(group, id) |
end |
end)]] |
end) |
-- Reposition this group |
repositionFrames(group) |