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