PageRenderTime 132ms CodeModel.GetById 70ms app.highlight 5ms RepoModel.GetById 56ms app.codeStats 0ms

/Tukui/modules/databars/durability.lua

http://github.com/Asphyxia/Tukui
Lua | 74 lines | 62 code | 11 blank | 1 comment | 9 complexity | b9df9cc9b6da7429bc452793028f5032 MD5 | raw file
 1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
 2
 3local ADDON_NAME, ns = ...
 4local oUF = ns.oUF or oUF
 5
 6ns._Objects = {}
 7ns._Headers = {}
 8
 9if not C["databars"].durability or C["databars"].durability == 0 then return end
10local barNum = C["databars"].durability
11
12T.databars[barNum]:Show()
13
14local Stat = CreateFrame("Frame", nil, T.databars[barNum])
15Stat:EnableMouse(true)
16Stat:SetFrameStrata("BACKGROUND")
17Stat:SetFrameLevel(4)
18
19local StatusBar = T.databars[barNum].statusbar
20local Text = T.databars[barNum].text
21
22local Total = 0
23local current, max
24
25local function OnEvent(self)
26	for i = 1, 11 do
27		if GetInventoryItemLink("player", L.Slots[i][1]) ~= nil then
28			current, max = GetInventoryItemDurability(L.Slots[i][1])
29			if current then 
30				L.Slots[i][3] = current/max
31				Total = Total + 1
32			end
33		end
34	end
35	table.sort(L.Slots, function(a, b) return a[3] < b[3] end)
36	
37	if Total > 0 then
38		local r, g, b
39		r, g, b = oUF.ColorGradient(floor(L.Slots[1][3]*100)/100, 0.8,0.2,0.2, 0.8,0.8,0.2, 0.2,0.8,0.2)
40		Text:SetText(floor(L.Slots[1][3]*100).."% "..T.datacolor..L.datatext_armor)
41		StatusBar:SetStatusBarColor(r,g,b)
42		StatusBar:SetMinMaxValues(0, 100)
43		StatusBar:SetValue(floor(L.Slots[1][3]*100))
44	else
45		StatusBar:SetStatusBarColor(.2, .8, .2)
46		Text:SetText(L.datatext_armor)
47	end
48	-- Setup Durability Tooltip
49	self:SetAllPoints(T.databars[barNum])
50	Total = 0
51end
52
53Stat:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
54Stat:RegisterEvent("MERCHANT_SHOW")
55Stat:RegisterEvent("PLAYER_ENTERING_WORLD")
56Stat:SetScript("OnMouseDown", function() ToggleCharacter("PaperDollFrame") end)
57Stat:SetScript("OnEvent", OnEvent)
58Stat:SetScript("OnEnter", function(self)
59	if not InCombatLockdown() then
60		local xoff, yoff = T.DataBarTooltipAnchor(barNum)
61		GameTooltip:SetOwner(T.databars[barNum], "ANCHOR_BOTTOMRIGHT", xoff, yoff)
62		
63		GameTooltip:ClearLines()
64		for i = 1, 11 do
65			if L.Slots[i][3] ~= 1000 then
66				green = L.Slots[i][3]*2
67				red = 1 - green
68				GameTooltip:AddDoubleLine(L.Slots[i][2], floor(L.Slots[i][3]*100).."%",1 ,1 , 1, red + 1, green, 0)
69			end
70		end
71		GameTooltip:Show()
72	end
73end)
74Stat:SetScript("OnLeave", function() GameTooltip:Hide() end)