0051bb4 - add death strike tracking
[wowui.git] / kehys / frame.lua
1 -- frame.lua
2 -- 2019 Aleksi Blinnikka
3 local _, addon = ...;
4 local unpack = unpack;
5 local format = string.format;
6 local CreateFrame = CreateFrame;
7 local CTimerAfter = C_Timer.After;
8
9 local guids = addon.FrameGuids;
10 local updaters = {};
11 local function showTooltip(frame)
12     GameTooltip_SetDefaultAnchor(GameTooltip, frame);
13     GameTooltip:SetUnit(frame:GetAttribute("unit"));
14 end
15 local function hideTooltip(frame)
16     if GameTooltip:IsOwned(frame) then GameTooltip:FadeOut() end
17 end
18
19 local barTexture = "Interface\\AddOns\\kehys\\images\\minimalist";
20 local frameid = 1;
21 function addon.NewRaidFrame(parent, width, height, unit, attributes,
22                             update, event, onshow)
23     assert(type(parent) == "table", "Frame creation missing parent!");
24     assert(type(width) == "number", "Frame creation missing width!");
25     assert(type(height) == "number", "Frame creation missing height!");
26     assert(type(unit) == "string", "Frame creation missing unit!");
27     assert(type(update) == "function",
28            "Frame creation missing update function!");
29     assert(type(event) == "function",
30            "Frame creation missing event function!");
31     assert(type(onshow) == "function",
32            "Frame creation missing onshow function!");
33     if type(attributes) ~= "table" then
34         attributes = {};
35     end
36
37     local f = CreateFrame(
38         "Button",
39         format("kehys%i", frameid),
40         parent,
41         "SecureUnitButtonTemplate,SecureHandlerStateTemplate"
42     );
43     frameid = frameid + 1;
44     f:Hide(); -- hide frame to have an initial frame:OnShow call
45     f:SetWidth(width);
46     f:SetHeight(height);
47     f.barwidth = width - 2; -- 1px padding on both sides
48     f:SetAttribute("unit", unit);
49     f.unit = unit;
50     f.displayed = unit;
51     f.vehicle = unit == "player" and "vehicle" or format("%spet", unit);
52     f.prev = {} -- values stored from previous update
53     f.alert = {}; -- alerting auras
54     f.heal = {}; -- high healing auras
55     f.tankcd = {}; -- tank CD auras
56     f.stacks = {}; -- stacking aura tracking
57     f.buff1 = {}; -- custom buff indicator 1
58     f.buff2 = {}; -- custom buff indicator 2
59     f.incoming = {}; -- incoming ability indicator
60     f.rolename = nil; -- TANK, HEALER, DAMAGER, NONE
61     f.isdk = nil; -- DK death strike tracker for DK tanks
62     f.raid = true;
63     f.rounds = 0;
64     -- set up periodic updates
65     updaters[f] = function()
66         if f.updating then
67             CTimerAfter(0.1, updaters[f]);
68             return update(f)
69         end
70     end
71     -- set scripts
72     f:SetScript("OnShow", function()
73         onshow(f);
74         f.updating = true;
75         updaters[f]();
76     end);
77     f:SetScript("OnHide", function()
78         f:UnregisterAllEvents();
79         f.updating = false;
80         if f.guid then
81             guids[f.guid] = nil;
82             f.guid = nil;
83         end
84     end);
85     f:SetScript("OnEvent", event);
86     f:SetScript("OnEnter", showTooltip);
87     f:SetScript("OnLeave", hideTooltip);
88     -- set attributes
89     f:RegisterForClicks("AnyDown");
90     for attr, val in pairs(attributes) do
91         f:SetAttribute(attr, val);
92     end
93     -- rest give target and menu
94     f:SetAttribute("*type1", "target");
95     f:SetAttribute("*type2", "togglemenu");
96
97     -- create visuals
98     f.base = f:CreateTexture(nil, "BACKGROUND");
99     f.base:SetAllPoints();
100     f.base:SetColorTexture(1, 1, 1);
101     f.base:SetVertexColor(unpack(addon.Colors.Base));
102     f.glow = f:CreateTexture(nil, "BACKGROUND", nil, 1);
103     f.glow:SetAllPoints();
104     f.glow:SetColorTexture(1, 1, 1);
105     f.glow:SetVertexColor(unpack(addon.Colors.Glow));
106     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 2);
107     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
108     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
109     f.background:SetColorTexture(0.7, 0.7, 0.7);
110     f.health = f:CreateTexture(nil, "BORDER");
111     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
112     f.health:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
113     f.health:SetTexture(barTexture);
114     f.health:SetVertexColor(0.3, 0.3, 0.3);
115     f.health:Hide();
116     f.shield = f:CreateTexture(nil, "BORDER");
117     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
118     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
119     f.shield:SetTexture(barTexture);
120     f.shield:SetVertexColor(0, 0.7, 1);
121     f.shield:Hide();
122     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
123     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
124     f.shieldhl:SetPoint("BOTTOMRIGHT");
125     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
126     f.shieldhl:Hide();
127     f.healpred = f:CreateTexture(nil, "ARTWORK");
128     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
129     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
130     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
131     f.healpred:Hide();
132     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
133     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
134     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
135     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
136     f.healabsorb:Hide();
137     f.overlay = f:CreateTexture(nil, "ARTWORK", nil, 1);
138     f.overlay:SetPoint("TOPLEFT", f.background, "TOPLEFT");
139     f.overlay:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT");
140     f.overlay:SetColorTexture(1, 1, 1);
141     f.overlay:Hide();
142     f.bottomwarn = f:CreateTexture(nil, "ARTWORK", nil, 1);
143     f.bottomwarn:SetPoint("TOPLEFT", f.background, "BOTTOMLEFT", 16, 5);
144     f.bottomwarn:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT", -16, 1);
145     f.bottomwarn:SetColorTexture(1, 0.3, 0.1, 0.66);
146     f.bottomwarn:Hide();
147     f.role = f:CreateTexture(nil, "ARTWORK", nil, 2);
148     f.role:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -3, 3);
149     f.role:SetPoint("TOPLEFT", f, "BOTTOMRIGHT", -15, 15);
150     f.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
151     f.role:Hide();
152     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
153     f.name:SetPoint("CENTER", f, "CENTER", 0, 11);
154     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
155     f.text:SetPoint("CENTER", f, "CENTER", 0, -1);
156     f.text:SetFont(STANDARD_TEXT_FONT, 13);
157     f.text:Hide();
158     f.stack = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
159     f.stack:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
160     f.stack:Hide();
161     f.ready = f:CreateTexture(nil, "OVERLAY");
162     f.ready:SetPoint("TOPLEFT", f, "BOTTOMLEFT", 1, 15);
163     f.ready:SetPoint("BOTTOMRIGHT", f, "BOTTOMLEFT", 15, 1);
164     f.ready:Hide();
165     f.buffind1 = f:CreateTexture(nil, "OVERLAY");
166     f.buffind1:SetPoint("TOPRIGHT", f.background, "TOPRIGHT", -1, -1);
167     f.buffind1:SetWidth(6);
168     f.buffind1:SetHeight(6);
169     f.buffind1:SetColorTexture(0.8, 0.1, 0.1);
170     f.buffind1:Hide();
171     f.buffind2 = f:CreateTexture(nil, "OVERLAY");
172     f.buffind2:SetPoint("TOPLEFT", f.background, "TOPLEFT", 1, -1);
173     f.buffind2:SetWidth(4);
174     f.buffind2:SetHeight(4);
175     f.buffind2:SetColorTexture(0.8, 0.7, 0.1);
176     f.buffind2:Hide();
177     f.defensive = f:CreateTexture(nil, "OVERLAY", nil, 1);
178     f.defensive:SetPoint("TOPLEFT", f.background, "TOPLEFT", 1, -1);
179     f.defensive:SetWidth(6);
180     f.defensive:SetHeight(6);
181     f.defensive:SetColorTexture(1, 0.3, 0);
182     f.defensive:Hide();
183     f.targeticon = f:CreateTexture(nil, "OVERLAY");
184     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
185     f.targeticon:SetWidth(12);
186     f.targeticon:SetHeight(12);
187     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
188     f.targeticon:Hide();
189
190     return f;
191 end