Prepping UI Label
This commit is contained in:
@ -51,7 +51,7 @@ std::vector<std::shared_ptr<IRenderPass>> 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<std::shared_ptr<IRenderPass>> UICanvas::getPasses(
|
||||
textures,
|
||||
mesh,
|
||||
MeshDrawMode::TRIANGLES,
|
||||
0, quadCount * QUAD_INDICE_COUNT
|
||||
0,
|
||||
quadCount * QUAD_INDICE_COUNT
|
||||
);
|
||||
passes.push_back(pass);
|
||||
|
||||
|
@ -7,4 +7,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
UIComponent.cpp
|
||||
UIRectangle.cpp
|
||||
UILabel.cpp
|
||||
)
|
@ -11,22 +11,10 @@ std::vector<std::shared_ptr<UIComponent>> UIComponent::getChildren() {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<struct UIShaderQuad> 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<struct UIShaderQuad> UIComponent::getQuads(const glm::vec2 parent) {
|
||||
glm::vec2 transform = parent + position;
|
||||
std::vector<struct UIShaderQuad> quads = this->getSelfQuads(transform);
|
||||
|
||||
// Get children
|
||||
auto children = getChildren();
|
||||
for(auto &c : children) {
|
||||
auto childQuads = c->getQuads(transform);
|
||||
|
@ -13,14 +13,12 @@ namespace Dawn {
|
||||
class UIComponent {
|
||||
protected:
|
||||
virtual std::vector<struct UIShaderQuad> getSelfQuads(
|
||||
const glm::mat4 transform
|
||||
const glm::vec2 t
|
||||
) = 0;
|
||||
|
||||
virtual std::vector<std::shared_ptr<UIComponent>> getChildren();
|
||||
|
||||
std::vector<struct UIShaderQuad> getQuads(
|
||||
const glm::mat4 parent
|
||||
);
|
||||
std::vector<struct UIShaderQuad> getQuads(const glm::vec2 parent);
|
||||
|
||||
public:
|
||||
glm::vec2 position;
|
||||
|
38
src/dawn/ui/UILabel.cpp
Normal file
38
src/dawn/ui/UILabel.cpp
Normal file
@ -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<struct UIShaderQuad> UILabel::getSelfQuads(const glm::vec2 t) {
|
||||
std::vector<struct UIShaderQuad> 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<TrueTypeTexture> texture) {
|
||||
this->texture = texture;
|
||||
}
|
21
src/dawn/ui/UILabel.hpp
Normal file
21
src/dawn/ui/UILabel.hpp
Normal file
@ -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<TrueTypeTexture> texture = nullptr;
|
||||
|
||||
protected:
|
||||
std::vector<struct UIShaderQuad> getSelfQuads(const glm::vec2 t) override;
|
||||
|
||||
public:
|
||||
void setFont(std::shared_ptr<TrueTypeTexture> texture);
|
||||
};
|
||||
}
|
@ -7,13 +7,10 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<struct UIShaderQuad> UIRectangle::getSelfQuads(
|
||||
const glm::mat4 transform
|
||||
) {
|
||||
std::vector<struct UIShaderQuad> UIRectangle::getSelfQuads(const glm::vec2 t) {
|
||||
std::vector<struct UIShaderQuad> quads;
|
||||
quads.push_back({
|
||||
transform,
|
||||
glm::vec4(0, 0, size.x, size.y),
|
||||
glm::vec4(t, t + size),
|
||||
uv,
|
||||
color
|
||||
});
|
||||
|
@ -9,9 +9,7 @@
|
||||
namespace Dawn {
|
||||
class UIRectangle final : public UIComponent {
|
||||
protected:
|
||||
std::vector<struct UIShaderQuad> getSelfQuads(
|
||||
const glm::mat4 transform
|
||||
) override;
|
||||
std::vector<struct UIShaderQuad> getSelfQuads(const glm::vec2 t) override;
|
||||
|
||||
public:
|
||||
struct Color color = COLOR_WHITE;
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
#include "component/ui/UICanvas.hpp"
|
||||
#include "ui/UIRectangle.hpp"
|
||||
#include "ui/UILabel.hpp"
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
@ -21,7 +22,7 @@ using namespace Dawn;
|
||||
std::shared_ptr<TrueTypeTexture> texture;
|
||||
|
||||
void Dawn::helloWorldScene(Scene &s) {
|
||||
texture = s.getGame()->assetManager.get<TrueTypeTexture>("ysabeau_regular", 32);
|
||||
texture = s.getGame()->assetManager.get<TrueTypeTexture>("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<SimpleTexturedMaterial>();
|
||||
// quadMaterial->setTexture(texture->texture);
|
||||
|
||||
|
||||
auto uiCanvasItem = s.createSceneItem();
|
||||
auto uiCanvas = uiCanvasItem->addComponent<UICanvas>();
|
||||
|
||||
auto rect = std::make_shared<UIRectangle>();
|
||||
rect->position = { -32, -32 };
|
||||
rect->color = COLOR_MAGENTA;
|
||||
uiCanvas->components.push_back(rect);
|
||||
|
||||
auto rect2 = std::make_shared<UIRectangle>();
|
||||
rect2->color = COLOR_MAGENTA;
|
||||
rect2->position = { 10, 10 };
|
||||
uiCanvas->components.push_back(rect2);
|
||||
auto label = std::make_shared<UILabel>();
|
||||
label->setFont(texture);
|
||||
uiCanvas->components.push_back(label);
|
||||
}
|
@ -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<struct ShaderParameter> ¶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
|
||||
));
|
||||
}
|
@ -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;
|
||||
|
Reference in New Issue
Block a user