eeb4189 - Update all bars after loading
[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     registerEvents(frame);
95     frame:SetScript("OnUpdate", unitUpdate);
96     unitEvent(frame, "UPDATE_ALL_BARS", frame.displayed);
97 end
98
99 local function frameHide(frame)
100     frame:UnregisterAllEvents();
101     frame:SetScript("OnUpdate", nil);
102 end
103
104 local function setupFrame(frame, secure, unit)
105     secure:SetAttribute("unit", unit);
106     frame:SetAttribute("unit", unit);
107     frame.unit = unit;
108     frame.displayed = unit;
109     -- hide frame to get initial frameShow call
110     frame:Hide();
111     if unit == "player" then
112         frame.vehicle = "vehicle";
113     else
114         frame.vehicle = unit.."pet";
115     end
116     -- create visuals
117     secure:SetWidth(width+2);
118     secure:SetHeight(height+2);
119     frame:SetWidth(width+2);
120     frame:SetHeight(height+2);
121     frame.base = frame:CreateTexture(nil, "BACKGROUND");
122     frame.base:SetAllPoints();
123     frame.base:SetColorTexture(1, 1, 1);
124     frame.base:SetVertexColor(unpack(baseColor));
125     frame.background = frame:CreateTexture(nil, "BACKGROUND", nil, 1);
126     frame.background:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1);
127     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
128     frame.background:SetColorTexture(unpack(bgColor));
129     frame.health = frame:CreateTexture(nil, "BORDER");
130     frame.health:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
131     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
132     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
133     frame.health:SetVertexColor(unpack(healthColor));
134     frame.health:SetWidth(width);
135     frame.health.max = UnitHealthMax(unit);
136     frame.mana = frame:CreateTexture(nil, "BORDER");
137     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
138     frame.mana:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
139     frame.mana:SetColorTexture(1, 1, 1);
140     frame.mana:SetWidth(width);
141     frame.mana.max = UnitPowerMax(unit);
142     frame.shield = frame:CreateTexture(nil, "BORDER");
143     frame.shield:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
144     frame.shield:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
145     frame.shield:SetColorTexture(unpack(shieldColor));
146     frame.shield:Hide();
147     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
148     frame.shieldhl:SetPoint("TOPLEFT", frame.background, "TOPRIGHT", -1, 0);
149     frame.shieldhl:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 1, 0);
150     frame.shieldhl:SetColorTexture(unpack(shieldhlColor));
151     frame.shieldhl:Hide();
152     frame.healpred = frame:CreateTexture(nil, "ARTWORK");
153     frame.healpred:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
154     frame.healpred:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
155     frame.healpred:SetColorTexture(unpack(healpredColor));
156     frame.healpred:Hide();
157     frame.healabsorb = frame:CreateTexture(nil, "ARTWORK");
158     frame.healabsorb:SetPoint("TOPRIGHT", frame.health, "TOPRIGHT");
159     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT");
160     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
161     frame.healabsorb:Hide();
162     frame.role = frame:CreateTexture(nil, "ARTWORK");
163     frame.role:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", -2, 2);
164     frame.role:SetPoint("TOPLEFT", frame.background, "BOTTOMRIGHT", -14, 14);
165     frame.role:SetTexCoord(0.75, 1, 0, 1);
166     frame.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
167     frame.role:Hide();
168     frame.overlay = frame:CreateTexture(nil, "ARTWORK", nil, 1);
169     frame.overlay:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
170     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
171     frame.overlay:SetColorTexture(1, 1, 1);
172     frame.overlay:Hide();
173     frame.name = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
174     frame.name:SetPoint("CENTER", frame.background, "CENTER", 0, 11);
175     frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
176     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
177     frame.text:SetPoint("CENTER", frame.background, "CENTER", 0, -1);
178     frame.text:Hide();
179     frame.rez = frame:CreateTexture(nil, "OVERLAY");
180     frame.rez:SetPoint("TOPLEFT", frame.background, "CENTER", -12, 12);
181     frame.rez:SetPoint("BOTTOMRIGHT", frame.background, "CENTER", 12, -12);
182     frame.rez:SetTexture("Interface\\RaidFrame\\Raid-Icon-Rez");
183     frame.rez:Hide();
184     -- set up indicators
185     setupIndicators(frame);
186     -- set scripts
187     frame:SetScript("OnShow", frameShow);
188     frame:SetScript("OnHide", frameHide);
189     frame:SetScript("OnEvent", unitEvent);
190     -- set attributes
191     secure:RegisterForClicks("AnyDown");
192     for attr, val in pairs(attributes) do
193         secure:SetAttribute(attr, val);
194     end
195     -- rest give target and menu
196     secure:SetAttribute("*type1", "target");
197     secure:SetAttribute("*type2", "togglemenu");
198 end
199
200 -- vehicle toggle from Shadowed Unit Frames
201 local vehicletoggle = [=[
202     local unit = self:GetAttribute("unit");
203     if unit and newstate == "vehicle" and not UnitTargetsVehicleInRaidUI(unit) then
204         self:SetAttribute("toggleForVehicle", false);
205     else
206         self:SetAttribute("toggleForVehicle", true);
207     end
208 ]=]
209
210 local function initializeParty(parent)
211     local secure = CreateFrame("Button", "OmaPartySecure0", parent, inheritedFrames);
212     local frame = CreateFrame("Frame", "OmaParty0", parent);
213     local unit = "player";
214     secure:SetPoint("TOPLEFT", parent, "TOPLEFT");
215     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
216     setupFrame(frame, secure, unit);
217     RegisterStateDriver(frame, "visibility", "[@player,group:raid] hide; show");
218     RegisterStateDriver(secure, "visibility", "[@player,group:raid] hide; show");
219     RegisterStateDriver(secure, "vehicleui", "[vehicleui] vehicle; no");
220     secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
221     party[0] = {secure=secure, frame=frame};
222     for i = 1,4 do
223         local secure = CreateFrame("Button", "OmaPartySecure"..i, parent, inheritedFrames);
224         local frame = CreateFrame("Frame", "OmaParty"..i, parent);
225         local unit = "party"..i;
226         secure:SetPoint("TOPLEFT", party[i-1].secure, "TOPRIGHT");
227         frame:SetPoint("TOPLEFT", party[i-1].frame, "TOPRIGHT");
228         setupFrame(frame, secure, unit);
229         local visible = format("[@%s,exists,group:raid] hide; [@%s,exists] show; hide", unit, unit);
230         local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
231         RegisterStateDriver(frame, "visibility", visible);
232         RegisterStateDriver(secure, "visibility", visible);
233         RegisterStateDriver(secure, "vehicleui", vehicle);
234         secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
235         party[i] = {secure=secure, frame=frame};
236     end
237 end
238
239 local function initializeRaid(parent)
240     local secure = CreateFrame("Button", "OmaRaidSecure1", parent, inheritedFrames);
241     local frame = CreateFrame("Frame", "OmaRaid1", parent);
242     local unit = "raid1";
243     secure:SetPoint("TOPLEFT", parent, "TOPLEFT");
244     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
245     setupFrame(frame, secure, unit);
246     RegisterUnitWatch(frame);
247     RegisterUnitWatch(secure);
248     local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
249     RegisterStateDriver(secure, "vehicleui", vehicle);
250     secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
251     raid[1] = {secure=secure, frame=frame};
252     for y = 1,7 do
253         local i = y*5+1;
254         local secure = CreateFrame("Button", "OmaRaidSecure"..i, parent, inheritedFrames);
255         local frame = CreateFrame("Frame", "OmaRaid"..i, parent);
256         local unit = "raid"..i;
257         secure:SetPoint("TOPLEFT", raid[i-5].secure, "BOTTOMLEFT");
258         frame:SetPoint("TOPLEFT", raid[i-5].frame, "BOTTOMLEFT");
259         setupFrame(frame, secure, unit);
260         RegisterUnitWatch(frame);
261         RegisterUnitWatch(secure);
262         local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
263         RegisterStateDriver(secure, "vehicleui", vehicle);
264         secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
265         raid[i] = {secure=secure, frame=frame};
266     end
267     for y = 0,7 do
268         for x = 1,4 do
269             local i = y*5+x+1;
270             local secure = CreateFrame("Button", "OmaRaidSecure"..i, parent, inheritedFrames);
271             local frame = CreateFrame("Frame", "OmaRaid"..i, parent);
272             local unit = "raid"..i;
273             secure:SetPoint("TOPLEFT", raid[i-1].secure, "TOPRIGHT");
274             frame:SetPoint("TOPLEFT", raid[i-1].frame, "TOPRIGHT");
275             setupFrame(frame, secure, unit);
276             RegisterUnitWatch(frame);
277             RegisterUnitWatch(secure);
278             local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
279             RegisterStateDriver(secure, "vehicleui", vehicle);
280             secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
281             raid[i] = {secure=secure, frame=frame};
282         end
283     end
284 end
285
286 local function initialize()
287     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
288     CFrame:SetHeight((height+2)*8);
289     CFrame:SetWidth((width+2)*5+1);
290     initializeParty(CFrame);
291     initializeRaid(CFrame);
292 end
293
294 local function loadCharSettings()
295     width, height = Settings.Character.Width, Settings.Character.Height;
296     anchorX, anchorY = Settings.Character.AnchorX, Settings.Character.AnchorY;
297     attributes = Settings.Character.Clickheal;
298     positions = Settings.Character.Positions;
299 end
300
301 CFrame:RegisterEvent("ADDON_LOADED");
302 CFrame:RegisterEvent("PLAYER_LOGIN");
303 CFrame:SetScript("OnEvent", function(self, event)
304     if event == "PLAYER_LOGIN" then
305         initialize();
306     elseif event == "ADDON_LOADED" then
307         OmaRFLoadChar();
308         loadCharSettings();
309         OmaRFEvents.LoadChar();
310         OmaRFIndicators.LoadChar();
311     end
312 end);