Toying with alignment API.

This commit is contained in:
2023-12-18 09:10:31 -06:00
parent 2074cc9211
commit 0c6ac2dfbb
7 changed files with 73 additions and 27 deletions

View File

@ -59,15 +59,23 @@ struct RenderPassContext &ctx
quadCount = 0; quadCount = 0;
nextBinding = 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 itComponents = components.begin();
auto self = std::ref(*this); auto self = std::ref(*this);
while(itComponents != components.end()) { while(itComponents != components.end()) {
auto component = *itComponents; auto component = *itComponents;
component->getQuads({ 0, 0 }, self); component->getQuads(rootAlignment, self);
++itComponents; ++itComponents;
} }
flushPass();
// Flush the remaining quads
flushPass();
return passes; return passes;
} }

View File

@ -11,12 +11,22 @@ std::vector<std::shared_ptr<UIComponent>> UIComponent::getChildren() {
return {}; return {};
} }
void UIComponent::getQuads(const glm::vec2 parent, UICanvas &ctx) { void UIComponent::getQuads(
glm::vec2 transform = parent + position; const struct UIAlign alignment,
this->getSelfQuads(transform, ctx); UICanvas &ctx
) {
auto selfAlignment = getSelfAlignment(alignment);
this->getSelfQuads(selfAlignment, ctx);
auto children = getChildren(); auto children = getChildren();
for(auto &c : children) { 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 // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "ui/UIAlign.hpp"
#include "display/shader/UIShader.hpp" #include "display/shader/UIShader.hpp"
#include "component/ui/UICanvas.hpp" #include "component/ui/UICanvas.hpp"
namespace Dawn { namespace Dawn {
struct UIAlign {
glm::vec2 position;
glm::vec2 size;
};
class UIComponent { class UIComponent {
protected: protected:
/** /**
* Virtual method overridden by the UIComponent to get the quads for the * Virtual method overridden by the UIComponent to get the quads for the
* component. * 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. * @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 * 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. * 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. * @param ctx The canvas to add the quads to.
*/ */
void getQuads(const glm::vec2 parent, UICanvas &ctx); void getQuads(
const struct UIAlign alignment,
public: UICanvas &ctx
glm::vec2 position; );
friend class UICanvas;
}; };
} }

View File

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

View File

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

View File

@ -7,10 +7,13 @@
using namespace Dawn; 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; std::vector<struct UIShaderQuad> quads;
ctx.addQuad( ctx.addQuad(
glm::vec4(t, t + size), glm::vec4(alignment.position, alignment.position + size),
uv, uv,
color, color,
UIShaderQuadStyle::TEXTURED, UIShaderQuadStyle::TEXTURED,

View File

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