08ea8fef24af0d0f952abb40fe7d216946080341
[wowui.git] / kehys / player.lua
1 -- player.lua
2 local _, addon = ...;
3 local unpack = unpack;
4 local format = string.format;
5 local CFrame = CreateFrame("Frame", "kehysPlayerInit", UIParent);
6 local CTimerAfter = C_Timer.After;
7
8 local barTexture = "Interface\\AddOns\\kehys\\images\\minimalist";
9 local vehicletoggle = [=[
10     if newstate == "vehicle" then
11         self:SetAttribute("displayed", self:GetAttribute("vehicle"));
12     else
13         self:SetAttribute("displayed", self:GetAttribute("unit"));
14     end
15 ]=]
16
17 local playerUpdate = nil;
18 local function initPlayer(parent, width, height, update, event)
19     assert(type(parent) == "table", "Player frame creation missing parent!");
20     assert(type(width) == "number", "Player frame creation missing width!");
21     assert(type(height) == "number", "Player frame creation missing height!");
22     assert(type(update) == "function",
23            "Player frame creation missing update function!");
24     assert(type(event) == "function",
25            "Player frame creation missing event function!");
26
27     local f = CreateFrame("Button", "kehysPlayer", parent,
28                           "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
29     f:Hide();
30     f:SetPoint("CENTER", parent, "CENTER", -300, -178);
31     f:SetWidth(width);
32     f:SetHeight(height);
33     f.barwidth = width - 2; -- 1px padding
34     f:SetAttribute("unit", "player");
35     f:SetAttribute("displayed", "player");
36     f:SetAttribute("vehicle", "vehicle");
37     f.unit = "player";
38     f.displayed = "player";
39     f.vehicle = "vehicle"
40     f.prev = {};
41
42     playerUpdate = function()
43         CTimerAfter(0.1, playerUpdate);
44         update(f);
45     end
46     f:SetScript("OnEvent", event);
47     f:RegisterForClicks("AnyDown");
48     f:SetAttribute("*type1", "target");
49     f:SetAttribute("*type2", "togglemenu");
50     f:SetAttribute("toggleForVehicle", true);
51     RegisterStateDriver(f, "vehicleui", "[vehicleui] vehicle; no");
52     f:SetAttribute("_onstate-vehicleui", vehicletoggle);
53
54     -- create visuals
55     f.base = f:CreateTexture(nil, "BACKGROUND");
56     f.base:SetAllPoints();
57     f.base:SetColorTexture(0, 0, 0, 0.5);
58     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 1);
59     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
60     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
61     f.health = f:CreateTexture(nil, "BORDER");
62     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
63     f.health:SetPoint("BOTTOMLEFT", f.background, "LEFT", 0, -height/8);
64     f.health:SetTexture(barTexture);
65     f.health:SetVertexColor(0.7, 0.7, 0.7);
66     f.health:Hide();
67     f.mana = f:CreateTexture(nil, "BORDER");
68     f.mana:SetPoint("TOPLEFT", f.background, "LEFT", 0, -height/8);
69     f.mana:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
70     f.mana:SetTexture(barTexture);
71     f.mana:SetVertexColor(0.1, 0.5, 0.9);
72     f.mana:Hide();
73     f.manatext = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
74     f.manatext:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT", -2, 4);
75     f.manatext:Hide();
76     f.shield = f:CreateTexture(nil, "BORDER");
77     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
78     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
79     f.shield:SetTexture(barTexture);
80     f.shield:SetVertexColor(0, 0.7, 1);
81     f.shield:Hide();
82     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
83     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
84     f.shieldhl:SetPoint("BOTTOMRIGHT", f, "RIGHT", 0, -height/8);
85     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
86     f.shieldhl:Hide();
87     f.healpred = f:CreateTexture(nil, "ARTWORK");
88     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
89     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
90     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
91     f.healpred:Hide();
92     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
93     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
94     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
95     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
96     f.healabsorb:Hide();
97     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
98     f.text:SetPoint("RIGHT", f, "RIGHT", -2, 8);
99     f.text:SetFont(STANDARD_TEXT_FONT, 13);
100     f.text:Hide();
101     f.targeticon = f:CreateTexture(nil, "OVERLAY");
102     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
103     f.targeticon:SetWidth(12);
104     f.targeticon:SetHeight(12);
105     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
106     f.targeticon:Hide();
107     f.status = f:CreateTexture(nil, "OVERLAY");
108     f.status:SetPoint("TOPLEFT", f.background, "BOTTOMLEFT", -8, 8);
109     f.status:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMLEFT", 8, -8);
110     f.status:SetTexture("Interface\\CHARACTERFRAME\\UI-StateIcon");
111     f.status:Hide();
112
113     addon.RegisterEvents(f);
114     addon.RegisterUnitEvents(f);
115     event(f, "UPDATE_ALL_BARS");
116     playerUpdate();
117     f:Show();
118     return f;
119 end
120
121 local petUpdate = nil;
122 local function initPet(parent, width, height, update, event)
123     assert(type(parent) == "table", "Pet frame creation missing parent!");
124     assert(type(width) == "number", "Pet frame creation missing width!");
125     assert(type(height) == "number", "Pet frame creation missing height!");
126     assert(type(update) == "function",
127            "Pet frame creation missing update function!");
128     assert(type(event) == "function",
129            "Pet frame creation missing event function!");
130
131     local f = CreateFrame("Button", "kehysPet", parent,
132                           "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
133     f:Hide();
134     f:SetPoint("BOTTOMRIGHT", parent, "BOTTOMLEFT", -5, 0);
135     f:SetWidth(width);
136     f:SetHeight(height);
137     f.barwidth = width - 2; -- 1px padding
138     f:SetAttribute("unit", "pet");
139     f:SetAttribute("displayed", "pet");
140     f:SetAttribute("vehicle", "player");
141     f.unit = "pet";
142     f.displayed = "pet";
143     f.vehicle = "player"
144     f.prev = {};
145
146     petUpdate = function()
147         CTimerAfter(0.1, petUpdate);
148         update(f);
149     end
150     f:SetScript("OnEvent", event);
151     f:RegisterForClicks("AnyDown");
152     f:SetAttribute("*type1", "target");
153     f:SetAttribute("*type2", "togglemenu");
154     f:SetAttribute("toggleForVehicle", true);
155     RegisterUnitWatch(f);
156     RegisterStateDriver(f, "vehicleui", "[vehicleui] vehicle; no");
157     f:SetAttribute("_onstate-vehicleui", vehicletoggle);
158
159     -- create visuals
160     f.base = f:CreateTexture(nil, "BACKGROUND");
161     f.base:SetAllPoints();
162     f.base:SetColorTexture(0, 0, 0, 0.5);
163     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 1);
164     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
165     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
166     f.health = f:CreateTexture(nil, "BORDER");
167     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
168     f.health:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
169     f.health:SetTexture(barTexture);
170     f.health:SetVertexColor(0.7, 0.7, 0.7);
171     f.health:Hide();
172     f.shield = f:CreateTexture(nil, "BORDER");
173     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
174     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
175     f.shield:SetTexture(barTexture);
176     f.shield:SetVertexColor(0, 0.7, 1);
177     f.shield:Hide();
178     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
179     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
180     f.shieldhl:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT");
181     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
182     f.shieldhl:Hide();
183     f.healpred = f:CreateTexture(nil, "ARTWORK");
184     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
185     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
186     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
187     f.healpred:Hide();
188     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
189     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
190     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
191     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
192     f.healabsorb:Hide();
193     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
194     f.name:SetPoint("LEFT", f, "LEFT", 5, 0);
195     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
196     f.text:SetPoint("RIGHT", f, "RIGHT", -5, 0);
197     f.text:SetFont(STANDARD_TEXT_FONT, 13);
198     f.text:Hide();
199     f.targeticon = f:CreateTexture(nil, "OVERLAY");
200     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
201     f.targeticon:SetWidth(12);
202     f.targeticon:SetHeight(12);
203     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
204     f.targeticon:Hide();
205
206     addon.RegisterEvents(f);
207     addon.RegisterUnitEvents(f);
208     event(f, "UPDATE_ALL_BARS");
209     petUpdate();
210     f:Show();
211 end
212
213 CFrame:SetScript("OnEvent", function(self)
214     self:UnregisterAllEvents();
215     CFrame:SetFrameStrata("LOW");
216     CFrame:SetPoint("CENTER", nil, "CENTER");
217     CFrame:SetWidth(2);
218     CFrame:SetHeight(2);
219     local player = initPlayer(self, 160, 48, addon.FrameUpdate, addon.UnitEvent);
220     initPet(player, 80, 24, addon.FrameUpdate, addon.UnitEvent);
221 end);
222 CFrame:RegisterEvent("PLAYER_LOGIN");