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