Fixed bug with measure text function.

This commit is contained in:
Kapendev 2024-12-18 17:20:57 +02:00
parent e6263492b8
commit 22c8cf700a
2 changed files with 33 additions and 24 deletions

View file

@ -1880,9 +1880,9 @@ Vec2 measureTextSize(Font font, IStr text, DrawOptions options = DrawOptions())
auto glyphIndex = rl.GetGlyphIndex(font.data, codepoint); auto glyphIndex = rl.GetGlyphIndex(font.data, codepoint);
if (codepoint != '\n') { if (codepoint != '\n') {
if (font.data.glyphs[glyphIndex].advanceX) { if (font.data.glyphs[glyphIndex].advanceX) {
textWidth += font.data.glyphs[glyphIndex].advanceX; textWidth += font.data.glyphs[glyphIndex].advanceX + font.runeSpacing;
} else { } else {
textWidth += cast(int) (font.data.recs[glyphIndex].width + font.data.glyphs[glyphIndex].offsetX); textWidth += cast(int) (font.data.recs[glyphIndex].width + font.data.glyphs[glyphIndex].offsetX + font.runeSpacing);
} }
} else { } else {
if (maxTextWidth < textWidth) maxTextWidth = textWidth; if (maxTextWidth < textWidth) maxTextWidth = textWidth;
@ -1895,10 +1895,7 @@ Vec2 measureTextSize(Font font, IStr text, DrawOptions options = DrawOptions())
} }
if (maxTextWidth < textWidth) maxTextWidth = textWidth; if (maxTextWidth < textWidth) maxTextWidth = textWidth;
if (maxTextWidth < options.alignmentWidth) maxTextWidth = options.alignmentWidth; if (maxTextWidth < options.alignmentWidth) maxTextWidth = options.alignmentWidth;
return Vec2( return Vec2(maxTextWidth * options.scale.x, textHeight * options.scale.y).floor();
floor(maxTextWidth * options.scale.x + ((maxLineCodepointCount - 1) * font.runeSpacing * options.scale.x)),
floor(textHeight * options.scale.y),
);
} }
/// Measures the size of the specified text when rendered with the given font and draw options. /// Measures the size of the specified text when rendered with the given font and draw options.
@ -2330,7 +2327,7 @@ void drawText(Font font, IStr text, Vec2 position, DrawOptions options = DrawOpt
if (font.data.glyphs[glyphIndex].advanceX) { if (font.data.glyphs[glyphIndex].advanceX) {
textOffsetX += font.data.glyphs[glyphIndex].advanceX + font.runeSpacing; textOffsetX += font.data.glyphs[glyphIndex].advanceX + font.runeSpacing;
} else { } else {
textOffsetX += cast(int) (font.data.recs[glyphIndex].width) + font.runeSpacing; textOffsetX += cast(int) (font.data.recs[glyphIndex].width + font.runeSpacing);
} }
lineCodepointIndex += codepointByteCount; lineCodepointIndex += codepointByteCount;
codepointIndex += codepointByteCount; codepointIndex += codepointByteCount;

View file

@ -7,7 +7,7 @@
// --- // ---
// TODO: Think about overlapping UI items. // TODO: Think about overlapping UI items.
// TODO: Look at the text alignment code again. Maybe add a clamp so text gets all the button always and stuff. // TODO: Fix the ui text! Also maybe we should not base things on the text size?
// TODO: Add way to get item point for some stuff. This is nice when making lists. // TODO: Add way to get item point for some stuff. This is nice when making lists.
/// The `ui` module functions as a immediate mode UI library. /// The `ui` module functions as a immediate mode UI library.
@ -47,7 +47,7 @@ struct UiButtonOptions {
Vec2 dragLimitX = Vec2(-100000.0f, 100000.0f); Vec2 dragLimitX = Vec2(-100000.0f, 100000.0f);
Vec2 dragLimitY = Vec2(-100000.0f, 100000.0f); Vec2 dragLimitY = Vec2(-100000.0f, 100000.0f);
Alignment textAlignment = Alignment.center; Alignment textAlignment = Alignment.center;
short textAlignmentMargin = 4; short textAlignmentMargin = 0;
@safe @nogc nothrow: @safe @nogc nothrow:
@ -306,6 +306,33 @@ void updateUiState(Vec2 itemPoint, Vec2 itemSize, bool isHot, bool isActive, boo
} }
} }
void updateUiText(Vec2 size, IStr text, UiButtonOptions options = UiButtonOptions()) {
if (options.font.isEmpty) options.font = engineFont;
auto point = uiState.layoutStartPoint + uiState.layoutStartPointOffest;
auto maxSize = measureTextSize(options.font, text);
if (maxSize.x < size.x) maxSize.x = size.x;
if (maxSize.y < size.y) maxSize.y = size.y;
updateUiState(point, maxSize, false, false, false);
}
void drawUiText(Vec2 size, IStr text, Vec2 point, UiButtonOptions options = UiButtonOptions()) {
if (options.font.isEmpty) options.font = engineFont;
auto area = Rect(point, size);
auto textPoint = area.centerPoint;
if (options.textAlignment == Alignment.left) textPoint.x += options.textAlignmentMargin;
else if (options.textAlignment == Alignment.right) textPoint.x -= options.textAlignmentMargin;
auto textOptions = DrawOptions(options.textAlignment, cast(int) (size.x));
textOptions.hook = Hook.center;
drawText(options.font, text, textPoint, textOptions);
}
void uiText(Vec2 size, IStr text, UiButtonOptions options = UiButtonOptions()) {
updateUiText(size, text, options);
drawUiText(uiState.itemSize, text, uiState.itemPoint, options);
}
bool updateUiButton(Vec2 size, IStr text, UiButtonOptions options = UiButtonOptions()) { bool updateUiButton(Vec2 size, IStr text, UiButtonOptions options = UiButtonOptions()) {
if (options.font.isEmpty) options.font = engineFont; if (options.font.isEmpty) options.font = engineFont;
auto m = uiMouse; auto m = uiMouse;
@ -432,18 +459,3 @@ bool uiDragHandle(Vec2 size, ref Vec2 point, UiButtonOptions options = UiButtonO
return false; return false;
} }
} }
void uiText(Vec2 size, IStr text, UiButtonOptions options = UiButtonOptions()) {
if (options.font.isEmpty) options.font = engineFont;
auto point = uiState.layoutStartPoint + uiState.layoutStartPointOffest;
auto area = Rect(point, size);
auto textPoint = area.centerPoint;
if (options.textAlignment == Alignment.left) textPoint.x += options.textAlignmentMargin;
else if (options.textAlignment == Alignment.right) textPoint.x -= options.textAlignmentMargin;
auto textOptions = DrawOptions(options.textAlignment, cast(int) (size.x));
textOptions.hook = Hook.center;
drawText(options.font, text, textPoint, textOptions);
updateUiState(point, size, false, false, false);
}