View Single Post
09-10-13, 10:12 PM   #1
siweia
A Flamescale Wyrmkin
 
siweia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2011
Posts: 126
Need some help on totembar please

I tried to add a timer on the ouf_totembar, but the problem is that I can't make it update itself well,
and when I destroyed the totem, the timer would stop at the time it left instead of disppear.
Please help!

Code:
local _, ns = ...
local oUF = ns.oUF or oUF

if not oUF then return end

local _, pClass = UnitClass("player")
local total = 0
local delay = 0.01

-- In the order, fire, earth, water, air
local colors = {
	[1] = {.58,.23,.10},
	[2] = {.23,.45,.13},		
	[3] = {.19,.48,.60},
	[4] = {.42,.18,.74},	
}

local GetTotemInfo, SetValue, GetTime = GetTotemInfo, SetValue, GetTime
local Timer
	
local Abbrev = function(name)	
	return (string.len(name) > 10) and string.gsub(name, "%s*(.)%S*%s*", "%1. ") or name
end
local function TotemOnClick(self,...)
	local id = self.ID
	local mouse = ...
	if IsShiftKeyDown() then
		for j = 1,4 do 
			DestroyTotem(j)
		end 
	else 
		DestroyTotem(id) 
	end
end
	
local function InitDestroy(self)
	local totem = self.TotemBar
	for i = 1 , 4 do
		local Destroy = CreateFrame("Button",nil, totem[i])
		Destroy:SetAllPoints(totem[i])
		Destroy:RegisterForClicks("LeftButtonUp", "RightButtonUp")
		Destroy.ID = i
		Destroy:SetScript("OnClick", TotemOnClick)
	end
end
	
local function UpdateSlot(self, slot)
	local totem = self.TotemBar

	local haveTotem, name, startTime, duration, totemIcon = GetTotemInfo(slot)

	totem[slot]:SetStatusBarColor(unpack(totem.colors[slot]))
	totem[slot]:SetValue(0)
	totem[slot].Time = totem[slot]:CreateFontString(nil, "OVERLAY")
	totem[slot].Time:SetPoint('CENTER', totem[slot], 'CENTER', 0, 1)
	totem[slot].Time:SetFont(NAMEPLATE_FONT, 14, "THINOUTLINE")
	-- Multipliers
	if (totem[slot].bg.multiplier) then
		local mu = totem[slot].bg.multiplier
		local r, g, b = totem[slot]:GetStatusBarColor()
		r, g, b = r*mu, g*mu, b*mu
		totem[slot].bg:SetVertexColor(r, g, b) 
	end
	
	totem[slot].ID = slot
	-- If we have a totem then set his value 
	if(haveTotem) then
		if totem[slot].Name then
			totem[slot].Name:SetText(Abbrev(name))
		end					
		if(duration >= 0) then
			if duration == 0 then
				totem[slot]:SetValue(0)
			else
				totem[slot]:SetValue(1 - ((GetTime() - startTime) / duration))
			end
			-- Status bar update
			totem[slot]:SetScript("OnUpdate", function(self,elapsed)
				total = total + elapsed
				if total >= delay then
					total = 0
					haveTotem, name, startTime, duration, totemIcon = GetTotemInfo(self.ID)
					if ((GetTime() - startTime) == 0) or (duration == 0) then
						self:SetValue(0)
					else
						self:SetValue(1 - ((GetTime() - startTime) / duration))
					end
				end
				Timer = startTime + duration - GetTime()
				if haveTotem then
					if Timer > 0 then
						self.Time:SetFormattedText("%d", Timer)
					else
						self.Time:SetText(" ")
					end
				else
					self.Time:SetText(" ")
				end
			end)
		else
			-- There's no need to update because it doesn't have any duration
			totem[slot]:SetScript("OnUpdate",nil)
			totem[slot]:SetValue(0)
		end
	else
		-- No totem = no time
		if totem[slot].Name then
			totem[slot].Name:SetText(" ")
		end
		totem[slot]:SetValue(0)
	end
end

local function Update(self, unit)
	-- Update every slot on login, still have issues with it
	for i = 1, 4 do 
		UpdateSlot(self, i)
	end
end

local function Event(self,event,...)
	if event == "PLAYER_TOTEM_UPDATE" then
		UpdateSlot(self, ...)
	end
end

local function Enable(self, unit)
	local totem = self.TotemBar
	
	if(totem) then
		self:RegisterEvent("PLAYER_TOTEM_UPDATE" , Event, true)
		totem.colors = setmetatable(totem.colors or {}, {__index = colors})
		delay = totem.delay or delay
		if totem.Destroy then
			InitDestroy(self)
		end		
		TotemFrame:UnregisterAllEvents()
		return true
	end	
end

local function Disable(self,unit)
	local totem = self.TotemBar
	if(totem) then
		self:UnregisterEvent("PLAYER_TOTEM_UPDATE", Event)
		
		TotemFrame:Show()
	end
end
			
oUF:AddElement("TotemBar",Update,Enable,Disable)
  Reply With Quote