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