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