2a7fd87 - Change to UnitIsPlayer
[wowui.git] / Indicators.lua
1 local media = LibStub:GetLibrary("LibSharedMedia-3.0");
2 local f = RaidFrameCustomization.frames;
3 local positions = RaidFrameCustomization.positions;
4 local pad = 2;
5 local paddings = {
6     TOPLEFT = {pad, -pad},
7     TOPRIGHT = {-pad, -pad},
8     CENTER = {0, 0},
9     BOTTOMLEFT = {pad, pad},
10     BOTTOMRIGHT = {-pad, pad}
11 };
12 local watchedAuras; -- all watched auras
13 local indicatorAuras; -- watched auras per indicator
14 local auraFilters = {"HELPFUL", "HARMFUL"};
15 local DEFAULT_ICON = "Interface\\AddOns\\RaidFrameCustomization\\images\\rhomb";
16 local _;
17
18 -- global functions used every update
19 local GetTime = GetTime;
20 local UnitAura = UnitAura;
21 local UnitIsPlayer = UnitIsPlayer;
22 local UnitIsConnected = UnitIsConnected;
23 local UnitIsDeadOrGhost = UnitIsDeadOrGhost;
24 local CompactRaidFrameContainer_ApplyToFrames = CompactRaidFrameContainer_ApplyToFrames;
25
26 -- list of important auras TODO try to use spellIDs
27 local centerAuras = {
28     "Power Word: Shield"
29 };
30
31 local function configureIndicators(frame)
32     local frameName = frame:GetName();
33     if not f[frameName] then return end
34
35     local config = RaidFrameCustomization.db.profile;
36     local font = media and media:Fetch('font', config.indicatorFont) or STANDARD_TEXT_FONT;
37     for pos, ind in pairs(f[frameName]) do
38         ind.text:SetFont(font, config[pos]["textSize"]);
39         ind.text:SetTextColor(unpack(config[pos]["color"]));
40         ind.icon:SetWidth(config[pos]["iconSize"]);
41         ind.icon:SetHeight(config[pos]["iconSize"]);
42         ind.icon:SetTexture(DEFAULT_ICON);
43         if config[pos]["showIcon"] then
44             ind.icon:Show();
45         else
46             ind.icon:Hide();
47         end
48     end
49 end
50
51 -- Create the FontStrings used for indicators
52 local function setupCompactUnitFrame(frame)
53     local name = frame:GetName();
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);
63 end
64
65 -- Get all unit auras TODO change to event driven, only remaining updating with timer
66 local function getAuras(unit)
67     local unitAuras = {};
68     local auraName, icon, count, expires, caster, debuffType, spellId;
69     local filter;
70
71     for _, filter in ipairs(auraFilters) do
72         local i = 1;
73         while true do
74             auraName, _, icon, count, debuffType, _, expires, caster, _, _, spellId = UnitAura(unit, i, filter);
75             if not spellId then break end
76             if watchedAuras[auraName] or watchedAuras[spellId] or watchedAuras[debuffType] then
77                 local aura = {};
78                 aura.auraName = auraName;
79                 aura.spellId = spellId;
80                 aura.count = count;
81                 aura.expires = expires;
82                 aura.mine = UnitIsPlayer(caster);
83                 aura.icon = icon;
84                 aura.debuffType = debuffType;
85                 table.insert(unitAuras, aura);
86             end
87             i = i + 1;
88         end
89     end
90     return unitAuras;
91 end
92
93 -- Check the indicators on a frame and update the times on them
94 local function updateIndicators(frame)
95     if not frame.unit then return end
96     local unit = frame.unit;
97     local frameName = frame:GetName();
98
99     -- Check if the indicator object exists, else create it
100     if not f[frameName] then setupCompactUnitFrame(frame) end
101     -- Hide if unit is dead/disconnected
102     if (not UnitIsConnected(unit)) or UnitIsDeadOrGhost(frame.displayedUnit) then
103         for _, ind in pairs(f[frameName]) do
104             ind.text:SetText("");
105             ind.icon:SetTexture("");
106         end
107         return;
108     end
109
110     local unitAuras = getAuras(unit);
111     for pos, ind in pairs(f[frameName]) do
112         -- try to find matching aura
113         local found, aura;
114         for _, aura in pairs(unitAuras) do
115             if indicatorAuras[pos][aura.auraName] or indicatorAuras[pos][aura.spellId] or
116                indicatorAuras[pos][aura.debuffType] then
117                 found = aura;
118                 -- break on first matching buff/debuff cast by me
119                 -- otherwise continue through
120                 if aura.mine then break end
121             end
122         end
123
124         local config = RaidFrameCustomization.db.profile[pos];
125         if found then
126             if config.mine and not found.mine then
127                 -- don't show
128                 ind.icon:SetTexture("");
129                 ind.text:SetText("");
130             else
131                 if config.showIcon and not config.useDefaultIcon then
132                     -- show icon TODO coloring
133                     ind.icon:SetTexture(found.icon);
134                 end
135                 if config.showText then
136                     -- show text
137                     local text;
138                     local remaining = found.expires - GetTime();
139                     if remaining > 60 then
140                         text = string.format("%dm", ceil(remaining/60));
141                     else
142                         text = string.format("%d", floor(remaining+0.5));
143                     end
144
145                     if config.stack and found.count > 0 then
146                         if text then
147                             text = found.count.."-"..text;
148                         else
149                             text = found.count;
150                         end
151                     end
152
153                     ind.text:SetText(text);
154                 end
155             end
156         else
157             -- not found, show nothing
158             ind.icon:SetTexture("");
159             ind.text:SetText("");
160         end
161     end
162 end
163
164 -- Update all indicators
165 function RaidFrameCustomization:UpdateAllIndicators()
166     CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", updateIndicators);
167 end
168
169 -- Used to update everything that is affected by the configuration
170 function RaidFrameCustomization:RefreshConfig()
171     self:OnDisable(); -- clear everything
172     if self.db.profile.enabled then
173         CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", configureIndicators);
174         -- Format aura strings
175         watchedAuras = {};
176         indicatorAuras = {};
177         for _, pos in ipairs(positions) do
178             indicatorAuras[pos] = {};
179             for _, aura in ipairs(self.db.profile[pos]["auras"]) do
180                 watchedAuras[aura] = true;
181                 indicatorAuras[pos][aura] = true;
182             end
183         end
184
185         if next(watchedAuras) ~= nil then
186             self.updateTimer = self:ScheduleRepeatingTimer("UpdateAllIndicators", 0.15);
187         end
188     end
189 end