diff --git a/src/dawn/display/Color.hpp b/src/dawn/display/Color.hpp index aee9147f..466d8ca5 100644 --- a/src/dawn/display/Color.hpp +++ b/src/dawn/display/Color.hpp @@ -47,15 +47,15 @@ namespace Dawn { } }; - #define COLOR_WHITE { 255, 255, 255, 100 } - #define COLOR_RED { 255, 0, 0, 100 } - #define COLOR_GREEN { 0, 255, 0, 100 } - #define COLOR_BLUE { 0, 0, 255, 100 } - #define COLOR_BLACK { 0, 0, 0, 100 } - #define COLOR_MAGENTA { 255, 0, 255, 100 } - #define COLOR_DARK_GREY { 50, 50, 50, 100 } - #define COLOR_LIGHT_GREY { 204, 204, 204, 100 } - #define COLOR_CORNFLOWER_BLUE { 100, 149, 237, 100 } + #define COLOR_WHITE { 255, 255, 255, 255 } + #define COLOR_RED { 255, 0, 0, 255 } + #define COLOR_GREEN { 0, 255, 0, 255 } + #define COLOR_BLUE { 0, 0, 255, 255 } + #define COLOR_BLACK { 0, 0, 0, 255 } + #define COLOR_MAGENTA { 255, 0, 255, 255 } + #define COLOR_DARK_GREY { 50, 50, 50, 255 } + #define COLOR_LIGHT_GREY { 204, 204, 204, 255 } + #define COLOR_CORNFLOWER_BLUE { 100, 149, 237, 255 } #define COLOR_WHITE_TRANSPARENT { 255, 255, 255, 0 } #define COLOR_BLACK_TRANSPARENT { 0, 0, 0, 0 } #define COLOR_TRANSPARENT COLOR_BLACK_TRANSPARENT diff --git a/src/dawn/scene/SceneItem.hpp b/src/dawn/scene/SceneItem.hpp index 06c7a4e2..37489e97 100644 --- a/src/dawn/scene/SceneItem.hpp +++ b/src/dawn/scene/SceneItem.hpp @@ -149,11 +149,11 @@ namespace Dawn { std::vector itemsFound; while(transformsToCheck.size() > 0) { - auto tras = transformsToCheck.begin(); - vectorAppend(&transformsToCheck, (*tras)->children); - auto component = (*tras)->item->getComponent(); + Transform *tras = *transformsToCheck.begin(); + vectorAppend(&transformsToCheck, tras->children); + auto component = tras->item->getComponent(); if(component != nullptr) itemsFound.push_back(component); - transformsToCheck.erase(tras); + transformsToCheck.erase(transformsToCheck.begin()); } return itemsFound; diff --git a/src/dawn/scene/components/ui/CMakeLists.txt b/src/dawn/scene/components/ui/CMakeLists.txt index 0fa3da19..2c0b9329 100644 --- a/src/dawn/scene/components/ui/CMakeLists.txt +++ b/src/dawn/scene/components/ui/CMakeLists.txt @@ -10,6 +10,7 @@ target_sources(${DAWN_TARGET_NAME} UIComponent.cpp UILabel.cpp UIImage.cpp + UIBorder.cpp ) add_subdirectory(menu) \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIBorder.cpp b/src/dawn/scene/components/ui/UIBorder.cpp new file mode 100644 index 00000000..d823fd40 --- /dev/null +++ b/src/dawn/scene/components/ui/UIBorder.cpp @@ -0,0 +1,119 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UIBorder.hpp" +#include "game/DawnGame.hpp" + +using namespace Dawn; + +UIBorder::UIBorder(SceneItem *item) : + texture(nullptr), + borderSize(glm::vec2(8, 8)), + UIComponent(item) +{ +} + +float_t UIBorder::getContentWidth() { + if(this->texture != nullptr) return this->texture->getWidth(); + return this->width; +} + +float_t UIBorder::getContentHeight() { + if(this->texture != nullptr) return this->texture->getHeight(); + return this->height; +} + +std::vector UIBorder::getPassItems( + glm::mat4 proj, glm::mat4 view +) { + struct ShaderPassItem item; + auto shader = &getGame()->renderManager.uiShader->program; + item.shaderProgram = shader; + item.colorValues[shader->paramColor] = COLOR_WHITE; + item.matrixValues[shader->paramProjection] = proj; + item.matrixValues[shader->paramView] = view; + item.matrixValues[shader->paramModel] = this->transform->getWorldTransform(); + if(this->texture == nullptr) { + item.boolValues[shader->paramHasTexture] = false; + } else { + item.boolValues[shader->paramHasTexture] = true; + item.textureSlots[0] = this->texture; + item.textureValues[shader->paramTexture] = 0; + } + item.w = this->transform->getWorldPosition().z; + item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND; + item.mesh = &mesh; + + return { item }; +} + +void UIBorder::onStart() { + UIComponent::onStart(); + + auto rebufferQuad = [&] { + glm::vec2 tSize = glm::vec2(1, 1) / 3.0f; + glm::vec2 bSize = (glm::vec2)borderSize; + glm::vec2 iSize = glm::vec2(this->getWidth(), this->getHeight()) - ( bSize * 2.0f ); + + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(0, 0), glm::vec2(0, 0), + bSize, tSize, + 0, 0 + ); + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(bSize.x, 0), glm::vec2(tSize.x, 0), + glm::vec2(iSize.x + bSize.x, bSize.y), glm::vec2(tSize.x * 2, tSize.y), + QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT + ); + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(iSize.x + bSize.x, 0), glm::vec2(tSize.x + tSize.x, 0), + glm::vec2(this->getWidth(), bSize.y), glm::vec2(1.0f, tSize.y), + 2 * QUAD_VERTICE_COUNT, 2 * QUAD_INDICE_COUNT + ); + + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(0, bSize.y), glm::vec2(0, tSize.y), + bSize + glm::vec2(0, iSize.y), tSize + glm::vec2(0, tSize.y), + 3 * QUAD_VERTICE_COUNT, 3 * QUAD_INDICE_COUNT + ); + QuadMesh::bufferQuadMesh(&mesh, + bSize, tSize, + bSize + iSize, tSize + tSize, + 4 * QUAD_VERTICE_COUNT, 4 * QUAD_INDICE_COUNT + ); + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(iSize.x + bSize.x, 0), tSize + glm::vec2(tSize.x, 0), + glm::vec2(this->getWidth(), bSize.y + iSize.y), glm::vec2(1.0f, tSize.y + tSize.y), + 5 * QUAD_VERTICE_COUNT, 5 * QUAD_INDICE_COUNT + ); + + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(0, iSize.y + bSize.y), glm::vec2(0, tSize.y + tSize.y), + glm::vec2(bSize.x, this->getHeight()), glm::vec2(tSize.x, 1.0f), + 6 * QUAD_VERTICE_COUNT, 6 * QUAD_INDICE_COUNT + ); + QuadMesh::bufferQuadMesh(&mesh, + glm::vec2(bSize.x, iSize.y + bSize.y), glm::vec2(tSize.x, tSize.y + tSize.y), + glm::vec2(iSize.x + bSize.x, this->getHeight()), glm::vec2(tSize.x * 2, 1.0f), + 7 * QUAD_VERTICE_COUNT, 7 * QUAD_INDICE_COUNT + ); + QuadMesh::bufferQuadMesh(&mesh, + bSize + iSize, tSize + tSize, + glm::vec2(this->getWidth(), this->getHeight()), glm::vec2(1.0f, 1.0f), + 8 * QUAD_VERTICE_COUNT, 8 * QUAD_INDICE_COUNT + ); + }; + + this->mesh.createBuffers( + QUAD_VERTICE_COUNT * UI_BORDER_QUAD_COUNT, + QUAD_INDICE_COUNT * UI_BORDER_QUAD_COUNT + ); + rebufferQuad(); + + useEvent(rebufferQuad, this->eventAlignmentUpdated); + useEffect([&]{ + this->alignmentNeedsUpdating = true; + }, this->borderSize); +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIBorder.hpp b/src/dawn/scene/components/ui/UIBorder.hpp new file mode 100644 index 00000000..91d85e44 --- /dev/null +++ b/src/dawn/scene/components/ui/UIBorder.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2023 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" + +#define UI_BORDER_QUAD_COUNT 9 + +namespace Dawn { + class UIBorder : public UIComponent, public UIComponentRenderable { + private: + Mesh mesh; + + public: + // @optional + StateProperty borderSize; + // @optional + StateProperty texture; + + UIBorder(SceneItem *item); + + float_t getContentWidth() override; + float_t getContentHeight() override; + std::vector getPassItems( + glm::mat4 proj, glm::mat4 view + ) override; + void onStart() override; + }; +} \ No newline at end of file diff --git a/src/dawnliminal/scenes/HelloWorldScene.hpp b/src/dawnliminal/scenes/HelloWorldScene.hpp index fe34516c..8f2cd6ce 100644 --- a/src/dawnliminal/scenes/HelloWorldScene.hpp +++ b/src/dawnliminal/scenes/HelloWorldScene.hpp @@ -14,6 +14,7 @@ namespace Dawn { protected: Camera *camera; UICanvas *canvas; + Texture text; int32_t test = 0; @@ -28,6 +29,17 @@ namespace Dawn { auto textbox = VNTextbox::create(this); textbox->transform.setParent(canvas->transform); + + struct Color colors[] = { + COLOR_RED, COLOR_MAGENTA, COLOR_RED, + COLOR_MAGENTA, COLOR_CORNFLOWER_BLUE, COLOR_MAGENTA, + COLOR_RED, COLOR_MAGENTA, COLOR_RED + }; + text.setSize(3, 3); + text.buffer(colors); + textbox->border->texture = &text; + + textbox->border->alignment = glm::vec4(32, 32, 128, 128); } std::vector getRequiredAssets() override {