ADD: Change color of Text

This commit is contained in:
sebastian 2026-02-11 23:04:21 +01:00
parent 441ea64a2f
commit 1e266709c5
7 changed files with 27 additions and 6 deletions

View File

@ -8,9 +8,13 @@
#include "../text/Font.h"
UiText::UiText(Font &font, std::string text, const glm::vec2 &relativePos, const glm::vec2& relativeSize) : font(font), text(std::move(text)) {
UiText::UiText(Font &font, std::string text, const glm::vec2 &relativePos, glm::vec3 color): font(font), text(std::move(text)), color(color) {
uiPositioner.setRelativePos(relativePos);
uiPositioner.setRelativeSize(relativeSize);
}
UiText::UiText(Font &font, std::string text, const glm::vec2 &relativePos) : font(font), text(std::move(text)) {
uiPositioner.setRelativePos(relativePos);
color = glm::vec3(0.f);
}
void UiText::onCollectRenderData(UiRenderBundle& ui_render_bundle) {

View File

@ -6,13 +6,15 @@
#define DICEWARS_SIEDLER_UITEXT_H
#include "UiComponent.h"
#include "UiRenderBundle.h"
#include "glm/vec3.hpp"
class Font;
class UiText : public UiComponent {
public:
UiText(Font& font, std::string text, const glm::vec2& relativePos, const glm::vec2& relativeSize);
UiText(Font& font, std::string text, const glm::vec2& relativePos, glm::vec3 size);
UiText(Font& font, std::string text, const glm::vec2& relativePos);
void setText(const std::string& text);
[[nodiscard]] const Font& getFont() const { return font; }
@ -21,11 +23,15 @@ public:
return text;
}
[[nodiscard]] glm::vec3 getColor() const { return color; }
protected:
void onCollectRenderData(UiRenderBundle &uiRenderBundle) override;
private:
std::string text;
Font& font;
glm::vec3 color;
};

View File

@ -20,9 +20,10 @@ void TextRenderer::renderTexts(const std::vector<UiText*> &textsToRender) {
}
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
shader.stop();
}
void TextRenderer::renderText(const UiText &textToRender) const {
void TextRenderer::renderText(const UiText &textToRender) {
const Dimensions& d = textToRender.uiPositioner.screenSpace;
const Font& font = textToRender.getFont();
@ -36,7 +37,7 @@ void TextRenderer::renderText(const UiText &textToRender) const {
float scale = 1.0f;
shader.loadTextColor(textToRender.getColor());
for (char c : text) {
const Font::Character& ch = font.getCharacter(c);

View File

@ -17,6 +17,9 @@ class TextRenderer {
public:
TextRenderer() : textModel(Loader::instance().loadTextModel()) {};
void renderTexts(const std::vector<UiText *> &textsToRender);
void renderText(const UiText &textToRender);
void renderText(const UiText &textToRender) const;
private:
TextShader shader;

View File

@ -8,8 +8,13 @@ void TextShader::loadProjectionMatrix(glm::mat4 matrix) {
ShaderProgram::loadMatrix(location_projectionMatrix, matrix);
}
void TextShader::loadTextColor(glm::vec3 color) {
ShaderProgram::loadVector(location_textColor, color);
}
void TextShader::getAllUniformLocations() {
location_projectionMatrix = ShaderProgram::getUniformLocation("projectionMatrix");
location_textColor = ShaderProgram::getUniformLocation("textColor");
}
void TextShader::bindAttributes() const {

View File

@ -14,6 +14,7 @@ public :
TextShader::getAllUniformLocations();
}
void loadProjectionMatrix(glm::mat4 matrix);
void loadTextColor(glm::vec3 color);
protected:
void getAllUniformLocations() override;
@ -23,6 +24,7 @@ private:
inline static const std::string FRAGMENT_FILE = "assets/shaders/textFragmentShader.glsl";
int location_projectionMatrix;
int location_textColor;
};

View File

@ -32,7 +32,7 @@ void UILayer::onAttach() {
Font myFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 48);
font = std::make_unique<Font>(myFont);
auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec2(1.f));
auto text = std::make_unique<UiText>(*font, "Hello World!", glm::vec2(0.5f, 0.5f), glm::vec3(1,1,1));
rootContainer->addChild(std::move(text));
}