290e5ed - Ready check support
[wowui.git] / OmaRF / RaidFrame.lua
1 -- RaidFrame.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 STANDARD_TEXT_FONT = STANDARD_TEXT_FONT;
9
10 local registerEvents = OmaRFEvents.RegisterEvents;
11 local unitEvent = OmaRFEvents.UnitEvent;
12
13 local Settings = OmaRFSettings;
14 local indSize = Settings.IndSize;
15 local baseColor = Settings.BaseColor;
16 local bgColor = Settings.BgColor;
17 local healthColor = Settings.HealthColor;
18 local shieldColor = Settings.ShieldColor;
19 local shieldhlColor = Settings.ShieldhlColor;
20 local healpredColor = Settings.HealpredColor;
21 local healabsorbColor = Settings.HealabsorbColor;
22 -- placeholders with visible values when error happens
23 local positions = {};
24 local width, height = 10, 10;
25 local anchorX, anchorY = 10, 10;
26 local attributes = {};
27
28 local CFrame = CreateFrame("Frame", "OmaRFFrame", UIParent);
29 local inheritedFrames = "SecureUnitButtonTemplate,SecureHandlerStateTemplate";
30 local party = {};
31 local raid = {};
32
33 local M = {};
34 OmaRaidFrame = M;
35
36 local function setupIndicators(frame)
37     frame.inds = CreateFrame("Frame", nil, frame);
38     frame.inds:SetAllPoints();
39     frame.inds:Hide();
40     for _, pos in pairs(positions) do
41         frame.inds[pos] = frame.inds:CreateTexture(nil, "OVERLAY");
42         frame.inds[pos]:SetPoint(pos, frame.inds, pos);
43         frame.inds[pos]:SetWidth(indSize);
44         frame.inds[pos]:SetHeight(indSize);
45         frame.inds[pos]:SetTexture("Interface\\AddOns\\OmaRF\\images\\rhomb");
46         frame.inds[pos]:SetVertexColor(1, 0, 0);
47         frame.inds[pos]:Hide();
48         frame.inds[pos].text = frame.inds:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
49         frame.inds[pos].text:SetPoint("BOTTOMRIGHT", frame.inds[pos], "BOTTOMRIGHT");
50         frame.inds[pos].text:Hide();
51     end
52     frame.major = CreateFrame("Frame", nil, frame);
53     frame.major:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -indSize + 4);
54     frame.major:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT");
55     for i = 1,3 do
56         frame.major[i] = frame.major:CreateTexture(nil, "OVERLAY");
57         if i == 1 then
58             frame.major[i]:SetPoint("TOPLEFT", frame.major, "TOPLEFT");
59         else
60             frame.major[i]:SetPoint("TOPLEFT", frame.major[i-1], "TOPRIGHT");
61         end
62         frame.major[i]:SetWidth(indSize*2);
63         frame.major[i]:SetHeight(indSize*2);
64         frame.major[i]:Hide();
65         frame.major[i].text = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
66         frame.major[i].text:SetPoint("BOTTOMRIGHT", frame.major[i], "BOTTOMRIGHT");
67         frame.major[i].text:Hide();
68         frame.major[i].stack = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
69         frame.major[i].stack:SetPoint("TOPLEFT", frame.major[i], "TOPLEFT");
70         frame.major[i].stack:Hide();
71     end
72 end
73
74 local function unitUpdate(self, elapsed)
75     -- there's no in/out of range event, have to check each frame
76     -- from FrameXML/CompactUnitFrame.lua
77     local inRange, checked = UnitInRange(self.displayed);
78     if checked and not inRange then
79         self:SetAlpha(0.55);
80     else
81         self:SetAlpha(1);
82     end
83 end
84
85 local function frameShow(frame)
86     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
87     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
88     frame:RegisterEvent("UNIT_ENTERED_VEHICLE");
89     frame:RegisterEvent("UNIT_EXITED_VEHICLE");
90     frame:RegisterEvent("UNIT_PET");
91     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
92     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
93     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
94     frame:RegisterEvent("READY_CHECK");
95     frame:RegisterEvent("READY_CHECK_CONFIRM");
96     frame:RegisterEvent("READY_CHECK_FINISHED");
97     registerEvents(frame);
98     frame:SetScript("OnUpdate", unitUpdate);
99     unitEvent(frame, "UPDATE_ALL_BARS", frame.displayed);
100 end
101
102 local function frameHide(frame)
103     frame:UnregisterAllEvents();
104     frame:SetScript("OnUpdate", nil);
105 end
106
107 local function setupFrame(frame, secure, unit)
108     secure:SetAttribute("unit", unit);
109     frame:SetAttribute("unit", unit);
110     frame.unit = unit;
111     frame.displayed = unit;
112     -- hide frame to get initial frameShow call
113     frame:Hide();
114     if unit == "player" then
115         frame.vehicle = "vehicle";
116     else
117         frame.vehicle = unit.."pet";
118     end
119     -- create visuals
120     secure:SetWidth(width+2);
121     secure:SetHeight(height+2);
122     frame:SetWidth(width+2);
123     frame:SetHeight(height+2);
124     frame.base = frame:CreateTexture(nil, "BACKGROUND");
125     frame.base:SetAllPoints();
126     frame.base:SetColorTexture(1, 1, 1);
127     frame.base:SetVertexColor(unpack(baseColor));
128     frame.background = frame:CreateTexture(nil, "BACKGROUND", nil, 1);
129     frame.background:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1);
130     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
131     frame.background:SetColorTexture(unpack(bgColor));
132     frame.health = frame:CreateTexture(nil, "BORDER");
133     frame.health:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
134     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
135     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
136     frame.health:SetVertexColor(unpack(healthColor));
137     frame.health:SetWidth(width);
138     frame.health.max = UnitHealthMax(unit);
139     frame.mana = frame:CreateTexture(nil, "BORDER");
140     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
141     frame.mana:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
142     frame.mana:SetColorTexture(1, 1, 1);
143     frame.mana:SetWidth(width);
144     frame.mana.max = UnitPowerMax(unit);
145     frame.shield = frame:CreateTexture(nil, "BORDER");
146     frame.shield:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
147     frame.shield:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
148     frame.shield:SetColorTexture(unpack(shieldColor));
149     frame.shield:Hide();
150     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
151     frame.shieldhl:SetPoint("TOPLEFT", frame.background, "TOPRIGHT", -1, 0);
152     frame.shieldhl:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 1, 0);
153     frame.shieldhl:SetColorTexture(unpack(shieldhlColor));
154     frame.shieldhl:Hide();
155     frame.healpred = frame:CreateTexture(nil, "ARTWORK");
156     frame.healpred:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
157     frame.healpred:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
158     frame.healpred:SetColorTexture(unpack(healpredColor));
159     frame.healpred:Hide();
160     frame.healabsorb = frame:CreateTexture(nil, "ARTWORK");
161     frame.healabsorb:SetPoint("TOPRIGHT", frame.health, "TOPRIGHT");
162     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT");
163     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
164     frame.healabsorb:Hide();
165     frame.role = frame:CreateTexture(nil, "ARTWORK");
166     frame.role:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", -2, 2);
167     frame.role:SetPoint("TOPLEFT", frame.background, "BOTTOMRIGHT", -14, 14);
168     frame.role:SetTexCoord(0.75, 1, 0, 1);
169     frame.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
170     frame.role:Hide();
171     frame.overlay = frame:CreateTexture(nil, "ARTWORK", nil, 1);
172     frame.overlay:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
173     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
174     frame.overlay:SetColorTexture(1, 1, 1);
175     frame.overlay:Hide();
176     frame.name = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
177     frame.name:SetPoint("CENTER", frame.background, "CENTER", 0, 11);
178     frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
179     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
180     frame.text:SetPoint("CENTER", frame.background, "CENTER", 0, -1);
181     frame.text:Hide();
182     frame.rez = frame:CreateTexture(nil, "OVERLAY");
183     frame.rez:SetPoint("TOPLEFT", frame.background, "CENTER", -12, 12);
184     frame.rez:SetPoint("BOTTOMRIGHT", frame.background, "CENTER", 12, -12);
185     frame.rez:SetTexture("Interface\\RaidFrame\\Raid-Icon-Rez");
186     frame.rez:Hide();
187     frame.ready = frame:CreateTexture(nil, "OVERLAY");
188     frame.ready:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 12);
189     frame.ready:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMLEFT", 12, 0);
190     frame.ready:Hide();
191     -- set up indicators
192     setupIndicators(frame);
193     -- set scripts
194     frame:SetScript("OnShow", frameShow);
195     frame:SetScript("OnHide", frameHide);
196     frame:SetScript("OnEvent", unitEvent);
197     -- set attributes
198     secure:RegisterForClicks("AnyDown");
199     for attr, val in pairs(attributes) do
200         secure:SetAttribute(attr, val);
201     end
202     -- rest give target and menu
203     secure:SetAttribute("*type1", "target");
204     secure:SetAttribute("*type2", "togglemenu");
205 end
206
207 -- vehicle toggle from Shadowed Unit Frames
208 local vehicletoggle = [=[
209     local unit = self:GetAttribute("unit");
210     if unit and newstate == "vehicle" and not UnitTargetsVehicleInRaidUI(unit) then
211         self:SetAttribute("toggleForVehicle", false);
212     else
213         self:SetAttribute("toggleForVehicle", true);
214     end
215 ]=]
216
217 local function initializeParty(parent)
218     local secure = CreateFrame("Button", "OmaPartySecure0", parent, inheritedFrames);
219     local frame = CreateFrame("Frame", "OmaParty0", parent);
220     local unit = "player";
221     secure:SetPoint("TOPLEFT", parent, "TOPLEFT");
222     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
223     setupFrame(frame, secure, unit);
224     RegisterStateDriver(frame, "visibility", "[@player,group:raid] hide; show");
225     RegisterStateDriver(secure, "visibility", "[@player,group:raid] hide; show");
226     RegisterStateDriver(secure, "vehicleui", "[vehicleui] vehicle; no");
227     secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
228     party[0] = {secure=secure, frame=frame};
229     for i = 1,4 do
230         local secure = CreateFrame("Button", "OmaPartySecure"..i, parent, inheritedFrames);
231         local frame = CreateFrame("Frame", "OmaParty"..i, parent);
232         local unit = "party"..i;
233         secure:SetPoint("TOPLEFT", party[i-1].secure, "TOPRIGHT");
234         frame:SetPoint("TOPLEFT", party[i-1].frame, "TOPRIGHT");
235         setupFrame(frame, secure, unit);
236         local visible = format("[@%s,exists,group:raid] hide; [@%s,exists] show; hide", unit, unit);
237         local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
238         RegisterStateDriver(frame, "visibility", visible);
239         RegisterStateDriver(secure, "visibility", visible);
240         RegisterStateDriver(secure, "vehicleui", vehicle);
241         secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
242         party[i] = {secure=secure, frame=frame};
243     end
244 end
245
246 local function initializeRaid(parent)
247     local secure = CreateFrame("Button", "OmaRaidSecure1", parent, inheritedFrames);
248     local frame = CreateFrame("Frame", "OmaRaid1", parent);
249     local unit = "raid1";
250     secure:SetPoint("TOPLEFT", parent, "TOPLEFT");
251     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
252     setupFrame(frame, secure, unit);
253     RegisterUnitWatch(frame);
254     RegisterUnitWatch(secure);
255     local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
256     RegisterStateDriver(secure, "vehicleui", vehicle);
257     secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
258     raid[1] = {secure=secure, frame=frame};
259     for y = 1,7 do
260         local i = y*5+1;
261         local secure = CreateFrame("Button", "OmaRaidSecure"..i, parent, inheritedFrames);
262         local frame = CreateFrame("Frame", "OmaRaid"..i, parent);
263         local unit = "raid"..i;
264         secure:SetPoint("TOPLEFT", raid[i-5].secure, "BOTTOMLEFT");
265         frame:SetPoint("TOPLEFT", raid[i-5].frame, "BOTTOMLEFT");
266         setupFrame(frame, secure, unit);
267         RegisterUnitWatch(frame);
268         RegisterUnitWatch(secure);
269         local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
270         RegisterStateDriver(secure, "vehicleui", vehicle);
271         secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
272         raid[i] = {secure=secure, frame=frame};
273     end
274     for y = 0,7 do
275         for x = 1,4 do
276             local i = y*5+x+1;
277             local secure = CreateFrame("Button", "OmaRaidSecure"..i, parent, inheritedFrames);
278             local frame = CreateFrame("Frame", "OmaRaid"..i, parent);
279             local unit = "raid"..i;
280             secure:SetPoint("TOPLEFT", raid[i-1].secure, "TOPRIGHT");
281             frame:SetPoint("TOPLEFT", raid[i-1].frame, "TOPRIGHT");
282             setupFrame(frame, secure, unit);
283             RegisterUnitWatch(frame);
284             RegisterUnitWatch(secure);
285             local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
286             RegisterStateDriver(secure, "vehicleui", vehicle);
287             secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
288             raid[i] = {secure=secure, frame=frame};
289         end
290     end
291 end
292
293 local function initialize()
294     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
295     CFrame:SetHeight((height+2)*8);
296     CFrame:SetWidth((width+2)*5+1);
297     initializeParty(CFrame);
298     initializeRaid(CFrame);
299 end
300
301 local function loadCharSettings()
302     width, height = Settings.Character.Width, Settings.Character.Height;
303     anchorX, anchorY = Settings.Character.AnchorX, Settings.Character.AnchorY;
304     attributes = Settings.Character.Clickheal;
305     positions = Settings.Character.Positions;
306 end
307
308 CFrame:RegisterEvent("ADDON_LOADED");
309 CFrame:RegisterEvent("PLAYER_LOGIN");
310 CFrame:SetScript("OnEvent", function(self, event)
311     if event == "PLAYER_LOGIN" then
312         initialize();
313     elseif event == "ADDON_LOADED" then
314         OmaRFLoadChar();
315         loadCharSettings();
316         OmaRFEvents.LoadChar();
317         OmaRFIndicators.LoadChar();
318     end
319 end);