c4088153aacddcf77f2f95d457f040a19dd97c53
[wowui.git] / OmaRF / Indicators.lua
1 -- Indicators.lua
2 local pairs, ipairs = pairs, ipairs;
3 local GetTime = GetTime;
4 local UnitAura = UnitAura;
5 local UnitIsPlayer = UnitIsPlayer;
6 local CTimerAfter = C_Timer.After;
7
8 local watchedAuras = {
9     [53563] = "TOPRIGHT",
10     [156910] = "TOPRIGHT",
11     [200025] = "TOPRIGHT",
12     [200654] = "BOTTOMLEFT",
13 };
14 local majorAuras = {
15     ["Psychic Assault"] = true,
16     ["Everburning Flames"] = true,
17     ["Corrupt"] = true,
18     ["Sleep Canister"] = true,
19     ["Misery"] = true,
20     ["Necrotic Embrace"] = true,
21     ["Fulminating Pulse"] = true,
22     ["Chilled Blood"] = true,
23     ["Soulblight"] = true,
24     ["Soulburst"] = true,
25     ["Soulbomb"] = true,
26     ["Aqua Bomb"] = true,
27 };
28
29 local updaters = {};
30 local positions = {"TOPRIGHT", "BOTTOMLEFT"};
31 local auraFilters = {"HELPFUL", "HARMFUL"};
32
33 -- TODO text
34 --[[local function remaining(expires, current)
35     if expires == 0 then return "" end
36     local remain = expires - current;
37     if remain > 60 then
38         return format("%dm", ceil(remain/60));
39     end
40     return floor(remain+0.5);
41 end]]
42
43 --[[local function updateIndicators(frame, unit)
44     if not frame:IsShown() then
45         return;
46     elseif not UnitIsConnected(unit) or UnitIsDeadOrGhost(unit) then
47         if frame.inds:IsShown() then frame.inds:Hide() end
48         return;
49     elseif not frame.inds:IsShown() then
50         frame.inds:Show();
51     end
52
53     local current = GetTime();
54     for pos, ind in pairs(frame.inds) do
55         if ind.expires ~= nil then
56             if OmaRF.db.profile.indicators[pos].showText then
57                 ind.text:SetText(remaining(ind.expires, current));
58             end
59         else
60             if ind.icon:IsShown() then ind.icon:Hide() end
61             if ind.text:IsShown() then ind.text:Hide() end
62         end
63     end
64     for _, ind in ipairs(frame.majorInds) do
65         if ind.expires ~= nil then
66             ind.expireText:SetText(remaining(ind.expires, current));
67         else
68             if ind.icon:IsShown() then ind.icon:Hide() end
69             if ind.expireText:IsShown() then ind.expireText:Hide() end
70             if ind.stackText:IsShown() then ind.stackText:Hide() end
71         end
72     end
73 end]]
74
75 --local function showIndicator(ind, caster, expires, current, config)
76 local function showIndicator(ind, caster)
77     --if not config.mine or UnitIsPlayer(caster) then
78     if UnitIsPlayer(caster) then
79         --if config.showIcon and not ind:IsShown() then
80         if not ind:IsShown() then
81             ind:Show();
82         end
83         --ind.expires = expires;
84         --[[if config.showText then
85             ind.text:SetText(remaining(expires, current));
86             if not ind.text:IsShown() then ind.text:Show() end
87         end]]
88     end
89 end
90
91 function OmaCheckIndicators(frame, unit)
92     for _, pos in pairs(positions) do
93         --ind.expires = nil;
94         if frame.inds[pos]:IsShown() then frame.inds[pos]:Hide() end
95     end
96     for i = 1,3 do
97         --ind.expires = nil;
98         if frame.major[1]:IsShown() then frame.major[1]:Hide() end
99     end
100     frame.showInds = false;
101     frame.showMajors = false;
102
103     local name, icon, count, expires, caster, id;
104     local majorPos = 1;
105     local current = GetTime();
106     for _, filter in ipairs(auraFilters) do
107         local i = 1;
108         while true do
109             name, _, icon, count, _, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
110             if not id then break end
111             local pos = watchedAuras[id] or watchedAuras[name];
112             if pos then
113                 frame.showInds = true;
114                 if UnitIsPlayer(caster) and not frame.inds[pos]:IsShown() then
115                     frame.inds[pos]:Show();
116                 end
117             end
118             if (majorAuras[id] or majorAuras[name]) and majorPos <= 3 then
119                 frame.major[majorPos]:SetTexture(icon);
120                 frame.major[majorPos]:Show();
121                 frame.showMajors = true;
122                 majorPos = majorPos + 1;
123             end
124             i = i + 1;
125         end
126     end
127     if frame.showInds then
128         if not frame.inds:IsShown() then frame.inds:Show() end
129         -- create a function for updating the indicator each second
130         --[[local func = updaters[frame];
131         if not func then
132             func = function() updateIndicators(frame, unit) end;
133             updaters[frame] = func;
134         end
135         CTimerAfter(1, func);]]
136     elseif frame.inds:IsShown() then
137         frame.inds:Hide();
138     end
139     if frame.showMajors then
140         if not frame.major:IsShown() then frame.major:Show() end
141     elseif frame.major:IsShown() then
142         frame.major:Hide();
143     end
144 end
145