]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
drawstring_aspect: fix wrong horizontal string alignment sometimes due to r_font_size...
authorterencehill <piuntn@gmail.com>
Sun, 7 Apr 2024 14:50:06 +0000 (16:50 +0200)
committerterencehill <piuntn@gmail.com>
Sun, 7 Apr 2024 17:19:35 +0000 (19:19 +0200)
While at it I wrapped a couple very long lines in timer panel code

qcsrc/client/draw.qc
qcsrc/client/hud/panel/timer.qc

index f08fbf5d9a51ca3625d926c00fb0ad2c6ad5473b..5540ae4fb121da440ed7867810fa21390e5ed745 100644 (file)
@@ -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);
 }
 
index 269d12b5edc800f9f2d50df6e7e70faad317bbb2..9b10f63e308c19d79dd246d0b7f5db88b8008a80 100644 (file)
@@ -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();
 }