From 9ef692ab5dd16671b4d5bee6ec1347705c3b154c Mon Sep 17 00:00:00 2001 From: sebastian Date: Sun, 15 Feb 2026 16:45:32 +0100 Subject: [PATCH] FIX: Correctly Positioning Rendered Texts --- src/engine/core/gui/uiComponent/UiButton.cpp | 7 +++- src/engine/core/gui/uiComponent/UiButton.h | 2 +- src/engine/renderer/TextRenderer.cpp | 44 +++++++++----------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/engine/core/gui/uiComponent/UiButton.cpp b/src/engine/core/gui/uiComponent/UiButton.cpp index 81205c2..df8a91b 100644 --- a/src/engine/core/gui/uiComponent/UiButton.cpp +++ b/src/engine/core/gui/uiComponent/UiButton.cpp @@ -7,11 +7,16 @@ #include #include "UiText.h" +#include "../text/Font.h" -UiButton::UiButton(const GLuint textureID, std::string label, Font &font, const LayoutStyle &style): ClickableUiComponent(style), text(std::move(label)), font(font), textureID(textureID) { +UiButton::UiButton(const GLuint textureID, std::string label, Font &font, LayoutStyle &style): ClickableUiComponent(style), text(std::move(label)), font(font), textureID(textureID) { visualStyles[UiEventType::NONE] = {1.0f, glm::vec3(0.0f), 0.0f}; visualStyles[UiEventType::MOUSE_OVER] = {1.15f, glm::vec3(0.0f), 0.0f}; visualStyles[UiEventType::MOUSE_CLICK] = {1.0f, glm::vec3(0.3f, 0.6f, 1.0f), 0.15f}; + + style.width = SizeValue(font.getTextWidth(label, 1.0f), SizeUnit::Pixels); + style.height = SizeValue(font.getLineHeight(), SizeUnit::Pixels); + } void UiButton::onCollectRenderData(UiRenderBundle &uiRenderBundle) { diff --git a/src/engine/core/gui/uiComponent/UiButton.h b/src/engine/core/gui/uiComponent/UiButton.h index bcc6043..71f1c3d 100644 --- a/src/engine/core/gui/uiComponent/UiButton.h +++ b/src/engine/core/gui/uiComponent/UiButton.h @@ -12,7 +12,7 @@ class Font; class UiButton : public ClickableUiComponent { public: - UiButton(GLuint textureID, std::string label, Font& font, const LayoutStyle& style); + UiButton(GLuint textureID, std::string label, Font& font, LayoutStyle& style); protected: void onCollectRenderData(UiRenderBundle &uiRenderBundle) override; diff --git a/src/engine/renderer/TextRenderer.cpp b/src/engine/renderer/TextRenderer.cpp index f2560c5..041c1cc 100644 --- a/src/engine/renderer/TextRenderer.cpp +++ b/src/engine/renderer/TextRenderer.cpp @@ -92,34 +92,23 @@ void TextRenderer::renderGuiTexts(const std::vector> &t } void TextRenderer::renderGuiText(const GUIText &text) { + glm::vec2 textPos = text.getPosition(); + glm::vec2 textSize = text.getSize(); + const Font& font = text.getFont(); + const std::string& string = text.getText(); + + float screenX = textPos.x * Application::getInstance().getWindow().GetWidth(); + float screenY = (1.f - textPos.y) * Application::getInstance().getWindow().GetHeight(); + + float textHeight = font.getAscent() - font.getDescent(); // ascent - descent + float buttonWidthPx = textSize.x * Application::getInstance().getWindow().GetWidth(); + float x = screenX + (buttonWidthPx - font.getTextWidth(text.getText(), 1.0f)) * 0.5f; + float y = screenY - textHeight - font.getDescent(); + float scale = 1.0f; - - const auto screenWidth = static_cast(Application::getInstance().getWindow().GetWidth()); - const auto screenHeight = static_cast(Application::getInstance().getWindow().GetHeight()); - - float x = text.getPosition().x * screenWidth; - float y = (1.f - text.getPosition().y) * screenHeight; - - const float textWidth = font.getTextWidth(text.getText(), scale); - - const float buttonHeightPx = text.getSize().y * screenHeight; - - const float ascent = font.getAscent(); - const float descent = font.getDescent(); // negativ! - - const float textVisualHeight = ascent - descent; - - // Button-Mitte - const float buttonCenterY = y - buttonHeightPx * 0.5f; - - // Zentrieren - x += (text.getSize().x * screenWidth - textWidth) * 0.5f; - y -= buttonCenterY + textVisualHeight * 0.5f - descent; - - shader.loadTextColor(glm::vec3(1.0f)); - for (char c : text.getText()) { + for (char c : string) { const Font::Character& ch = font.getCharacter(c); float xpos = x + static_cast(ch.bearing.x) * scale; @@ -141,6 +130,7 @@ void TextRenderer::renderGuiText(const GUIText &text) { glBindVertexArray(textModel.vaoID); glEnableVertexAttribArray(0); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ch.textureID); @@ -154,8 +144,12 @@ void TextRenderer::renderGuiText(const GUIText &text) { glBindVertexArray(0); glDisableVertexAttribArray(0); } + + glBindTexture(GL_TEXTURE_2D, 0); } + + glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix() { const auto screenWidth = static_cast(Application::getInstance().getWindow().GetWidth()); const auto screenHeight = static_cast(Application::getInstance().getWindow().GetHeight());