UI Hello World

This commit is contained in:
2022-10-23 01:01:16 -07:00
parent be529b70c1
commit 182eb70361
25 changed files with 663 additions and 41 deletions

View File

@ -0,0 +1,11 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
UIComponent.cpp
UISprite.cpp
)

119
src/dawn/ui/UIComponent.cpp Normal file
View File

@ -0,0 +1,119 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIComponent.hpp"
using namespace Dawn;
UIComponent::UIComponent(UICanvas &canvas) :
canvas(canvas)
{
}
void UIComponent::updatePositions() {
// X Alignment
if(this->alignX == UI_COMPONENT_ALIGN_STRETCH) {
if(this->parent == nullptr) {
this->width = this->canvas.getWidth();
} else {
this->width = this->parent->getWidth();
}
this->relativeX = this->alignment[0];
this->width -= (this->alignment[0] + this->alignment[2]);
} else {
this->relativeX = this->alignment[0];
this->width = this->alignment[2];
}
// Y Alignment
if(this->alignY == UI_COMPONENT_ALIGN_STRETCH) {
if(this->parent == nullptr) {
this->height = this->canvas.getHeight();
} else {
this->height = this->parent->getHeight();
}
this->relativeY = this->alignment[1];
this->height -= (this->alignment[1] + this->alignment[3]);
} else {
this->relativeY = this->alignment[1];
this->height = this->alignment[3];
}
// Update children
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->updatePositions();
++it;
}
}
float_t UIComponent::getWidth() {
return this->width;
}
float_t UIComponent::getHeight() {
return this->height;
}
float_t UIComponent::getRelativeX() {
return this->relativeX;
}
float_t UIComponent::getRelativeY() {
return this->relativeY;
}
void UIComponent::setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
) {
this->alignX = xAlign;
this->alignY = yAlign;
this->alignment = alignment;
this->z = z;
this->updatePositions();
}
void UIComponent::draw(UIShader &uiShader, glm::mat4 parentTransform) {
// Calculate self transform matrix
glm::mat4 selfTransform = parentTransform * glm::translate(
glm::mat4(1.0f), glm::vec3(this->relativeX, this->relativeY, this->z)
);
// Draw Self
this->drawSelf(uiShader, selfTransform);
// Render children
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->draw(uiShader, selfTransform);
++it;
}
}
void UIComponent::addChild(std::shared_ptr<UIComponent> child) {
if(child->parent == this) return;
if(child->parent != nullptr) child->parent->removeChild(child);
this->children.push_back(child);
child->parent = this;
}
void UIComponent::removeChild(std::shared_ptr<UIComponent> child) {
if(child->parent != this) throw "Invalid child";
auto it = this->children.begin();
while(it != this->children.end()) {
if(it->get() == child.get()) {
this->children.erase(it);
break;
}
++it;
}
}
UIComponent::~UIComponent() {
}

View File

@ -0,0 +1,75 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/ui/UICanvas.hpp"
#include "scene/Scene.hpp"
#include "display/Color.hpp"
#include "display/shader/UIShader.hpp"
namespace Dawn {
enum UIComponentAlign {
UI_COMPONENT_ALIGN_START,
UI_COMPONENT_ALIGN_MIDDLE,
UI_COMPONENT_ALIGN_END,
UI_COMPONENT_ALIGN_STRETCH
};
class UIComponent {
protected:
// Calculated (and cached) values
float_t width = 1;
float_t height = 1;
float_t relativeX = 0;
float_t relativeY = 0;
// Setting values
UIComponentAlign alignX = UI_COMPONENT_ALIGN_START;
UIComponentAlign alignY = UI_COMPONENT_ALIGN_START;
glm::vec4 alignment = glm::vec4(0, 0, 1, 1);
float_t z = 0;
std::vector<std::shared_ptr<UIComponent>> children;
UIComponent *parent = nullptr;
// I currently don't support rotation or scale. Not because I can't but
// because it's basically un-necessary. Unity does support rotation but
// it doesn't affect how the alignment side of things work (similar to how
// CSS would handle things) When I need to support these I will add the
// code but right now it's not necessary
/**
* Updates the cached/stored values based on the setting internal values.
* You should watchdog this if you intend to do something when values are
* updated, e.g. if you need to resize a quad, or something.
*/
virtual void updatePositions();
public:
UICanvas &canvas;
UIComponent(UICanvas &canvas);
float_t getWidth();
float_t getHeight();
float_t getRelativeX();
float_t getRelativeY();
void setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
);
// virtual void update() = 0;
void draw(UIShader &uiShader, glm::mat4 parentTransform);
virtual void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) = 0;
void addChild(std::shared_ptr<UIComponent> chidl);
void removeChild(std::shared_ptr<UIComponent> child);
virtual ~UIComponent();
};
}

34
src/dawn/ui/UISprite.cpp Normal file
View File

@ -0,0 +1,34 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UISprite.hpp"
using namespace Dawn;
UISprite::UISprite(UICanvas &canvas) : UIComponent(canvas) {
this->mesh.createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
}
void UISprite::updatePositions() {
UIComponent::updatePositions();
std::cout << "Updating" << std::endl;
QuadMesh::bufferQuadMesh(
this->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(this->width, this->height), glm::vec2(1, 1),
0, 0
);
}
void UISprite::drawSelf(UIShader &uiShader, glm::mat4 selfTransform) {
uiShader.setUITexture(nullptr);
uiShader.setUIModel(selfTransform);
uiShader.setUIModel(glm::mat4(1.0f));
uiShader.setUIColor(COLOR_WHITE);
this->mesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
}

23
src/dawn/ui/UISprite.hpp Normal file
View File

@ -0,0 +1,23 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
namespace Dawn {
class UISprite : public UIComponent {
protected:
void updatePositions() override;
public:
Mesh mesh;
UISprite(UICanvas &canvas);
// void update() override;
void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) override;
};
}