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