0721829 - Remove unused code
[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 UnitIsUnit = UnitIsUnit;
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 = UnitIsUnit(caster, "player");
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                 -- TODO make show text into general setting
136                 -- under which you can select what text to show
137                 if config.showText then
138                     -- show text
139                     local text;
140                     local remaining = found.expires - GetTime();
141                     if remaining > 60 then
142                         text = string.format("%dm", ceil(remaining/60));
143                     else
144                         text = string.format("%d", floor(remaining+0.5));
145                     end
146
147                     if config.stack and found.count > 0 then
148                         if text then
149                             text = found.count.."-"..text;
150                         else
151                             text = found.count;
152                         end
153                     end
154
155                     ind.text:SetText(text);
156                 end
157             end
158         else
159             -- not found, show nothing
160             ind.icon:SetTexture("");
161             ind.text:SetText("");
162         end
163     end
164 end
165
166 -- Update all indicators
167 function RaidFrameCustomization:UpdateAllIndicators()
168     CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", updateIndicators);
169 end
170
171 -- Used to update everything that is affected by the configuration
172 function RaidFrameCustomization:RefreshConfig()
173     self:OnDisable(); -- clear everything
174     if self.db.profile.enabled then
175         CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", configureIndicators);
176         -- Format aura strings
177         watchedAuras = {};
178         indicatorAuras = {};
179         for _, pos in ipairs(positions) do
180             indicatorAuras[pos] = {};
181             for _, aura in ipairs(self.db.profile[pos]["auras"]) do
182                 watchedAuras[aura] = true;
183                 indicatorAuras[pos][aura] = true;
184             end
185         end
186
187         if next(watchedAuras) ~= nil then
188             self.updateTimer = self:ScheduleRepeatingTimer("UpdateAllIndicators", 0.15);
189         end
190     end
191 end