From f7ff21fa8df14b689482ab07c7f2dd9b7f8e5b40 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 5 Mar 2023 12:58:59 -0800 Subject: [PATCH] UI --- {src/dawn => archive}/ui/CMakeLists.txt | 0 {src/dawn => archive}/ui/UIBorder.cpp | 0 {src/dawn => archive}/ui/UIBorder.hpp | 0 {src/dawn => archive}/ui/UIComponent.cpp | 0 {src/dawn => archive}/ui/UIComponent.hpp | 0 {src/dawn => archive}/ui/UIEmpty.cpp | 38 +-- {src/dawn => archive}/ui/UIEmpty.hpp | 40 +-- {src/dawn => archive}/ui/UIGrid.cpp | 258 +++++++++---------- {src/dawn => archive}/ui/UIGrid.hpp | 164 ++++++------ {src/dawn => archive}/ui/UILabel.cpp | 0 {src/dawn => archive}/ui/UILabel.hpp | 0 {src/dawn => archive}/ui/UIMenu.cpp | 0 {src/dawn => archive}/ui/UIMenu.hpp | 248 +++++++++--------- {src/dawn => archive}/ui/UISprite.cpp | 0 {src/dawn => archive}/ui/UISprite.hpp | 0 src/dawn/CMakeLists.txt | 1 - src/dawn/display/RenderPipeline.cpp | 18 +- src/dawn/display/RenderPipeline.hpp | 3 +- src/dawn/scene/components/ui/UICanvas.cpp | 64 +---- src/dawn/scene/components/ui/UICanvas.hpp | 74 +----- src/dawn/scene/components/ui/UIComponent.cpp | 12 + src/dawn/scene/components/ui/UIComponent.hpp | 20 ++ src/dawn/scene/components/ui/UILabel.cpp | 49 ++++ src/dawn/scene/components/ui/UILabel.hpp | 32 +++ 24 files changed, 501 insertions(+), 520 deletions(-) rename {src/dawn => archive}/ui/CMakeLists.txt (100%) rename {src/dawn => archive}/ui/UIBorder.cpp (100%) rename {src/dawn => archive}/ui/UIBorder.hpp (100%) rename {src/dawn => archive}/ui/UIComponent.cpp (100%) rename {src/dawn => archive}/ui/UIComponent.hpp (100%) rename {src/dawn => archive}/ui/UIEmpty.cpp (95%) rename {src/dawn => archive}/ui/UIEmpty.hpp (95%) rename {src/dawn => archive}/ui/UIGrid.cpp (95%) rename {src/dawn => archive}/ui/UIGrid.hpp (96%) rename {src/dawn => archive}/ui/UILabel.cpp (100%) rename {src/dawn => archive}/ui/UILabel.hpp (100%) rename {src/dawn => archive}/ui/UIMenu.cpp (100%) rename {src/dawn => archive}/ui/UIMenu.hpp (96%) rename {src/dawn => archive}/ui/UISprite.cpp (100%) rename {src/dawn => archive}/ui/UISprite.hpp (100%) create mode 100644 src/dawn/scene/components/ui/UIComponent.cpp create mode 100644 src/dawn/scene/components/ui/UIComponent.hpp create mode 100644 src/dawn/scene/components/ui/UILabel.cpp create mode 100644 src/dawn/scene/components/ui/UILabel.hpp diff --git a/src/dawn/ui/CMakeLists.txt b/archive/ui/CMakeLists.txt similarity index 100% rename from src/dawn/ui/CMakeLists.txt rename to archive/ui/CMakeLists.txt diff --git a/src/dawn/ui/UIBorder.cpp b/archive/ui/UIBorder.cpp similarity index 100% rename from src/dawn/ui/UIBorder.cpp rename to archive/ui/UIBorder.cpp diff --git a/src/dawn/ui/UIBorder.hpp b/archive/ui/UIBorder.hpp similarity index 100% rename from src/dawn/ui/UIBorder.hpp rename to archive/ui/UIBorder.hpp diff --git a/src/dawn/ui/UIComponent.cpp b/archive/ui/UIComponent.cpp similarity index 100% rename from src/dawn/ui/UIComponent.cpp rename to archive/ui/UIComponent.cpp diff --git a/src/dawn/ui/UIComponent.hpp b/archive/ui/UIComponent.hpp similarity index 100% rename from src/dawn/ui/UIComponent.hpp rename to archive/ui/UIComponent.hpp diff --git a/src/dawn/ui/UIEmpty.cpp b/archive/ui/UIEmpty.cpp similarity index 95% rename from src/dawn/ui/UIEmpty.cpp rename to archive/ui/UIEmpty.cpp index f59fcb40..fe609ef0 100644 --- a/src/dawn/ui/UIEmpty.cpp +++ b/archive/ui/UIEmpty.cpp @@ -1,20 +1,20 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "UIEmpty.hpp" - -using namespace Dawn; - -UIEmpty::UIEmpty(UICanvas *canvas) : UIComponent(canvas) { - -} - -std::vector UIEmpty::getSelfPassItems( - glm::mat4 projection, - glm::mat4 view, - glm::mat4 transform -) { - return std::vector(); +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UIEmpty.hpp" + +using namespace Dawn; + +UIEmpty::UIEmpty(UICanvas *canvas) : UIComponent(canvas) { + +} + +std::vector UIEmpty::getSelfPassItems( + glm::mat4 projection, + glm::mat4 view, + glm::mat4 transform +) { + return std::vector(); } \ No newline at end of file diff --git a/src/dawn/ui/UIEmpty.hpp b/archive/ui/UIEmpty.hpp similarity index 95% rename from src/dawn/ui/UIEmpty.hpp rename to archive/ui/UIEmpty.hpp index 7bb7dc32..48e0e611 100644 --- a/src/dawn/ui/UIEmpty.hpp +++ b/archive/ui/UIEmpty.hpp @@ -1,21 +1,21 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "UIComponent.hpp" - -namespace Dawn { - class UIEmpty : public UIComponent { - protected: - std::vector getSelfPassItems( - glm::mat4 projection, - glm::mat4 view, - glm::mat4 transform - ) override; - - public: - UIEmpty(UICanvas *canvas); - }; +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "UIComponent.hpp" + +namespace Dawn { + class UIEmpty : public UIComponent { + protected: + std::vector getSelfPassItems( + glm::mat4 projection, + glm::mat4 view, + glm::mat4 transform + ) override; + + public: + UIEmpty(UICanvas *canvas); + }; } \ No newline at end of file diff --git a/src/dawn/ui/UIGrid.cpp b/archive/ui/UIGrid.cpp similarity index 95% rename from src/dawn/ui/UIGrid.cpp rename to archive/ui/UIGrid.cpp index f4b66b32..0dde4713 100644 --- a/src/dawn/ui/UIGrid.cpp +++ b/archive/ui/UIGrid.cpp @@ -1,130 +1,130 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "UIGrid.hpp" - -using namespace Dawn; - -UIGrid::UIGrid(UICanvas *canvas) : UIComponent(canvas) { - -} - -void UIGrid::updatePositions() { - UIComponent::updatePositions(); - - this->sizeCol = ( - this->width - (this->gutterX * (this->columns - 1)) - ) / this->columns; - this->sizeRow = ( - this->height - (this->gutterY * (this->rows - 1)) - ) / this->rows; - - auto it = this->gridChildren.begin(); - while(it != this->gridChildren.end()) { - this->alignChild(it->first, it->second); - ++it; - } -} - -std::vector UIGrid::getSelfPassItems( - glm::mat4 projection, - glm::mat4 view, - glm::mat4 transform -) { - return std::vector(); -} - -void UIGrid::onChildAligned(UIComponent *child) { - if(this->alignmentListenerLocked) return; - assertNotNull(child); - assertMapHasKey(this->gridChildren, child); - this->alignmentListenerLocked = true; - this->alignChild(child, this->gridChildren[child]); - this->alignmentListenerLocked = false; -} - -void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) { - assertNotNull(child); - - float_t gridX = (this->sizeCol * pos.x) + (this->gutterX * pos.x); - float_t gridY = (this->sizeRow * pos.y) + (this->gutterY * pos.y); - - // Hack for when content width is undefined. - child->width = this->sizeCol; - child->height = this->sizeRow; - - // Alignment - float_t x, y, sizeX, sizeY; - UIComponent::calculateDimensions( - pos.alignX, - &x, - &sizeX, - this->sizeCol, - child->getContentWidth(), - glm::vec2(0, 0) - ); - UIComponent::calculateDimensions( - pos.alignY, - &y, - &sizeY, - this->sizeRow, - child->getContentHeight(), - glm::vec2(0, 0) - ); - - child->setTransform( - UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START, - glm::vec4(gridX + x, gridY + y, sizeX, sizeY), - 0.0f - ); -} - -void UIGrid::setGridSize( - int32_t columns, int32_t rows, - float_t gutterX, float_t gutterY -) { - this->rows = rows; - this->columns = columns; - this->gutterX = gutterX; - this->gutterY = gutterY; - - // TODO: Need to fix children here. - - this->gridChildren.clear(); - this->updatePositions(); -} - -void UIGrid::addToGrid( - UIComponent *ui, - int32_t x, int32_t y, - enum UIComponentAlign alignX, enum UIComponentAlign alignY -) { - assertTrue(x >= 0 && x < this->columns); - assertTrue(y >= 0 && y < this->rows); - this->addChild(ui); - struct UIGridPosition pos; - pos.x = x; - pos.y = y; - pos.alignX = alignX; - pos.alignY = alignY; - this->gridChildren[ui] = pos; - this->alignChild(ui, pos); - - // Re-Add event listener - ui->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned); -} - -int32_t UIGrid::getRows() { - return this->rows; -} - -int32_t UIGrid::getColumns() { - return this->columns; -} - -void UIGrid::removeChild(UIComponent *component) { - UIComponent::removeChild(component); - assertUnreachable(); +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UIGrid.hpp" + +using namespace Dawn; + +UIGrid::UIGrid(UICanvas *canvas) : UIComponent(canvas) { + +} + +void UIGrid::updatePositions() { + UIComponent::updatePositions(); + + this->sizeCol = ( + this->width - (this->gutterX * (this->columns - 1)) + ) / this->columns; + this->sizeRow = ( + this->height - (this->gutterY * (this->rows - 1)) + ) / this->rows; + + auto it = this->gridChildren.begin(); + while(it != this->gridChildren.end()) { + this->alignChild(it->first, it->second); + ++it; + } +} + +std::vector UIGrid::getSelfPassItems( + glm::mat4 projection, + glm::mat4 view, + glm::mat4 transform +) { + return std::vector(); +} + +void UIGrid::onChildAligned(UIComponent *child) { + if(this->alignmentListenerLocked) return; + assertNotNull(child); + assertMapHasKey(this->gridChildren, child); + this->alignmentListenerLocked = true; + this->alignChild(child, this->gridChildren[child]); + this->alignmentListenerLocked = false; +} + +void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) { + assertNotNull(child); + + float_t gridX = (this->sizeCol * pos.x) + (this->gutterX * pos.x); + float_t gridY = (this->sizeRow * pos.y) + (this->gutterY * pos.y); + + // Hack for when content width is undefined. + child->width = this->sizeCol; + child->height = this->sizeRow; + + // Alignment + float_t x, y, sizeX, sizeY; + UIComponent::calculateDimensions( + pos.alignX, + &x, + &sizeX, + this->sizeCol, + child->getContentWidth(), + glm::vec2(0, 0) + ); + UIComponent::calculateDimensions( + pos.alignY, + &y, + &sizeY, + this->sizeRow, + child->getContentHeight(), + glm::vec2(0, 0) + ); + + child->setTransform( + UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START, + glm::vec4(gridX + x, gridY + y, sizeX, sizeY), + 0.0f + ); +} + +void UIGrid::setGridSize( + int32_t columns, int32_t rows, + float_t gutterX, float_t gutterY +) { + this->rows = rows; + this->columns = columns; + this->gutterX = gutterX; + this->gutterY = gutterY; + + // TODO: Need to fix children here. + + this->gridChildren.clear(); + this->updatePositions(); +} + +void UIGrid::addToGrid( + UIComponent *ui, + int32_t x, int32_t y, + enum UIComponentAlign alignX, enum UIComponentAlign alignY +) { + assertTrue(x >= 0 && x < this->columns); + assertTrue(y >= 0 && y < this->rows); + this->addChild(ui); + struct UIGridPosition pos; + pos.x = x; + pos.y = y; + pos.alignX = alignX; + pos.alignY = alignY; + this->gridChildren[ui] = pos; + this->alignChild(ui, pos); + + // Re-Add event listener + ui->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned); +} + +int32_t UIGrid::getRows() { + return this->rows; +} + +int32_t UIGrid::getColumns() { + return this->columns; +} + +void UIGrid::removeChild(UIComponent *component) { + UIComponent::removeChild(component); + assertUnreachable(); } \ No newline at end of file diff --git a/src/dawn/ui/UIGrid.hpp b/archive/ui/UIGrid.hpp similarity index 96% rename from src/dawn/ui/UIGrid.hpp rename to archive/ui/UIGrid.hpp index 685678b2..a201eb68 100644 --- a/src/dawn/ui/UIGrid.hpp +++ b/archive/ui/UIGrid.hpp @@ -1,83 +1,83 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "UIComponent.hpp" -#include "util/mathutils.hpp" - -namespace Dawn { - struct UIGridPosition { - int32_t x; - int32_t y; - enum UIComponentAlign alignX; - enum UIComponentAlign alignY; - }; - - - class UIGrid : public UIComponent { - private: - int32_t rows = 1; - int32_t columns = 1; - float_t gutterX = 0; - float_t gutterY = 0; - float_t sizeRow, sizeCol; - std::map gridChildren; - bool_t alignmentListenerLocked = false; - - /** - * Internal method to update the alignment of a child. - * - * @param child Child UI component. - * @param pos Positional information of the child UI item.. - */ - void alignChild(UIComponent *child, struct UIGridPosition pos); - - void onChildAligned(UIComponent *child); - - protected: - void updatePositions() override; - std::vector getSelfPassItems( - glm::mat4 projection, - glm::mat4 view, - glm::mat4 transform - ) override; - - public: - UIGrid(UICanvas *canvas); - - /** - * Sets the dimensions of the grid. - * - * @param columns Count of columns in the grid. - * @param rows Count of rows in the grid. - * @param gutterX Gutter spacing between the cells of the grid. - * @param gutterY Gutter spacing between the cells of the grid. - */ - void setGridSize( - int32_t columns, int32_t rows, - float_t gutterX, float_t gutterY - ); - - /** - * Adds a UI component to the grid. - * - * @param component Component to add to the grid. - * @param column Column Position. - * @param row Row Position. - * @param alignX X alignment of the component within the cell. - * @param alignY Y alignment of the component within the cell. - */ - void addToGrid( - UIComponent *ui, - int32_t x, int32_t y, - enum UIComponentAlign alignX, enum UIComponentAlign alignY - ); - - int32_t getRows(); - int32_t getColumns(); - - void removeChild(UIComponent *component) override; - }; +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "UIComponent.hpp" +#include "util/mathutils.hpp" + +namespace Dawn { + struct UIGridPosition { + int32_t x; + int32_t y; + enum UIComponentAlign alignX; + enum UIComponentAlign alignY; + }; + + + class UIGrid : public UIComponent { + private: + int32_t rows = 1; + int32_t columns = 1; + float_t gutterX = 0; + float_t gutterY = 0; + float_t sizeRow, sizeCol; + std::map gridChildren; + bool_t alignmentListenerLocked = false; + + /** + * Internal method to update the alignment of a child. + * + * @param child Child UI component. + * @param pos Positional information of the child UI item.. + */ + void alignChild(UIComponent *child, struct UIGridPosition pos); + + void onChildAligned(UIComponent *child); + + protected: + void updatePositions() override; + std::vector getSelfPassItems( + glm::mat4 projection, + glm::mat4 view, + glm::mat4 transform + ) override; + + public: + UIGrid(UICanvas *canvas); + + /** + * Sets the dimensions of the grid. + * + * @param columns Count of columns in the grid. + * @param rows Count of rows in the grid. + * @param gutterX Gutter spacing between the cells of the grid. + * @param gutterY Gutter spacing between the cells of the grid. + */ + void setGridSize( + int32_t columns, int32_t rows, + float_t gutterX, float_t gutterY + ); + + /** + * Adds a UI component to the grid. + * + * @param component Component to add to the grid. + * @param column Column Position. + * @param row Row Position. + * @param alignX X alignment of the component within the cell. + * @param alignY Y alignment of the component within the cell. + */ + void addToGrid( + UIComponent *ui, + int32_t x, int32_t y, + enum UIComponentAlign alignX, enum UIComponentAlign alignY + ); + + int32_t getRows(); + int32_t getColumns(); + + void removeChild(UIComponent *component) override; + }; } \ No newline at end of file diff --git a/src/dawn/ui/UILabel.cpp b/archive/ui/UILabel.cpp similarity index 100% rename from src/dawn/ui/UILabel.cpp rename to archive/ui/UILabel.cpp diff --git a/src/dawn/ui/UILabel.hpp b/archive/ui/UILabel.hpp similarity index 100% rename from src/dawn/ui/UILabel.hpp rename to archive/ui/UILabel.hpp diff --git a/src/dawn/ui/UIMenu.cpp b/archive/ui/UIMenu.cpp similarity index 100% rename from src/dawn/ui/UIMenu.cpp rename to archive/ui/UIMenu.cpp diff --git a/src/dawn/ui/UIMenu.hpp b/archive/ui/UIMenu.hpp similarity index 96% rename from src/dawn/ui/UIMenu.hpp rename to archive/ui/UIMenu.hpp index e3357f76..eaccbd64 100644 --- a/src/dawn/ui/UIMenu.hpp +++ b/archive/ui/UIMenu.hpp @@ -1,125 +1,125 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "event/Event.hpp" -#include "ui/UIComponent.hpp" -#include "util/memory.hpp" - -namespace Dawn { - class UIMenuItem { - public: - /** - * Called when the item is selected (Accepted) on. - */ - virtual void onItemSelected() = 0; - - /** - * Called when either the mouse or the user controller input is removed - * off this item. - */ - virtual void onItemOver() = 0; - - /** - * Called when either the mouth or the user controller input is put over - * this item. - */ - virtual void onItemOff() = 0; - - /** - * Returns whether the UI item can be hovered overed or not. - * @return Whether it can be overed or not. - */ - virtual bool_t canBeOvered() = 0; - - /** - * Returns whether or not the item can be selected or not. - * @return Whether it can be selected or not. - */ - virtual bool_t canBeSelected() = 0; - }; - - struct UIMenu { - private: - UICanvas *canvas; - int32_t x = -1; - int32_t y = -1; - int32_t rows = 1; - int32_t columns = 1; - UIMenuItem **items = nullptr; - - protected: - /** Invoked by UICanvas when this menu has been made inactive. */ - void onInactive(); - /** Invoked by UICanvas when this menu has been made active. */ - void onActive(); - /** Invoked by UICanvas every tick this menu is active. */ - void onTick(); - - public: - Event<> eventMenuActive; - Event<> eventMenuInactive; - Event eventCursorChange; - Event eventItemSelected; - - /** - * Construct a new UI Menu Host. - * - * @param canvas Canvas that this menu belongs to. - * @param columns Iniitial size of the menu X axis. - * @param rows Initial size of the menu Y axis. - */ - UIMenu(UICanvas *canvas, int32_t columns, int32_t rows); - - /** - * Sets the size of the UI Menu. - * - * @param columns How many columns in the menu. - * @param rows How many rows in the menu. - */ - void setSize(int32_t columns, int32_t rows); - - /** - * Returns the UI Item at the given position. - * - * @param x X coordinate of the item to get. - * @param y Y coordinate of the item to get. - * @return The pointer to the menu item, or null if invalid. - */ - UIMenuItem * getItem(int32_t x, int32_t y); - - /** - * Sets the position of the cursor in the grid. - * - * @param x X position of the cursor. - * @param y Y position of the cursor. - */ - void setPosition(int32_t x, int32_t y); - - /** - * Move the cursor relative to the current position. - * - * @param x X position to move relative. - * @param y Y position to move relative. - */ - void moveRelative(int32_t x, int32_t y); - - /** - * Adds/Sets an item onto the menu. - * - * @param x X coordinate to set the item. - * @param y Y coordinate to set the item. - * @param item Item to set. - */ - void setItem(int32_t x, int32_t y, UIMenuItem *item); - - /** - * Cleans up the menu items, doesn't free the children themselves. - */ - ~UIMenu(); - - friend class UICanvas; - }; +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "event/Event.hpp" +#include "ui/UIComponent.hpp" +#include "util/memory.hpp" + +namespace Dawn { + class UIMenuItem { + public: + /** + * Called when the item is selected (Accepted) on. + */ + virtual void onItemSelected() = 0; + + /** + * Called when either the mouse or the user controller input is removed + * off this item. + */ + virtual void onItemOver() = 0; + + /** + * Called when either the mouth or the user controller input is put over + * this item. + */ + virtual void onItemOff() = 0; + + /** + * Returns whether the UI item can be hovered overed or not. + * @return Whether it can be overed or not. + */ + virtual bool_t canBeOvered() = 0; + + /** + * Returns whether or not the item can be selected or not. + * @return Whether it can be selected or not. + */ + virtual bool_t canBeSelected() = 0; + }; + + struct UIMenu { + private: + UICanvas *canvas; + int32_t x = -1; + int32_t y = -1; + int32_t rows = 1; + int32_t columns = 1; + UIMenuItem **items = nullptr; + + protected: + /** Invoked by UICanvas when this menu has been made inactive. */ + void onInactive(); + /** Invoked by UICanvas when this menu has been made active. */ + void onActive(); + /** Invoked by UICanvas every tick this menu is active. */ + void onTick(); + + public: + Event<> eventMenuActive; + Event<> eventMenuInactive; + Event eventCursorChange; + Event eventItemSelected; + + /** + * Construct a new UI Menu Host. + * + * @param canvas Canvas that this menu belongs to. + * @param columns Iniitial size of the menu X axis. + * @param rows Initial size of the menu Y axis. + */ + UIMenu(UICanvas *canvas, int32_t columns, int32_t rows); + + /** + * Sets the size of the UI Menu. + * + * @param columns How many columns in the menu. + * @param rows How many rows in the menu. + */ + void setSize(int32_t columns, int32_t rows); + + /** + * Returns the UI Item at the given position. + * + * @param x X coordinate of the item to get. + * @param y Y coordinate of the item to get. + * @return The pointer to the menu item, or null if invalid. + */ + UIMenuItem * getItem(int32_t x, int32_t y); + + /** + * Sets the position of the cursor in the grid. + * + * @param x X position of the cursor. + * @param y Y position of the cursor. + */ + void setPosition(int32_t x, int32_t y); + + /** + * Move the cursor relative to the current position. + * + * @param x X position to move relative. + * @param y Y position to move relative. + */ + void moveRelative(int32_t x, int32_t y); + + /** + * Adds/Sets an item onto the menu. + * + * @param x X coordinate to set the item. + * @param y Y coordinate to set the item. + * @param item Item to set. + */ + void setItem(int32_t x, int32_t y, UIMenuItem *item); + + /** + * Cleans up the menu items, doesn't free the children themselves. + */ + ~UIMenu(); + + friend class UICanvas; + }; } \ No newline at end of file diff --git a/src/dawn/ui/UISprite.cpp b/archive/ui/UISprite.cpp similarity index 100% rename from src/dawn/ui/UISprite.cpp rename to archive/ui/UISprite.cpp diff --git a/src/dawn/ui/UISprite.hpp b/archive/ui/UISprite.hpp similarity index 100% rename from src/dawn/ui/UISprite.hpp rename to archive/ui/UISprite.hpp diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt index 0ee68451..87fc9815 100644 --- a/src/dawn/CMakeLists.txt +++ b/src/dawn/CMakeLists.txt @@ -35,7 +35,6 @@ add_subdirectory(save) add_subdirectory(scene) add_subdirectory(state) add_subdirectory(time) -add_subdirectory(ui) if(DAWN_VISUAL_NOVEL) add_subdirectory(visualnovel) diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 6d153c48..deb05880 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -153,14 +153,16 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { assertUnreachable(); } - auto itChild = canvas->children.begin(); - while(itChild != canvas->children.end()) { - vectorAppend(&shaderPassItems, (*itChild)->getPassItems( - projection, view, model - )); - ++itChild; - } - ++itCanvas; + auto children = canvas->item.chil + + // auto itChild = canvas->children.begin(); + // while(itChild != canvas->children.end()) { + // vectorAppend(&shaderPassItems, (*itChild)->getPassItems( + // projection, view, model + // )); + // ++itChild; + // } + // ++itCanvas; } // Debug Lines diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index e8541540..bdfb3f04 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -9,8 +9,7 @@ #include "scene/components/display/MeshRenderer.hpp" #include "scene/components/display/Camera.hpp" #include "scene/components/scene/SubSceneController.hpp" -#include "ui/UIComponent.hpp" - +#include "scene/components/ui/UIComponent.hpp" namespace Dawn { class RenderManager; diff --git a/src/dawn/scene/components/ui/UICanvas.cpp b/src/dawn/scene/components/ui/UICanvas.cpp index fe591f36..ff7d066f 100644 --- a/src/dawn/scene/components/ui/UICanvas.cpp +++ b/src/dawn/scene/components/ui/UICanvas.cpp @@ -4,9 +4,7 @@ // https://opensource.org/licenses/MIT #include "UICanvas.hpp" -#include "ui/UIComponent.hpp" #include "game/DawnGame.hpp" -#include "ui/UIMenu.hpp" using namespace Dawn; @@ -15,71 +13,11 @@ UICanvas * UICanvas::create(Scene *scene) { return item->addComponent(); } -UICanvas::UICanvas(SceneItem *item) : - SceneItemComponent(item), - camera(nullptr), - currentMenu(nullptr) -{ -} - -float_t UICanvas::getWidth() { - if(this->camera == nullptr) { - return this->getGame()->renderManager.getBackBuffer()->getWidth(); - } - return this->camera->getRenderTarget()->getWidth(); -} - -float_t UICanvas::getHeight() { - if(this->camera == nullptr) { - return this->getGame()->renderManager.getBackBuffer()->getHeight(); - } - return this->camera->getRenderTarget()->getHeight(); -} - -void UICanvas::setCurrentMenu(struct UIMenu *menu) { +UICanvas::UICanvas(SceneItem *item) : SceneItemComponent(item) { } void UICanvas::onStart() { - useEffectWithTeardown([&]{ - if(this->camera == nullptr) return evtCamResize = [&]{}; - - auto it = this->children.begin(); - while(it != this->children.end()) { - (*it)->updatePositions(); - ++it; - } - - return evtCamResize = useEvent([&](float_t w, float_t h){ - auto it = this->children.begin(); - while(it != this->children.end()) { - (*it)->updatePositions(); - ++it; - } - }, camera->eventRenderTargetResized); - }, camera); - - useEffectWithTeardown([&]{ - if(currentMenu != nullptr) currentMenu->onActive(); - - return [&] { - if(currentMenu == nullptr) currentMenu->onInactive(); - }; - }, this->currentMenu); - - // Scene Update - useEvent([&](float_t delta){ - if(this->currentMenu == nullptr) return; - this->currentMenu->onTick(); - }, getScene()->eventSceneUpdate); - - // Find Camera if we need to. - if(camera == nullptr) camera = this->getScene()->findComponent(); } void UICanvas::onDispose() { - auto it = this->children.begin(); - while(it != this->children.end()) { - delete *it; - ++it; - } } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UICanvas.hpp b/src/dawn/scene/components/ui/UICanvas.hpp index e07f26e0..ebdf4ec4 100644 --- a/src/dawn/scene/components/ui/UICanvas.hpp +++ b/src/dawn/scene/components/ui/UICanvas.hpp @@ -15,19 +15,8 @@ namespace Dawn { UI_DRAW_TYPE_CAMERA_OVERLAY }; - class UIComponent; - struct UIMenu; - class UICanvas : public SceneItemComponent { - protected: - std::function evtCamResize; - - void onRenderTargetResize(float_t w, float_t h); - public: - StateProperty currentMenu; - StateProperty camera; - /** * Creates a UI Canvas Scene Item Element, and attaches it to the provided * scene. @@ -38,8 +27,8 @@ namespace Dawn { static UICanvas * create(Scene *scene); //======================================================================// - std::vector children; - UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE; + + enum UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE; /** * Constructs the UI Canvas Scene Item Component. @@ -48,65 +37,6 @@ namespace Dawn { */ UICanvas(SceneItem *item); - /** - * Construct and append a UI item to this UI Canvas. - * - * @tparam Type of the UI Item. - * @return Pointer to the created UI Item. - */ - template - T * addElement() { - auto item = new T(this); - this->children.push_back(item); - return item; - } - - /** - * Find a UI Element attached to this canvas. - * - * @tparam Type of the UI item to find. - * @return Pointer to first matching element of type T. - */ - template - T * findElement() { - auto it = this->children.begin(); - while(it != this->children.end()) { - auto castedAs = dynamic_cast(*it); - if(castedAs != nullptr) return castedAs; - ++it; - } - return nullptr; - } - - /** - * Returns the width of the root UI Canvas size. In future I may allow - * this to be dynamic, right now it uses the render canvas however. - * - * @return Width of the UI Canvas. - */ - float_t getWidth(); - - /** - * Returns the height of this UI Canvas element. - * - * @return Height of the UI Canvas. - */ - float_t getHeight(); - - /** - * Returns the currently active menu for this UI Canvas. - * - * @return The currently active menu, or nullptr if there is none. - */ - struct UIMenu * getCurrentMenu(); - - /** - * Sets the currently active menu, and ticks it appropriately. - * - * @param menu Menu to set as the current active menu. - */ - void setCurrentMenu(struct UIMenu *menu); - void onStart() override; void onDispose() override; }; diff --git a/src/dawn/scene/components/ui/UIComponent.cpp b/src/dawn/scene/components/ui/UIComponent.cpp new file mode 100644 index 00000000..493bcf7e --- /dev/null +++ b/src/dawn/scene/components/ui/UIComponent.cpp @@ -0,0 +1,12 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UIComponent.hpp" + +using namespace Dawn; + +UIComponent::UIComponent(SceneItem *item) : SceneItemComponent(item) { + +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIComponent.hpp b/src/dawn/scene/components/ui/UIComponent.hpp new file mode 100644 index 00000000..715f4026 --- /dev/null +++ b/src/dawn/scene/components/ui/UIComponent.hpp @@ -0,0 +1,20 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "UICanvas.hpp" + +namespace Dawn { + class UIComponent : public SceneItemComponent { + private: + UICanvas *canvas = nullptr; + + public: + UIComponent(SceneItem *item); + + friend class UICanvas; + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabel.cpp b/src/dawn/scene/components/ui/UILabel.cpp new file mode 100644 index 00000000..8c3a3310 --- /dev/null +++ b/src/dawn/scene/components/ui/UILabel.cpp @@ -0,0 +1,49 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UILabel.hpp" + +using namespace Dawn; + +UILabel::UILabel(SceneItem *item) : + UIComponent(item), + text(""), + fontSize(10.0f), + font(nullptr) +{ +} + +void UILabel::updateMesh() { + if(!this->needsRebuffering || !this->hasText) return; + if(((Font*)this->font) == nullptr || !this->font->isReady()) return; + + // float_t width = this->width; + // if(width == 0) width = -1; + + // std::string text = this->getGame()->localeManager.getString(key); + this->font->buffer( + this->text, + this->fontSize, + -1, + &this->mesh, + &this->measure + ); + this->needsRebuffering = false; +} + +void UILabel::onStart() { + useEffect([&]{ + hasText = ((std::string)text).size() > 0; + needsRebuffering = true; + }, text); + + useEffect([&]{ + needsRebuffering = true; + }, fontSize); + + useEffect([&]{ + needsRebuffering = true; + }, font); +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabel.hpp b/src/dawn/scene/components/ui/UILabel.hpp new file mode 100644 index 00000000..20a95d33 --- /dev/null +++ b/src/dawn/scene/components/ui/UILabel.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" + +namespace Dawn { + class UILabel : public UIComponent { + private: + bool_t needsRebuffering = true; + bool_t hasText = false; + + Mesh mesh; + + void updateMesh(); + + public: + StateProperty text; + StateProperty fontSize; + StateProperty font; + struct Color textColor = COLOR_MAGENTA; + struct FontMeasure measure; + int32_t startQuad = 0; + int32_t quadCount = -1; + + UILabel(SceneItem *item); + + void onStart() override; + }; +} \ No newline at end of file