0b3d41a - Allow frame creation without clickheal
[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     -- set up periodic updates
56     updaters[f] = function()
57         if f.updating then
58             CTimerAfter(0.1, updaters[f]);
59             return update(f)
60         end
61     end
62     -- set scripts
63     f:SetScript("OnShow", function()
64         onshow(f);
65         f.updating = true;
66         updaters[f]();
67     end);
68     f:SetScript("OnHide", function()
69         f:UnregisterAllEvents();
70         f.updating = false;
71         if f.guid then
72             guids[f.guid] = nil;
73             f.guid = nil;
74         end
75     end);
76     f:SetScript("OnEvent", event);
77     f:SetScript("OnEnter", showTooltip);
78     f:SetScript("OnLeave", hideTooltip);
79     -- set attributes
80     f:RegisterForClicks("AnyDown");
81     for attr, val in pairs(attributes) do
82         f:SetAttribute(attr, val);
83     end
84     -- rest give target and menu
85     f:SetAttribute("*type1", "target");
86     f:SetAttribute("*type2", "togglemenu");
87
88     -- create visuals
89     f.base = f:CreateTexture(nil, "BACKGROUND");
90     f.base:SetAllPoints();
91     f.base:SetColorTexture(1, 1, 1);
92     f.base:SetVertexColor(unpack(addon.Colors.Base));
93     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 1);
94     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
95     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
96     f.background:SetColorTexture(0.7, 0.7, 0.7);
97     f.health = f:CreateTexture(nil, "BORDER");
98     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
99     f.health:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
100     f.health:SetTexture(barTexture);
101     f.health:SetVertexColor(0.3, 0.3, 0.3);
102     f.health:Hide();
103     f.shield = f:CreateTexture(nil, "BORDER");
104     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
105     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
106     f.shield:SetTexture(barTexture);
107     f.shield:SetVertexColor(0, 0.7, 1);
108     f.shield:Hide();
109     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
110     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
111     f.shieldhl:SetPoint("BOTTOMRIGHT");
112     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
113     f.shieldhl:Hide();
114     f.healpred = f:CreateTexture(nil, "ARTWORK");
115     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
116     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
117     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
118     f.healpred:Hide();
119     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
120     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
121     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
122     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
123     f.healabsorb:Hide();
124     f.overlay = f:CreateTexture(nil, "ARTWORK", nil, 1);
125     f.overlay:SetPoint("TOPLEFT", f.background, "TOPLEFT");
126     f.overlay:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT");
127     f.overlay:SetColorTexture(1, 1, 1);
128     f.overlay:Hide();
129     f.role = f:CreateTexture(nil, "ARTWORK", nil, 2);
130     f.role:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -3, 3);
131     f.role:SetPoint("TOPLEFT", f, "BOTTOMRIGHT", -15, 15);
132     f.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
133     f.role:Hide();
134     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
135     f.name:SetPoint("CENTER", f, "CENTER", 0, 11);
136     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
137     f.text:SetPoint("CENTER", f, "CENTER", 0, -1);
138     f.text:SetFont(STANDARD_TEXT_FONT, 13);
139     f.text:Hide();
140     f.ready = f:CreateTexture(nil, "OVERLAY");
141     f.ready:SetPoint("TOPLEFT", f, "BOTTOMLEFT", 1, 15);
142     f.ready:SetPoint("BOTTOMRIGHT", f, "BOTTOMLEFT", 15, 1);
143     f.ready:Hide();
144     f.targeticon = f:CreateTexture(nil, "OVERLAY");
145     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
146     f.targeticon:SetWidth(12);
147     f.targeticon:SetHeight(12);
148     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
149     f.targeticon:Hide();
150
151     return f;
152 end