01c1ae9e4872b1a761bb7a8f6749669b4d7d3b79
[wowui.git] / OmaRF / NewIndicators.lua
1 -- Indicators.lua
2 local unpack, ipairs, pairs, ceil, floor = unpack, ipairs, pairs, ceil, floor;
3 local UnitIsConnected, UnitIsDeadOrGhost = UnitIsConnected, UnitIsDeadOrGhost;
4 local UnitAura, UnitIsPlayer, GetTime = UnitAura, UnitIsPlayer, GetTime;
5 local C_TimerAfter = C_Timer.After;
6 local format = string.format;
7
8 local positions = OmaRF.positions;
9 local auraFilters = {"HELPFUL", "HARMFUL"};
10 local DEFAULT_ICON = "Interface\\AddOns\\OmaRF\\images\\rhomb";
11 local _;
12
13 local frameBase;
14 local frames = {};
15 local watchedAuras = {};
16 local majorAuras = {};
17 local majorMax;
18
19 local function remaining(expires, current)
20     if expires == 0 then return "" end
21     local remain = expires - current;
22     if remain > 60 then
23         return format("%dm", ceil(remain/60));
24     end
25     return floor(remain+0.5);
26 end
27
28 local function showIndicator(ind, icon, caster, expires, current, config)
29     if not config.mine or UnitIsPlayer(caster) then
30         if config.showIcon then
31             if config.useDefaultIcon then
32                 ind.icon:SetTexture(DEFAULT_ICON);
33             else
34                 ind.icon:SetTexture(icon);
35             end
36             if not ind.icon:IsShown() then ind.icon:Show() end
37         end
38         ind.expires = expires;
39         if config.showText then
40             ind.text:SetText(remaining(expires, current));
41             if not ind.text:IsShown() then ind.text:Show() end
42         end
43     end
44 end
45
46 local function showMajorIndicator(ind, icon, count, expires, current)
47     ind.icon:SetTexture(icon);
48     ind.expires = expires;
49     ind.expireText:SetText(remaining(expires, current));
50     if count > 1 then
51         ind.stackText:SetText(count);
52         if not ind.stackText:IsShown() then ind.stackText:Show() end
53     else
54         if ind.stackText:IsShown() then ind.stackText:Hide() end
55     end
56     if not ind.icon:IsShown() then ind.icon:Show() end
57     if not ind.expireText:IsShown() then ind.expireText:Show() end
58 end
59
60 -- update current auras
61 hooksecurefunc("CompactUnitFrame_UpdateAuras", function(unitFrame)
62     local frameName = unitFrame:GetName();
63     if frames[frameName] then
64         local frame = frames[frameName];
65         for _, ind in pairs(frame.inds) do ind.expires = nil end
66         for _, ind in ipairs(frame.majorInds) do ind.expires = nil end
67
68         local name, icon, count, expires, caster, id;
69         local unit = unitFrame.displayedUnit;
70         local majorPos = 1;
71         local current = GetTime();
72         for _, filter in ipairs(auraFilters) do
73             local i = 1;
74             while true do
75                 name, _, icon, count, _, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
76                 if not id then break end
77                 local pos = watchedAuras[id] or watchedAuras[name];
78                 if pos then
79                     showIndicator(
80                         frame.inds[pos], icon, caster, expires, current,
81                         OmaRF.db.profile.indicators[pos]
82                     );
83                 end
84                 if (majorAuras[id] or majorAuras[name]) and majorPos <= majorMax then
85                     showMajorIndicator(frame.majorInds[majorPos], icon, count, expires, current);
86                     majorPos = majorPos + 1;
87                 end
88                 i = i + 1;
89             end
90         end
91     end
92 end);
93
94 -- Check the indicators on a frame and update the times on them
95 local function updateIndicators(frame, name)
96     local unitFrame = _G[name]; -- has to always reference global
97     if unitFrame == nil then
98         return;
99     elseif not unitFrame:IsVisible() then
100         if frame:IsShown() then frame:Hide() end
101         return;
102     end
103
104     local unit = unitFrame.unit;
105     local displayedUnit = unitFrame.displayedUnit;
106     if not unit or not UnitIsConnected(unit) or UnitIsDeadOrGhost(displayedUnit) then
107         if frame:IsShown() then frame:Hide() end
108         return;
109     end
110     if not frame:IsShown() then frame:Show() end
111
112     local current = GetTime();
113     for pos, ind in pairs(frame.inds) do
114         if ind.expires ~= nil then
115             if OmaRF.db.profile.indicators[pos].showText then
116                 ind.text:SetText(remaining(ind.expires, current));
117             end
118         else
119             if ind.icon:IsShown() then ind.icon:Hide() end
120             if ind.text:IsShown() then ind.text:Hide() end
121         end
122     end
123     for _, ind in ipairs(frame.majorInds) do
124         if ind.expires ~= nil then
125             ind.expireText:SetText(remaining(ind.expires, current));
126         else
127             if ind.icon:IsShown() then ind.icon:Hide() end
128             if ind.expireText:IsShown() then ind.expireText:Hide() end
129             if ind.stackText:IsShown() then ind.stackText:Hide() end
130         end
131     end
132 end
133
134 local function configureIndicators(frame)
135     local config = OmaRF.db.profile.indicators;
136     for pos, ind in pairs(frame.inds) do
137         ind.text:SetFont(STANDARD_TEXT_FONT, config[pos]["textSize"]);
138         ind.text:SetTextColor(unpack(config[pos]["textColor"]));
139         ind.icon:SetWidth(config[pos]["iconSize"]);
140         ind.icon:SetHeight(config[pos]["iconSize"]);
141         ind.icon:SetTexture(DEFAULT_ICON);
142         ind.icon:SetVertexColor(unpack(config[pos]["iconColor"]));
143     end
144
145     config = OmaRF.db.profile.majorAuras;
146     for i, ind in ipairs(frame.majorInds) do
147         if i == 1 then
148             ind.icon:ClearAllPoints();
149             ind.icon:SetPoint("CENTER", frame, "CENTER", -config["iconSize"], 0);
150         end
151         ind.icon:SetWidth(config["iconSize"]);
152         ind.icon:SetHeight(config["iconSize"]);
153         ind.expireText:SetFont(STANDARD_TEXT_FONT, config["textSize"], "OUTLINE");
154         ind.stackText:SetFont(STANDARD_TEXT_FONT, config["textSize"], "OUTLINE");
155     end
156 end
157
158 local function createIndicators(frame)
159     frame.inds = {};
160     for _, pos in ipairs(positions) do
161         frame.inds[pos] = {};
162         frame.inds[pos].text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
163         frame.inds[pos].text:SetPoint(pos, frame, pos);
164         frame.inds[pos].icon = frame:CreateTexture(nil, "OVERLAY");
165         frame.inds[pos].icon:SetPoint(pos, frame, pos);
166     end
167
168     frame.majorInds = {};
169     majorMax = OmaRF.db.profile.majorAuras.max;
170     for i = 1, majorMax do
171         frame.majorInds[i] = {};
172         local ind = frame.majorInds[i];
173         ind.icon = frame:CreateTexture(nil, "OVERLAY");
174         if i > 1 then
175             ind.icon:SetPoint("TOPLEFT", frame.majorInds[i-1].icon, "TOPRIGHT");
176         end
177         ind.expireText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
178         ind.expireText:SetPoint("BOTTOMRIGHT", ind.icon, "BOTTOMRIGHT");
179         ind.stackText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
180         ind.stackText:SetPoint("TOPLEFT", ind.icon, "TOPLEFT");
181     end
182
183     configureIndicators(frame);
184 end
185
186 -- Update all indicators
187 local function updateAllIndicators()
188     for name, frame in pairs(frames) do updateIndicators(frame, name) end
189     if OmaRF.running then C_TimerAfter(0.15, updateAllIndicators) end
190 end
191
192 -- Used to update everything that is affected by the configuration
193 function OmaRF:RefreshConfig()
194     self:OnDisable(); -- clear everything
195     if self.db.profile.enabled then
196         for _, f in pairs(frames) do configureIndicators(f) end
197
198         watchedAuras = {};
199         for _, pos in ipairs(positions) do
200             for _, aura in ipairs(self.db.profile.indicators[pos]["auras"]) do
201                 watchedAuras[aura] = pos; -- TODO single aura only in one position
202             end
203         end
204         majorAuras = {};
205         for _, aura in ipairs(self.db.profile.majorAuras["auras"]) do
206             majorAuras[aura] = true;
207         end
208
209         if next(watchedAuras) ~= nil or next(majorAuras) ~= nil then
210             frameBase:Show();
211             self.running = true;
212             C_TimerAfter(0.15, updateAllIndicators);
213         end
214     end
215 end
216
217 local function updateFrames()
218     for name, frame in pairs(frames) do
219         local unitFrame = _G[name];
220         if unitFrame then
221             if not frame.pointsSet then
222                 frame:SetAllPoints(unitFrame);
223                 frame.pointsSet = true;
224             end
225         end
226     end
227 end
228
229 local function initialize(frame)
230     local name = "CompactRaidFrame1";
231     frames[name] = CreateFrame("Frame", "OmaRF1", frameBase);
232     frames[name]:SetAllPoints(name);
233     frames[name]:Hide();
234     createIndicators(frames[name]);
235     local i = 2;
236     for y = 2,5 do
237         local name = "CompactRaidFrame"..i;
238         frames[name] = CreateFrame("Frame", "OmaRF"..i, frameBase);
239         frames[name]:Hide();
240         createIndicators(frames[name]);
241         i = i + 1;
242     end
243     for x = 1,7 do
244         for y = 1,5 do
245             local name = "CompactRaidFrame"..i;
246             frames[name] = CreateFrame("Frame", "OmaRF"..i, frameBase);
247             frames[name]:Hide();
248             createIndicators(frames[name]);
249             i = i + 1;
250         end
251     end
252 end
253
254 local function onEvent(self, event, ...)
255     if event == "GROUP_ROSTER_UPDATE" then
256         -- not sure if fired before LayoutFrames, wait a bit
257         C_TimerAfter(0.01, updateFrames);
258     elseif event == "PLAYER_LOGIN" then
259         initialize();
260     end
261 end
262
263 frameBase = CreateFrame("Frame", nil, UIParent);
264 frameBase:SetFrameStrata("HIGH");
265 frameBase:RegisterEvent("PLAYER_LOGIN");
266 frameBase:RegisterEvent("GROUP_ROSTER_UPDATE");
267 frameBase:SetScript("OnEvent", onEvent);
268 OmaRF.frameBase = frameBase;