public
Description: WoW Addon - Durability display
Homepage: http://www.tekkub.net/
Clone URL: git://github.com/tekkub/tekability.git
Click here to lend your support to: tekability and make a donation at www.pledgie.com !
tekability / tekability.lua
100644 103 lines (77 sloc) 3.186 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local SLOTIDS, FONTSIZE = {}, 12
for _,slot in pairs({"Head", "Shoulder", "Chest", "Waist", "Legs", "Feet", "Wrist", "Hands", "MainHand", "SecondaryHand", "Ranged"}) do SLOTIDS[slot] = GetInventorySlotInfo(slot .. "Slot") end
local frame = CreateFrame("Frame", nil, CharacterFrame)
local dataobj = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("tekability", {icon = "Interface\\Minimap\\Tracking\\Repair", text = "100%"})
local about = LibStub("tekKonfig-AboutPanel").new(nil, "tekability")
 
 
local function RYGColorGradient(perc)
  local relperc = perc*2 % 1
  if perc <= 0 then return 1, 0, 0
  elseif perc < 0.5 then return 1, relperc, 0
  elseif perc == 0.5 then return 1, 1, 0
  elseif perc < 1.0 then return 1 - relperc, 1, 0
  else return 0, 1, 0 end
end
 
 
local fontstrings = setmetatable({}, {
  __index = function(t,i)
    local gslot = _G["Character"..i.."Slot"]
    assert(gslot, "Character"..i.."Slot does not exist")
 
    local fstr = gslot:CreateFontString(nil, "OVERLAY")
    local font, _, flags = NumberFontNormal:GetFont()
    fstr:SetFont(font, FONTSIZE, flags)
    fstr:SetPoint("CENTER", gslot, "BOTTOM", 0, 8)
    t[i] = fstr
    return fstr
  end,
})
 
 
function frame:OnEvent(event, arg1)
  if event == "ADDON_LOADED" and arg1:lower() ~= "tekability" then
    for i,fstr in pairs(fontstrings) do
      -- Re-apply the font, so that we catch any changes to NumberFontNormal by addons like ClearFont
      local font, _, flags = NumberFontNormal:GetFont()
      fstr:SetFont(font, FONTSIZE, flags)
    end
    return
  end
 
  local min = 1
  for slot,id in pairs(SLOTIDS) do
    local v1, v2 = GetInventoryItemDurability(id)
 
    if v1 and v2 and v2 ~= 0 then
      min = math.min(v1/v2, min)
      local str = fontstrings[slot]
      str:SetTextColor(RYGColorGradient(v1/v2))
      str:SetText(string.format("%d%%", v1/v2*100))
    else
      local str = rawget(fontstrings, slot)
      if str then str:SetText(nil) end
    end
  end
 
  local r,g,b = RYGColorGradient(min)
  dataobj.text = string.format("|cff%02x%02x%02x%d%%", r*255, g*255, b*255, min*100)
end
 
 
frame:SetScript("OnEvent", frame.OnEvent)
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
 
 
------------------------
-- Tooltip! --
------------------------
 
local function GetTipAnchor(frame)
  local x,y = frame:GetCenter()
  if not x or not y then return "TOPLEFT", "BOTTOMLEFT" end
  local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
  local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
  return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
end
 
 
function dataobj.OnLeave() GameTooltip:Hide() end
 
 
function dataobj.OnEnter(self)
   GameTooltip:SetOwner(self, "ANCHOR_NONE")
  GameTooltip:SetPoint(GetTipAnchor(self))
  GameTooltip:ClearLines()
 
  GameTooltip:AddLine("tekability")
 
  for slot,id in pairs(SLOTIDS) do
    local v1, v2 = GetInventoryItemDurability(id)
 
    if v1 and v2 and v2 ~= 0 then
      GameTooltip:AddDoubleLine(slot, string.format("%d%%", v1/v2*100), 1, 1, 1, RYGColorGradient(v1/v2))
    end
  end
 
  GameTooltip:Show()
end