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