001062c - Refector indicators to be more flexible
[wowui.git] / OmaUF / Auras.lua
1 -- Auras.lua
2 local _;
3 local CreateFrame = CreateFrame;
4 local UnitAura = UnitAura;
5 local GameTooltip = nil;
6
7 local auraFilters = {"HELPFUL", "HARMFUL"};
8
9 local M = {};
10 OmaUFAuras = M;
11
12 local function updateTooltip(frame)
13     if GameTooltip:IsOwned(frame) then
14         GameTooltip:SetUnitAura(frame.unit, frame.index, frame.filter);
15     else
16         frame:SetScript("OnUpdate", nil);
17     end
18 end
19
20 local function showTooltip(frame)
21     -- tooltip handling from TargetFrame.xml
22     GameTooltip:SetOwner(frame, "ANCHOR_BOTTOMRIGHT", 15, -25);
23     GameTooltip:SetUnitAura(frame.unit, frame.index, frame.filter);
24     frame:SetScript("OnUpdate", updateTooltip);
25 end
26
27 local function hideTooltip(frame)
28     GameTooltip:Hide();
29     frame:SetScript("OnUpdate", nil);
30 end
31
32 local function createAura(parent, prev, anchor, name, unit)
33     local aura = CreateFrame("Frame", name, parent);
34     aura:SetPoint("TOPLEFT", prev, anchor);
35     aura:SetWidth(16);
36     aura:SetHeight(16);
37     aura.icon = aura:CreateTexture(nil, "ARTWORK");
38     aura.icon:SetAllPoints();
39     aura.cd = CreateFrame("Cooldown", name.."CD", aura, "CooldownFrameTemplate");
40     aura.cd:SetReverse(true);
41     aura.cd:SetHideCountdownNumbers(true);
42     aura.cd:SetAllPoints();
43     aura.unit = unit;
44     aura:SetScript("OnEnter", showTooltip);
45     aura:SetScript("OnLeave", hideTooltip);
46     aura:Hide();
47     return aura;
48 end
49
50 function M.CreateAuraFrame(parent, unit)
51     GameTooltip = _G["GameTooltip"];
52     local name = parent:GetName().."Auras";
53     parent.auras = CreateFrame("Frame", name, parent);
54     parent.auras:SetPoint("TOPLEFT", parent, "BOTTOMLEFT", 0, -8);
55     parent.auras:SetWidth(10);
56     parent.auras:SetHeight(10);
57     local i = 1;
58     -- max auras per row
59     for x=1,10 do
60         local auraName = name..i;
61         if i == 1 then
62             parent.auras[i] = createAura(parent.auras, parent.auras, "TOPLEFT", auraName, unit);
63         else
64             parent.auras[i] = createAura(parent.auras, parent.auras[i-1], "TOPRIGHT", auraName, unit);
65         end
66         i = i + 1;
67     end
68     -- max rows
69     for y=0,1 do
70         for x=1,10 do
71             local auraName = name..i;
72             parent.auras[i] = createAura(parent.auras, parent.auras[y*10+x], "BOTTOMLEFT", auraName, unit);
73             i = i + 1;
74         end
75     end
76 end
77
78 function M.UpdateAuras(frame, unit)
79     local auras = frame.auras;
80     if not auras then return end
81     for _, aura in ipairs(auras) do
82         if not aura:IsShown() then break end
83         aura:Hide();
84     end
85     local icon, count, duration, expires, caster, id;
86     local pos = 1;
87     for _, filter in ipairs(auraFilters) do
88         local i = 1;
89         while true do
90             _, _, icon, count, _, duration, expires, caster, _, _, id = UnitAura(unit, i, filter);
91             if not id or not auras[pos] then break end
92             -- aura filter self-applied, player-applied, list of important auras TODO
93             local aura = auras[pos];
94             aura.icon:SetTexture(icon);
95             aura.index = i;
96             aura.filter = filter;
97             if expires > 0 then
98                 aura.cd:SetCooldown(expires - duration, duration);
99             else
100                 aura.cd:Hide();
101             end
102             aura:Show();
103             pos = pos + 1;
104             i = i + 1;
105         end
106     end
107 end