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