From 0d2bcbbdf612145299dc36bc9af065677712ae78 Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Tue, 4 Feb 2025 23:21:13 +0100 Subject: [PATCH] Add scale for label --- .../Scene/Prefabs/LabelDrawable.cpp | 16 +++++++------- .../Scene/Prefabs/LabelDrawable.hpp | 1 + .../Scene/Text/IFontAtlasGenerator.hpp | 2 +- openVulkanoCpp/Scene/TextDrawable.cpp | 22 +++++++++---------- openVulkanoCpp/Scene/TextDrawable.hpp | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/openVulkanoCpp/Scene/Prefabs/LabelDrawable.cpp b/openVulkanoCpp/Scene/Prefabs/LabelDrawable.cpp index 8c0e8a0..eeb5201 100644 --- a/openVulkanoCpp/Scene/Prefabs/LabelDrawable.cpp +++ b/openVulkanoCpp/Scene/Prefabs/LabelDrawable.cpp @@ -84,9 +84,9 @@ namespace OpenVulkano::Scene m_labelData.color = settings.backgroundColor; m_labelData.hasRoundedCorners = settings.hasRoundedCorners; m_labelData.hasArrow = settings.hasArrow; - m_labelData.cornerRadius = settings.cornerRadius * settings.cornerRadius; - m_labelData.arrowLength = settings.arrowLength; - m_labelData.arrowWidth = settings.arrowWidth; + m_labelData.cornerRadius = settings.cornerRadius * settings.cornerRadius * settings.scale; + m_labelData.arrowLength = settings.arrowLength * settings.scale; + m_labelData.arrowWidth = settings.arrowWidth * settings.scale; } void LabelDrawable::AddText(const std::string& text, const TextConfig& config) @@ -96,18 +96,18 @@ namespace OpenVulkano::Scene TextDrawable& textDrawable = m_texts.emplace_back(m_atlasData, config); textDrawable.GetConfig().backgroundColor.a = 0; // do not render glyph's background double lineHeight = m_atlasData->GetLineHeight(); - textDrawable.GenerateText(text, m_position); + textDrawable.GenerateText(text, m_position, m_settings.scale); textDrawable.SetShader(GetTextShader(m_atlasData->GetAtlasType(), IsBillboard())); m_bbox.Grow(textDrawable.GetBoundingBox()); // update position for next text entry - m_position.y = m_bbox.GetMin().y - lineHeight; + m_position.y = m_bbox.GetMin().y - lineHeight * m_settings.scale; - Math::Vector2f padding = m_settings.padding * 2; - if (m_settings.hasArrow) padding.y += m_settings.arrowLength; + Math::Vector2f padding = m_settings.padding * 2 * m_settings.scale; + if (m_settings.hasArrow) padding.y += m_settings.arrowLength * m_settings.scale; m_labelData.textSize = m_bbox.GetSize() + padding; m_labelData.bboxCenter = m_bbox.GetCenter(); - if (m_settings.hasArrow) m_labelData.bboxCenter.y -= m_settings.arrowLength * 0.5f; + if (m_settings.hasArrow) m_labelData.bboxCenter.y -= m_settings.arrowLength * 0.5f * m_settings.scale; } std::optional LabelDrawable::Intersect(const Ray& ray) const diff --git a/openVulkanoCpp/Scene/Prefabs/LabelDrawable.hpp b/openVulkanoCpp/Scene/Prefabs/LabelDrawable.hpp index 6cf4c45..ab3be68 100644 --- a/openVulkanoCpp/Scene/Prefabs/LabelDrawable.hpp +++ b/openVulkanoCpp/Scene/Prefabs/LabelDrawable.hpp @@ -23,6 +23,7 @@ namespace OpenVulkano::Scene float cornerRadius = 0.05f; float arrowLength = 0.5f; float arrowWidth = 0.2f; + float scale = 1.0f; bool hasRoundedCorners = false; bool hasArrow = false; bool isBillboard = false; diff --git a/openVulkanoCpp/Scene/Text/IFontAtlasGenerator.hpp b/openVulkanoCpp/Scene/Text/IFontAtlasGenerator.hpp index 930c6f1..8c249ba 100644 --- a/openVulkanoCpp/Scene/Text/IFontAtlasGenerator.hpp +++ b/openVulkanoCpp/Scene/Text/IFontAtlasGenerator.hpp @@ -14,7 +14,7 @@ namespace OpenVulkano::Scene { - struct FontAtlas; + class FontAtlas; class IFontAtlasGenerator { diff --git a/openVulkanoCpp/Scene/TextDrawable.cpp b/openVulkanoCpp/Scene/TextDrawable.cpp index 4f67902..c4ced15 100644 --- a/openVulkanoCpp/Scene/TextDrawable.cpp +++ b/openVulkanoCpp/Scene/TextDrawable.cpp @@ -167,7 +167,7 @@ namespace OpenVulkano::Scene return c == '\t' || c == '\n' || c == '\v'; } - void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos) + void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos, float scale) { if (text.empty()) return; if (m_vertexBuffer.data) throw std::runtime_error("Text has already been initialized"); @@ -180,8 +180,7 @@ namespace OpenVulkano::Scene TextGlyphVertex* vertices = m_vertexBuffer.Init(len); const std::map& symbols = m_atlasData->GetGlyphs(); - float cursorX = pos.x; - float cursorY = pos.y; + Math::Vector2f cursor = pos / scale; Math::Vector2f bmin(pos), bmax(pos); float prevGlyphXBound = -INFINITY; const float heightBetweenLines = GetHeightBetweenLines(text); @@ -191,7 +190,7 @@ namespace OpenVulkano::Scene uint32_t c = utf8::next(begin, end); if (IsSpecialCharacter(c)) { - HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursorX, cursorY, prevGlyphXBound); + HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursor.x, cursor.y, prevGlyphXBound); continue; } @@ -208,31 +207,32 @@ namespace OpenVulkano::Scene const bool isBitmap = m_atlasData->GetAtlasType().IsBitmap(); if (prevGlyphXBound != -INFINITY && !isZeroLenGlyph && !isBitmap) { - offset = prevGlyphXBound - (info.pos[0].x + cursorX); + offset = prevGlyphXBound - (info.pos[0].x + cursor.x); } for (int i = 0; i < 4; i++) { - vertices->position[i].x = info.pos[i].x + cursorX + offset; + vertices->position[i].x = info.pos[i].x + cursor.x + offset; vertices->uv[i] = info.uv[i]; if (i < 2) { - vertices->position[i].y = cursorY - info.pos[i].y; + vertices->position[i].y = cursor.y - info.pos[i].y; } else { - vertices->position[i].y = cursorY + info.pos[i].y; + vertices->position[i].y = cursor.y + info.pos[i].y; } vertices->color = m_cfg.textColor; vertices->background = m_cfg.backgroundColor; + vertices->position[i] *= scale; } // somehow it's possible that cursorX can be less than prevGlyphXBound for sdf atlases // e.g. string `A, _` where space is not noticeable in the resulting output - cursorX = std::max(cursorX, prevGlyphXBound); + cursor.x = std::max(cursor.x, prevGlyphXBound); // slight offset for bitmap atlas since it's tightly packed without any extra padding, while sdf has additional padding - cursorX += info.advance + (isBitmap ? 0.05 : 0); - prevGlyphXBound = isZeroLenGlyph ? cursorX : vertices->position[2].x; + cursor.x += info.advance + (isBitmap ? 0.05 : 0); + prevGlyphXBound = isZeroLenGlyph ? cursor.x : vertices->position[2].x / scale; if (!m_symbolCount) bmin.x = vertices->position[0].x; bmax.x = std::max(bmax.x, vertices->position[1].x); diff --git a/openVulkanoCpp/Scene/TextDrawable.hpp b/openVulkanoCpp/Scene/TextDrawable.hpp index 90ea18b..4109b63 100644 --- a/openVulkanoCpp/Scene/TextDrawable.hpp +++ b/openVulkanoCpp/Scene/TextDrawable.hpp @@ -51,7 +51,7 @@ namespace OpenVulkano::Scene TextDrawable(const Array& atlasMetadata, const TextConfig& config = TextConfig()); TextDrawable(const std::shared_ptr& atlasData, const TextConfig& config = TextConfig()); - void GenerateText(const std::string& text, const Math::Vector2f& pos = Math::Vector2f(0.f)); + void GenerateText(const std::string& text, const Math::Vector2f& pos = Math::Vector2f(0.f), float scale = 1.0f); void SetConfig(const TextConfig& cfg) { m_cfg = cfg; } void SetAtlasData(const std::shared_ptr& atlasData);