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