PageRenderTime 145ms CodeModel.GetById 80ms app.highlight 14ms RepoModel.GetById 46ms app.codeStats 1ms

/Tukui/modules/buffs/buffs.lua

http://github.com/Asphyxia/Tukui
Lua | 236 lines | 201 code | 33 blank | 2 comment | 36 complexity | 5276bc31c2de916768561ac712e16009 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2if (C.auras.player ~= true) or (C.unitframes.playerauras) then return end
  3
  4local font = C["media"].pixelfont
  5
  6local FormatTime = function(s)
  7	local day, hour, minute = 86400, 3600, 60
  8	if s >= day then
  9		return format("|cffeeeeee%d"..T.datacolor.." d|r", ceil(s / day))
 10	elseif s >= hour then
 11		return format("|cffeeeeee%d"..T.datacolor.." h|r", ceil(s / hour))
 12	elseif s >= minute then
 13		return format("|cffeeeeee%d"..T.datacolor.." m|r", ceil(s / minute))
 14	elseif s >= minute / 12 then
 15		return floor(s)
 16	end
 17	return format("%.1f", s)
 18end
 19
 20local function UpdateTime(self, elapsed)
 21	if(self.expiration) then	
 22		self.expiration = math.max(self.expiration - elapsed, 0)
 23		if(self.expiration <= 0) then
 24			self.time:SetText("")
 25		else
 26			local time = FormatTime(self.expiration)
 27			if self.expiration <= 86400.5 and self.expiration > 3600.5 then
 28				self.time:SetText("|cffcccccc"..time.."|r")
 29			elseif self.expiration <= 3600.5 and self.expiration > 60.5 then
 30				self.time:SetText("|cffcccccc"..time.."|r")
 31			elseif self.expiration <= 60.5 and self.expiration > 10.5 then
 32				self.time:SetText("|cffE8D911"..time.."|r")
 33			elseif self.expiration <= 10.5 then
 34				self.time:SetText("|cffff0000"..time.."|r")
 35			end
 36		end
 37	end
 38end
 39
 40local function UpdateWeapons(button, slot, active, expiration)
 41	if not button.texture then
 42		button.texture = button:CreateTexture(nil, "BORDER")
 43		button.texture:SetAllPoints()
 44		
 45		button.time = button:CreateFontString(nil, "ARTWORK")
 46		button.time:SetPoint("BOTTOM", 0, -5)
 47		button.time:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
 48				
 49		button.bg = CreateFrame("Frame", nil, button)
 50		button.bg:CreatePanel("Default", 30, 30, "CENTER", button, "CENTER", 0, 0)
 51		button.bg:SetFrameLevel(button:GetFrameLevel() - 1)
 52		button.bg:SetFrameStrata(button:GetFrameStrata())
 53		button.bg:CreateShadow("Default")
 54		button.bg:SetAlpha(0)
 55	end
 56	
 57	if active then
 58		button.id = GetInventorySlotInfo(slot)
 59		button.icon = GetInventoryItemTexture("player", button.id)
 60		button.texture:SetTexture(button.icon)
 61		button.texture:SetTexCoord(0.08, 0.92, 0.08, 0.92)
 62		button.expiration = (expiration/1000)
 63		
 64		if UnitHasVehicleUI("player") then
 65			button:SetAlpha(0.3)
 66			button.bg:SetAlpha(0.3)
 67		else
 68			button:SetAlpha(1)
 69			button.bg:SetAlpha(1)
 70		end
 71		
 72		button:SetScript("OnUpdate", UpdateTime)		
 73	elseif not active then
 74		button.texture:SetTexture(nil)
 75		button.time:SetText("")
 76		button.bg:SetAlpha(0)
 77		button:SetScript("OnUpdate", nil)
 78	end
 79end
 80
 81local function UpdateAuras(header, button, weapon)
 82	local name, _, texture, count, dtype, duration, expiration, caster = UnitAura(header:GetAttribute("unit"), button:GetID(), header:GetAttribute("filter"))
 83	
 84	if(not button.texture) then
 85		button.texture = button:CreateTexture(nil, "BORDER")
 86		button.texture:SetAllPoints()
 87
 88		button.count = button:CreateFontString(nil, "ARTWORK")
 89		button.count:SetPoint("BOTTOMRIGHT", -1, 1)
 90		button.count:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
 91
 92		button.time = button:CreateFontString(nil, "ARTWORK")
 93		button.time:SetPoint("BOTTOM", 0, -5)
 94		button.time:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
 95
 96		button:SetScript("OnUpdate", UpdateTime)
 97		
 98		button.bg = CreateFrame("Frame", nil, button)
 99		button.bg:CreatePanel("Default", 30, 30, "CENTER", button, "CENTER", 0, 0)
100		button.bg:SetFrameLevel(button:GetFrameLevel() - 1)
101		button.bg:SetFrameStrata(button:GetFrameStrata())
102		button.bg:CreateShadow("")
103	end
104		
105	if(name) then
106		button.texture:SetTexture(texture)
107		button.texture:SetTexCoord(0.08, 0.92, 0.08, 0.92)
108		button.count:SetText(count > 1 and count or "")
109		button.expiration = expiration - GetTime()
110		
111		if(header:GetAttribute("filter") == "HARMFUL") then
112			local color = DebuffTypeColor[dtype] or DebuffTypeColor.none
113			button.bg:SetBackdropBorderColor(color.r * 3/5, color.g * 3/5, color.b * 3/5)
114		else
115			if caster == "vehicle" then
116				button.bg:SetBackdropBorderColor(75/255,  175/255, 76/255)
117			else
118				button.bg:SetBackdropBorderColor(unpack(C.media.bordercolor))
119			end
120		end
121	end
122	
123	if UnitHasVehicleUI("player") and caster ~= "vehicle" then
124		button:SetAlpha(0.3)
125	else
126		button:SetAlpha(1)
127	end
128end
129
130local function ScanAuras(self, event, unit)
131	if(unit) then
132		if(unit ~= PlayerFrame.unit) then return end
133		if(unit ~= self:GetAttribute("unit")) then
134			self:SetAttribute("unit", unit)
135		end
136	end
137	
138	for index = 1, 32 do		
139		local child = self:GetAttribute("child" .. index)
140		if(child) then
141			UpdateAuras(self, child)
142		end
143	end
144end
145
146local TimeSinceLastUpdate = 1
147local function CheckWeapons(self, elapsed)
148	TimeSinceLastUpdate = TimeSinceLastUpdate + elapsed
149	
150	if (TimeSinceLastUpdate >= 1) then
151		local e1, e1time, _, e2, e2time, _, e3, e3time, _  = GetWeaponEnchantInfo()
152		
153		local w1 = self:GetAttribute("tempEnchant1")
154		local w2 = self:GetAttribute("tempEnchant2")
155		local w3 = self:GetAttribute("tempEnchant3")
156
157		if w1 then UpdateWeapons(w1, "MainHandSlot", e1, e1time) end
158		if w2 then UpdateWeapons(w2, "SecondaryHandSlot", e2, e2time) end
159		if w3 then UpdateWeapons(w3, "RangedSlot", e3, e3time) end
160
161		TimeSinceLastUpdate = 0
162	end
163end
164
165local function CreateAuraHeader(filter, ...)
166	local name	
167	if filter == "HELPFUL" then name = "TukuiPlayerBuffs" else name = "TukuiPlayerDebuffs" end
168
169	local header = CreateFrame("Frame", name, UIParent, "SecureAuraHeaderTemplate")
170	header:RegisterEvent("UNIT_ENTERED_VEHICLE")
171	header:RegisterEvent("UNIT_EXITED_VEHICLE")
172	header:SetPoint(...)
173	header:SetClampedToScreen(true)
174	header:SetMovable(true)
175	header:HookScript("OnEvent", ScanAuras)	
176	header:SetAttribute("unit", "player")
177	header:SetAttribute("sortMethod", "TIME")
178	header:SetAttribute("template", "TukuiAuraTemplate")
179	header:SetAttribute("filter", filter)
180	header:SetAttribute("point", "TOPRIGHT")
181	header:SetAttribute("minWidth", 300)
182	header:SetAttribute("minHeight", 94)
183	header:SetAttribute("xOffset", -36)
184	header:SetAttribute("wrapYOffset", -38)
185	header:SetAttribute("wrapAfter", 11)
186	header:SetAttribute("maxWraps", 3)
187	
188	-- look for weapons buffs
189	if filter == "HELPFUL" then
190		header:SetAttribute("includeWeapons", 1)
191		header:SetAttribute("weaponTemplate", "TukuiAuraTemplate")
192		header:HookScript("OnUpdate", CheckWeapons)
193	end
194	
195	header:SetTemplate("Default")
196	header:SetBackdropColor(0,0,0,0)
197	header:SetBackdropBorderColor(0,0,0,0)
198	header:Show()
199	
200	header.text = T.SetFontString(header, C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
201	header.text:SetPoint("CENTER")
202	if filter == "HELPFUL" then
203		header.text:SetText(L.move_buffs)
204	else
205		header.text:SetText(L.move_debuffs)
206	end	
207	header.text:Hide()
208
209	return header
210end
211
212ScanAuras(CreateAuraHeader("HELPFUL", "TOPRIGHT", -206, -10))
213ScanAuras(CreateAuraHeader("HARMFUL", "TOPRIGHT", -206, -148))
214
215-- create our aura
216local start = CreateFrame("Frame")
217start:RegisterEvent("VARIABLES_LOADED")
218start:SetScript("OnEvent", function(self)
219	local frames = {TukuiPlayerBuffs,TukuiPlayerDebuffs}
220	for i = 1, getn(frames) do
221		local frame = frames[i]
222		local position = frame:GetPoint()
223		if position:match("TOPLEFT") or position:match("BOTTOMLEFT") or position:match("BOTTOMRIGHT") then
224			frame:SetAttribute("point", position)
225		end
226		if position:match("LEFT") then
227			frame:SetAttribute("xOffset", 36)
228		end
229		if position:match("BOTTOM") then
230			frame:SetAttribute("wrapYOffset", 68)
231		end
232		if T.lowversion then
233			frame:SetAttribute("wrapAfter", 8)
234		end
235	end
236end)