]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Merge branch 'divVerent/simpler-clipped-rectangle' into 'master'
[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;
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                 centerprint_message = is_bold ? strzone(substring(centerprint_messages[j], 5, -1)) : strzone(centerprint_messages[j]);
220
221                 // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
222                 // height = panel_size_y/entries;
223                 // fontsize = '1 1 0' * height;
224                 height = (is_bold) ? vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale_bold : vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
225                 fontsize = '1 1 0' * height;
226                 entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
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                 if(centerprint_msgID[j] == CPID_TIMEIN)
261                         a = 1;
262
263                 // while counting down show it anyway in order to hold the current message position
264                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
265                         continue;
266                 if (a > 1)
267                         a = 1;
268
269                 // set the size from fading in/out before subsequent fading
270                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
271
272                 // also fade it based on positioning
273                 if(autocvar_hud_panel_centerprint_fade_subsequent)
274                 {
275                         // pass one: all messages after the first have half alpha
276                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
277                         // pass two: after that, gradually lower alpha even more for each message
278                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
279                 }
280                 a *= panel_fg_alpha;
281
282                 // finally set the size based on the new alpha from subsequent fading
283                 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
284                 drawfontscale = hud_scale * sz;
285
286                 if (centerprint_countdown_num[j])
287                         n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_message), "\n");
288                 else
289                         n = tokenizebyseparator(centerprint_message, "\n");
290
291                 if (autocvar_hud_panel_centerprint_flip)
292                 {
293                         // check if the message can be entirely shown
294                         for(k = 0; k < n; ++k)
295                         {
296                                 getWrappedLine_remaining = argv(k);
297                                 while(getWrappedLine_remaining)
298                                 {
299                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
300                                         if (ts != "")
301                                                 pos.y -= fontsize.y;
302                                         else
303                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
304                                 }
305                         }
306                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
307                 }
308
309                 msg_size = pos.y;
310                 for(k = 0; k < n; ++k)
311                 {
312                         getWrappedLine_remaining = argv(k);
313                         while(getWrappedLine_remaining)
314                         {
315                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
316                                 if (ts != "")
317                                 {
318                                         if (align)
319                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
320                                         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.
321                                                 if (is_bold) draw_beginBoldFont();
322                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
323                                                 if (is_bold) draw_endBoldFont();
324                                         pos.y += fontsize.y;
325                                 }
326                                 else
327                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
328                         }
329                 }
330
331                 ++g; // move next position number up
332
333                 msg_size = pos.y - msg_size;
334                 if (autocvar_hud_panel_centerprint_flip)
335                 {
336                         pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
337                         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
338                                 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
339
340                         if (pos.y < panel_pos.y) // check if the next message can be shown
341                         {
342                                 drawfontscale = hud_scale;
343                                 return;
344                         }
345                 }
346                 else
347                 {
348                         pos.y += CENTERPRINT_SPACING * fontsize.y;
349                         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
350                                 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
351
352                         if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
353                         {
354                                 drawfontscale = hud_scale;
355                                 return;
356                         }
357                 }
358
359                 // free up memory
360                 strunzone(centerprint_message);
361         }
362         drawfontscale = hud_scale;
363         if (all_messages_expired)
364         {
365                 centerprint_showing = false;
366                 reset_centerprint_messages();
367         }
368 }