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