]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Merge branch 'master' into Mario/pure_entity_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / centerprint.qc
1 #include "centerprint.qh"
2
3 #include "scoreboard.qh"
4 #include <common/notifications/all.qh>
5 #include <client/defs.qh>
6 #include <client/miscfunctions.qh>
7
8 // CenterPrint (#16)
9
10 void HUD_CenterPrint_Export(int fh)
11 {
12         // allow saving cvars that aesthetically change the panel into hud skin files
13         HUD_Write_Cvar("hud_panel_centerprint_align");
14         HUD_Write_Cvar("hud_panel_centerprint_flip");
15         HUD_Write_Cvar("hud_panel_centerprint_fontscale");
16         HUD_Write_Cvar("hud_panel_centerprint_fontscale_bold");
17         HUD_Write_Cvar("hud_panel_centerprint_time");
18         HUD_Write_Cvar("hud_panel_centerprint_fade_in");
19         HUD_Write_Cvar("hud_panel_centerprint_fade_out");
20         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent");
21         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone");
22         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone_minalpha");
23         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo");
24         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo_minalpha");
25         HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_minfontsize");
26         HUD_Write_Cvar("hud_panel_centerprint_fade_minfontsize");
27 }
28
29 // These are the functions that draw the text at the center of the screen (e.g. frag messages and server MOTDs).
30 // Usually local_notification_centerprint_generic() is called, which in turn calls centerprint_generic(), which
31 // uses some kind of macro magic to call HUD_CenterPrint, which draws them on screen using drawcolorcodedstring().
32
33 const int CENTERPRINT_MAX_MSGS = 10;
34 const int CENTERPRINT_MAX_ENTRIES = 50;
35 const float CENTERPRINT_SPACING = 0.7;
36 int cpm_index;
37 string centerprint_messages[CENTERPRINT_MAX_MSGS];
38 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
39 float centerprint_time[CENTERPRINT_MAX_MSGS];
40 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
41 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
42 bool centerprint_showing;
43
44 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
45 {
46         TC(int, new_id); TC(int, countdown_num);
47         //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
48         int i, j;
49
50         if(strMessage == "" && new_id == 0)
51                 return;
52
53         // strip trailing newlines
54         j = strlen(strMessage) - 1;
55         while(substring(strMessage, j, 1) == "\n" && j >= 0)
56                 --j;
57         if (j < strlen(strMessage) - 1)
58                 strMessage = substring(strMessage, 0, j + 1);
59
60         if(strMessage == "" && new_id == 0)
61                 return;
62
63         // strip leading newlines
64         j = 0;
65         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
66                 ++j;
67         if (j > 0)
68                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
69
70         if(strMessage == "" && new_id == 0)
71                 return;
72
73         if (!centerprint_showing)
74                 centerprint_showing = true;
75
76         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
77         {
78                 if (j == CENTERPRINT_MAX_MSGS)
79                         j = 0;
80                 if (new_id && new_id == centerprint_msgID[j])
81                 {
82                         if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
83                         {
84                                 // fade out the current msg (duration and countdown_num are ignored)
85                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
86                                 centerprint_expire_time[j] = -1; // don't use the variable time here!
87                                 return;
88                         }
89                         break; // found a msg with the same id, at position j
90                 }
91         }
92
93         if (i == CENTERPRINT_MAX_MSGS)
94         {
95                 // a msg with the same id was not found, add the msg at the next position
96                 --cpm_index;
97                 if (cpm_index == -1)
98                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
99                 j = cpm_index;
100         }
101         strcpy(centerprint_messages[j], strMessage);
102         centerprint_msgID[j] = new_id;
103         if (duration < 0)
104         {
105                 centerprint_time[j] = -1;
106                 centerprint_expire_time[j] = -1; // don't use the variable time here!
107         }
108         else
109         {
110                 if(duration == 0)
111                         duration = max(1, autocvar_hud_panel_centerprint_time);
112                 centerprint_time[j] = duration;
113                 centerprint_expire_time[j] = -1; // don't use the variable time here!
114         }
115         centerprint_countdown_num[j] = countdown_num;
116 }
117
118 void centerprint_kill(int id)
119 {
120         TC(int, id);
121         centerprint_generic(id, "", 0, 0);
122 }
123
124 void centerprint_hud(string strMessage)
125 {
126         centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
127 }
128
129 void reset_centerprint_messages()
130 {
131         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
132         {
133                 centerprint_expire_time[i] = 0;
134                 centerprint_time[i] = 1;
135                 centerprint_msgID[i] = 0;
136                 strfree(centerprint_messages[i]);
137         }
138 }
139
140 float hud_configure_cp_generation_time;
141 void HUD_CenterPrint ()
142 {
143         if(!autocvar__hud_configure)
144         {
145                 if(!autocvar_hud_panel_centerprint) return;
146
147                 if(hud_configure_prev)
148                         reset_centerprint_messages();
149         }
150         else
151         {
152                 if(!hud_configure_prev)
153                 {
154                         reset_centerprint_messages();
155                         hud_configure_cp_generation_time = time; // show a message immediately
156                 }
157                 if (time > hud_configure_cp_generation_time)
158                 {
159                         if(highlightedPanel == HUD_PANEL(CENTERPRINT))
160                         {
161                                 float r;
162                                 r = random();
163                                 if (r > 0.8)
164                                         centerprint_generic(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
165                                 else if (r > 0.55)
166                                         centerprint_generic(0, sprintf(_("^1Multiline message at time %s that\n^1lasts longer than normal"), seconds_tostring(time)), 20, 0);
167                                 else
168                                         centerprint_hud(sprintf(_("Message at time %s"), seconds_tostring(time)));
169                                 hud_configure_cp_generation_time = time + 1 + random()*4;
170                         }
171                         else
172                         {
173                                 centerprint_generic(0, _("Generic message"), 10, 0);
174                                 hud_configure_cp_generation_time = time + 10 - random()*3;
175                         }
176                 }
177         }
178
179         HUD_Panel_LoadCvars();
180
181         if ( HUD_Radar_Clickable() )
182         {
183                 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
184                         return;
185
186                 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
187                 panel_pos.y = hud_panel_radar_bottom;
188                 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
189         }
190         else if(!autocvar__hud_configure && scoreboard_fade_alpha)
191         {
192                 // move the panel below the scoreboard
193                 if (scoreboard_bottom >= 0.96 * vid_conheight)
194                         return;
195                 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
196                 if(target_pos.y > panel_pos.y)
197                 {
198                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
199                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
200                 }
201         }
202
203         if (autocvar_hud_panel_centerprint_dynamichud)
204                 HUD_Scale_Enable();
205         else
206                 HUD_Scale_Disable();
207         HUD_Panel_DrawBg();
208
209         if (!centerprint_showing)
210                 return;
211
212         if(panel_bg_padding)
213         {
214                 panel_pos += '1 1 0' * panel_bg_padding;
215                 panel_size -= '2 2 0' * panel_bg_padding;
216         }
217
218         bool is_bold;
219         string centerprint_message = strzone("");
220         int entries;
221         float height;
222         vector fontsize;
223
224         int i, j, k, n, g;
225         float a, sz, align, current_msg_posY = 0, msg_size;
226         vector pos;
227         string ts;
228         bool all_messages_expired = true;
229
230         pos = panel_pos;
231         if (autocvar_hud_panel_centerprint_flip)
232                 pos.y += panel_size.y;
233         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
234         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
235         {
236                 // if the notification starts with ^BOLD make it bold (e.g. frag messages);
237                 is_bold = (substring(centerprint_messages[j], 0, 5) == BOLD_OPERATOR);
238                 // remove ^BOLD so it doesn't get printed onscreen
239                 if (is_bold)
240                         strcpy(centerprint_message, substring(centerprint_messages[j], 5, -1));
241                 else
242                         strcpy(centerprint_message, centerprint_messages[j]);
243
244                 // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
245                 // height = panel_size_y/entries;
246                 // fontsize = '1 1 0' * height;
247                 height = (is_bold) ? vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale_bold : vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
248                 fontsize = '1 1 0' * height;
249                 entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
250
251                 if (j == CENTERPRINT_MAX_MSGS)
252                         j = 0;
253                 if (centerprint_expire_time[j] == -1)
254                 {
255                         // here we are sure the time variable is not altered by CSQC_Ent_Update
256                         centerprint_expire_time[j] = time;
257                         if (centerprint_time[j] > 0)
258                                 centerprint_expire_time[j] += centerprint_time[j];
259                 }
260                 if (centerprint_expire_time[j] <= time)
261                 {
262                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
263                         {
264                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
265                                 if (centerprint_countdown_num[j] == 0)
266                                         continue;
267                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
268                         }
269                         else if(centerprint_time[j] != -1)
270                                 continue;
271                 }
272
273                 all_messages_expired = false;
274
275                 // fade the centerprint_hud in/out
276                 if(centerprint_time[j] < 0)  // Expired but forced. Expire time is the fade-in time.
277                         a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
278                 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time)  // Regularily printed. Not fading out yet.
279                         a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
280                 else // Expiring soon, so fade it out.
281                         a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
282
283                 if(centerprint_msgID[j] == CPID_TIMEIN)
284                         a = 1;
285
286                 // while counting down show it anyway in order to hold the current message position
287                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
288                         continue;
289                 if (a > 1)
290                         a = 1;
291
292                 // set the size from fading in/out before subsequent fading
293                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
294
295                 // also fade it based on positioning
296                 if(autocvar_hud_panel_centerprint_fade_subsequent)
297                 {
298                         // pass one: all messages after the first have half alpha
299                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
300                         // pass two: after that, gradually lower alpha even more for each message
301                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
302                 }
303                 a *= panel_fg_alpha;
304
305                 // finally set the size based on the new alpha from subsequent fading
306                 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
307                 drawfontscale = hud_scale * sz;
308
309                 if (centerprint_countdown_num[j])
310                         n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_message), "\n");
311                 else
312                         n = tokenizebyseparator(centerprint_message, "\n");
313
314                 if (autocvar_hud_panel_centerprint_flip)
315                 {
316                         // check if the message can be entirely shown
317                         for(k = 0; k < n; ++k)
318                         {
319                                 getWrappedLine_remaining = argv(k);
320                                 while(getWrappedLine_remaining)
321                                 {
322                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
323                                         if (ts != "")
324                                                 pos.y -= fontsize.y;
325                                         else
326                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
327                                 }
328                         }
329                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
330                 }
331
332                 msg_size = pos.y;
333                 for(k = 0; k < n; ++k)
334                 {
335                         getWrappedLine_remaining = argv(k);
336                         while(getWrappedLine_remaining)
337                         {
338                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
339                                 if (ts != "")
340                                 {
341                                         if (align)
342                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
343                                         if (a > 0.5/255.0)  // Otherwise guaranteed invisible - don't show. This is checked a second time after some multiplications with other factors were done so temporary changes of these cannot cause flicker.
344                                                 if (is_bold) draw_beginBoldFont();
345                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
346                                                 if (is_bold) draw_endBoldFont();
347                                         pos.y += fontsize.y;
348                                 }
349                                 else
350                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
351                         }
352                 }
353
354                 ++g; // move next position number up
355
356                 msg_size = pos.y - msg_size;
357                 if (autocvar_hud_panel_centerprint_flip)
358                 {
359                         pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
360                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
361                                 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
362
363                         if (pos.y < panel_pos.y) // check if the next message can be shown
364                         {
365                                 drawfontscale = hud_scale;
366                                 strfree(centerprint_message);
367                                 return;
368                         }
369                 }
370                 else
371                 {
372                         pos.y += CENTERPRINT_SPACING * fontsize.y;
373                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
374                                 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
375
376                         if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
377                         {
378                                 drawfontscale = hud_scale;
379                                 strfree(centerprint_message);
380                                 return;
381                         }
382                 }
383         }
384         strfree(centerprint_message);
385
386         drawfontscale = hud_scale;
387         if (all_messages_expired)
388         {
389                 centerprint_showing = false;
390                 reset_centerprint_messages();
391         }
392 }