From bef222d39dddd97483d8d6a47dc27d33f0fc97eb Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 12 Dec 2023 16:08:13 -0600 Subject: [PATCH] Prepping UI Label --- src/dawn/component/ui/UICanvas.cpp | 5 +-- src/dawn/ui/CMakeLists.txt | 1 + src/dawn/ui/UIComponent.cpp | 16 ++------- src/dawn/ui/UIComponent.hpp | 6 ++-- src/dawn/ui/UILabel.cpp | 38 ++++++++++++++++++++ src/dawn/ui/UILabel.hpp | 21 +++++++++++ src/dawn/ui/UIRectangle.cpp | 7 ++-- src/dawn/ui/UIRectangle.hpp | 4 +-- src/dawnhelloworld/scene/HelloWorldScene.cpp | 12 +++---- src/dawnopengl/display/shader/UIShader.cpp | 11 ++---- src/dawnopengl/display/shader/UIShader.hpp | 1 - 11 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 src/dawn/ui/UILabel.cpp create mode 100644 src/dawn/ui/UILabel.hpp diff --git a/src/dawn/component/ui/UICanvas.cpp b/src/dawn/component/ui/UICanvas.cpp index c6f18cf3..dbc4b18f 100644 --- a/src/dawn/component/ui/UICanvas.cpp +++ b/src/dawn/component/ui/UICanvas.cpp @@ -51,7 +51,7 @@ std::vector> UICanvas::getPasses( auto component = *itComponents; // Get this components' quads. - auto quads = component->getQuads(glm::mat4(1.0)); + auto quads = component->getQuads({ 0, 0 }); for(auto quad : quads) { data.quads[quadCount++] = quad; assertTrue(quadCount <= UI_SHADER_QUAD_COUNT, "Too many UI quads!"); @@ -68,7 +68,8 @@ std::vector> UICanvas::getPasses( textures, mesh, MeshDrawMode::TRIANGLES, - 0, quadCount * QUAD_INDICE_COUNT + 0, + quadCount * QUAD_INDICE_COUNT ); passes.push_back(pass); diff --git a/src/dawn/ui/CMakeLists.txt b/src/dawn/ui/CMakeLists.txt index c5a485e0..8e1f7db0 100644 --- a/src/dawn/ui/CMakeLists.txt +++ b/src/dawn/ui/CMakeLists.txt @@ -7,4 +7,5 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE UIComponent.cpp UIRectangle.cpp + UILabel.cpp ) \ No newline at end of file diff --git a/src/dawn/ui/UIComponent.cpp b/src/dawn/ui/UIComponent.cpp index 2469e7cb..c7e377ee 100644 --- a/src/dawn/ui/UIComponent.cpp +++ b/src/dawn/ui/UIComponent.cpp @@ -11,22 +11,10 @@ std::vector> UIComponent::getChildren() { return {}; } -std::vector UIComponent::getQuads( - const glm::mat4 parent -) { - // Get self transform - glm::mat4 transform = glm::translate( - glm::mat4(1.0f), - glm::vec3(position, 0.0f) - ); - - // Add parent transform - transform = parent * transform; - - // Get self quads and insert new transform. +std::vector UIComponent::getQuads(const glm::vec2 parent) { + glm::vec2 transform = parent + position; std::vector quads = this->getSelfQuads(transform); - // Get children auto children = getChildren(); for(auto &c : children) { auto childQuads = c->getQuads(transform); diff --git a/src/dawn/ui/UIComponent.hpp b/src/dawn/ui/UIComponent.hpp index 66a43de3..b8ec683d 100644 --- a/src/dawn/ui/UIComponent.hpp +++ b/src/dawn/ui/UIComponent.hpp @@ -13,14 +13,12 @@ namespace Dawn { class UIComponent { protected: virtual std::vector getSelfQuads( - const glm::mat4 transform + const glm::vec2 t ) = 0; virtual std::vector> getChildren(); - std::vector getQuads( - const glm::mat4 parent - ); + std::vector getQuads(const glm::vec2 parent); public: glm::vec2 position; diff --git a/src/dawn/ui/UILabel.cpp b/src/dawn/ui/UILabel.cpp new file mode 100644 index 00000000..ac44532a --- /dev/null +++ b/src/dawn/ui/UILabel.cpp @@ -0,0 +1,38 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UILabel.hpp" + +using namespace Dawn; + +std::vector UILabel::getSelfQuads(const glm::vec2 t) { + std::vector quads; + if(this->texture == nullptr) return quads; + + const std::wstring text = L"Hello World!"; + glm::vec2 position = t; + glm::vec4 quad; + + for(wchar_t c : text) { + auto info = texture->getCharacterData(c); + quads.push_back({ + .quad = { + position.x, + position.y, + position.x + info.size.x, + position.y + info.size.y + }, + .uv = info.quad, + .color = COLOR_WHITE + }); + position += info.advance; + } + + return quads; +} + +void UILabel::setFont(std::shared_ptr texture) { + this->texture = texture; +} \ No newline at end of file diff --git a/src/dawn/ui/UILabel.hpp b/src/dawn/ui/UILabel.hpp new file mode 100644 index 00000000..4faac414 --- /dev/null +++ b/src/dawn/ui/UILabel.hpp @@ -0,0 +1,21 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "ui/UIComponent.hpp" +#include "display/font/TrueTypeTexture.hpp" + +namespace Dawn { + class UILabel final : public UIComponent { + private: + std::shared_ptr texture = nullptr; + + protected: + std::vector getSelfQuads(const glm::vec2 t) override; + + public: + void setFont(std::shared_ptr texture); + }; +} \ No newline at end of file diff --git a/src/dawn/ui/UIRectangle.cpp b/src/dawn/ui/UIRectangle.cpp index 9c25786d..c27399fa 100644 --- a/src/dawn/ui/UIRectangle.cpp +++ b/src/dawn/ui/UIRectangle.cpp @@ -7,13 +7,10 @@ using namespace Dawn; -std::vector UIRectangle::getSelfQuads( - const glm::mat4 transform -) { +std::vector UIRectangle::getSelfQuads(const glm::vec2 t) { std::vector quads; quads.push_back({ - transform, - glm::vec4(0, 0, size.x, size.y), + glm::vec4(t, t + size), uv, color }); diff --git a/src/dawn/ui/UIRectangle.hpp b/src/dawn/ui/UIRectangle.hpp index 3f6dbe21..f98ccf2d 100644 --- a/src/dawn/ui/UIRectangle.hpp +++ b/src/dawn/ui/UIRectangle.hpp @@ -9,9 +9,7 @@ namespace Dawn { class UIRectangle final : public UIComponent { protected: - std::vector getSelfQuads( - const glm::mat4 transform - ) override; + std::vector getSelfQuads(const glm::vec2 t) override; public: struct Color color = COLOR_WHITE; diff --git a/src/dawnhelloworld/scene/HelloWorldScene.cpp b/src/dawnhelloworld/scene/HelloWorldScene.cpp index fa0ac942..e3b65a84 100644 --- a/src/dawnhelloworld/scene/HelloWorldScene.cpp +++ b/src/dawnhelloworld/scene/HelloWorldScene.cpp @@ -12,6 +12,7 @@ #include "component/ui/UICanvas.hpp" #include "ui/UIRectangle.hpp" +#include "ui/UILabel.hpp" #include #include FT_FREETYPE_H @@ -21,7 +22,7 @@ using namespace Dawn; std::shared_ptr texture; void Dawn::helloWorldScene(Scene &s) { - texture = s.getGame()->assetManager.get("ysabeau_regular", 32); + texture = s.getGame()->assetManager.get("ysabeau_regular", 16); while(!s.getGame()->assetManager.isLoaded("ysabeau_regular")) { s.getGame()->assetManager.update(); @@ -48,16 +49,15 @@ void Dawn::helloWorldScene(Scene &s) { // auto quadMaterial = quad->addComponent(); // quadMaterial->setTexture(texture->texture); - auto uiCanvasItem = s.createSceneItem(); auto uiCanvas = uiCanvasItem->addComponent(); auto rect = std::make_shared(); rect->position = { -32, -32 }; + rect->color = COLOR_MAGENTA; uiCanvas->components.push_back(rect); - auto rect2 = std::make_shared(); - rect2->color = COLOR_MAGENTA; - rect2->position = { 10, 10 }; - uiCanvas->components.push_back(rect2); + auto label = std::make_shared(); + label->setFont(texture); + uiCanvas->components.push_back(label); } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/UIShader.cpp b/src/dawnopengl/display/shader/UIShader.cpp index f794439b..ba8f9052 100644 --- a/src/dawnopengl/display/shader/UIShader.cpp +++ b/src/dawnopengl/display/shader/UIShader.cpp @@ -30,7 +30,6 @@ void UIShader::getStages( "uniform mat4 u_View;\n" "uniform mat4 u_Model;\n" "struct UIShaderQuad {\n" - "mat4 transform;\n" "vec4 quad;\n" "vec4 uv;\n" "vec4 color;\n" @@ -70,7 +69,7 @@ void UIShader::getStages( "}\n" "pos.z = 0;\n" "pos.w = 1;\n" - "gl_Position = u_Projection * u_View * u_Model * quad.transform * pos;\n" + "gl_Position = u_Projection * u_View * u_Model * pos;\n" "o_TextCoord = coord;\n" "v_Color = quad.color;\n" "}" @@ -120,12 +119,6 @@ void UIShader::getStages( &rel->quads, ShaderOpenGLStructureType::STD140, [&](const struct UIShaderQuad &rel, std::vector ¶meters) { - parameters.push_back(ShaderParameter( - "u_Transform", - &rel.transform, - ShaderParameterType::MAT4 - )); - parameters.push_back(ShaderParameter( "u_Quad", &rel.quad, @@ -144,6 +137,6 @@ void UIShader::getStages( ShaderParameterType::COLOR )); }, - 2 + UI_SHADER_QUAD_COUNT )); } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/UIShader.hpp b/src/dawnopengl/display/shader/UIShader.hpp index 741d11d9..0fdf7aa8 100644 --- a/src/dawnopengl/display/shader/UIShader.hpp +++ b/src/dawnopengl/display/shader/UIShader.hpp @@ -10,7 +10,6 @@ namespace Dawn { #define UI_SHADER_QUAD_COUNT 32 struct UIShaderQuad { - glm::mat4 transform; glm::vec4 quad; glm::vec4 uv; struct Color color;