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