PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 1ms

/Tukui/modules/unitframes/plugins/oUF_TotemBar/oUF_TotemBar.lua

http://github.com/Asphyxia/Tukui
Lua | 162 lines | 111 code | 22 blank | 29 comment | 20 complexity | 7b9ed90c1405b2077329ffb8f5589516 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2if C.unitframes.enable ~= true or T.myclass ~= "SHAMAN" then return end
  3--[[
  4	Documentation:
  5	
  6		Element handled:
  7			.TotemBar (must be a table with statusbar inside)
  8		
  9		.TotemBar only:
 10			.delay : The interval for updates (Default: 0.1)
 11			.colors : The colors for the statusbar, depending on the totem
 12			.Name : The totem name
 13			.Destroy (boolean): Enables/Disable the totem destruction on right click
 14			
 15			NOT YET IMPLEMENTED
 16			.Icon (boolean): If true an icon will be added to the left or right of the bar
 17			.IconSize : If the Icon is enabled then changed the IconSize (default: 8)
 18			.IconJustify : any anchor like "TOPLEFT", "BOTTOMRIGHT", "TOP", etc
 19			
 20		.TotemBar.bg only:
 21			.multiplier : Sets the multiplier for the text or the background (can be two differents multipliers)
 22
 23--]]
 24local _, ns = ...
 25local oUF = ns.oUF or oUF
 26
 27if not oUF then return end
 28
 29local _, pClass = UnitClass("player")
 30local total = 0
 31local delay = 0.01
 32
 33-- In the order, fire, earth, water, air
 34local colors = {
 35	[1] = {.58,.23,.10},
 36	[2] = {.23,.45,.13},		
 37	[3] = {.19,.48,.60},
 38	[4] = {.42,.18,.74},	
 39}
 40
 41local GetTotemInfo, SetValue, GetTime = GetTotemInfo, SetValue, GetTime
 42	
 43local Abbrev = function(name)	
 44	return (string.len(name) > 10) and string.gsub(name, "%s*(.)%S*%s*", "%1. ") or name
 45end
 46local function TotemOnClick(self,...)
 47	local id = self.ID
 48	local mouse = ...
 49--~ 	print(id, mouse)
 50		if IsShiftKeyDown() then
 51			for j = 1,4 do 
 52				DestroyTotem(j)
 53			end 
 54		else 
 55			DestroyTotem(id) 
 56		end
 57end
 58	
 59local function InitDestroy(self)
 60	local totem = self.TotemBar
 61	for i = 1 , 4 do
 62		local Destroy = CreateFrame("Button",nil, totem[i])
 63		Destroy:SetAllPoints(totem[i])
 64		Destroy:RegisterForClicks("LeftButtonUp", "RightButtonUp")
 65		Destroy.ID = i
 66		Destroy:SetScript("OnClick", TotemOnClick)
 67	end
 68end			
 69local function UpdateSlot(self, slot)
 70	local totem = self.TotemBar
 71
 72
 73	local haveTotem, name, startTime, duration, totemIcon = GetTotemInfo(slot)
 74
 75	totem[slot]:SetStatusBarColor(unpack(totem.colors[slot]))
 76	totem[slot]:SetValue(0)
 77	
 78	-- Multipliers
 79	if (totem[slot].bg.multiplier) then
 80		local mu = totem[slot].bg.multiplier
 81		local r, g, b = totem[slot]:GetStatusBarColor()
 82		r, g, b = r*mu, g*mu, b*mu
 83		totem[slot].bg:SetVertexColor(r, g, b) 
 84	end
 85	
 86	totem[slot].ID = slot
 87	
 88	-- If we have a totem then set his value 
 89	if(haveTotem) then
 90		
 91		if totem[slot].Name then
 92			totem[slot].Name:SetText(Abbrev(name))
 93		end					
 94		if(duration >= 0) then	
 95			totem[slot]:SetValue(1 - ((GetTime() - startTime) / duration))	
 96			-- Status bar update
 97			totem[slot]:SetScript("OnUpdate",function(self,elapsed)
 98					total = total + elapsed
 99					if total >= delay then
100						total = 0
101						haveTotem, name, startTime, duration, totemIcon = GetTotemInfo(self.ID)
102							if ((GetTime() - startTime) == 0) then
103								self:SetValue(0)
104							else
105								self:SetValue(1 - ((GetTime() - startTime) / duration))
106							end
107					end
108				end)					
109		else
110			-- There's no need to update because it doesn't have any duration
111			totem[slot]:SetScript("OnUpdate",nil)
112			totem[slot]:SetValue(0)
113		end 
114	else
115		-- No totem = no time 
116		if totem[slot].Name then
117			totem[slot].Name:SetText(" ")
118		end
119		totem[slot]:SetValue(0)
120	end
121
122end
123
124local function Update(self, unit)
125	-- Update every slot on login, still have issues with it
126	for i = 1, 4 do 
127		UpdateSlot(self, i)
128	end
129end
130
131local function Event(self,event,...)
132	if event == "PLAYER_TOTEM_UPDATE" then
133		UpdateSlot(self, ...)
134	end
135end
136
137local function Enable(self, unit)
138	local totem = self.TotemBar
139	
140	if(totem) then
141		self:RegisterEvent("PLAYER_TOTEM_UPDATE" ,Event)
142		totem.colors = setmetatable(totem.colors or {}, {__index = colors})
143		delay = totem.delay or delay
144		if totem.Destroy then
145			InitDestroy(self)
146		end		
147		TotemFrame:UnregisterAllEvents()		
148		return true
149	end	
150end
151
152local function Disable(self,unit)
153	local totem = self.TotemBar
154	if(totem) then
155		self:UnregisterEvent("PLAYER_TOTEM_UPDATE", Event)
156		
157		TotemFrame:Show()
158	end
159end
160			
161oUF:AddElement("TotemBar",Update,Enable,Disable)
162