]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Merge branch 'master' into martin-t/defaults
[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 const int CENTERPRINT_MAX_MSGS = 10;
11 const int CENTERPRINT_MAX_ENTRIES = 50;
12 const float CENTERPRINT_SPACING = 0.7;
13 int cpm_index;
14 string centerprint_messages[CENTERPRINT_MAX_MSGS];
15 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
16 float centerprint_time[CENTERPRINT_MAX_MSGS];
17 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
18 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
19 bool centerprint_showing;
20
21 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
22 {
23     TC(int, new_id); TC(int, countdown_num);
24         //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
25         int i, j;
26
27         if(strMessage == "" && new_id == 0)
28                 return;
29
30         // strip trailing newlines
31         j = strlen(strMessage) - 1;
32         while(substring(strMessage, j, 1) == "\n" && j >= 0)
33                 --j;
34         if (j < strlen(strMessage) - 1)
35                 strMessage = substring(strMessage, 0, j + 1);
36
37         if(strMessage == "" && new_id == 0)
38                 return;
39
40         // strip leading newlines
41         j = 0;
42         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
43                 ++j;
44         if (j > 0)
45                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
46
47         if(strMessage == "" && new_id == 0)
48                 return;
49
50         if (!centerprint_showing)
51                 centerprint_showing = true;
52
53         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
54         {
55                 if (j == CENTERPRINT_MAX_MSGS)
56                         j = 0;
57                 if (new_id && new_id == centerprint_msgID[j])
58                 {
59                         if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
60                         {
61                                 // fade out the current msg (duration and countdown_num are ignored)
62                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
63                                 centerprint_expire_time[j] = -1; // don't use the variable time here!
64                                 return;
65                         }
66                         break; // found a msg with the same id, at position j
67                 }
68         }
69
70         if (i == CENTERPRINT_MAX_MSGS)
71         {
72                 // a msg with the same id was not found, add the msg at the next position
73                 --cpm_index;
74                 if (cpm_index == -1)
75                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
76                 j = cpm_index;
77         }
78         if(centerprint_messages[j])
79                 strunzone(centerprint_messages[j]);
80         centerprint_messages[j] = strzone(strMessage);
81         centerprint_msgID[j] = new_id;
82         if (duration < 0)
83         {
84                 centerprint_time[j] = -1;
85                 centerprint_expire_time[j] = -1; // don't use the variable time here!
86         }
87         else
88         {
89                 if(duration == 0)
90                         duration = max(1, autocvar_hud_panel_centerprint_time);
91                 centerprint_time[j] = duration;
92                 centerprint_expire_time[j] = -1; // don't use the variable time here!
93         }
94         centerprint_countdown_num[j] = countdown_num;
95 }
96
97 void centerprint_kill(int id)
98 {
99     TC(int, id);
100         centerprint_generic(id, "", 0, 0);
101 }
102
103 void centerprint_hud(string strMessage)
104 {
105         centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
106 }
107
108 void reset_centerprint_messages()
109 {
110         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
111         {
112                 centerprint_expire_time[i] = 0;
113                 centerprint_time[i] = 1;
114                 centerprint_msgID[i] = 0;
115                 if(centerprint_messages[i])
116                         strunzone(centerprint_messages[i]);
117                 centerprint_messages[i] = string_null;
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), strcat(sprintf("^3Countdown message at time %s", seconds_tostring(time)), ", seconds left: ^COUNT"), 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, sprintf("Centerprint message", seconds_tostring(time)), 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         int entries;
199         float height;
200         vector fontsize;
201         // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
202         // height = panel_size_y/entries;
203         // fontsize = '1 1 0' * height;
204         height = vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
205         fontsize = '1 1 0' * height;
206         entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
207
208         int i, j, k, n, g;
209         float a, sz, align, current_msg_posY = 0, msg_size;
210         vector pos;
211         string ts;
212         bool all_messages_expired = true;
213
214         pos = panel_pos;
215         if (autocvar_hud_panel_centerprint_flip)
216                 pos.y += panel_size.y;
217         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
218         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
219         {
220                 if (j == CENTERPRINT_MAX_MSGS)
221                         j = 0;
222                 if (centerprint_expire_time[j] == -1)
223                 {
224                         // here we are sure the time variable is not altered by CSQC_Ent_Update
225                         centerprint_expire_time[j] = time;
226                         if (centerprint_time[j] > 0)
227                                 centerprint_expire_time[j] += centerprint_time[j];
228                 }
229                 if (centerprint_expire_time[j] <= time)
230                 {
231                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
232                         {
233                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
234                                 if (centerprint_countdown_num[j] == 0)
235                                         continue;
236                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
237                         }
238                         else if(centerprint_time[j] != -1)
239                                 continue;
240                 }
241
242                 all_messages_expired = false;
243
244                 // fade the centerprint_hud in/out
245                 if(centerprint_time[j] < 0)  // Expired but forced. Expire time is the fade-in time.
246                         a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
247                 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time)  // Regularily printed. Not fading out yet.
248                         a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
249                 else // Expiring soon, so fade it out.
250                         a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
251
252                 if(centerprint_msgID[j] == CPID_TIMEIN)
253                         a = 1;
254
255                 // while counting down show it anyway in order to hold the current message position
256                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
257                         continue;
258                 if (a > 1)
259                         a = 1;
260
261                 // set the size from fading in/out before subsequent fading
262                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
263
264                 // also fade it based on positioning
265                 if(autocvar_hud_panel_centerprint_fade_subsequent)
266                 {
267                         // pass one: all messages after the first have half alpha
268                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
269                         // pass two: after that, gradually lower alpha even more for each message
270                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
271                 }
272                 a *= panel_fg_alpha;
273
274                 // finally set the size based on the new alpha from subsequent fading
275                 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
276                 drawfontscale = hud_scale * sz;
277
278                 if (centerprint_countdown_num[j])
279                         n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
280                 else
281                         n = tokenizebyseparator(centerprint_messages[j], "\n");
282
283                 if (autocvar_hud_panel_centerprint_flip)
284                 {
285                         // check if the message can be entirely shown
286                         for(k = 0; k < n; ++k)
287                         {
288                                 getWrappedLine_remaining = argv(k);
289                                 while(getWrappedLine_remaining)
290                                 {
291                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
292                                         if (ts != "")
293                                                 pos.y -= fontsize.y;
294                                         else
295                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
296                                 }
297                         }
298                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
299                 }
300
301                 msg_size = pos.y;
302                 for(k = 0; k < n; ++k)
303                 {
304                         getWrappedLine_remaining = argv(k);
305                         while(getWrappedLine_remaining)
306                         {
307                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
308                                 if (ts != "")
309                                 {
310                                         if (align)
311                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
312                                         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.
313                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
314                                         pos.y += fontsize.y;
315                                 }
316                                 else
317                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
318                         }
319                 }
320
321                 ++g; // move next position number up
322
323                 msg_size = pos.y - msg_size;
324                 if (autocvar_hud_panel_centerprint_flip)
325                 {
326                         pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
327                         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
328                                 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
329
330                         if (pos.y < panel_pos.y) // check if the next message can be shown
331                         {
332                                 drawfontscale = hud_scale;
333                                 return;
334                         }
335                 }
336                 else
337                 {
338                         pos.y += CENTERPRINT_SPACING * fontsize.y;
339                         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
340                                 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
341
342                         if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
343                         {
344                                 drawfontscale = hud_scale;
345                                 return;
346                         }
347                 }
348         }
349         drawfontscale = hud_scale;
350         if (all_messages_expired)
351         {
352                 centerprint_showing = false;
353                 reset_centerprint_messages();
354         }
355 }