bccab8e - Add castbars
[wowui.git] / kehys / target.lua
1 -- target.lua
2 local _, addon = ...;
3 local unpack = unpack;
4 local format = string.format;
5 local CFrame = CreateFrame("Frame", "kehysTargetInit", UIParent);
6 local CTimerAfter = C_Timer.After;
7
8 local barTexture = "Interface\\AddOns\\kehys\\images\\minimalist";
9
10 local targetUpdate = nil;
11 local function initTarget(parent, width, height, update, event)
12     assert(type(parent) == "table", "Target frame creation missing parent!");
13     assert(type(width) == "number", "Target frame creation missing width!");
14     assert(type(height) == "number", "Target frame creation missing height!");
15     assert(type(update) == "function",
16            "Target frame creation missing update function!");
17     assert(type(event) == "function",
18            "Target frame creation missing event function!");
19
20     local f = CreateFrame("Button", "kehysTarget", parent,
21                           "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
22     f:Hide();
23     f:SetPoint("CENTER", parent, "CENTER", 300, -178);
24     f:SetWidth(width);
25     f:SetHeight(height);
26     f.barwidth = width - 2; -- 1px padding
27     f:SetAttribute("unit", "target");
28     f:SetAttribute("displayed", "target");
29     f.unit = "target";
30     f.displayed = "target";
31     f.nonraid = true;
32     f.prev = {};
33
34     targetUpdate = function()
35         CTimerAfter(0.1, targetUpdate);
36         update(f);
37     end
38     f:SetScript("OnEvent", event);
39     f:RegisterForClicks("AnyDown");
40     f:SetAttribute("*type1", "target");
41     f:SetAttribute("*type2", "togglemenu");
42     f:SetAttribute("toggleForVehicle", false);
43     RegisterUnitWatch(f);
44
45     -- create visuals
46     f.base = f:CreateTexture(nil, "BACKGROUND");
47     f.base:SetAllPoints();
48     f.base:SetColorTexture(0, 0, 0, 0.5);
49     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 1);
50     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
51     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
52     f.health = f:CreateTexture(nil, "BORDER");
53     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
54     f.health:SetPoint("BOTTOMLEFT", f.background, "LEFT", 0, -height/8);
55     f.health:SetTexture(barTexture);
56     f.health:SetVertexColor(0.8, 0.8, 0.8);
57     f.health:Hide();
58     f.mana = f:CreateTexture(nil, "BORDER");
59     f.mana:SetPoint("TOPLEFT", f.background, "LEFT", 0, -height/8);
60     f.mana:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
61     f.mana:SetTexture(barTexture);
62     f.mana:SetVertexColor(0.1, 0.5, 0.9);
63     f.mana:Hide();
64     f.manatext = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
65     f.manatext:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT", -2, 4);
66     f.manatext:Hide();
67     f.shield = f:CreateTexture(nil, "BORDER");
68     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
69     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
70     f.shield:SetTexture(barTexture);
71     f.shield:SetVertexColor(0, 0.7, 1);
72     f.shield:Hide();
73     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
74     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
75     f.shieldhl:SetPoint("BOTTOMRIGHT", f, "RIGHT", 0, -height/8);
76     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
77     f.shieldhl:Hide();
78     f.healpred = f:CreateTexture(nil, "ARTWORK");
79     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
80     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
81     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
82     f.healpred:Hide();
83     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
84     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
85     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
86     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
87     f.healabsorb:Hide();
88     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
89     f.name:SetPoint("LEFT", f, "LEFT", 2, 8);
90     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
91     f.text:SetPoint("RIGHT", f, "RIGHT", -2, 8);
92     f.text:SetFont(STANDARD_TEXT_FONT, 13);
93     f.text:Hide();
94     f.targeticon = f:CreateTexture(nil, "OVERLAY");
95     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
96     f.targeticon:SetWidth(12);
97     f.targeticon:SetHeight(12);
98     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
99     f.targeticon:Hide();
100     f.status = f:CreateTexture(nil, "OVERLAY");
101     f.status:SetPoint("TOPLEFT", f.background, "BOTTOMLEFT", -8, 8);
102     f.status:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMLEFT", 8, -8);
103     f.status:SetTexture("Interface\\CHARACTERFRAME\\UI-StateIcon");
104     f.status:Hide();
105
106     addon.RegisterEvents(f);
107     addon.RegisterUnitEvents(f);
108     event(f, "UPDATE_ALL_BARS");
109     targetUpdate();
110     f:Show();
111     return f;
112 end
113
114 CFrame:SetScript("OnEvent", function(self)
115     self:UnregisterAllEvents();
116     CFrame:SetFrameStrata("LOW");
117     CFrame:SetPoint("CENTER", nil, "CENTER");
118     CFrame:SetWidth(2);
119     CFrame:SetHeight(2);
120     initTarget(self, 160, 48, addon.FrameUpdate, addon.UnitEvent);
121 end);
122 CFrame:RegisterEvent("PLAYER_LOGIN");