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