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; \
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);
}
// 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);
}
}
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);
}
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();
}