Xonotic
centerprint.qc
Go to the documentation of this file.
1 #include "centerprint.qh"
2 
3 #include <client/draw.qh>
6 
7 // CenterPrint (#16)
8 
10 {
11  // allow saving cvars that aesthetically change the panel into hud skin files
12  HUD_Write_Cvar("hud_panel_centerprint_align");
13  HUD_Write_Cvar("hud_panel_centerprint_flip");
14  HUD_Write_Cvar("hud_panel_centerprint_fontscale");
15  HUD_Write_Cvar("hud_panel_centerprint_fontscale_bold");
16  HUD_Write_Cvar("hud_panel_centerprint_time");
17  HUD_Write_Cvar("hud_panel_centerprint_fade_in");
18  HUD_Write_Cvar("hud_panel_centerprint_fade_out");
19  HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent");
20  HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone");
21  HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passone_minalpha");
22  HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo");
23  HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_passtwo_minalpha");
24  HUD_Write_Cvar("hud_panel_centerprint_fade_subsequent_minfontsize");
25  HUD_Write_Cvar("hud_panel_centerprint_fade_minfontsize");
26 }
27 
28 // These are the functions that draw the text at the center of the screen (e.g. frag messages and server MOTD).
29 // centerprint_Add parses a message and puts it in the circular buffer centerprint_messages
30 // centerprint_Add is usually called by Local_Notification_centerprint_Add, which is called
31 // by Local_Notification.
32 // HUD_CenterPrint draws all the messages on screen
33 
34 const int CENTERPRINT_MAX_MSGS = 10;
35 const int CENTERPRINT_MAX_ENTRIES = 50;
36 const float CENTERPRINT_SPACING = 0.7;
45 
46 void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
47 {
48  TC(int, new_id); TC(int, countdown_num);
49  //LOG_INFOF("centerprint_Add: ^2id: %d ^3dur: %d ^5countdown: %d\n'%s'", new_id, duration, countdown_num, strreplace("\n", "^7\\n^7", strMessage));
50  int i, j;
51 
52  if(strMessage == "" && new_id == 0)
53  return;
54 
55  // strip BOLD_OPERATOR
56  bool is_bold = (substring(strMessage, 0, 5) == BOLD_OPERATOR);
57  if (is_bold)
58  strMessage = substring(strMessage, 5, -1);
59 
60  // strip trailing newlines
61  j = strlen(strMessage) - 1;
62  while(substring(strMessage, j, 1) == "\n" && j >= 0)
63  --j;
64  if (j < strlen(strMessage) - 1)
65  strMessage = substring(strMessage, 0, j + 1);
66 
67  if(strMessage == "" && new_id == 0)
68  return;
69 
70  // strip leading newlines
71  j = 0;
72  while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
73  ++j;
74  if (j > 0)
75  strMessage = substring(strMessage, j, strlen(strMessage) - j);
76 
77  if(strMessage == "" && new_id == 0)
78  return;
79 
81  centerprint_showing = true;
82 
83  for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
84  {
85  if (j == CENTERPRINT_MAX_MSGS)
86  j = 0;
87  if (new_id && new_id == centerprint_msgID[j])
88  {
89  if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
90  {
91  // fade out the current msg (duration and countdown_num are ignored)
93  centerprint_expire_time[j] = -1; // don't use the variable time here!
94  return;
95  }
96  break; // found a msg with the same id, at position j
97  }
98  }
99 
100  if (i == CENTERPRINT_MAX_MSGS)
101  {
102  // a msg with the same id was not found, add the msg at the next position
103  --cpm_index;
104  if (cpm_index == -1)
105  cpm_index = CENTERPRINT_MAX_MSGS - 1;
106  j = cpm_index;
107  }
108  strcpy(centerprint_messages[j], strMessage);
109  centerprint_bold[j] = is_bold;
110  centerprint_msgID[j] = new_id;
111  if (duration < 0)
112  {
113  centerprint_time[j] = -1;
114  centerprint_expire_time[j] = -1; // don't use the variable time here!
115  }
116  else
117  {
118  if(duration == 0)
120  centerprint_time[j] = duration;
121  centerprint_expire_time[j] = -1; // don't use the variable time here!
122  }
123  centerprint_countdown_num[j] = countdown_num;
124 }
125 
126 void centerprint_Kill(int id)
127 {
128  TC(int, id);
129  centerprint_Add(id, "", 0, 0);
130 }
131 
132 void centerprint_AddStandard(string strMessage)
133 {
135 }
136 
138 {
139  for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
140  {
142  centerprint_time[i] = 1;
143  centerprint_msgID[i] = 0;
144  centerprint_bold[i] = false;
146  }
147 }
148 
151 {
153  {
154  if(!autocvar_hud_panel_centerprint) return;
155 
158  }
159  else
160  {
161  if(!hud_configure_prev)
162  {
164  hud_configure_cp_generation_time = time; // show a message immediately
165  }
167  {
168  if(highlightedPanel == HUD_PANEL(CENTERPRINT))
169  {
170  float r;
171  r = random();
172  if (r > 0.8)
173  centerprint_Add(floor(r*1000), sprintf(_("^3Countdown message at time %s, seconds left: ^COUNT"), seconds_tostring(time)), 1, 10);
174  else if (r > 0.55)
175  centerprint_Add(0, sprintf(_("^1Multiline message at time %s that\n^1lasts longer than normal"), seconds_tostring(time)), 20, 0);
176  else
177  centerprint_AddStandard(sprintf(_("Message at time %s"), seconds_tostring(time)));
179  }
180  else
181  {
182  centerprint_Add(0, _("Generic message"), 10, 0);
184  }
185  }
186  }
187 
189 
190  if ( HUD_Radar_Clickable() )
191  {
193  return;
194 
195  panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
198  }
200  {
201  vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
202  if(target_pos.y > panel_pos.y)
203  {
206  }
207 
208  // move the panel below the scoreboard
209  if (panel_pos.y >= vid_conheight)
210  return;
211  }
212 
215  else
218 
219  if (!centerprint_showing)
220  return;
221 
222  if(panel_bg_padding)
223  {
224  panel_pos += '1 1 0' * panel_bg_padding;
225  panel_size -= '2 2 0' * panel_bg_padding;
226  }
227 
228  int entries;
229  float height;
230  vector fontsize;
231 
232  int i, j, k, n, g;
233  float a, sz, align, current_msg_posY = 0, msg_size;
234  vector pos;
235  string ts;
236  bool all_messages_expired = true;
237 
238  pos = panel_pos;
240  pos.y += panel_size.y;
242  for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
243  {
244  bool is_bold = centerprint_bold[j];
245 
246  // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
247  // height = panel_size_y/entries;
248  // fontsize = '1 1 0' * height;
250  fontsize = '1 1 0' * height;
251  entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
252 
253  if (j == CENTERPRINT_MAX_MSGS)
254  j = 0;
255  if (centerprint_expire_time[j] == -1)
256  {
257  // here we are sure the time variable is not altered by CSQC_Ent_Update
259  if (centerprint_time[j] > 0)
261  }
262  if (centerprint_expire_time[j] <= time)
263  {
265  {
267  if (centerprint_countdown_num[j] == 0)
268  continue;
270  }
271  else if(centerprint_time[j] != -1)
272  continue;
273  }
274 
275  all_messages_expired = false;
276 
277  float fade_in_time = autocvar_hud_panel_centerprint_fade_in;
278  if (centerprint_time[j] <= 2)
280 
281  // fade
282  if(centerprint_time[j] < 0) // Expired but forced. Expire time is the fade-in time.
283  a = (time - centerprint_expire_time[j]) / max(0.0001, fade_in_time);
284  else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time) // Regularily printed. Not fading out yet.
285  a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, fade_in_time);
286  else // Expiring soon, so fade it out.
288 
289  if(centerprint_msgID[j] == ORDINAL(CPID_TIMEIN))
290  a = 1;
291 
292  // while counting down show it anyway in order to hold the current message position
293  if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0) // Guaranteed invisible - don't show.
294  continue;
295  if (a > 1)
296  a = 1;
297 
298  // set the size from fading in/out before subsequent fading
300 
301  // also fade it based on positioning
303  {
304  // pass one: all messages after the first have half alpha
306  // pass two: after that, gradually lower alpha even more for each message
308  }
309  a *= panel_fg_alpha;
310 
311  // finally set the size based on the new alpha from subsequent fading
313  drawfontscale = hud_scale * sz;
314 
316  n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
317  else
319 
321  {
322  // check if the message can be entirely shown
323  for(k = 0; k < n; ++k)
324  {
327  {
328  ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
329  if (ts != "")
330  pos.y -= fontsize.y;
331  else
332  pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
333  }
334  }
335  current_msg_posY = pos.y; // save starting pos (first line) of the current message
336  }
337 
338  msg_size = pos.y;
339  for(k = 0; k < n; ++k)
340  {
343  {
344  ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
345  if (ts != "")
346  {
347  if (align)
348  pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
349  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.
350  if (is_bold) draw_beginBoldFont();
351  drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
352  if (is_bold) draw_endBoldFont();
353  pos.y += fontsize.y;
354  }
355  else
356  pos.y += fontsize.y * CENTERPRINT_SPACING/2;
357  }
358  }
359 
360  ++g; // move next position number up
361 
362  msg_size = pos.y - msg_size;
364  {
365  pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
366  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
367  pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
368 
369  if (pos.y < panel_pos.y) // check if the next message can be shown
370  {
372  return;
373  }
374  }
375  else
376  {
377  pos.y += CENTERPRINT_SPACING * fontsize.y;
378  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
379  pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
380 
381  if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
382  {
384  return;
385  }
386  }
387  }
388 
390  if (all_messages_expired)
391  {
392  centerprint_showing = false;
394  }
395 }
float autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha
Definition: centerprint.qh:13
float vid_conheight
#define draw_endBoldFont()
Definition: draw.qh:5
float panel_fg_alpha
Definition: hud.qh:166
int cpm_index
Definition: centerprint.qc:37
vector hud_scale
Definition: hud.qh:216
entity highlightedPanel
Definition: hud.qh:106
const vector eY
Definition: vector.qh:45
string getWrappedLine_remaining
Definition: util.qh:108
void centerprint_Kill(int id)
Definition: centerprint.qc:126
#define BOLD_OPERATOR
Definition: all.qh:12
#define ORDINAL(it)
Definition: enumclass.qh:25
bool autocvar__hud_configure
Definition: hud_config.qh:3
#define stringwidth
Definition: csprogsdefs.qh:29
#define draw_beginBoldFont()
Definition: draw.qh:4
vector panel_size
Definition: hud.qh:160
float autocvar_hud_panel_centerprint_fade_subsequent
Definition: centerprint.qh:9
void HUD_Scale_Disable()
Definition: hud.qc:83
vector drawfontscale
Definition: draw.qh:3
const int CENTERPRINT_MAX_ENTRIES
Definition: centerprint.qc:35
float vid_conwidth
void HUD_CenterPrint_Export(int fh)
Definition: centerprint.qc:9
float autocvar_hud_panel_centerprint_fade_subsequent_minfontsize
Definition: centerprint.qh:14
#define strcpy(this, s)
Definition: string.qh:49
bool HUD_Radar_Clickable()
Definition: radar.qc:25
float autocvar_hud_panel_centerprint_fontscale_bold
Definition: centerprint.qh:18
#define HUD_Panel_DrawBg()
Definition: hud.qh:54
float hud_panel_radar_bottom
Definition: hud.qh:71
bool centerprint_showing
Definition: centerprint.qc:44
const int CENTERPRINT_MAX_MSGS
Definition: centerprint.qc:34
void HUD_Scale_Enable()
Definition: hud.qc:90
float autocvar_hud_panel_centerprint_fade_minfontsize
Definition: centerprint.qh:15
void HUD_CenterPrint()
Definition: centerprint.qc:150
float height
Definition: jumppads.qh:12
int centerprint_countdown_num[CENTERPRINT_MAX_MSGS]
Definition: centerprint.qc:43
const float DRAWFLAG_NORMAL
Definition: csprogsdefs.qc:317
float autocvar_hud_panel_centerprint_fontscale
Definition: centerprint.qh:17
float autocvar_hud_panel_centerprint_fade_in_short
Definition: centerprint.qh:7
#define TC(T, sym)
Definition: _all.inc:82
vector panel_pos
Definition: hud.qh:159
float autocvar_hud_panel_centerprint_time
Definition: centerprint.qh:20
float autocvar_hud_panel_centerprint_fade_out
Definition: centerprint.qh:8
const float CENTERPRINT_SPACING
Definition: centerprint.qc:36
bool autocvar_hud_panel_centerprint
Definition: centerprint.qh:4
vector(float skel, float bonenum) _skel_get_boneabs_hidden
#define count_seconds(time)
Definition: counting.qh:56
float autocvar_hud_panel_centerprint_fade_subsequent_passtwo
Definition: centerprint.qh:12
float autocvar_hud_panel_centerprint_fade_subsequent_passone
Definition: centerprint.qh:10
void centerprint_Add(int new_id, string strMessage, float duration, int countdown_num)
Definition: centerprint.qc:46
#define tokenizebyseparator
Definition: dpextensions.qh:21
float centerprint_time[CENTERPRINT_MAX_MSGS]
Definition: centerprint.qc:41
#define vec2(...)
Definition: vector.qh:90
float hud_configure_prev
Definition: hud_config.qh:17
#define strfree(this)
Definition: string.qh:56
int centerprint_msgID[CENTERPRINT_MAX_MSGS]
Definition: centerprint.qc:39
float autocvar_hud_panel_centerprint_align
Definition: centerprint.qh:5
void centerprint_AddStandard(string strMessage)
Definition: centerprint.qc:132
float scoreboard_bottom
Definition: scoreboard.qh:15
bool autocvar_hud_panel_centerprint_dynamichud
Definition: centerprint.qh:19
bool autocvar_hud_panel_centerprint_flip
Definition: centerprint.qh:16
bool centerprint_bold[CENTERPRINT_MAX_MSGS]
Definition: centerprint.qc:40
string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition: util.qc:880
float time
Definition: csprogsdefs.qc:16
void centerprint_KillAll()
Definition: centerprint.qc:137
#define HUD_Write_Cvar(cvar)
Definition: hud_config.qh:38
float hud_configure_cp_generation_time
Definition: centerprint.qc:149
float autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha
Definition: centerprint.qh:11
float centerprint_expire_time[CENTERPRINT_MAX_MSGS]
Definition: centerprint.qc:42
#define HUD_PANEL(NAME)
Definition: hud.qh:51
float scoreboard_fade_alpha
Definition: scoreboard.qh:10
float autocvar_hud_panel_centerprint_fade_in
Definition: centerprint.qh:6
void HUD_Panel_LoadCvars()
Definition: hud.qc:216
float panel_bg_padding
Definition: hud.qh:171
ERASEABLE string seconds_tostring(float seconds)
Definition: string.qh:123
string centerprint_messages[CENTERPRINT_MAX_MSGS]
Definition: centerprint.qc:38