Toying with alignment API.

This commit is contained in:
2023-12-18 09:10:31 -06:00
parent 8f0274d1bf
commit 11ed44b480
7 changed files with 73 additions and 27 deletions

View File

@ -59,15 +59,23 @@ struct RenderPassContext &ctx
quadCount = 0;
nextBinding = 0;
// Define the root alignment
struct UIAlign rootAlignment = {
glm::vec2(0, 0),
glm::vec2(1280, 720)
};
// Get the quads for each component
auto itComponents = components.begin();
auto self = std::ref(*this);
while(itComponents != components.end()) {
auto component = *itComponents;
component->getQuads({ 0, 0 }, self);
component->getQuads(rootAlignment, self);
++itComponents;
}
flushPass();
// Flush the remaining quads
flushPass();
return passes;
}

View File

@ -11,12 +11,22 @@ std::vector<std::shared_ptr<UIComponent>> UIComponent::getChildren() {
return {};
}
void UIComponent::getQuads(const glm::vec2 parent, UICanvas &ctx) {
glm::vec2 transform = parent + position;
this->getSelfQuads(transform, ctx);
void UIComponent::getQuads(
const struct UIAlign alignment,
UICanvas &ctx
) {
auto selfAlignment = getSelfAlignment(alignment);
this->getSelfQuads(selfAlignment, ctx);
auto children = getChildren();
for(auto &c : children) {
c->getQuads(transform, ctx);
c->getQuads(selfAlignment, ctx);
}
}
struct UIAlign UIComponent::getSelfAlignment(const struct UIAlign alignment) {
return (struct UIAlign){
alignment.position + position,
alignment.size
};
}

View File

@ -4,21 +4,39 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "ui/UIAlign.hpp"
#include "display/shader/UIShader.hpp"
#include "component/ui/UICanvas.hpp"
namespace Dawn {
struct UIAlign {
glm::vec2 position;
glm::vec2 size;
};
class UIComponent {
protected:
/**
* Virtual method overridden by the UIComponent to get the quads for the
* component.
*
* @param t The translation of this component already applied.
* @param alignment The alignment of this component.
* @param ctx The canvas to add the quads to.
*/
virtual void getSelfQuads(const glm::vec2 t, UICanvas &ctx) = 0;
virtual void getSelfQuads(
const struct UIAlign alignment,
UICanvas &ctx
) = 0;
/**
* Allows a component to modify its self alignment.
*
* @param alignment Aliugnment to modify.
* @return New alignment for this component.
*/
virtual struct UIAlign getSelfAlignment(const struct UIAlign alignment);
public:
glm::vec2 position;
/**
* Virtual method overridden by the UIComponent to get the children of
@ -29,14 +47,12 @@ namespace Dawn {
/**
* Method called by the UICanvas to get the quads for this component.
*
* @param parent The parent translation to apply to the component.
* @param alignment The alignment of this component.
* @param ctx The canvas to add the quads to.
*/
void getQuads(const glm::vec2 parent, UICanvas &ctx);
public:
glm::vec2 position;
friend class UICanvas;
void getQuads(
const struct UIAlign alignment,
UICanvas &ctx
);
};
}

View File

@ -7,21 +7,24 @@
using namespace Dawn;
void UILabel::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
void UILabel::getSelfQuads(
const struct UIAlign alignment,
UICanvas &ctx
) {
std::vector<struct UIShaderQuad> quads;
if(this->texture == nullptr || this->text.empty()) return;
glm::vec2 position = t;
glm::vec4 quad;
glm::vec2 pos = alignment.position;
for(wchar_t c : text) {
auto info = texture->getCharacterData(c);
ctx.addQuad(
{
position.x + info.offset.x,
position.y + info.offset.y,
position.x + info.size.x + info.offset.x,
position.y + info.size.y + info.offset.y
pos.x + info.offset.x,
pos.y + info.offset.y,
pos.x + info.size.x + info.offset.x,
pos.y + info.size.y + info.offset.y
},
{
info.quad.x,
@ -33,7 +36,7 @@ void UILabel::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
UIShaderQuadStyle::FONT,
texture->texture
);
position += info.advance;
pos += info.advance;
}
}

View File

@ -14,7 +14,10 @@ namespace Dawn {
std::wstring text = L"Hello World";
protected:
void getSelfQuads(const glm::vec2 t, UICanvas &ctx) override;
void getSelfQuads(
const struct UIAlign alignment,
UICanvas &ctx
) override;
public:
/**

View File

@ -7,10 +7,13 @@
using namespace Dawn;
void UIRectangle::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
void UIRectangle::getSelfQuads(
const struct UIAlign alignment,
UICanvas &ctx
) {
std::vector<struct UIShaderQuad> quads;
ctx.addQuad(
glm::vec4(t, t + size),
glm::vec4(alignment.position, alignment.position + size),
uv,
color,
UIShaderQuadStyle::TEXTURED,

View File

@ -9,7 +9,10 @@
namespace Dawn {
class UIRectangle final : public UIComponent {
protected:
void getSelfQuads(const glm::vec2 t, UICanvas &ctx) override;
void getSelfQuads(
const struct UIAlign alignment,
UICanvas &ctx
) override;
public:
struct Color color = COLOR_WHITE;