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