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;
|
||||
|
Reference in New Issue
Block a user