8da63c0 - Add incoming res icon
[wowui.git] / OmaRF / FrameInit.lua
1 -- FrameInit.lua
2 local _;
3 local unpack, pairs, rawget = unpack, pairs, rawget;
4 local format = string.format;
5 local UnitHealthMax, UnitPowerMax = UnitHealthMax, UnitPowerMax;
6 local UnitInRange = UnitInRange;
7 local CreateFrame, RegisterStateDriver, RegisterUnitWatch = CreateFrame, RegisterStateDriver, RegisterUnitWatch;
8 local PowerTypeMana = Enum.PowerType.Mana;
9 local PowerTypeRage = Enum.PowerType.Rage;
10 local PowerTypeFocus = Enum.PowerType.Focus;
11 local PowerTypeEnergy = Enum.PowerType.Energy;
12 local PowerTypeRunic = Enum.PowerType.RunicPower;
13
14 local unitEvent = nil;
15
16 local M = {};
17 OmaFrames = M;
18
19 -- configurable settings
20 M.Positions = {"TOPRIGHT", "BOTTOMLEFT"};
21 M.Width, M.Height = 80, 40;
22 M.AnchorX, M.AnchorY = 0, -330;
23 M.IndSize = 14; M.BaseColor = {0, 0, 0};
24 M.BgColor = {0.7, 0.7, 0.7};
25 M.HealthColor = {0.3, 0.3, 0.3};
26 M.ShieldColor = {0, 0.7, 1};
27 M.ShieldhlColor = {0.5, 0.8, 1};
28 M.HealpredColor = {0.5, 0.6, 0.5};
29 M.HealabsorbColor = {0.1, 0.1, 0.1};
30 M.OverlayColorDispel = {1, 0.5, 0, 0.5};
31 M.OverlayColorCharm = {0.8, 0, 1, 0.5};
32 M.OverlayColorAlert = {1, 0, 0, 0.5};
33 M.PowerColors = {
34     [PowerTypeMana] = {0, 0.5, 1},
35     [PowerTypeRage] = {1, 0, 0},
36     [PowerTypeFocus] = {1, 0.5, 0},
37     [PowerTypeEnergy] = {1, 0.8, 0},
38     [PowerTypeRunic] = {0.9, 0, 0.1},
39 };
40 -- watch to not remove mana entry
41 setmetatable(M.PowerColors, {__index = function(t) return rawget(t, PowerTypeMana) end});
42
43 local positions = M.Positions;
44 local width, height = M.Width, M.Height;
45 local indSize = M.IndSize;
46 local baseColor = M.BaseColor;
47 local bgColor = M.BgColor;
48 local healthColor = M.HealthColor;
49 local shieldColor = M.ShieldColor;
50 local shieldhlColor = M.ShieldhlColor;
51 local healpredColor = M.HealpredColor;
52 local healabsorbColor = M.HealabsorbColor;
53
54 local function setupIndicators(frame)
55     frame.inds = CreateFrame("Frame", nil, frame);
56     frame.inds:SetAllPoints();
57     frame.inds:Hide();
58     for _, pos in pairs(positions) do
59         frame.inds[pos] = frame.inds:CreateTexture(nil, "OVERLAY");
60         frame.inds[pos]:SetPoint(pos, frame.inds, pos);
61         frame.inds[pos]:SetWidth(indSize);
62         frame.inds[pos]:SetHeight(indSize);
63         frame.inds[pos]:SetTexture("Interface\\AddOns\\OmaRF\\images\\rhomb");
64         frame.inds[pos]:SetVertexColor(1, 0, 0);
65         frame.inds[pos]:Hide();
66         frame.inds[pos].text = frame.inds:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
67         frame.inds[pos].text:SetPoint("BOTTOMRIGHT", frame.inds[pos], "BOTTOMRIGHT");
68         frame.inds[pos].text:Hide();
69     end
70     frame.major = CreateFrame("Frame", nil, frame);
71     frame.major:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -indSize + 4);
72     frame.major:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT");
73     for i = 1,3 do
74         frame.major[i] = frame.major:CreateTexture(nil, "OVERLAY");
75         if i == 1 then
76             frame.major[i]:SetPoint("TOPLEFT", frame.major, "TOPLEFT");
77         else
78             frame.major[i]:SetPoint("TOPLEFT", frame.major[i-1], "TOPLEFT");
79         end
80         frame.major[i]:SetWidth(indSize*2);
81         frame.major[i]:SetHeight(indSize*2);
82         frame.major[i]:Hide();
83         frame.major[i].text = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
84         frame.major[i].text:SetPoint("BOTTOMRIGHT", frame.major[i], "BOTTOMRIGHT");
85         frame.major[i].text:Hide();
86         frame.major[i].stack = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
87         frame.major[i].stack:SetPoint("TOPLEFT", frame.major[i], "TOPLEFT");
88         frame.major[i].stack:Hide();
89     end
90 end
91
92 local function unitUpdate(self, elapsed)
93     -- there's no in/out of range event, have to check each frame
94     -- from FrameXML/CompactUnitFrame.lua
95     local inRange, checked = UnitInRange(self.displayed);
96     if checked and not inRange then
97         self:SetAlpha(0.55);
98     else
99         self:SetAlpha(1);
100     end
101 end
102
103 function M.RegisterEvents(frame)
104     -- events are taken from FrameXML/CompactUnitFrame.lua
105     -- TODO ready check support, raid marker support,
106     -- player flags support (/afk, /dnd)
107     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
108     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
109     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
110     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
111     frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
112     frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
113     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
114     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
115     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
116     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
117     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
118     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
119     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
120     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
121     frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
122 end
123 local registerEvents = M.RegisterEvents;
124
125 local function frameShow(frame)
126     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
127     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
128     frame:RegisterEvent("UNIT_ENTERED_VEHICLE");
129     frame:RegisterEvent("UNIT_EXITED_VEHICLE");
130     frame:RegisterEvent("UNIT_PET");
131     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
132     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
133     registerEvents(frame);
134     frame:SetScript("OnUpdate", unitUpdate);
135     unitEvent(frame, "UPDATE_ALL_BARS", frame.displayed);
136 end
137
138 local function frameHide(frame)
139     frame:UnregisterAllEvents();
140     frame:SetScript("OnUpdate", nil);
141 end
142
143 local function setupFrame(frame, secure, unit)
144     -- create visuals
145     secure:SetWidth(width+2);
146     frame:SetWidth(width+2);
147     frame.base = frame:CreateTexture(nil, "BACKGROUND");
148     frame.base:SetAllPoints();
149     frame.base:SetColorTexture(1, 1, 1);
150     frame.base:SetVertexColor(unpack(baseColor));
151     frame.background = frame:CreateTexture(nil, "BACKGROUND", nil, 1);
152     frame.background:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1);
153     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
154     frame.background:SetColorTexture(unpack(bgColor));
155     frame.health = frame:CreateTexture(nil, "BORDER");
156     frame.health:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
157     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
158     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
159     frame.health:SetVertexColor(unpack(healthColor));
160     frame.health:SetWidth(width);
161     frame.health.max = UnitHealthMax(unit);
162     frame.mana = frame:CreateTexture(nil, "BORDER");
163     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
164     frame.mana:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
165     frame.mana:SetColorTexture(1, 1, 1);
166     frame.mana:SetWidth(width);
167     frame.mana.max = UnitPowerMax(unit);
168     frame.shield = frame:CreateTexture(nil, "BORDER");
169     frame.shield:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
170     frame.shield:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
171     frame.shield:SetColorTexture(unpack(shieldColor));
172     frame.shield:Hide();
173     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
174     frame.shieldhl:SetPoint("TOPLEFT", frame.background, "TOPRIGHT", -1, 0);
175     frame.shieldhl:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 1, 0);
176     frame.shieldhl:SetColorTexture(unpack(shieldhlColor));
177     frame.shieldhl:Hide();
178     frame.healpred = frame:CreateTexture(nil, "ARTWORK");
179     frame.healpred:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
180     frame.healpred:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
181     frame.healpred:SetColorTexture(unpack(healpredColor));
182     frame.healpred:Hide();
183     frame.healabsorb = frame:CreateTexture(nil, "ARTWORK");
184     frame.healabsorb:SetPoint("TOPRIGHT", frame.health, "TOPRIGHT");
185     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT");
186     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
187     frame.healabsorb:Hide();
188     frame.role = frame:CreateTexture(nil, "ARTWORK");
189     frame.role:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", -2, 2);
190     frame.role:SetPoint("TOPLEFT", frame.background, "BOTTOMRIGHT", -14, 14);
191     frame.role:SetTexCoord(0.75, 1, 0, 1);
192     frame.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
193     frame.role:Hide();
194     frame.overlay = frame:CreateTexture(nil, "ARTWORK", nil, 1);
195     frame.overlay:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
196     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
197     frame.overlay:SetColorTexture(1, 1, 1);
198     frame.overlay:Hide();
199     frame.name = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
200     frame.name:SetPoint("CENTER", frame.background, "CENTER", 0, 11);
201     frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
202     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
203     frame.text:SetPoint("CENTER", frame.background, "CENTER", 0, -1);
204     frame.text:Hide();
205     frame.rez = frame:CreateTexture(nil, "OVERLAY");
206     frame.rez:SetPoint("TOPLEFT", frame.background, "CENTER", -12, 12);
207     frame.rez:SetPoint("BOTTOMRIGHT", frame.background, "CENTER", 12, -12);
208     frame.rez:SetTexture("Interface\\RaidFrame\\Raid-Icon-Rez");
209     frame.rez:Hide();
210     setupIndicators(frame);
211     frame:SetScript("OnShow", frameShow);
212     frame:SetScript("OnHide", frameHide);
213     -- set attributes
214     secure:RegisterForClicks("AnyDown");
215     secure:SetAttribute("type1", "spell"); -- left click
216     secure:SetAttribute("type2", "spell"); -- right click
217     secure:SetAttribute("shift-type1", "spell"); -- shift left click
218     secure:SetAttribute("shift-type2", "spell"); -- shift right click
219     secure:SetAttribute("ctrl-type1", "spell"); -- ctrl left click
220     secure:SetAttribute("alt-type2", "spell"); -- alt right click
221     secure:SetAttribute("alt-shift-type1", "spell"); -- alt+shift left click
222     secure:SetAttribute("alt-shift-type2", "spell"); -- alt+shift right click
223     secure:SetAttribute("spell1", "Holy Light");
224     secure:SetAttribute("spell2", "Bestow Faith");
225     secure:SetAttribute("shift-spell1", "Flash of Light");
226     secure:SetAttribute("shift-spell2", "Light of the Martyr");
227     secure:SetAttribute("ctrl-spell1", "Cleanse");
228     secure:SetAttribute("alt-spell2", "Lay on Hands");
229     secure:SetAttribute("alt-shift-spell1", "Beacon of Light");
230     secure:SetAttribute("alt-shift-spell2", "Beacon of Faith");
231     -- rest give target and menu
232     secure:SetAttribute("*type1", "target");
233     secure:SetAttribute("*type2", "togglemenu");
234 end
235
236 -- vehicle toggle from Shadowed Unit Frames
237 local vehicletoggle = [=[
238     local unit = self:GetAttribute("unit");
239     if unit and newstate == "vehicle" and not UnitTargetsVehicleInRaidUI(unit) then
240         print(unit, "in vehicle, still target player");
241         self:SetAttribute("toggleForVehicle", false);
242     else
243         self:SetAttribute("toggleForVehicle", true);
244     end
245 ]=]
246
247 -- TODO reorganizing needed for eventhandler stuff
248 function M.InitializeParty(parent, party, eventhandler)
249     unitEvent = eventhandler;
250     local secure = CreateFrame("Button", "OmaPartySecure0", parent, "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
251     local frame = CreateFrame("Frame", "OmaParty0", parent);
252     local unit = "player";
253     secure:SetAttribute("unit", unit);
254     secure:SetPoint("TOPLEFT", parent, "TOPLEFT");
255     secure:SetHeight(height+2);
256     frame.unit = unit;
257     frame.vehicle = "vehicle";
258     frame.displayed = unit;
259     frame:SetAttribute("unit", unit);
260     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
261     frame:SetHeight(height+2);
262     frame:SetScript("OnEvent", eventhandler);
263     frame:Hide();
264     setupFrame(frame, secure, unit);
265     RegisterStateDriver(frame, "visibility", "[@player,group:raid] hide; show");
266     RegisterStateDriver(secure, "visibility", "[@player,group:raid] hide; show");
267     RegisterStateDriver(secure, "vehicleui", "[vehicleui] vehicle; no");
268     secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
269     party[0] = {secure=secure, frame=frame};
270     for i = 1,4 do
271         local secure = CreateFrame("Button", "OmaPartySecure"..i, parent, "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
272         local frame = CreateFrame("Frame", "OmaParty"..i, parent);
273         local unit = "party"..i;
274         secure:SetAttribute("unit", unit);
275         secure:SetPoint("TOPLEFT", party[i-1].secure, "TOPRIGHT");
276         secure:SetPoint("BOTTOMLEFT", party[i-1].secure, "BOTTOMRIGHT");
277         frame.unit = unit;
278         frame.vehicle = unit.."pet";
279         frame.displayed = unit;
280         frame:SetAttribute("unit", unit);
281         frame:SetPoint("TOPLEFT", party[i-1].frame, "TOPRIGHT");
282         frame:SetPoint("BOTTOMLEFT", party[i-1].frame, "BOTTOMRIGHT");
283         frame:SetScript("OnEvent", unitEvent);
284         frame:Hide();
285         setupFrame(frame, secure, unit);
286         local visible = format("[@%s,exists,group:raid] hide; [@%s,exists] show; hide", unit, unit);
287         local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
288         RegisterStateDriver(frame, "visibility", visible);
289         RegisterStateDriver(secure, "visibility", visible);
290         RegisterStateDriver(secure, "vehicleui", vehicle);
291         secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
292         party[i] = {secure=secure, frame=frame};
293     end
294 end
295
296 function M.InitializeRaid(parent, raid, eventhandler)
297     local secure = CreateFrame("Button", "OmaRaidSecure1", parent, "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
298     local frame = CreateFrame("Frame", "OmaRaid1", parent);
299     local unit = "raid1";
300     secure:SetAttribute("unit", unit);
301     secure:SetPoint("TOPLEFT", parent, "TOPLEFT");
302     secure:SetHeight(height+2);
303     frame.unit = unit;
304     frame.vehicle = unit.."pet";
305     frame.displayed = unit;
306     frame:SetAttribute("unit", unit);
307     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
308     frame:SetHeight(height+2);
309     frame:SetScript("OnEvent", eventhandler);
310     frame:Hide();
311     setupFrame(frame, secure, unit);
312     RegisterUnitWatch(frame);
313     RegisterUnitWatch(secure);
314     local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
315     RegisterStateDriver(secure, "vehicleui", vehicle);
316     secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
317     raid[1] = {secure=secure, frame=frame};
318     for y = 1,7 do
319         local i = y*5+1;
320         local secure = CreateFrame("Button", "OmaRaidSecure"..i, parent, "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
321         local frame = CreateFrame("Frame", "OmaRaid"..i, parent);
322         local unit = "raid"..i;
323         secure:SetAttribute("unit", unit);
324         secure:SetPoint("TOPLEFT", raid[i-5].secure, "BOTTOMLEFT");
325         secure:SetHeight(height+2);
326         frame.unit = unit;
327         frame.vehicle = unit.."pet";
328         frame.displayed = unit;
329         frame:SetAttribute("unit", unit);
330         frame:SetPoint("TOPLEFT", raid[i-5].frame, "BOTTOMLEFT");
331         frame:SetHeight(height+2);
332         frame:SetScript("OnEvent", unitEvent);
333         frame:Hide();
334         setupFrame(frame, secure, unit);
335         RegisterUnitWatch(frame);
336         RegisterUnitWatch(secure);
337         local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
338         RegisterStateDriver(secure, "vehicleui", vehicle);
339         secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
340         raid[i] = {secure=secure, frame=frame};
341     end
342     for y = 0,7 do
343         for x = 1,4 do
344             local i = y*5+x+1;
345             local secure = CreateFrame("Button", "OmaRaidSecure"..i, parent, "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
346             local frame = CreateFrame("Frame", "OmaRaid"..i, parent);
347             local unit = "raid"..i;
348             secure:SetAttribute("unit", unit);
349             secure:SetPoint("TOPLEFT", raid[i-1].secure, "TOPRIGHT");
350             secure:SetPoint("BOTTOMLEFT", raid[i-1].secure, "BOTTOMRIGHT");
351             frame.unit = unit;
352             frame.vehicle = unit.."pet";
353             frame.displayed = unit;
354             frame:SetAttribute("unit", unit);
355             frame:SetPoint("TOPLEFT", raid[i-1].frame, "TOPRIGHT");
356             frame:SetPoint("BOTTOMLEFT", raid[i-1].frame, "BOTTOMRIGHT");
357             frame:SetScript("OnEvent", unitEvent);
358             frame:Hide();
359             setupFrame(frame, secure, unit);
360             RegisterUnitWatch(frame);
361             RegisterUnitWatch(secure);
362             local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
363             RegisterStateDriver(secure, "vehicleui", vehicle);
364             secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
365             raid[i] = {secure=secure, frame=frame};
366         end
367     end
368 end