/Tukui/modules/databars/durability.lua

http://github.com/Asphyxia/Tukui · Lua · 74 lines · 62 code · 11 blank · 1 comment · 13 complexity · b9df9cc9b6da7429bc452793028f5032 MD5 · raw file

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