a7ed5dd - Fix target cast bar when changing targets
[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", UIParent);
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, parent)
146     local frame = CreateFrame("Frame", name, parent);
147     frame:SetPoint("TOPLEFT", parent, "BOTTOMLEFT", config.x, config.y+config.height);
148     frame:SetPoint("BOTTOMRIGHT", parent, "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     Indicators:SetPoint("BOTTOMLEFT");
168     Indicators:SetWidth(1);
169     Indicators:SetHeight(1);
170     currentSpec = GetSpecialization();
171     local name, realm = UnitFullName("player");
172     if chars[realm] and chars[realm][name] then
173         for _, config in pairs(chars[realm][name]) do
174             table.insert(settings, config)
175         end
176     end
177     for i, config in pairs(settings) do
178         if config.unit then
179             if not units[config.unit] then units[config.unit] = {} end
180             table.insert(units[config.unit], i);
181         end
182         if config.totems then
183             for _, slot in pairs(config.totems) do
184                 if not totems[slot] then totems[slot] = {} end
185                 table.insert(totems[slot], i);
186             end
187         end
188         frames[i] = createTMW("OmaTMW"..i, config, Indicators);
189     end
190
191     for _, frame in pairs(frames) do
192         if frame.auras then updateAuraFrame(frame) end
193         if frame.totems then updateTotemFrame(frame) end
194     end
195 end
196
197 -- TODO have healthstone icon for player separate from this
198 Indicators:RegisterEvent("UNIT_AURA");
199 Indicators:RegisterEvent("PLAYER_TARGET_CHANGED");
200 Indicators:RegisterEvent("PLAYER_TOTEM_UPDATE");
201 Indicators:RegisterEvent("INSTANCE_ENCOUNTER_ENGAGE_UNIT");
202 Indicators:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED");
203 Indicators:RegisterEvent("PLAYER_LOGIN");
204 Indicators:SetScript("OnEvent", function(self, event, arg1)
205     if event == "UNIT_AURA" then
206         updateAuras(arg1);
207     elseif event == "PLAYER_TARGET_CHANGED" then
208         updateAuras("target");
209     elseif event == "PLAYER_TOTEM_UPDATE" then
210         updateTotems(arg1);
211     elseif event == "INSTANCE_ENCOUNTER_ENGAGE_UNIT" then
212         for _, boss in pairs(bosses) do
213             updateAuras(boss);
214         end
215     elseif event == "PLAYER_SPECIALIZATION_CHANGED" then
216         currentSpec = GetSpecialization();
217         for _, frame in pairs(frames) do
218             if frame.auras then updateAuraFrame(frame) end
219             if frame.totems then updateTotemFrame(frame) end
220         end
221     elseif event == "PLAYER_LOGIN" then
222         initialize();
223     end
224 end);