a88a019 - Add TellMeWhen replacement
[wowui.git] / OmaAB / TellMeWhen.lua
1 -- TellMeWhen.lua
2 local _;
3 local pairs = pairs;
4 local GetSpecialization = GetSpecialization;
5 local UnitExists = UnitExists;
6 local UnitAura = UnitAura;
7
8 -- character specific frames
9 local chars = {
10     ["Stormreaver"] = {
11         ["Vildan"] = {
12             {
13                 unit = "target",
14                 spec = 3, -- Retribution
15                 auras = {"Judgment"},
16                 auraFilter = "PLAYER HARMFUL",
17                 x = 570, -- placed over Innervate frame
18                 y = 440,
19                 width = 80,
20                 height = 80,
21             },
22             {
23                 unit = "player",
24                 spec = 3, -- Retribution
25                 auras = {"Divine Purpose"},
26                 auraFilter = "PLAYER HELPFUL",
27                 x = 570,
28                 y = 530,
29                 width = 80,
30                 height = 80,
31             },
32             {
33                 unit = "player",
34                 spec = 2, -- Protection
35                 auras = {"Shield of the Righteous"},
36                 auraFilter = "PLAYER HELPFUL",
37                 x = 570,
38                 y = 440,
39                 width = 80,
40                 height = 80,
41             },
42             {
43                 unit = "player",
44                 auras = {"Divine Shield"},
45                 auraFilter = "PLAYER HELPFUL",
46                 x = 660,
47                 y = 440,
48                 width = 80,
49                 height = 80,
50             },
51         },
52     },
53 };
54
55 -- settings entry:
56 -- unit = unitID where to check auras, not required for totem checkers
57 -- spec = player spec index to show frame, if nil show always
58 -- auras = list of auras to track, in priority order
59 -- auraFilter = filter for UnitAura
60 -- totems = list of totem slots to track, in priority order, only checked if auras is nil
61 -- x = x position relative to UIParent bottom left
62 -- y = y position relative to UIParent bottom left
63 -- width = width
64 -- height = height
65
66 -- global frames' settings
67 local settings = {
68     {
69         unit = "player",
70         auras = {"Innervate"},
71         auraFilter = "HELPFUL",
72         x = 570,
73         y = 440,
74         width = 80,
75         height = 80,
76     },
77     {
78         unit = "player",
79         auras = {
80             "Necrotic Embrace", "Flametouched", "Shadowtouched", "Blazing Eruption",
81             "Shattering Scream", "Consuming Hunger", "Unstable Soul", "Time Bomb",
82             "Broken Shard",
83         },
84         auraFilter = "HARMFUL",
85         x = 660,
86         y = 530,
87         width = 80,
88         height = 80,
89     },
90 };
91
92 local frames = {};
93 local units = {}; -- mapping from unitID to frames
94 local totems = {}; -- mapping to frames with totems
95 local bosses = {};
96 for i = 1, MAX_BOSS_FRAMES do
97     bosses[i] = "boss"..i;
98 end
99 local currentSpec = 0; -- 0 is invalid
100
101 local Indicators = CreateFrame("Frame", "OmaTMW");
102
103 local function updateAuraFrame(frame)
104     local unit = frame.unit;
105     if UnitExists(unit) and (not frame.spec or frame.spec == currentSpec) then
106         local name, icon, count, duration, expires;
107         for _, aura in pairs(frame.auras) do
108             name, _, icon, count, _, duration, expires =  UnitAura(unit, aura, nil, frame.auraFilter);
109             if name then
110                 if expires > 0 then
111                     frame.cd:SetCooldown(expires - duration, duration);
112                     frame.cd:Show();
113                 else
114                     frame.cd:Hide();
115                 end
116                 frame.icon:SetTexture(icon);
117                 frame:Show();
118                 return;
119             end
120         end
121     end
122     frame:Hide();
123 end
124
125 local function updateAuras(unit)
126     if units[unit] then
127         for _, i in pairs(units[unit]) do
128             updateAuraFrame(frames[i]);
129         end
130     end
131 end
132
133 local function updateTotemFrame(frame)
134     print("totem frame not implemented");
135 end
136
137 local function updateTotems(slot)
138     if totems[slot] then
139         for _, i in pairs(totems[slot]) do
140             updateTotemFrame(frames[i]);
141         end
142     end
143 end
144
145 local function createTMW(name, config)
146     local frame = CreateFrame("Frame", name, UIParent);
147     frame:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", config.x, config.y+config.height);
148     frame:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMLEFT", config.x+config.width, config.y);
149     frame.unit = config.unit;
150     frame.spec = config.spec;
151     frame.auras = config.auras;
152     frame.auraFilter = config.auraFilter;
153     frame.totems = config.totems;
154     frame:Hide();
155     -- TODO a background, like Masque, stack count
156     frame.icon = frame:CreateTexture(nil, "ARTWORK");
157     frame.icon:SetAllPoints();
158     frame.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93);
159     frame.cd = CreateFrame("Cooldown", name.."CD", frame, "CooldownFrameTemplate");
160     frame.cd:SetReverse(true);
161     frame.cd:SetAllPoints();
162     return frame;
163 end
164
165 local function initialize()
166     Indicators:SetFrameStrata("LOW");
167     currentSpec = GetSpecialization();
168     local name, realm = UnitFullName("player");
169     if chars[realm] and chars[realm][name] then
170         for _, config in pairs(chars[realm][name]) do
171             table.insert(settings, config)
172         end
173     end
174     for i, config in pairs(settings) do
175         if config.unit then
176             if not units[config.unit] then units[config.unit] = {} end
177             table.insert(units[config.unit], i);
178         end
179         if config.totems then
180             for _, slot in pairs(config.totems) do
181                 if not totems[slot] then totems[slot] = {} end
182                 table.insert(totems[slot], i);
183             end
184         end
185         frames[i] = createTMW("OmaTMW"..i, config);
186     end
187
188     for _, frame in pairs(frames) do
189         if frame.auras then updateAuraFrame(frame) end
190         if frame.totems then updateTotemFrame(frame) end
191     end
192 end
193
194 -- TODO have healthstone icon for player separate from this
195 Indicators:RegisterEvent("UNIT_AURA");
196 Indicators:RegisterEvent("PLAYER_TARGET_CHANGED");
197 Indicators:RegisterEvent("PLAYER_TOTEM_UPDATE");
198 Indicators:RegisterEvent("INSTANCE_ENCOUNTER_ENGAGE_UNIT");
199 Indicators:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED");
200 Indicators:RegisterEvent("PLAYER_LOGIN");
201 Indicators:SetScript("OnEvent", function(self, event, arg1)
202     if event == "UNIT_AURA" then
203         updateAuras(arg1);
204     elseif event == "PLAYER_TARGET_CHANGED" then
205         updateAuras("target");
206     elseif event == "PLAYER_TOTEM_UPDATE" then
207         updateTotems(arg1);
208     elseif event == "INSTANCE_ENCOUNTER_ENGAGE_UNIT" then
209         for _, boss in pairs(bosses) do
210             updateAuras(boss);
211         end
212     elseif event == "PLAYER_SPECIALIZATION_CHANGED" then
213         currentSpec = GetSpecialization();
214         for _, frame in pairs(frames) do
215             if frame.auras then updateAuraFrame(frame) end
216             if frame.totems then updateTotemFrame(frame) end
217         end
218     elseif event == "PLAYER_LOGIN" then
219         initialize();
220     end
221 end);