156861c - Non-raid unit frame fixes
[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.raid = true;
61     f.rounds = 0;
62     -- set up periodic updates
63     updaters[f] = function()
64         if f.updating then
65             CTimerAfter(0.1, updaters[f]);
66             return update(f)
67         end
68     end
69     -- set scripts
70     f:SetScript("OnShow", function()
71         onshow(f);
72         f.updating = true;
73         updaters[f]();
74     end);
75     f:SetScript("OnHide", function()
76         f:UnregisterAllEvents();
77         f.updating = false;
78         if f.guid then
79             guids[f.guid] = nil;
80             f.guid = nil;
81         end
82     end);
83     f:SetScript("OnEvent", event);
84     f:SetScript("OnEnter", showTooltip);
85     f:SetScript("OnLeave", hideTooltip);
86     -- set attributes
87     f:RegisterForClicks("AnyDown");
88     for attr, val in pairs(attributes) do
89         f:SetAttribute(attr, val);
90     end
91     -- rest give target and menu
92     f:SetAttribute("*type1", "target");
93     f:SetAttribute("*type2", "togglemenu");
94
95     -- create visuals
96     f.base = f:CreateTexture(nil, "BACKGROUND");
97     f.base:SetAllPoints();
98     f.base:SetColorTexture(1, 1, 1);
99     f.base:SetVertexColor(unpack(addon.Colors.Base));
100     f.glow = f:CreateTexture(nil, "BACKGROUND", nil, 1);
101     f.glow:SetAllPoints();
102     f.glow:SetColorTexture(1, 1, 1);
103     f.glow:SetVertexColor(unpack(addon.Colors.Glow));
104     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 2);
105     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
106     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
107     f.background:SetColorTexture(0.7, 0.7, 0.7);
108     f.health = f:CreateTexture(nil, "BORDER");
109     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
110     f.health:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
111     f.health:SetTexture(barTexture);
112     f.health:SetVertexColor(0.3, 0.3, 0.3);
113     f.health:Hide();
114     f.shield = f:CreateTexture(nil, "BORDER");
115     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
116     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
117     f.shield:SetTexture(barTexture);
118     f.shield:SetVertexColor(0, 0.7, 1);
119     f.shield:Hide();
120     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
121     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
122     f.shieldhl:SetPoint("BOTTOMRIGHT");
123     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
124     f.shieldhl:Hide();
125     f.healpred = f:CreateTexture(nil, "ARTWORK");
126     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
127     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
128     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
129     f.healpred:Hide();
130     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
131     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
132     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
133     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
134     f.healabsorb:Hide();
135     f.overlay = f:CreateTexture(nil, "ARTWORK", nil, 1);
136     f.overlay:SetPoint("TOPLEFT", f.background, "TOPLEFT");
137     f.overlay:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT");
138     f.overlay:SetColorTexture(1, 1, 1);
139     f.overlay:Hide();
140     f.role = f:CreateTexture(nil, "ARTWORK", nil, 2);
141     f.role:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -3, 3);
142     f.role:SetPoint("TOPLEFT", f, "BOTTOMRIGHT", -15, 15);
143     f.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
144     f.role:Hide();
145     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
146     f.name:SetPoint("CENTER", f, "CENTER", 0, 11);
147     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
148     f.text:SetPoint("CENTER", f, "CENTER", 0, -1);
149     f.text:SetFont(STANDARD_TEXT_FONT, 13);
150     f.text:Hide();
151     f.stack = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
152     f.stack:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
153     f.stack:Hide();
154     f.ready = f:CreateTexture(nil, "OVERLAY");
155     f.ready:SetPoint("TOPLEFT", f, "BOTTOMLEFT", 1, 15);
156     f.ready:SetPoint("BOTTOMRIGHT", f, "BOTTOMLEFT", 15, 1);
157     f.ready:Hide();
158     f.buffind1 = f:CreateTexture(nil, "OVERLAY");
159     f.buffind1:SetPoint("TOPRIGHT", f.background, "TOPRIGHT", -1, -1);
160     f.buffind1:SetWidth(6);
161     f.buffind1:SetHeight(6);
162     f.buffind1:SetColorTexture(0.8, 0.1, 0.1);
163     f.buffind1:Hide();
164     f.buffind2 = f:CreateTexture(nil, "OVERLAY");
165     f.buffind2:SetPoint("TOPLEFT", f.background, "TOPLEFT", 1, -1);
166     f.buffind2:SetWidth(4);
167     f.buffind2:SetHeight(4);
168     f.buffind2:SetColorTexture(0.8, 0.7, 0.1);
169     f.buffind2:Hide();
170     f.defensive = f:CreateTexture(nil, "OVERLAY", nil, 1);
171     f.defensive:SetPoint("TOPLEFT", f.background, "TOPLEFT", 1, -1);
172     f.defensive:SetWidth(6);
173     f.defensive:SetHeight(6);
174     f.defensive:SetColorTexture(1, 0.3, 0);
175     f.defensive:Hide();
176     f.targeticon = f:CreateTexture(nil, "OVERLAY");
177     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
178     f.targeticon:SetWidth(12);
179     f.targeticon:SetHeight(12);
180     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
181     f.targeticon:Hide();
182
183     return f;
184 end