Lots of UI Component Updates
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
UIBorder.cpp
|
||||
UIComponent.cpp
|
||||
UILabel.cpp
|
||||
UISprite.cpp
|
||||
|
130
src/dawn/ui/UIBorder.cpp
Normal file
130
src/dawn/ui/UIBorder.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "UIBorder.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
UIBorder::UIBorder(UICanvas &canvas) : UIComponent(canvas) {
|
||||
this->mesh.createBuffers(QUAD_VERTICE_COUNT * 9, QUAD_INDICE_COUNT * 9);
|
||||
|
||||
this->texture = new Texture();
|
||||
this->texture->setSize(3, 3);
|
||||
struct Color pixels[9] = {
|
||||
COLOR_WHITE, COLOR_RED, COLOR_BLUE,
|
||||
COLOR_GREEN, COLOR_MAGENTA, COLOR_BLACK,
|
||||
COLOR_CORNFLOWER_BLUE, COLOR_WHITE, COLOR_MAGENTA
|
||||
};
|
||||
this->texture->buffer(pixels);
|
||||
|
||||
this->updatePositions();
|
||||
}
|
||||
|
||||
void UIBorder::updatePositions() {
|
||||
UIComponent::updatePositions();
|
||||
|
||||
glm::vec2 oneThird = this->uv1 / 3.0f;
|
||||
glm::vec2 overallDimensions = glm::vec2(this->getWidth(), this->getHeight());
|
||||
glm::vec2 innerDimensions = overallDimensions - (this->edgeDimensions * 2.0f);
|
||||
|
||||
// Top Left.
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
glm::vec2(0, 0),
|
||||
this->uv0,
|
||||
edgeDimensions,
|
||||
this->uv0 + oneThird,
|
||||
0, 0
|
||||
);
|
||||
|
||||
// Top Center
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
glm::vec2(edgeDimensions.x, 0),
|
||||
this->uv0 + glm::vec2(oneThird.x, 0),
|
||||
glm::vec2(edgeDimensions.x + innerDimensions.x, edgeDimensions.y),
|
||||
this->uv0 + glm::vec2(oneThird.x * 2.0f, oneThird.y),
|
||||
QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT
|
||||
);
|
||||
|
||||
// Top Right
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
glm::vec2(edgeDimensions.x + innerDimensions.x, 0),
|
||||
this->uv0 + glm::vec2(oneThird.x * 2.0f, 0),
|
||||
glm::vec2(overallDimensions.x, edgeDimensions.y),
|
||||
glm::vec2(this->uv1.x, this->uv0.y + oneThird.y),
|
||||
QUAD_VERTICE_COUNT * 2, QUAD_INDICE_COUNT * 2
|
||||
);
|
||||
|
||||
// Middle Left
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
glm::vec2(0, edgeDimensions.y),
|
||||
this->uv0 + glm::vec2(0, oneThird.y),
|
||||
glm::vec2(edgeDimensions.x, edgeDimensions.y + innerDimensions.y),
|
||||
this->uv0 + glm::vec2(oneThird.x, oneThird.y * 2.0f),
|
||||
QUAD_VERTICE_COUNT * 3, QUAD_INDICE_COUNT * 3
|
||||
);
|
||||
|
||||
// Center
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
edgeDimensions,
|
||||
this->uv0 + oneThird,
|
||||
edgeDimensions + innerDimensions,
|
||||
this->uv0 + (oneThird * 2.0f),
|
||||
QUAD_VERTICE_COUNT * 4, QUAD_INDICE_COUNT * 4
|
||||
);
|
||||
|
||||
// Middle Right
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
edgeDimensions + glm::vec2(innerDimensions.x, 0),
|
||||
this->uv0 + glm::vec2(oneThird.x * 2.0f, oneThird.y),
|
||||
edgeDimensions + innerDimensions + glm::vec2(edgeDimensions.x, 0),
|
||||
this->uv1 - glm::vec2(0.0f, oneThird.y),
|
||||
QUAD_VERTICE_COUNT * 5, QUAD_INDICE_COUNT * 5
|
||||
);
|
||||
|
||||
// Bottom Left
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
glm::vec2(0.0f, edgeDimensions.y + innerDimensions.y),
|
||||
this->uv0 + glm::vec2(0.0f, uv1.y - oneThird.y),
|
||||
glm::vec2(edgeDimensions.x, overallDimensions.y),
|
||||
this->uv1 - glm::vec2(oneThird.x * 2.0f, 0.0f),
|
||||
QUAD_VERTICE_COUNT * 6, QUAD_INDICE_COUNT * 6
|
||||
);
|
||||
|
||||
// Bottom Center
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
edgeDimensions + glm::vec2(0.0f, innerDimensions.y),
|
||||
this->uv1 - oneThird,
|
||||
overallDimensions - glm::vec2(edgeDimensions.x, 0.0f),
|
||||
this->uv1 - glm::vec2(oneThird.x, 0.0f),
|
||||
QUAD_VERTICE_COUNT * 7, QUAD_INDICE_COUNT * 7
|
||||
);
|
||||
|
||||
// Bottom Right
|
||||
QuadMesh::bufferQuadMesh(this->mesh,
|
||||
overallDimensions - edgeDimensions,
|
||||
this->uv1 - oneThird,
|
||||
overallDimensions,
|
||||
this->uv1,
|
||||
QUAD_VERTICE_COUNT * 8, QUAD_INDICE_COUNT * 8
|
||||
);
|
||||
}
|
||||
|
||||
void UIBorder::drawSelf(UIShader &shader, glm::mat4 transform) {
|
||||
if(this->texture == nullptr) return;
|
||||
|
||||
shader.setUIColor(COLOR_WHITE);
|
||||
shader.setUIModel(transform);
|
||||
shader.setUITexture(this->texture);
|
||||
this->mesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
|
||||
}
|
||||
|
||||
void UIBorder::setBorderSize(glm::vec2 borderSize) {
|
||||
this->edgeDimensions = borderSize;
|
||||
this->updatePositions();
|
||||
}
|
||||
|
||||
glm::vec2 UIBorder::getBorderSize() {
|
||||
return this->edgeDimensions;
|
||||
}
|
30
src/dawn/ui/UIBorder.hpp
Normal file
30
src/dawn/ui/UIBorder.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
// 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"
|
||||
#include "display/Texture.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class UIBorder : public UIComponent {
|
||||
private:
|
||||
Mesh mesh;
|
||||
glm::vec2 edgeDimensions = glm::vec2(8.0f, 8.0f);
|
||||
glm::vec2 uv0 = glm::vec2(0.0f, 0.0f);
|
||||
glm::vec2 uv1 = glm::vec2(1.0f, 1.0f);
|
||||
|
||||
void updatePositions() override;
|
||||
|
||||
public:
|
||||
Texture *texture;
|
||||
|
||||
UIBorder(UICanvas &canvas);
|
||||
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
|
||||
|
||||
void setBorderSize(glm::vec2 borderSize);
|
||||
glm::vec2 getBorderSize();
|
||||
};
|
||||
}
|
@ -22,9 +22,26 @@ void UIComponent::updatePositions() {
|
||||
}
|
||||
this->relativeX = this->alignment[0];
|
||||
this->width -= (this->alignment[0] + this->alignment[2]);
|
||||
} else {
|
||||
} else if(this->alignX == UI_COMPONENT_ALIGN_START) {
|
||||
this->relativeX = this->alignment[0];
|
||||
this->width = this->alignment[2];
|
||||
} else if(this->alignX == UI_COMPONENT_ALIGN_END) {
|
||||
this->width = this->alignment[0];
|
||||
if(this->parent == nullptr) {
|
||||
this->relativeX = this->canvas.getWidth();
|
||||
} else {
|
||||
this->relativeX = this->parent->getWidth();
|
||||
}
|
||||
this->relativeX -= this->width;
|
||||
this->relativeX -= this->alignment[2];
|
||||
} else if(this->alignX == UI_COMPONENT_ALIGN_MIDDLE) {
|
||||
this->width = this->alignment[2];
|
||||
if(this->parent == nullptr) {
|
||||
this->relativeX = this->canvas.getWidth();
|
||||
} else {
|
||||
this->relativeX = this->parent->getWidth();
|
||||
}
|
||||
this->relativeX = (this->relativeX / 2.0f) - (this->width / 2.0f) + this->alignment[0];
|
||||
}
|
||||
|
||||
// Y Alignment
|
||||
@ -36,9 +53,26 @@ void UIComponent::updatePositions() {
|
||||
}
|
||||
this->relativeY = this->alignment[1];
|
||||
this->height -= (this->alignment[1] + this->alignment[3]);
|
||||
} else {
|
||||
} else if(this->alignY == UI_COMPONENT_ALIGN_START) {
|
||||
this->relativeY = this->alignment[1];
|
||||
this->height = this->alignment[3];
|
||||
} else if(this->alignY == UI_COMPONENT_ALIGN_END) {
|
||||
this->height = this->alignment[1];
|
||||
if(this->parent == nullptr) {
|
||||
this->relativeY = this->canvas.getHeight();
|
||||
} else {
|
||||
this->relativeY = this->parent->getHeight();
|
||||
}
|
||||
this->relativeY -= this->height;
|
||||
this->relativeY -= this->alignment[3];
|
||||
} else if(this->alignY == UI_COMPONENT_ALIGN_MIDDLE) {
|
||||
this->height = this->alignment[3];
|
||||
if(this->parent == nullptr) {
|
||||
this->relativeY = this->canvas.getHeight();
|
||||
} else {
|
||||
this->relativeY = this->parent->getHeight();
|
||||
}
|
||||
this->relativeY = (this->relativeY / 2.0f) - (this->height / 2.0f) + this->alignment[1];
|
||||
}
|
||||
|
||||
// Update children
|
||||
@ -95,18 +129,18 @@ void UIComponent::draw(UIShader &uiShader, glm::mat4 parentTransform) {
|
||||
}
|
||||
}
|
||||
|
||||
void UIComponent::addChild(std::shared_ptr<UIComponent> child) {
|
||||
void UIComponent::addChild(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) {
|
||||
void UIComponent::removeChild(UIComponent *child) {
|
||||
if(child->parent != this) throw "Invalid child";
|
||||
auto it = this->children.begin();
|
||||
while(it != this->children.end()) {
|
||||
if(it->get() == child.get()) {
|
||||
if(*it == child) {
|
||||
this->children.erase(it);
|
||||
break;
|
||||
}
|
||||
|
@ -28,9 +28,9 @@ namespace Dawn {
|
||||
// Setting values
|
||||
UIComponentAlign alignX = UI_COMPONENT_ALIGN_START;
|
||||
UIComponentAlign alignY = UI_COMPONENT_ALIGN_START;
|
||||
glm::vec4 alignment = glm::vec4(0, 0, 1, 1);
|
||||
glm::vec4 alignment = glm::vec4(0, 0, 32, 32);
|
||||
float_t z = 0;
|
||||
std::vector<std::shared_ptr<UIComponent>> children;
|
||||
std::vector<UIComponent*> children;
|
||||
UIComponent *parent = nullptr;
|
||||
|
||||
// I currently don't support rotation or scale. Not because I can't but
|
||||
@ -51,6 +51,11 @@ namespace Dawn {
|
||||
|
||||
UIComponent(UICanvas &canvas);
|
||||
|
||||
/**
|
||||
* Returns the calculated width, based on the internal alignment values.
|
||||
*
|
||||
* @return Width of the component.
|
||||
*/
|
||||
float_t getWidth();
|
||||
float_t getHeight();
|
||||
float_t getRelativeX();
|
||||
@ -67,9 +72,11 @@ namespace Dawn {
|
||||
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);
|
||||
void addChild(UIComponent *child);
|
||||
void removeChild(UIComponent *child);
|
||||
|
||||
virtual ~UIComponent();
|
||||
|
||||
friend class UICanvas;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user