diff --git a/source/parin/engine.d b/source/parin/engine.d index 323404f..4207129 100644 --- a/source/parin/engine.d +++ b/source/parin/engine.d @@ -1880,9 +1880,9 @@ Vec2 measureTextSize(Font font, IStr text, DrawOptions options = DrawOptions()) auto glyphIndex = rl.GetGlyphIndex(font.data, codepoint); if (codepoint != '\n') { if (font.data.glyphs[glyphIndex].advanceX) { - textWidth += font.data.glyphs[glyphIndex].advanceX; + textWidth += font.data.glyphs[glyphIndex].advanceX + font.runeSpacing; } 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 { 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 < options.alignmentWidth) maxTextWidth = options.alignmentWidth; - return Vec2( - floor(maxTextWidth * options.scale.x + ((maxLineCodepointCount - 1) * font.runeSpacing * options.scale.x)), - floor(textHeight * options.scale.y), - ); + return Vec2(maxTextWidth * options.scale.x, textHeight * options.scale.y).floor(); } /// 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) { textOffsetX += font.data.glyphs[glyphIndex].advanceX + font.runeSpacing; } else { - textOffsetX += cast(int) (font.data.recs[glyphIndex].width) + font.runeSpacing; + textOffsetX += cast(int) (font.data.recs[glyphIndex].width + font.runeSpacing); } lineCodepointIndex += codepointByteCount; codepointIndex += codepointByteCount; diff --git a/source/parin/ui.d b/source/parin/ui.d index 028f928..74d9e87 100644 --- a/source/parin/ui.d +++ b/source/parin/ui.d @@ -7,7 +7,7 @@ // --- // 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. /// The `ui` module functions as a immediate mode UI library. @@ -47,7 +47,7 @@ struct UiButtonOptions { Vec2 dragLimitX = Vec2(-100000.0f, 100000.0f); Vec2 dragLimitY = Vec2(-100000.0f, 100000.0f); Alignment textAlignment = Alignment.center; - short textAlignmentMargin = 4; + short textAlignmentMargin = 0; @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()) { if (options.font.isEmpty) options.font = engineFont; auto m = uiMouse; @@ -432,18 +459,3 @@ bool uiDragHandle(Vec2 size, ref Vec2 point, UiButtonOptions options = UiButtonO 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); -}