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