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