1 #include "centerprint.qh"
3 #include "scoreboard.qh"
7 const int CENTERPRINT_MAX_MSGS = 10;
8 const int CENTERPRINT_MAX_ENTRIES = 50;
9 const float CENTERPRINT_SPACING = 0.7;
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;
18 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
20 TC(int, new_id); TC(int, countdown_num);
21 //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
24 if(strMessage == "" && new_id == 0)
27 // strip trailing newlines
28 j = strlen(strMessage) - 1;
29 while(substring(strMessage, j, 1) == "\n" && j >= 0)
31 if (j < strlen(strMessage) - 1)
32 strMessage = substring(strMessage, 0, j + 1);
34 if(strMessage == "" && new_id == 0)
37 // strip leading newlines
39 while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
42 strMessage = substring(strMessage, j, strlen(strMessage) - j);
44 if(strMessage == "" && new_id == 0)
47 if (!centerprint_showing)
48 centerprint_showing = true;
50 for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
52 if (j == CENTERPRINT_MAX_MSGS)
54 if (new_id && new_id == centerprint_msgID[j])
56 if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
58 // fade out the current msg (duration and countdown_num are ignored)
59 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
60 centerprint_expire_time[j] = -1; // don't use the variable time here!
63 break; // found a msg with the same id, at position j
67 if (i == CENTERPRINT_MAX_MSGS)
69 // a msg with the same id was not found, add the msg at the next position
72 cpm_index = CENTERPRINT_MAX_MSGS - 1;
75 if(centerprint_messages[j])
76 strunzone(centerprint_messages[j]);
77 centerprint_messages[j] = strzone(strMessage);
78 centerprint_msgID[j] = new_id;
81 centerprint_time[j] = -1;
82 centerprint_expire_time[j] = -1; // don't use the variable time here!
87 duration = max(1, autocvar_hud_panel_centerprint_time);
88 centerprint_time[j] = duration;
89 centerprint_expire_time[j] = -1; // don't use the variable time here!
91 centerprint_countdown_num[j] = countdown_num;
94 void centerprint_kill(int id)
97 centerprint_generic(id, "", 0, 0);
100 void centerprint_hud(string strMessage)
102 centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
105 void reset_centerprint_messages()
107 for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
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;
117 float hud_configure_cp_generation_time;
118 void HUD_CenterPrint ()
120 if(!autocvar__hud_configure)
122 if(!autocvar_hud_panel_centerprint) return;
124 if(hud_configure_prev)
125 reset_centerprint_messages();
129 if(!hud_configure_prev)
131 reset_centerprint_messages();
132 hud_configure_cp_generation_time = time; // show a message immediately
134 if (time > hud_configure_cp_generation_time)
136 if(highlightedPanel == HUD_PANEL(CENTERPRINT))
141 centerprint_generic(floor(r*1000), strcat(sprintf("^3Countdown message at time %s", seconds_tostring(time)), ", seconds left: ^COUNT"), 1, 10);
143 centerprint_generic(0, sprintf("^1Multiline message at time %s that\n^1lasts longer than normal", seconds_tostring(time)), 20, 0);
145 centerprint_hud(sprintf("Message at time %s", seconds_tostring(time)));
146 hud_configure_cp_generation_time = time + 1 + random()*4;
150 centerprint_generic(0, sprintf("Centerprint message", seconds_tostring(time)), 10, 0);
151 hud_configure_cp_generation_time = time + 10 - random()*3;
156 // this panel fades only when the menu does
157 HUD_Panel_LoadCvars(1); // keep with the scoreboard
159 if ( HUD_Radar_Clickable() )
161 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
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);
167 else if(!autocvar__hud_configure && scoreboard_fade_alpha)
169 // move the panel below the scoreboard
170 if (scoreboard_bottom >= 0.96 * vid_conheight)
173 target_pos = eY * scoreboard_bottom + eX * 0.5 * (vid_conwidth - panel_size.x);
174 if(target_pos.y > panel_pos.y)
176 panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
177 panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
181 if (autocvar_hud_panel_centerprint_dynamichud)
187 if (!centerprint_showing)
192 panel_pos += '1 1 0' * panel_bg_padding;
193 panel_size -= '2 2 0' * panel_bg_padding;
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);
207 float a, sz, align, current_msg_posY = 0, msg_size;
210 bool all_messages_expired = true;
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)
218 if (j == CENTERPRINT_MAX_MSGS)
220 if (centerprint_expire_time[j] == -1)
222 // here we are sure the time variable is not altered by CSQC_Ent_Update
223 centerprint_expire_time[j] = time;
224 if (centerprint_time[j] > 0)
225 centerprint_expire_time[j] += centerprint_time[j];
227 if (centerprint_expire_time[j] <= time)
229 if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
231 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
232 if (centerprint_countdown_num[j] == 0)
234 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
236 else if(centerprint_time[j] != -1)
240 all_messages_expired = false;
242 // fade the centerprint_hud in/out
243 if(centerprint_time[j] < 0) // Expired but forced. Expire time is the fade-in time.
244 a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
245 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time) // Regularily printed. Not fading out yet.
246 a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
247 else // Expiring soon, so fade it out.
248 a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
250 // while counting down show it anyway in order to hold the current message position
251 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
256 // set the size from fading in/out before subsequent fading
257 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
259 // also fade it based on positioning
260 if(autocvar_hud_panel_centerprint_fade_subsequent)
262 // pass one: all messages after the first have half alpha
263 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
264 // pass two: after that, gradually lower alpha even more for each message
265 a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
269 // finally set the size based on the new alpha from subsequent fading
270 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
271 drawfontscale = hud_scale * sz;
273 if (centerprint_countdown_num[j])
274 n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
276 n = tokenizebyseparator(centerprint_messages[j], "\n");
278 if (autocvar_hud_panel_centerprint_flip)
280 // check if the message can be entirely shown
281 for(k = 0; k < n; ++k)
283 getWrappedLine_remaining = argv(k);
284 while(getWrappedLine_remaining)
286 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
290 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
293 current_msg_posY = pos.y; // save starting pos (first line) of the current message
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.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
307 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.
308 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
312 pos.y += fontsize.y * CENTERPRINT_SPACING/2;
316 ++g; // move next position number up
318 msg_size = pos.y - msg_size;
319 if (autocvar_hud_panel_centerprint_flip)
321 pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
322 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
323 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
325 if (pos.y < panel_pos.y) // check if the next message can be shown
327 drawfontscale = hud_scale;
333 pos.y += CENTERPRINT_SPACING * fontsize.y;
334 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
335 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
337 if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
339 drawfontscale = hud_scale;
344 drawfontscale = hud_scale;
345 if (all_messages_expired)
347 centerprint_showing = false;
348 reset_centerprint_messages();