]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud.qh
Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qh
1 #ifndef HUD_H
2 #define HUD_H
3
4 #include "../common/weapons/all.qh"
5
6 const int HUD_PANEL_MAX = 24;
7 entity hud_panel[HUD_PANEL_MAX];
8 const int HUD_PANEL_FIRST = 0;
9 int HUD_PANEL_NUM;
10 int HUD_PANEL_LAST;
11
12 int panel_order[HUD_PANEL_MAX];
13 string hud_panelorder_prev;
14
15 void HUD_Reset (void);
16 void HUD_Main (void);
17
18 int vote_yescount;
19 int vote_nocount;
20 int vote_needed;
21 int vote_highlighted; // currently selected vote
22
23 int vote_active; // is there an active vote?
24 int vote_prev; // previous state of vote_active to check for a change
25 float vote_alpha;
26 float vote_change; // "time" when vote_active changed
27
28 float hud_draw_maximized;
29 float hud_panel_radar_maximized;
30 float chat_panel_modified;
31 float radar_panel_modified;
32
33 vector mousepos;
34 vector panel_click_distance; // mouse cursor distance from the top left corner of the panel (saved only upon a click)
35 vector panel_click_resizeorigin; // coordinates for opposite point when resizing
36 float resizeCorner; // 1 = topleft, 2 = topright, 3 = bottomleft, 4 = bottomright
37 entity highlightedPanel;
38 float highlightedAction; // 0 = nothing, 1 = move, 2 = resize
39
40 const float BORDER_MULTIPLIER = 0.25;
41 float scoreboard_bottom;
42 int weapon_accuracy[WEP_MAXCOUNT];
43
44 int complain_weapon;
45 string complain_weapon_name;
46 float complain_weapon_type;
47 float complain_weapon_time;
48
49 int ps_primary, ps_secondary;
50 int ts_primary, ts_secondary;
51
52 int last_switchweapon;
53 int last_activeweapon;
54 float weapontime;
55 float weaponprevtime;
56
57 float teamnagger;
58
59 float hud_configure_checkcollisions;
60 float hud_configure_prev;
61 vector hud_configure_gridSize;
62 vector hud_configure_realGridSize;
63
64 int hudShiftState;
65 const int S_SHIFT = 1;
66 const int S_CTRL = 2;
67 const int S_ALT = 4;
68
69 float menu_enabled; // 1 showing the entire HUD, 2 showing only the clicked panel
70
71 float hud_fade_alpha;
72
73 string hud_skin_path;
74 string hud_skin_prev;
75
76 vector myteamcolors;
77
78 entity highlightedPanel_backup;
79 vector panel_pos_backup;
80 vector panel_size_backup;
81
82 vector panel_size_copied;
83
84 entity panel;
85 entityclass(HUDPanel);
86 class(HUDPanel) .string panel_name;
87 class(HUDPanel) .int panel_id;
88 class(HUDPanel) .vector current_panel_pos;
89 class(HUDPanel) .vector current_panel_size;
90 class(HUDPanel) .string current_panel_bg;
91 class(HUDPanel) .float current_panel_bg_alpha;
92 class(HUDPanel) .float current_panel_bg_border;
93 class(HUDPanel) .vector current_panel_bg_color;
94 class(HUDPanel) .float current_panel_bg_color_team;
95 class(HUDPanel) .float current_panel_bg_padding;
96 class(HUDPanel) .float current_panel_fg_alpha;
97 class(HUDPanel) .float update_time;
98 float panel_enabled;
99 vector panel_pos;
100 vector panel_size;
101 string panel_bg_str; // "_str" vars contain the raw value of the cvar, non-"_str" contains what hud.qc code should use
102 vector panel_bg_color;
103 string panel_bg_color_str;
104 float panel_bg_color_team;
105 string panel_bg_color_team_str;
106 float panel_fg_alpha;
107 float panel_bg_alpha;
108 string panel_bg_alpha_str;
109 float panel_bg_border;
110 string panel_bg_border_str;
111 float panel_bg_padding;
112 string panel_bg_padding_str;
113
114 class(HUDPanel) .void() panel_draw;
115
116 // chat panel can be reduced / moved while the mapvote is active
117 // let know the mapvote panel about chat pos and size
118 float chat_posy;
119 float chat_sizey;
120
121 float current_player;
122
123 float GetPlayerColorForce(int i);
124
125 float stringwidth_colors(string s, vector theSize);
126 int GetPlayerColor(int i);
127 string GetPlayerName(int i);
128 float stringwidth_nocolors(string s, vector theSize);
129 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag);
130
131
132 // prev_* vars contain the health/armor at the previous FRAME
133 // set to -1 when player is dead or was not playing
134 int prev_health, prev_armor;
135 float health_damagetime, armor_damagetime;
136 int health_beforedamage, armor_beforedamage;
137 // old_p_* vars keep track of previous values when smoothing value changes of the progressbar
138 int old_p_health, old_p_armor;
139 float old_p_healthtime, old_p_armortime;
140 // prev_p_* vars contain the health/armor progressbar value at the previous FRAME
141 // set to -1 to forcedly stop effects when we switch spectated player (e.g. from playerX: 70h to playerY: 50h)
142 int prev_p_health, prev_p_armor;
143
144
145 #define HUD_PANELS(HUD_PANEL) \
146         HUD_PANEL(WEAPONS      , HUD_Weapons      , weapons) \
147         HUD_PANEL(AMMO         , HUD_Ammo         , ammo) \
148         HUD_PANEL(POWERUPS     , HUD_Powerups     , powerups) \
149         HUD_PANEL(HEALTHARMOR  , HUD_HealthArmor  , healtharmor) \
150         HUD_PANEL(NOTIFY       , HUD_Notify       , notify) \
151         HUD_PANEL(TIMER        , HUD_Timer        , timer) \
152         HUD_PANEL(RADAR        , HUD_Radar        , radar) \
153         HUD_PANEL(SCORE        , HUD_Score        , score) \
154         HUD_PANEL(RACETIMER    , HUD_RaceTimer    , racetimer) \
155         HUD_PANEL(VOTE         , HUD_Vote         , vote) \
156         HUD_PANEL(MODICONS     , HUD_ModIcons     , modicons) \
157         HUD_PANEL(PRESSEDKEYS  , HUD_PressedKeys  , pressedkeys) \
158         HUD_PANEL(CHAT         , HUD_Chat         , chat) \
159         HUD_PANEL(ENGINEINFO   , HUD_EngineInfo   , engineinfo) \
160         HUD_PANEL(INFOMESSAGES , HUD_InfoMessages , infomessages) \
161         HUD_PANEL(PHYSICS      , HUD_Physics      , physics) \
162         HUD_PANEL(CENTERPRINT  , HUD_CenterPrint  , centerprint) \
163         HUD_PANEL(MAPVOTE      , MapVote_Draw     , mapvote) \
164         HUD_PANEL(ITEMSTIME    , HUD_ItemsTime    , itemstime) \
165         // always add new panels to the end of list
166
167 #define HUD_PANEL(NAME, draw_func, name)                                                                                                                                                        \
168         int HUD_PANEL_##NAME;                                                                                                                                                                                   \
169         void draw_func(void);                                                                                                                                                                                   \
170         void RegisterHUD_Panel_##NAME() {                                                                                                                                                               \
171                 HUD_PANEL_LAST = HUD_PANEL_##NAME = HUD_PANEL_NUM;                                                                                                                      \
172                 entity hud_panelent = spawn();                                                                                                                                                          \
173                 hud_panel[HUD_PANEL_##NAME] = hud_panelent;                                                                                                                             \
174                 hud_panelent.classname = "hud_panel";                                                                                                                                           \
175                 hud_panelent.panel_name = #name;                                                                                                                                                        \
176                 hud_panelent.panel_id = HUD_PANEL_##NAME;                                                                                                                                       \
177                 hud_panelent.panel_draw = draw_func;                                                                                                                                            \
178                 HUD_PANEL_NUM++;                                                                                                                                                                                        \
179         }                                                                                                                                                                                                                               \
180         ACCUMULATE_FUNCTION(RegisterHUD_Panels, RegisterHUD_Panel_##NAME);
181
182 HUD_PANELS(HUD_PANEL)
183 #undef HUD_PANEL
184
185 #define HUD_PANEL(NAME) hud_panel[HUD_PANEL_##NAME]
186
187
188 // Because calling lots of functions in QC apparently cuts fps in half on many machines:
189 // ----------------------
190 // MACRO HELL STARTS HERE
191 // ----------------------
192 // Little help for the poor people who have to make sense of this: Start from the bottom ;)
193
194 // Get value for panel.current_panel_bg: if "" fetch default, else use panel_bg_str
195 // comment on last line of macro: // we probably want to see a background in config mode at all times...
196 #define HUD_Panel_GetBg() do {                                                                                      \
197         string panel_bg;                                                                                                \
198         if (!autocvar__hud_configure && panel_bg_str == "0") {                                                          \
199                 panel_bg = "0";                                                                                             \
200         } else {                                                                                                        \
201                 if (panel_bg_str == "") {                                                                                   \
202                         panel_bg_str = autocvar_hud_panel_bg;                                                                   \
203                 }                                                                                                           \
204                 if (panel_bg_str == "0" && !autocvar__hud_configure) {                                                      \
205                         panel_bg = "0";                                                                                         \
206                 } else {                                                                                                    \
207                         if (panel_bg_str == "0" && autocvar__hud_configure)                                                     \
208                                 panel_bg_alpha_str = "0";                                                                           \
209                         panel_bg = strcat(hud_skin_path, "/", panel_bg_str);                                                    \
210                         if (precache_pic(panel_bg) == "") {                                                                     \
211                                 panel_bg = strcat(hud_skin_path, "/", "border_default");                                            \
212                                 if (precache_pic(panel_bg) == "") {                                                                 \
213                                         panel_bg = strcat("gfx/hud/default/", "border_default");                                        \
214                                 }                                                                                                   \
215                         }                                                                                                       \
216                 }                                                                                                           \
217         }                                                                                                               \
218         if (panel.current_panel_bg)                                                                                     \
219                 strunzone(panel.current_panel_bg);                                                                          \
220         panel.current_panel_bg = strzone(panel_bg);                                                                     \
221 } while(0)
222
223 // Get value for panel_bg_color: if "" fetch default, else use panel_bg_color. Convert pants, shirt or teamcolor into a vector.
224 #define HUD_Panel_GetColor() do {                                                                                   \
225         if ((teamplay) && panel_bg_color_team) {                                                                        \
226                 if (autocvar__hud_configure && myteam == NUM_SPECTATOR)                                                     \
227                         panel_bg_color = '1 0 0' * panel_bg_color_team;                                                         \
228                 else                                                                                                        \
229                         panel_bg_color = myteamcolors * panel_bg_color_team;                                                    \
230         } else if (autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && panel_bg_color_team) {          \
231                 panel_bg_color = '1 0 0' * panel_bg_color_team;                                                             \
232         } else {                                                                                                        \
233                 if (panel_bg_color_str == "") {                                                                             \
234                         panel_bg_color = autocvar_hud_panel_bg_color;                                                           \
235                 } else {                                                                                                    \
236                         if (panel_bg_color_str == "shirt") {                                                                    \
237                                 panel_bg_color = colormapPaletteColor(floor(stof(getplayerkeyvalue(current_player, "colors")) / 16), 0); \
238                         } else if (panel_bg_color_str == "pants") {                                                             \
239                                 panel_bg_color = colormapPaletteColor(stof(getplayerkeyvalue(current_player, "colors")) % 16, 1); \
240                         } else {                                                                                                \
241                                 panel_bg_color = stov(panel_bg_color_str);                                                          \
242                         }                                                                                                       \
243                 }                                                                                                           \
244         }                                                                                                               \
245 } while(0)
246
247 // Get value for panel_bg_color_team: if "" fetch default, else use panel_bg_color_team_str
248 #define HUD_Panel_GetColorTeam() do {                                                                               \
249         if (panel_bg_color_team_str == "") {                                                                            \
250                 panel_bg_color_team = autocvar_hud_panel_bg_color_team;                                                     \
251         } else {                                                                                                        \
252                 panel_bg_color_team = stof(panel_bg_color_team_str);                                                        \
253         }                                                                                                               \
254 } while(0)
255
256 // Get value for panel_bg_alpha: if "" fetch default, else use panel_bg_alpha. Also do various menu dialog fadeout/in checks, and minalpha checks
257 // comment on line 3 of macro: // do not set a minalpha cap when showing the config dialog for this panel
258 #define HUD_Panel_GetBgAlpha() do {                                                                                 \
259         if (panel_bg_alpha_str == "") {                                                                                 \
260                 panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);                                                     \
261         }                                                                                                               \
262         panel_bg_alpha = stof(panel_bg_alpha_str);                                                                      \
263         if (autocvar__hud_configure) {                                                                                  \
264                 if (!panel_enabled)                                                                                         \
265                         panel_bg_alpha = 0.25;                                                                                  \
266                 else if (menu_enabled == 2 && panel == highlightedPanel)                                                    \
267                         panel_bg_alpha = (1 - autocvar__menu_alpha) * max(cvar("hud_configure_bg_minalpha"), panel_bg_alpha) + autocvar__menu_alpha * panel_bg_alpha;\
268                 else                                                                                                        \
269                         panel_bg_alpha = max(cvar("hud_configure_bg_minalpha"), panel_bg_alpha);                                \
270         }                                                                                                               \
271 } while(0)
272
273 // Get value for panel_fg_alpha. Also do various minalpha checks
274 // comment on line 2 of macro: // ALWAYS show disabled panels at 0.25 alpha when in config mode
275 #define HUD_Panel_GetFgAlpha() do {                                                                                 \
276         panel_fg_alpha = autocvar_hud_panel_fg_alpha;                                                                   \
277         if (autocvar__hud_configure && !panel_enabled)                                                                  \
278                 panel_fg_alpha = 0.25;                                                                                      \
279 } while(0)
280
281 // Get border. See comments above, it's similar.
282 #define HUD_Panel_GetBorder() do {                                                                                  \
283         if (panel_bg_border_str == "") {                                                                                \
284                 panel_bg_border = autocvar_hud_panel_bg_border;                                                             \
285         } else {                                                                                                        \
286                 panel_bg_border = stof(panel_bg_border_str);                                                                \
287         }                                                                                                               \
288 } while(0)
289
290 // Get padding. See comments above, it's similar.
291 // last line is a port of the old function, basically always make sure the panel contents are at least 5 pixels tall/wide, to disallow extreme padding values
292 #define HUD_Panel_GetPadding() do {                                                                                 \
293         if (panel_bg_padding_str == "") {                                                                               \
294                 panel_bg_padding = autocvar_hud_panel_bg_padding;                                                           \
295         } else {                                                                                                        \
296                 panel_bg_padding = stof(panel_bg_padding_str);                                                              \
297         }                                                                                                               \
298         panel_bg_padding = min(min(panel_size.x, panel_size.y)/2 - 5, panel_bg_padding);                                \
299 } while(0)
300
301 // return smoothly faded pos and size of given panel when a dialog is active
302 // don't center too wide panels, it doesn't work with different resolutions
303 #define HUD_Panel_UpdatePosSize_ForMenu() do { \
304         vector menu_enable_size = panel_size; \
305         float max_panel_width = 0.52 * vid_conwidth; \
306         if(panel_size.x > max_panel_width) \
307         { \
308                 menu_enable_size.x = max_panel_width; \
309                 menu_enable_size.y = panel_size.y * (menu_enable_size.x / panel_size.x); \
310         } \
311         vector menu_enable_pos = eX * (panel_bg_border + 0.5 * max_panel_width) + eY * 0.5 * vid_conheight - 0.5 * menu_enable_size; \
312         panel_pos = (1 - autocvar__menu_alpha) * panel_pos + (autocvar__menu_alpha) * menu_enable_pos; \
313         panel_size = (1 - autocvar__menu_alpha) * panel_size + (autocvar__menu_alpha) * menu_enable_size; \
314 } while(0)
315
316 // Scale the pos and size vectors to absolute coordinates
317 #define HUD_Panel_ScalePosSize() do {                                                                               \
318         panel_pos.x *= vid_conwidth;  panel_pos.y *= vid_conheight;                                                     \
319         panel_size.x *= vid_conwidth; panel_size.y *= vid_conheight;                                                    \
320 } while(0)
321
322 // NOTE: in hud_configure mode cvars must be reloaded every frame
323 #define HUD_Panel_UpdateCvars() do {                                                                                \
324         if (panel.update_time <= time) {                                                                                \
325                 if (autocvar__hud_configure) panel_enabled = cvar(strcat("hud_panel_", panel.panel_name));                  \
326                 panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));                              \
327                 panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));                            \
328                 HUD_Panel_ScalePosSize();                                                                                   \
329                 panel_bg_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg"));                                  \
330                 panel_bg_color_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color"));                      \
331                 panel_bg_color_team_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color_team"));            \
332                 panel_bg_alpha_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_alpha"));                      \
333                 panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));                    \
334                 panel_bg_padding_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_padding"));                  \
335                 HUD_Panel_GetBg();                                                                                          \
336                 if (panel.current_panel_bg != "0") {                                                                        \
337                         HUD_Panel_GetColorTeam();                                                                               \
338                         HUD_Panel_GetColor();                                                                                   \
339                         HUD_Panel_GetBgAlpha();                                                                                 \
340                         HUD_Panel_GetBorder();                                                                                  \
341                 }                                                                                                           \
342                 HUD_Panel_GetFgAlpha();                                                                                     \
343                 HUD_Panel_GetPadding();                                                                                     \
344                 panel.current_panel_bg_alpha = panel_bg_alpha;                                                              \
345                 panel.current_panel_fg_alpha = panel_fg_alpha;                                                              \
346                 if (menu_enabled == 2 && panel == highlightedPanel) {                                                       \
347                         HUD_Panel_UpdatePosSize_ForMenu();                                                                      \
348                 } else {                                                                                                    \
349                         panel_bg_alpha *= hud_fade_alpha;                                                                       \
350                         panel_fg_alpha *= hud_fade_alpha;                                                                       \
351                 }                                                                                                           \
352                 panel.current_panel_pos = panel_pos;                                                                        \
353                 panel.current_panel_size = panel_size;                                                                      \
354                 panel.current_panel_bg_border = panel_bg_border;                                                            \
355                 panel.current_panel_bg_color = panel_bg_color;                                                              \
356                 panel.current_panel_bg_color_team = panel_bg_color_team;                                                    \
357                 panel.current_panel_bg_padding = panel_bg_padding;                                                          \
358                 panel.update_time = (autocvar__hud_configure) ? time : time + autocvar_hud_panel_update_interval;           \
359         } else {                                                                                                        \
360                 panel_pos = panel.current_panel_pos;                                                                        \
361                 panel_size = panel.current_panel_size;                                                                      \
362                 panel_bg_alpha = panel.current_panel_bg_alpha * hud_fade_alpha;                                             \
363                 panel_bg_border = panel.current_panel_bg_border;                                                            \
364                 panel_bg_color = panel.current_panel_bg_color;                                                              \
365                 panel_bg_color_team = panel.current_panel_bg_color_team;                                                    \
366                 panel_bg_padding = panel.current_panel_bg_padding;                                                          \
367                 panel_fg_alpha = panel.current_panel_fg_alpha * hud_fade_alpha;                                             \
368         }                                                                                                               \
369 } while(0)
370
371 #define HUD_Panel_UpdatePosSize() do {                                                                              \
372         panel_enabled = cvar(strcat("hud_panel_", panel.panel_name));                                                   \
373         panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));                                  \
374         panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));                                \
375         HUD_Panel_ScalePosSize();                                                                                       \
376         if (menu_enabled == 2 && panel == highlightedPanel) {                                                           \
377                 HUD_Panel_UpdatePosSize_ForMenu();                                                                          \
378         }                                                                                                               \
379         panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));                        \
380         HUD_Panel_GetBorder();                                                                                          \
381 } while(0)
382
383 const int NOTIFY_MAX_ENTRIES = 10;
384 const float NOTIFY_ICON_MARGIN = 0.02;
385
386 int notify_index;
387 int notify_count;
388 float notify_times[NOTIFY_MAX_ENTRIES];
389 string notify_attackers[NOTIFY_MAX_ENTRIES];
390 string notify_victims[NOTIFY_MAX_ENTRIES];
391 string notify_icons[NOTIFY_MAX_ENTRIES];
392
393 void HUD_Notify_Push(string icon, string attacker, string victim);
394
395 var void HUD_ModIcons_GameType(vector pos, vector size);
396 void HUD_ModIcons_SetFunc();
397 #endif