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;
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
219 centerprint_message = is_bold ? strzone(substring(centerprint_messages[j], 5, -1)) : strzone(centerprint_messages[j]);
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);
228 if (j == CENTERPRINT_MAX_MSGS)
230 if (centerprint_expire_time[j] == -1)
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];
237 if (centerprint_expire_time[j] <= time)
239 if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
241 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
242 if (centerprint_countdown_num[j] == 0)
244 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
246 else if(centerprint_time[j] != -1)
250 all_messages_expired = false;
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);
260 if(centerprint_msgID[j] == CPID_TIMEIN)
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.
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);
272 // also fade it based on positioning
273 if(autocvar_hud_panel_centerprint_fade_subsequent)
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);
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;
286 if (centerprint_countdown_num[j])
287 n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_message), "\n");
289 n = tokenizebyseparator(centerprint_message, "\n");
291 if (autocvar_hud_panel_centerprint_flip)
293 // check if the message can be entirely shown
294 for(k = 0; k < n; ++k)
296 getWrappedLine_remaining = argv(k);
297 while(getWrappedLine_remaining)
299 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
303 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
306 current_msg_posY = pos.y; // save starting pos (first line) of the current message
310 for(k = 0; k < n; ++k)
312 getWrappedLine_remaining = argv(k);
313 while(getWrappedLine_remaining)
315 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
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();
327 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
331 ++g; // move next position number up
333 msg_size = pos.y - msg_size;
334 if (autocvar_hud_panel_centerprint_flip)
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));
340 if (pos.y < panel_pos.y) // check if the next message can be shown
342 drawfontscale = hud_scale;
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));
352 if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
354 drawfontscale = hud_scale;
360 strunzone(centerprint_message);
362 drawfontscale = hud_scale;
363 if (all_messages_expired)
365 centerprint_showing = false;
366 reset_centerprint_messages();