]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Fix some centerprint mess
[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 int cpm_index;
38 string centerprint_messages[CENTERPRINT_MAX_MSGS];
39 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
40 float centerprint_time[CENTERPRINT_MAX_MSGS];
41 float centerprint_start_time[CENTERPRINT_MAX_MSGS];
42 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
43 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
44 bool centerprint_showing;
45
46 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
47 {
48         TC(int, new_id); TC(int, countdown_num);
49         LOG_INFOF("centerprint_Add: ^2id: %d ^3dur: %d ^5countdown: %d\n'%s'", new_id, duration, countdown_num, strreplace("\n", "^7\\n^7", strMessage));
50         int i, j;
51
52         if(strMessage == "" && new_id == 0)
53                 return;
54
55         // strip trailing newlines
56         j = strlen(strMessage) - 1;
57         while(substring(strMessage, j, 1) == "\n" && j >= 0)
58                 --j;
59         if (j < strlen(strMessage) - 1)
60                 strMessage = substring(strMessage, 0, j + 1);
61
62         if(strMessage == "" && new_id == 0)
63                 return;
64
65         // strip leading newlines
66         j = 0;
67         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
68                 ++j;
69         if (j > 0)
70                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
71
72         if(strMessage == "" && new_id == 0)
73                 return;
74
75         if (!centerprint_showing)
76                 centerprint_showing = true;
77
78         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
79         {
80                 if (j == CENTERPRINT_MAX_MSGS)
81                         j = 0;
82                 if (new_id && new_id == centerprint_msgID[j])
83                 {
84                         if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
85                         {
86                                 // fade out the current msg (duration and countdown_num are ignored)
87                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
88                                 centerprint_expire_time[j] = -1; // don't use the variable time here!
89                                 return;
90                         }
91                         break; // found a msg with the same id, at position j
92                 }
93         }
94
95         if (i == CENTERPRINT_MAX_MSGS)
96         {
97                 // a msg with the same id was not found, add the msg at the next position
98                 --cpm_index;
99                 if (cpm_index == -1)
100                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
101                 j = cpm_index;
102         }
103         strcpy(centerprint_messages[j], strMessage);
104         centerprint_start_time[j] = time;
105         centerprint_msgID[j] = new_id;
106         if (duration < 0)
107         {
108                 centerprint_time[j] = -1;
109                 centerprint_expire_time[j] = -1; // don't use the variable time here!
110         }
111         else
112         {
113                 if(duration == 0)
114                         duration = max(1, autocvar_hud_panel_centerprint_time);
115                 centerprint_time[j] = duration;
116                 centerprint_expire_time[j] = -1; // don't use the variable time here!
117         }
118         centerprint_countdown_num[j] = countdown_num;
119 }
120
121 void centerprint_Kill(int id)
122 {
123         TC(int, id);
124         centerprint_Add(id, "", 0, 0);
125 }
126
127 void centerprint_AddStandard(string strMessage)
128 {
129         centerprint_Add(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
130 }
131
132 void centerprint_KillAll()
133 {
134         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
135         {
136                 centerprint_start_time[i] = 0;
137                 centerprint_expire_time[i] = 0;
138                 centerprint_time[i] = 1;
139                 centerprint_msgID[i] = 0;
140                 strfree(centerprint_messages[i]);
141         }
142 }
143
144 float hud_configure_cp_generation_time;
145 void HUD_CenterPrint()
146 {
147         if(!autocvar__hud_configure)
148         {
149                 if(!autocvar_hud_panel_centerprint) return;
150
151                 if(hud_configure_prev)
152                         centerprint_KillAll();
153         }
154         else
155         {
156                 if(!hud_configure_prev)
157                 {
158                         centerprint_KillAll();
159                         hud_configure_cp_generation_time = time; // show a message immediately
160                 }
161                 if (time > hud_configure_cp_generation_time)
162                 {
163                         if(highlightedPanel == HUD_PANEL(CENTERPRINT))
164                         {
165                                 float r;
166                                 r = random();
167                                 if (r > 0.8)
168                                         centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
169                                 else if (r > 0.55)
170                                         centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^BOLDlasts longer than normal"), seconds_tostring(time)), 20, 0);
171                                 else
172                                         centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
173                                 //hud_configure_cp_generation_time = time + 1 + random()*4;
174                                 hud_configure_cp_generation_time = time + 1;
175                         }
176                         else
177                         {
178                                 centerprint_Add(0, _("Generic message"), 10, 0);
179                                 hud_configure_cp_generation_time = time + 10 - random()*3;
180                         }
181                 }
182         }
183
184         HUD_Panel_LoadCvars();
185
186         if ( HUD_Radar_Clickable() )
187         {
188                 if (hud_panel_radar_bottom >= vid_conheight)
189                         return;
190
191                 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
192                 panel_pos.y = hud_panel_radar_bottom;
193                 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
194         }
195         else if(!autocvar__hud_configure && scoreboard_fade_alpha)
196         {
197                 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
198                 if(target_pos.y > panel_pos.y)
199                 {
200                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
201                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
202                 }
203
204                 // move the panel below the scoreboard
205                 if (panel_pos.y >= vid_conheight)
206                         return;
207         }
208
209         if (autocvar_hud_panel_centerprint_dynamichud)
210                 HUD_Scale_Enable();
211         else
212                 HUD_Scale_Disable();
213         HUD_Panel_DrawBg();
214
215         if (!centerprint_showing)
216                 return;
217
218         if(panel_bg_padding)
219         {
220                 panel_pos += '1 1 0' * panel_bg_padding;
221                 panel_size -= '2 2 0' * panel_bg_padding;
222         }
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 (j == CENTERPRINT_MAX_MSGS)
237                         j = 0;
238                 if (centerprint_expire_time[j] == -1)
239                 {
240                         // here we are sure the time variable is not altered by CSQC_Ent_Update
241                         centerprint_expire_time[j] = time;
242                         if (centerprint_time[j] > 0)
243                                 centerprint_expire_time[j] += centerprint_time[j];
244                 }
245                 if (centerprint_expire_time[j] <= time)
246                 {
247                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
248                         {
249                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
250                                 if (centerprint_countdown_num[j] == 0)
251                                         continue;
252                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
253                         }
254                         else if(centerprint_time[j] != -1)
255                                 continue;
256                 }
257
258                 all_messages_expired = false;
259
260                 if (time < centerprint_start_time[j]) continue;
261
262                 float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
263                 float fade_out_time = autocvar_hud_panel_centerprint_fade_out;
264
265                 if (centerprint_countdown_num[j]) {
266                         fade_in_time = autocvar_hud_panel_centerprint_fade_in_short;
267                         fade_out_time = 0;
268                 }
269
270                 // fade
271                 a = 1;
272                 if(fade_in_time && centerprint_start_time[j] && time < centerprint_start_time[j] + fade_in_time) // Fade in
273                         a = (time - centerprint_start_time[j]) / fade_in_time;
274                 else if(time < centerprint_expire_time[j] - fade_out_time || centerprint_time[j] < 0) // Regularily printed or forced
275                         a = 1;
276                 else if(fade_out_time) // Expiring soon, so fade it out.
277                         a = (centerprint_expire_time[j] - time) / fade_out_time;
278
279                 if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
280                         a = 1;
281
282                 // while counting down show it anyway in order to hold the current message position
283                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
284                         continue;
285
286                 // set the size from fading in/out before subsequent fading
287                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
288
289                 // also fade it based on positioning
290                 if(autocvar_hud_panel_centerprint_fade_subsequent)
291                 {
292                         // pass one: all messages after the first have half alpha
293                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
294                         // pass two: after that, gradually lower alpha even more for each message
295                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
296                 }
297                 a *= panel_fg_alpha;
298
299                 // finally set the size based on the new alpha from subsequent fading
300                 // TODO: Apply this only if subsequent, otherwise it fucks it up
301                 if(g)
302                         sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
303                 drawfontscale = hud_scale * sz;
304
305                 if (centerprint_countdown_num[j])
306                         n = tokenizebyseparator(strreplace("^COUNT", ftos(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
307                 else
308                         n = tokenizebyseparator(centerprint_messages[j], "\n");
309
310                 if (autocvar_hud_panel_centerprint_flip)
311                 {
312                         // check if the message can be entirely shown
313                         for(k = 0; k < n; ++k)
314                         {
315                                 getWrappedLine_remaining = argv(k);
316                                 while(getWrappedLine_remaining)
317                                 {
318                                         bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
319                                         vector fontsize = hud_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
320
321                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
322                                         if (ts != "")
323                                                 pos.y -= fontsize.y;
324                                         else
325                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
326                                 }
327                         }
328                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
329                 }
330
331                 msg_size = pos.y;
332                 for(k = 0; k < n; ++k)
333                 {
334                         getWrappedLine_remaining = argv(k);
335
336                         bool is_bold = (substring(getWrappedLine_remaining, 0, 5) == BOLD_OPERATOR);
337                         vector fontsize = hud_fontsize * (is_bold ? autocvar_hud_panel_centerprint_fontscale_bold : autocvar_hud_panel_centerprint_fontscale);
338                         if (is_bold)
339                                 getWrappedLine_remaining = substring(getWrappedLine_remaining, 5, -1);
340
341                         while(getWrappedLine_remaining)
342                         {
343                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
344                                 if (ts != "")
345                                 {
346                                         if (align)
347                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
348                                         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.
349                                                 if (is_bold) draw_beginBoldFont();
350                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
351                                                 if (is_bold) draw_endBoldFont();
352                                         pos.y += fontsize.y;
353                                 }
354                                 else
355                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
356                         }
357                 }
358
359                 ++g; // move next position number up
360
361                 msg_size = pos.y - msg_size;
362
363                 if (autocvar_hud_panel_centerprint_flip)
364                 {
365                         pos.y -= msg_size + CENTERPRINT_SPACING * hud_fontsize.y;
366                         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
367                                 pos.y += (1 - sqrt(a));
368
369                         if (pos.y < panel_pos.y) // check if the next message can be shown
370                         {
371                                 drawfontscale = hud_scale;
372                                 return;
373                         }
374                 }
375                 else
376                 {
377                         pos.y += CENTERPRINT_SPACING * hud_fontsize.y;
378                         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
379                                 pos.y -= (1 - sqrt(a));
380
381                         if(pos.y > panel_pos.y + panel_size.y - hud_fontsize.y) // check if the next message can be shown
382                         {
383                                 drawfontscale = hud_scale;
384                                 return;
385                         }
386                 }
387         }
388
389         drawfontscale = hud_scale;
390         if (all_messages_expired)
391         {
392                 centerprint_showing = false;
393                 centerprint_KillAll();
394         }
395 }