922d6e4 - Remove OmaRF in favor of newer kehys frames
[wowui.git] / kehys / raid.lua
1 -- raid.lua
2 -- 2019 Aleksi Blinnikka
3 local _, addon = ...;
4 local format = string.format;
5 local UnitInRange = UnitInRange;
6 local RegisterStateDriver, RegisterUnitWatch = RegisterStateDriver, RegisterUnitWatch;
7
8 local registerEvents = addon.RegisterEvents;
9 local registerUnitEvents = addon.RegisterUnitEvents;
10 local unitEvent = addon.UnitEvent;
11 local unitUpdate = addon.FrameUpdate;
12
13 local width, height = 82, 42;
14 local anchorX, anchorY = 0, -330;
15 local attributes = {};
16
17 local CFrame = CreateFrame("Frame", "kehysMain", UIParent);
18 local class = nil;
19 local party = {};
20 local raid = {};
21
22 local function frameShow(frame)
23     registerEvents(frame);
24     registerUnitEvents(frame);
25     unitEvent(frame, "UPDATE_ALL_BARS");
26 end
27
28 -- vehicle toggle from Shadowed Unit Frames
29 local vehicletoggle = [=[
30 local unit = self:GetAttribute("unit");
31 if unit and newstate == "vehicle" and not UnitTargetsVehicleInRaidUI(unit) then
32     self:SetAttribute("toggleForVehicle", false);
33 else
34     self:SetAttribute("toggleForVehicle", true);
35 end
36 ]=]
37 local vehiclestates = "[@%s,unithasvehicleui] vehicle; no";
38
39 local function initializeParty(parent)
40     assert(type(parent) == "table",
41            "Party initialization missing parent frame!");
42     local visibility = "[@%s,exists,group:raid] hide; [@%s,exists] show; hide";
43     local unit = "player";
44     local frame = addon.NewRaidFrame(parent, width, height, unit, attributes,
45                                      unitUpdate, unitEvent, frameShow);
46     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
47     RegisterStateDriver(frame, "visibility", "[@player,group:raid] hide; show");
48     RegisterStateDriver(frame, "vehicleui", "[vehicleui] vehicle; no");
49     frame:SetAttribute("_onstate-vehicleui", vehicletoggle);
50     party[0] = frame;
51     for i = 1,4 do
52         unit = "party"..i;
53         frame = addon.NewRaidFrame(parent, width, height, unit, attributes,
54                                    unitUpdate, unitEvent, frameShow);
55         frame:SetPoint("TOPLEFT", party[i-1], "TOPRIGHT");
56         RegisterStateDriver(frame, "visibility", format(visibility, unit, unit));
57         RegisterStateDriver(frame, "vehicleui", format(vehiclestates, unit));
58         frame:SetAttribute("_onstate-vehicleui", vehicletoggle);
59         party[i] = frame;
60     end
61 end
62
63 local function initializeRaid(parent)
64     assert(type(parent) == "table",
65            "Raid initialization missing parent frame!");
66     local unit = "raid1";
67     local frame = addon.NewRaidFrame(parent, width, height, unit, attributes,
68                                      unitUpdate, unitEvent, frameShow);
69     frame:SetPoint("TOPLEFT", parent, "TOPLEFT");
70     RegisterUnitWatch(frame);
71     RegisterStateDriver(frame, "vehicleui", format(vehiclestates, unit));
72     frame:SetAttribute("_onstate-vehicleui", vehicletoggle);
73     raid[1] = frame;
74     for y = 1,7 do
75         local i = y*5+1;
76         unit = "raid"..i;
77         frame = addon.NewRaidFrame(parent, width, height, unit, attributes,
78                                    unitUpdate, unitEvent, frameShow);
79         frame:SetPoint("TOPLEFT", raid[i-5], "BOTTOMLEFT");
80         RegisterUnitWatch(frame);
81         RegisterStateDriver(frame, "vehicleui", format(vehiclestates, unit));
82         frame:SetAttribute("_onstate-vehicleui", vehicletoggle);
83         raid[i] = frame;
84     end
85     for y = 0,7 do
86         for x = 1,4 do
87             local i = y*5+x+1;
88             unit = "raid"..i;
89             frame = addon.NewRaidFrame(parent, width, height, unit, attributes,
90                                        unitUpdate, unitEvent, frameShow);
91             frame:SetPoint("TOPLEFT", raid[i-1], "TOPRIGHT");
92             RegisterUnitWatch(frame);
93             RegisterStateDriver(frame, "vehicleui", format(vehiclestates, unit));
94             frame:SetAttribute("_onstate-vehicleui", vehicletoggle);
95             raid[i] = frame;
96         end
97     end
98 end
99
100 local function initializeFocus(parent)
101     assert(type(parent) == "table",
102            "Focus initialization missing parent frame!");
103     local unit = "focus";
104     local frame = addon.NewRaidFrame(parent, width, height, unit, attributes,
105                                      unitUpdate, unitEvent, frameShow);
106     frame:SetPoint("BOTTOMLEFT", parent, "TOPLEFT");
107     RegisterUnitWatch(frame);
108     RegisterStateDriver(frame, "vehicleui", format(vehiclestates, unit));
109     frame:SetAttribute("_onstate-vehicleui", vehicletoggle);
110 end
111
112 local function initialize()
113     _, class = UnitClass("player");
114     attributes = addon.Clickheal[class];
115     CFrame:SetFrameStrata("LOW");
116     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
117     CFrame:SetHeight((height+2)*8);
118     CFrame:SetWidth((width+2)*5);
119     initializeParty(CFrame);
120     initializeRaid(CFrame);
121     initializeFocus(CFrame);
122 end
123
124 CFrame:SetScript("OnEvent", function(self)
125     self:UnregisterAllEvents();
126     initialize();
127 end);
128 CFrame:RegisterEvent("PLAYER_LOGIN");