929b960aacdf7a882fd55c9eb8b733b40d0c34f5
[wowui.git] / OmaRF / Indicators.lua
1 local f = OmaRF.frames;
2 local positions = OmaRF.positions;
3 local pad = 2;
4 local paddings = {
5     TOPLEFT = {pad, -pad},
6     TOPRIGHT = {-pad, -pad},
7     CENTER = {0, 0},
8     BOTTOMLEFT = {pad, pad},
9     BOTTOMRIGHT = {-pad, pad}
10 };
11 local watchedAuras; -- all watched auras
12 local auraFilters = {"HELPFUL", "HARMFUL"};
13 local DEFAULT_ICON = "Interface\\AddOns\\OmaRF\\images\\rhomb";
14 local _;
15
16 -- global functions used every update
17 local C_TimerAfter = C_Timer.After;
18 local GetTime = GetTime;
19 local UnitAura = UnitAura;
20 local UnitIsPlayer = UnitIsPlayer;
21 local UnitIsConnected = UnitIsConnected;
22 local UnitIsDeadOrGhost = UnitIsDeadOrGhost;
23 local CompactRaidFrameContainer_ApplyToFrames = CompactRaidFrameContainer_ApplyToFrames;
24 local format = string.format;
25 local unpack = unpack;
26
27 -- list of important auras TODO try to use spellIDs
28 local centerAuras = {
29     "Power Word: Shield"
30 };
31
32 local function configureIndicators(frame, name)
33     local frameName = name or frame:GetName();
34     if not f[frameName] then return end
35
36     local config = OmaRF.db.profile.indicators;
37     for pos, ind in pairs(f[frameName]) do
38         ind.text:SetFont(STANDARD_TEXT_FONT, config[pos]["textSize"]);
39         ind.text:SetTextColor(unpack(config[pos]["textColor"]));
40         ind.icon:SetWidth(config[pos]["iconSize"]);
41         ind.icon:SetHeight(config[pos]["iconSize"]);
42         ind.icon:SetTexture(DEFAULT_ICON);
43         ind.icon:SetVertexColor(unpack(config[pos]["iconColor"]));
44         if config[pos]["showIcon"] then
45             ind.icon:Show();
46         else
47             ind.icon:Hide();
48         end
49     end
50 end
51
52 -- Create the FontStrings used for indicators
53 local function setupCompactUnitFrame(frame, name)
54     f[name] = {};
55     for _, pos in ipairs(positions) do
56         f[name][pos] = {};
57         f[name][pos].text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
58         f[name][pos].text:SetPoint(pos, frame, pos, paddings[pos][1], paddings[pos][2]);
59         f[name][pos].icon = frame:CreateTexture(nil, "OVERLAY");
60         f[name][pos].icon:SetPoint(pos, frame, pos, paddings[pos][1], paddings[pos][2]);
61     end
62     configureIndicators(frame, name);
63 end
64
65 -- Check the indicators on a frame and update the times on them
66 local function updateIndicators(frame)
67     local frameName = frame:GetName();
68     local unit = frame.unit;
69     if not unit then return end -- possible if the frame is just being hidden
70
71     -- Create indicators if needed
72     if not f[frameName] then setupCompactUnitFrame(frame, frameName) end
73     -- Reset current
74     for _, ind in pairs(f[frameName]) do
75         ind.text:SetText("");
76         ind.icon:SetTexture("");
77     end
78     -- Hide if unit is dead/disconnected
79     if (not UnitIsConnected(unit)) or UnitIsDeadOrGhost(frame.displayedUnit) then
80         return;
81     end
82
83     local name, icon, count, debuff, expires, caster, id;
84     local current = GetTime();
85     for _, filter in ipairs(auraFilters) do
86         local i = 1;
87         while true do
88             name, _, icon, count, debuff, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
89             if not id then break end
90             local pos = watchedAuras[name] or watchedAuras[id] or watchedAuras[debuff];
91             if pos then
92                 local ind = f[frameName][pos];
93                 local config = OmaRF.db.profile.indicators[pos];
94                 if not config.mine or UnitIsPlayer(caster) then
95                     if config.showIcon then
96                         -- show icon
97                         if config.useDefaultIcon then
98                             ind.icon:SetTexture(DEFAULT_ICON);
99                         else
100                             ind.icon:SetTexture(icon);
101                         end
102                     end
103                     if config.showText then
104                         -- show text
105                         local text;
106                         local remaining = expires - current;
107                         if remaining > 60 then
108                             text = format("%dm", ceil(remaining/60));
109                         else
110                             text = format("%d", floor(remaining+0.5));
111                         end
112                         if count > 1 and config.stack then
113                             if text then
114                                 text = count.."-"..text;
115                             else
116                                 text = count;
117                             end
118                         end
119
120                         ind.text:SetText(text);
121                     end
122                 end
123             end
124             i = i + 1;
125         end
126     end
127 end
128
129 -- Update all indicators
130 function OmaRF:UpdateAllIndicators()
131     CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", updateIndicators);
132     if OmaRF.running then
133         C_TimerAfter(0.15, OmaRF.UpdateAllIndicators);
134     end
135 end
136
137 -- Used to update everything that is affected by the configuration
138 function OmaRF:RefreshConfig()
139     self:OnDisable(); -- clear everything
140     if self.db.profile.enabled then
141         CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", configureIndicators);
142         -- Format aura strings
143         watchedAuras = {};
144         for _, pos in ipairs(positions) do
145             for _, aura in ipairs(self.db.profile.indicators[pos]["auras"]) do
146                 watchedAuras[aura] = pos; -- TODO single aura only in one position
147             end
148         end
149
150         if next(watchedAuras) ~= nil then
151             self.running = true;
152             C_TimerAfter(0.15, self.UpdateAllIndicators);
153         end
154     end
155 end