1 #include "centerprint.qh"
3 #include "scoreboard.qh"
4 #include <common/notifications/all.qh>
5 #include <client/defs.qh>
6 #include <client/miscfunctions.qh>
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().
13 const int CENTERPRINT_MAX_MSGS = 10;
14 const int CENTERPRINT_MAX_ENTRIES = 50;
15 const float CENTERPRINT_SPACING = 0.7;
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;
24 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
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);
30 if(strMessage == "" && new_id == 0)
33 // strip trailing newlines
34 j = strlen(strMessage) - 1;
35 while(substring(strMessage, j, 1) == "\n" && j >= 0)
37 if (j < strlen(strMessage) - 1)
38 strMessage = substring(strMessage, 0, j + 1);
40 if(strMessage == "" && new_id == 0)
43 // strip leading newlines
45 while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
48 strMessage = substring(strMessage, j, strlen(strMessage) - j);
50 if(strMessage == "" && new_id == 0)
53 if (!centerprint_showing)
54 centerprint_showing = true;
56 for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
58 if (j == CENTERPRINT_MAX_MSGS)
60 if (new_id && new_id == centerprint_msgID[j])
62 if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
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!
69 break; // found a msg with the same id, at position j
73 if (i == CENTERPRINT_MAX_MSGS)
75 // a msg with the same id was not found, add the msg at the next position
78 cpm_index = CENTERPRINT_MAX_MSGS - 1;
81 strcpy(centerprint_messages[j], strMessage);
82 centerprint_msgID[j] = new_id;
85 centerprint_time[j] = -1;
86 centerprint_expire_time[j] = -1; // don't use the variable time here!
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!
95 centerprint_countdown_num[j] = countdown_num;
98 void centerprint_kill(int id)
101 centerprint_generic(id, "", 0, 0);
104 void centerprint_hud(string strMessage)
106 centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
109 void reset_centerprint_messages()
111 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
113 centerprint_expire_time[i] = 0;
114 centerprint_time[i] = 1;
115 centerprint_msgID[i] = 0;
116 strfree(centerprint_messages[i]);
120 float hud_configure_cp_generation_time;
121 void HUD_CenterPrint ()
123 if(!autocvar__hud_configure)
125 if(!autocvar_hud_panel_centerprint) return;
127 if(hud_configure_prev)
128 reset_centerprint_messages();
132 if(!hud_configure_prev)
134 reset_centerprint_messages();
135 hud_configure_cp_generation_time = time; // show a message immediately
137 if (time > hud_configure_cp_generation_time)
139 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
144 centerprint_generic(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
146 centerprint_generic(0, sprintf(_("^1Multiline message at time %s that\n^1lasts longer than normal"), seconds_tostring(time)), 20, 0);
148 centerprint_hud(sprintf(_("Message at time %s"), seconds_tostring(time)));
149 hud_configure_cp_generation_time = time + 1 + random()*4;
153 centerprint_generic(0, _("Generic message"), 10, 0);
154 hud_configure_cp_generation_time = time + 10 - random()*3;
159 HUD_Panel_LoadCvars();
161 if ( HUD_Radar_Clickable() )
163 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
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);
170 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
172 // move the panel below the scoreboard
173 if (scoreboard_bottom >= 0.96 * vid_conheight)
175 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
176 if(target_pos.y > panel_pos.y)
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);
183 if (autocvar_hud_panel_centerprint_dynamichud)
189 if (!centerprint_showing)
194 panel_pos += '1 1 0' * panel_bg_padding;
195 panel_size -= '2 2 0' * panel_bg_padding;
199 string centerprint_message = strzone("");
205 float a, sz, align, current_msg_posY = 0, msg_size;
208 bool all_messages_expired = true;
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)
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
220 strcpy(centerprint_message, substring(centerprint_messages[j], 5, -1));
222 strcpy(centerprint_message, centerprint_messages[j]);
224 // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
225 // height = panel_size_y/entries;
226 // fontsize = '1 1 0' * height;
227 height = (is_bold) ? vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale_bold : vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
228 fontsize = '1 1 0' * height;
229 entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
231 if (j == CENTERPRINT_MAX_MSGS)
233 if (centerprint_expire_time[j] == -1)
235 // here we are sure the time variable is not altered by CSQC_Ent_Update
236 centerprint_expire_time[j] = time;
237 if (centerprint_time[j] > 0)
238 centerprint_expire_time[j] += centerprint_time[j];
240 if (centerprint_expire_time[j] <= time)
242 if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
244 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
245 if (centerprint_countdown_num[j] == 0)
247 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
249 else if(centerprint_time[j] != -1)
253 all_messages_expired = false;
255 // fade the centerprint_hud in/out
256 if(centerprint_time[j] < 0) // Expired but forced. Expire time is the fade-in time.
257 a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
258 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time) // Regularily printed. Not fading out yet.
259 a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
260 else // Expiring soon, so fade it out.
261 a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
263 if(centerprint_msgID[j] == CPID_TIMEIN)
266 // while counting down show it anyway in order to hold the current message position
267 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
272 // set the size from fading in/out before subsequent fading
273 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
275 // also fade it based on positioning
276 if(autocvar_hud_panel_centerprint_fade_subsequent)
278 // pass one: all messages after the first have half alpha
279 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
280 // pass two: after that, gradually lower alpha even more for each message
281 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
285 // finally set the size based on the new alpha from subsequent fading
286 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
287 drawfontscale = hud_scale * sz;
289 if (centerprint_countdown_num[j])
290 n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_message), "\n");
292 n = tokenizebyseparator(centerprint_message, "\n");
294 if (autocvar_hud_panel_centerprint_flip)
296 // check if the message can be entirely shown
297 for(k = 0; k < n; ++k)
299 getWrappedLine_remaining = argv(k);
300 while(getWrappedLine_remaining)
302 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
306 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
309 current_msg_posY = pos.y; // save starting pos (first line) of the current message
313 for(k = 0; k < n; ++k)
315 getWrappedLine_remaining = argv(k);
316 while(getWrappedLine_remaining)
318 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
322 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
323 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.
324 if (is_bold) draw_beginBoldFont();
325 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
326 if (is_bold) draw_endBoldFont();
330 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
334 ++g; // move next position number up
336 msg_size = pos.y - msg_size;
337 if (autocvar_hud_panel_centerprint_flip)
339 pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
340 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
341 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
343 if (pos.y < panel_pos.y) // check if the next message can be shown
345 drawfontscale = hud_scale;
346 strfree(centerprint_message);
352 pos.y += CENTERPRINT_SPACING * fontsize.y;
353 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
354 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
356 if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
358 drawfontscale = hud_scale;
359 strfree(centerprint_message);
364 strfree(centerprint_message);
366 drawfontscale = hud_scale;
367 if (all_messages_expired)
369 centerprint_showing = false;
370 reset_centerprint_messages();