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