252b971c016b2331dbe1ee19340bf6669d3ff7dc
[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(attributes) == "table",
28            "Frame creation missing attributes table!");
29     assert(type(update) == "function",
30            "Frame creation missing update function!");
31     assert(type(event) == "function",
32            "Frame creation missing event function!");
33     assert(type(onshow) == "function",
34            "Frame creation missing onshow function!");
35
36     local f = CreateFrame(
37         "Button",
38         format("kehys%i", frameid),
39         parent,
40         "SecureUnitButtonTemplate,SecureHandlerStateTemplate"
41     );
42     frameid = frameid + 1;
43     f:Hide(); -- hide frame to have an initial frame:OnShow call
44     f:SetWidth(width);
45     f:SetHeight(height);
46     f.barwidth = width - 2; -- 1px padding on both sides
47     f:SetAttribute("unit", unit);
48     f.unit = unit;
49     f.displayed = unit;
50     f.vehicle = unit == "player" and "vehicle" or format("%spet", unit);
51     f.prev = {} -- values stored from previous update
52     f.alert = {}; -- alerting auras
53     f.heal = {}; -- high healing auras
54     -- set up periodic updates
55     updaters[f] = function()
56         if f.updating then
57             CTimerAfter(0.1, updaters[f]);
58             return update(f)
59         end
60     end
61     -- set scripts
62     f:SetScript("OnShow", function()
63         onshow(f);
64         f.updating = true;
65         updaters[f]();
66     end);
67     f:SetScript("OnHide", function()
68         f:UnregisterAllEvents();
69         f.updating = false;
70         if f.guid then
71             guids[f.guid] = nil;
72             f.guid = nil;
73         end
74     end);
75     f:SetScript("OnEvent", event);
76     f:SetScript("OnEnter", showTooltip);
77     f:SetScript("OnLeave", hideTooltip);
78     -- set attributes
79     f:RegisterForClicks("AnyDown");
80     for attr, val in pairs(attributes) do
81         f:SetAttribute(attr, val);
82     end
83     -- rest give target and menu
84     f:SetAttribute("*type1", "target");
85     f:SetAttribute("*type2", "togglemenu");
86
87     -- create visuals
88     f.base = f:CreateTexture(nil, "BACKGROUND");
89     f.base:SetAllPoints();
90     f.base:SetColorTexture(1, 1, 1);
91     f.base:SetVertexColor(unpack(addon.Colors.Base));
92     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 1);
93     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
94     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
95     f.background:SetColorTexture(0.7, 0.7, 0.7);
96     f.health = f:CreateTexture(nil, "BORDER");
97     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
98     f.health:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
99     f.health:SetTexture(barTexture);
100     f.health:SetVertexColor(0.3, 0.3, 0.3);
101     f.health:Hide();
102     f.shield = f:CreateTexture(nil, "BORDER");
103     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
104     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
105     f.shield:SetTexture(barTexture);
106     f.shield:SetVertexColor(0, 0.7, 1);
107     f.shield:Hide();
108     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
109     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
110     f.shieldhl:SetPoint("BOTTOMRIGHT");
111     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
112     f.shieldhl:Hide();
113     f.healpred = f:CreateTexture(nil, "ARTWORK");
114     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
115     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
116     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
117     f.healpred:Hide();
118     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
119     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
120     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
121     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
122     f.healabsorb:Hide();
123     f.overlay = f:CreateTexture(nil, "ARTWORK", nil, 1);
124     f.overlay:SetPoint("TOPLEFT", f.background, "TOPLEFT");
125     f.overlay:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT");
126     f.overlay:SetColorTexture(1, 1, 1);
127     f.overlay:Hide();
128     f.role = f:CreateTexture(nil, "ARTWORK", nil, 2);
129     f.role:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -3, 3);
130     f.role:SetPoint("TOPLEFT", f, "BOTTOMRIGHT", -15, 15);
131     f.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
132     f.role:Hide();
133     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
134     f.name:SetPoint("CENTER", f, "CENTER", 0, 11);
135     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
136     f.text:SetPoint("CENTER", f, "CENTER", 0, -1);
137     f.text:SetFont(STANDARD_TEXT_FONT, 13);
138     f.text:Hide();
139     f.ready = f:CreateTexture(nil, "OVERLAY");
140     f.ready:SetPoint("TOPLEFT", f, "BOTTOMLEFT", 1, 15);
141     f.ready:SetPoint("BOTTOMRIGHT", f, "BOTTOMLEFT", 15, 1);
142     f.ready:Hide();
143     f.targeticon = f:CreateTexture(nil, "OVERLAY");
144     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
145     f.targeticon:SetWidth(12);
146     f.targeticon:SetHeight(12);
147     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
148     f.targeticon:Hide();
149
150     return f;
151 end