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