From: terencehill Date: Sun, 7 Apr 2024 14:50:06 +0000 (+0200) Subject: drawstring_aspect: fix wrong horizontal string alignment sometimes due to r_font_size... X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=d29d074f0b3e212ad3e0728f7f973b179668cde4;p=xonotic%2Fxonotic-data.pk3dir.git drawstring_aspect: fix wrong horizontal string alignment sometimes due to r_font_size_snapping 4; document macro helper. Also don't display timer subtext bold to mitigate drawstring_aspect bug While at it I wrapped a couple very long lines in timer panel code --- diff --git a/qcsrc/client/draw.qc b/qcsrc/client/draw.qc index f08fbf5d9..5540ae4fb 100644 --- a/qcsrc/client/draw.qc +++ b/qcsrc/client/draw.qc @@ -81,12 +81,15 @@ float stringwidth(string text, float handleColors, vector sz) return r; } -#define SET_POS_AND_SZ_Y_ASPECT(allow_colors) MACRO_BEGIN \ +// it scales text up to box width +// NOTE it doesn't work perfectly because r_font_size_snapping 4 (default value) +// may render text with a slightly different size making text bigger or smaller +// NOTE this is implemented as a macro to reduce number of function calls per frame +#define DRAWSTRING_ASPECT_SCALE(pos, text, sz, allow_colors) MACRO_BEGIN \ float textaspect, oldsz; \ vector dfs = drawfontscale; \ drawfontscale = '1 1 0'; \ textaspect = stringwidth(text, allow_colors, '1 1 1' * sz.y) / sz.y; \ - drawfontscale = dfs; \ if(sz.x/sz.y > textaspect) { \ oldsz = sz.x; \ sz.x = sz.y * textaspect; \ @@ -95,18 +98,23 @@ float stringwidth(string text, float handleColors, vector sz) oldsz = sz.y; \ sz.y = sz.x / textaspect; \ pos.y += (oldsz - sz.y) * 0.5; \ + /* in case text is rendered with a different size, at least recenter it horizontally */ \ + /* unfortunately there is no way to correctly recenter it vertically */ \ + float new_textwidth = stringwidth(text, allow_colors, '1 1 1' * sz.y); \ + pos.x += (sz.x - new_textwidth) * 0.5; \ } \ + drawfontscale = dfs; \ MACRO_END // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) { - SET_POS_AND_SZ_Y_ASPECT(false); + DRAWSTRING_ASPECT_SCALE(pos, text, sz, false); drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag); } // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) { - SET_POS_AND_SZ_Y_ASPECT(true); + DRAWSTRING_ASPECT_SCALE(pos, text, sz, true); drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag); } @@ -129,7 +137,7 @@ void drawstring_expanding(vector position, string text, vector theScale, vector // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) { - SET_POS_AND_SZ_Y_ASPECT(false); + DRAWSTRING_ASPECT_SCALE(pos, text, sz, false); drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp); } @@ -145,7 +153,7 @@ void drawcolorcodedstring_expanding(vector position, string text, vector theScal } void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) { - SET_POS_AND_SZ_Y_ASPECT(true); + DRAWSTRING_ASPECT_SCALE(pos, text, sz, true); drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp); } diff --git a/qcsrc/client/hud/panel/timer.qc b/qcsrc/client/hud/panel/timer.qc index 269d12b5e..9b10f63e3 100644 --- a/qcsrc/client/hud/panel/timer.qc +++ b/qcsrc/client/hud/panel/timer.qc @@ -153,13 +153,17 @@ void HUD_Timer() if(subtimer_str) { float subtimer_padding = subtimer_size.y / 5; timer_size.x -= subtimer_size.x; - drawstring_aspect(pos + eX * timer_size.x + eY * subtimer_padding, (swap ? timer_str : subtimer_str), subtimer_size - eY * subtimer_padding * 2, (swap ? timer_color : subtimer_color), panel_fg_alpha, DRAWFLAG_NORMAL); + drawstring_aspect(pos + eX * timer_size.x + eY * subtimer_padding, + (swap ? timer_str : subtimer_str), subtimer_size - eY * 2 * subtimer_padding, + (swap ? timer_color : subtimer_color), panel_fg_alpha, DRAWFLAG_NORMAL); } - drawstring_aspect(pos, (swap ? subtimer_str : timer_str), timer_size, (swap ? subtimer_color : timer_color), panel_fg_alpha, DRAWFLAG_NORMAL); + drawstring_aspect(pos, + (swap ? subtimer_str : timer_str), timer_size, + (swap ? subtimer_color : timer_color), panel_fg_alpha, DRAWFLAG_NORMAL); + + draw_endBoldFont(); if(subtext) drawstring_aspect(pos + eY * timer_size.y, subtext, subtext_size, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL); - - draw_endBoldFont(); }