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