FIX: Correctly Positioning Rendered Texts

This commit is contained in:
sebastian 2026-02-15 16:45:32 +01:00
parent a8c8e1770c
commit 9ef692ab5d
3 changed files with 26 additions and 27 deletions

View File

@ -7,11 +7,16 @@
#include <utility> #include <utility>
#include "UiText.h" #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::NONE] = {1.0f, glm::vec3(0.0f), 0.0f};
visualStyles[UiEventType::MOUSE_OVER] = {1.15f, 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}; 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) { void UiButton::onCollectRenderData(UiRenderBundle &uiRenderBundle) {

View File

@ -12,7 +12,7 @@ class Font;
class UiButton : public ClickableUiComponent { class UiButton : public ClickableUiComponent {
public: public:
UiButton(GLuint textureID, std::string label, Font& font, const LayoutStyle& style); UiButton(GLuint textureID, std::string label, Font& font, LayoutStyle& style);
protected: protected:
void onCollectRenderData(UiRenderBundle &uiRenderBundle) override; void onCollectRenderData(UiRenderBundle &uiRenderBundle) override;

View File

@ -92,34 +92,23 @@ void TextRenderer::renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> &t
} }
void TextRenderer::renderGuiText(const GUIText &text) { void TextRenderer::renderGuiText(const GUIText &text) {
glm::vec2 textPos = text.getPosition();
glm::vec2 textSize = text.getSize();
const Font& font = text.getFont(); 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; float scale = 1.0f;
const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
const auto screenHeight = static_cast<float>(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)); shader.loadTextColor(glm::vec3(1.0f));
for (char c : text.getText()) { for (char c : string) {
const Font::Character& ch = font.getCharacter(c); const Font::Character& ch = font.getCharacter(c);
float xpos = x + static_cast<float>(ch.bearing.x) * scale; float xpos = x + static_cast<float>(ch.bearing.x) * scale;
@ -141,6 +130,7 @@ void TextRenderer::renderGuiText(const GUIText &text) {
glBindVertexArray(textModel.vaoID); glBindVertexArray(textModel.vaoID);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ch.textureID); glBindTexture(GL_TEXTURE_2D, ch.textureID);
@ -154,8 +144,12 @@ void TextRenderer::renderGuiText(const GUIText &text) {
glBindVertexArray(0); glBindVertexArray(0);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
} }
glBindTexture(GL_TEXTURE_2D, 0);
} }
glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix() { glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix() {
const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth()); const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
const auto screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight()); const auto screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight());