From 0bb97413ef4f6e3648ce7c0d726bebc9d7d56973 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 05:39:55 +0800 Subject: [PATCH 1/3] Added Canvas and UI Components --- SHADE_Engine/src/Camera/SHCameraSystem.h | 2 - SHADE_Engine/src/UI/SHCanvasComponent.cpp | 55 +++++++++++++++++++++++ SHADE_Engine/src/UI/SHCanvasComponent.h | 12 ++++- SHADE_Engine/src/UI/SHUIComponent.cpp | 20 +++++++++ SHADE_Engine/src/UI/SHUIComponent.h | 32 +++++++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 SHADE_Engine/src/UI/SHCanvasComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHUIComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHUIComponent.h diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h index fc6e9166..9c8157f4 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.h +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -37,8 +37,6 @@ namespace SHADE void Exit (void); - friend class EditorCameraUpdate; - class SH_API CameraSystemUpdate final: public SHSystemRoutine { public: diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.cpp b/SHADE_Engine/src/UI/SHCanvasComponent.cpp new file mode 100644 index 00000000..4c9ea360 --- /dev/null +++ b/SHADE_Engine/src/UI/SHCanvasComponent.cpp @@ -0,0 +1,55 @@ +#include "SHpch.h" + +#include "SHCanvasComponent.h" + +namespace SHADE +{ + + SHCanvasComponent::SHCanvasComponent() + :width(1),height(1), dirtyMatrix(false) + { + + } + + void SHCanvasComponent::SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept + { + this->width = width; + this->height = height; + } + + void SHCanvasComponent::SetCanvasWidth(CanvasSizeType val) noexcept + { + width = val; + } + + void SHCanvasComponent::SetCanvasHeight(CanvasSizeType val) noexcept + { + height = val; + } + + + SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasWidth() const noexcept + { + return width; + } + + SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasHeight() const noexcept + { + return height; + } + +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Canvas Component") + .property("Canvas Width", &SHCanvasComponent::GetCanvasWidth, &SHCanvasComponent::SetCanvasWidth) + .property("Canvas Height", &SHCanvasComponent::GetCanvasHeight, &SHCanvasComponent::SetCanvasHeight) + ; + + +} diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.h b/SHADE_Engine/src/UI/SHCanvasComponent.h index 2e9a54f1..63d8c2c4 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.h +++ b/SHADE_Engine/src/UI/SHCanvasComponent.h @@ -1,7 +1,10 @@ #pragma once +#include + #include "SH_API.h" #include "ECS_Base/Components/SHComponent.h" +#include "Math/SHMatrix.h" namespace SHADE @@ -13,6 +16,9 @@ namespace SHADE public: + friend class SHUISystem; + + SHCanvasComponent(); ~SHCanvasComponent() = default; @@ -26,9 +32,11 @@ namespace SHADE private: CanvasSizeType width; CanvasSizeType height; - - + bool dirtyMatrix; + SHMatrix modelToCanvasMatrix; + + RTTR_ENABLE() }; diff --git a/SHADE_Engine/src/UI/SHUIComponent.cpp b/SHADE_Engine/src/UI/SHUIComponent.cpp new file mode 100644 index 00000000..66522947 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUIComponent.cpp @@ -0,0 +1,20 @@ +#include "SHpch.h" +#include "SHUIComponent.h" + + + +namespace SHADE +{ + + SHUIComponent::SHUIComponent() + { + + } + + + SHMatrix const& SHUIComponent::GetMatrix()const noexcept + { + return LocalToCanvasMatrix; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUIComponent.h b/SHADE_Engine/src/UI/SHUIComponent.h new file mode 100644 index 00000000..70a667e8 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUIComponent.h @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" +#include "Math/SHMatrix.h" + + +namespace SHADE +{ + class SH_API SHUIComponent final: public SHComponent + { + public: + friend class SHUISystem; + + SHUIComponent(); + ~SHUIComponent() = default; + + SHMatrix const& GetMatrix() const noexcept; + + + private: + SHMatrix LocalToCanvasMatrix; + + + + RTTR_ENABLE() + } + + +} \ No newline at end of file From ad30f150d13e7f54dd1677c73d5ab0024ef29bed Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 07:12:38 +0800 Subject: [PATCH 2/3] UI component and UI System prep --- .../src/Application/SBApplication.cpp | 4 + SHADE_Engine/src/UI/SHCanvasComponent.cpp | 11 +- SHADE_Engine/src/UI/SHCanvasComponent.h | 7 +- SHADE_Engine/src/UI/SHUIComponent.cpp | 14 ++- SHADE_Engine/src/UI/SHUIComponent.h | 6 +- SHADE_Engine/src/UI/SHUISystem.cpp | 109 ++++++++++++++++++ SHADE_Engine/src/UI/SHUISystem.h | 55 +++++++++ 7 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 SHADE_Engine/src/UI/SHUISystem.cpp create mode 100644 SHADE_Engine/src/UI/SHUISystem.h diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index bf5b8d49..57b4c189 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -33,6 +33,7 @@ #include "Physics/System/SHPhysicsSystem.h" #include "Physics/System/SHPhysicsDebugDrawSystem.h" #include "Scripting/SHScriptEngine.h" +#include "UI/SHUISystem.h" // Components #include "Graphics/MiddleEnd/Interface/SHRenderable.h" @@ -75,6 +76,7 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); @@ -117,6 +119,8 @@ namespace Sandbox SHSystemManager::RegisterRoutine(); //SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); #ifdef SHEDITOR diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.cpp b/SHADE_Engine/src/UI/SHCanvasComponent.cpp index 4c9ea360..1ffc7a19 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.cpp +++ b/SHADE_Engine/src/UI/SHCanvasComponent.cpp @@ -6,7 +6,7 @@ namespace SHADE { SHCanvasComponent::SHCanvasComponent() - :width(1),height(1), dirtyMatrix(false) + :width(1),height(1), dirtyMatrix(false), canvasMatrix() { } @@ -28,16 +28,21 @@ namespace SHADE } - SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasWidth() const noexcept + SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasWidth() const noexcept { return width; } - SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasHeight() const noexcept + SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasHeight() const noexcept { return height; } + SHMatrix const& SHCanvasComponent::GetMatrix() const noexcept + { + return canvasMatrix; + } + } diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.h b/SHADE_Engine/src/UI/SHCanvasComponent.h index 63d8c2c4..145b3cb3 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.h +++ b/SHADE_Engine/src/UI/SHCanvasComponent.h @@ -26,14 +26,15 @@ namespace SHADE void SetCanvasWidth(CanvasSizeType width) noexcept; void SetCanvasHeight(CanvasSizeType height) noexcept; - CanvasSizeType const GetCanvasWidth() const noexcept; - CanvasSizeType const GetCanvasHeight() const noexcept; + CanvasSizeType GetCanvasWidth() const noexcept; + CanvasSizeType GetCanvasHeight() const noexcept; + SHMatrix const& GetMatrix() const noexcept; private: CanvasSizeType width; CanvasSizeType height; bool dirtyMatrix; - SHMatrix modelToCanvasMatrix; + SHMatrix canvasMatrix; RTTR_ENABLE() diff --git a/SHADE_Engine/src/UI/SHUIComponent.cpp b/SHADE_Engine/src/UI/SHUIComponent.cpp index 66522947..23c86ec3 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.cpp +++ b/SHADE_Engine/src/UI/SHUIComponent.cpp @@ -14,7 +14,19 @@ namespace SHADE SHMatrix const& SHUIComponent::GetMatrix()const noexcept { - return LocalToCanvasMatrix; + return localToCanvasMatrix; } +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Canvas Component") + ; + + } \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUIComponent.h b/SHADE_Engine/src/UI/SHUIComponent.h index 70a667e8..190a40e1 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.h +++ b/SHADE_Engine/src/UI/SHUIComponent.h @@ -21,12 +21,12 @@ namespace SHADE private: - SHMatrix LocalToCanvasMatrix; - + SHMatrix localToCanvasMatrix; + EntityID canvasID; RTTR_ENABLE() - } + }; } \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp new file mode 100644 index 00000000..3f7559b5 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -0,0 +1,109 @@ +#include "SHpch.h" +#include "SHUISystem.h" +#include "ECS_Base/Managers/SHComponentManager.h" +#include "ECS_Base/Managers/SHSystemManager.h" +#include "Math/Transform/SHTransformComponent.h" + +namespace SHADE +{ + + void SHUISystem::Init() + { + SystemFamily::GetID(); + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + } + + void SHUISystem::Exit() + { + + } + + + void SHUISystem::AddUIComponentRoutine::Execute(double dt) noexcept + { + auto& dense = SHComponentManager::GetDense(); + auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + + //Go through all the canvases and make sure all the children has a UIComponent. + for (auto& canvas : dense) + { + auto& children = sceneGraph.GetChildren(canvas.GetEID()); + for (auto& child : children) + { + RecurssiveUIComponentCheck(child); + } + } + + //Go through all the UI Component and make sure the parent has a UI or Canvas Component + std::vector entitiesToRemove; + auto& UIDense = SHComponentManager::GetDense(); + for (auto& ui : UIDense) + { + SHSceneNode* parentNode = sceneGraph.GetParent(ui.GetEID()); + if (parentNode == nullptr || !(SHComponentManager::HasComponent(parentNode->GetEntityID()) || SHComponentManager::HasComponent(parentNode->GetEntityID()))) + entitiesToRemove.push_back(ui.GetEID()); + } + + for (auto& id : entitiesToRemove) + { + SHComponentManager::RemoveComponent(id); + } + + + } + + void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node) noexcept + { + if (node == nullptr) + return; + + EntityID eid = node->GetEntityID(); + + if(SHComponentManager::HasComponent(eid) == false) + SHComponentManager::AddComponent(eid); + + auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eid); + for (auto& child : children) + { + RecurssiveUIComponentCheck(child); + } + + } + + + void SHUISystem::UpdateUIMatrixRoutine::Execute(double dt) noexcept + { + SHUISystem* system = (SHUISystem* )GetSystem(); + auto& dense = SHComponentManager::GetDense(); + for (auto& comp : dense) + { + system->UpdateUIComponent(comp); + } + + + } + + void SHUISystem::UpdateUIComponent(SHUIComponent& comp) noexcept + { + auto canvasComp = SHComponentManager::GetComponent_s(comp.canvasID); + if (SHComponentManager::HasComponent(comp.GetEID())) + { + auto transform = SHComponentManager::GetComponent(comp.GetEID()); + if (canvasComp != nullptr) + comp.localToCanvasMatrix = canvasComp->GetMatrix() * transform->GetTRS(); + else + comp.localToCanvasMatrix = transform->GetTRS(); + } + else //If for some reason UI Components entities does not have a transform. + { + if (canvasComp != nullptr) + comp.localToCanvasMatrix = canvasComp->GetMatrix(); + else + comp.localToCanvasMatrix = SHMatrix::Identity; + } + } + + + +}//end namespace diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h new file mode 100644 index 00000000..99891348 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -0,0 +1,55 @@ +#pragma once + +#include "SH_API.h" +#include "ECS_Base/System/SHSystem.h" +#include "ECS_Base/System/SHSystemRoutine.h" +#include "SHUIComponent.h" +#include "SHCanvasComponent.h" +#include "Scene/SHSceneGraph.h" +#include "Scene/SHSceneManager.h" + +namespace SHADE +{ + class SH_API SHUISystem final: public SHSystem + { + public: + + SHUISystem() = default; + ~SHUISystem() = default; + + class SH_API AddUIComponentRoutine final: public SHSystemRoutine + { + public: + AddUIComponentRoutine() :SHSystemRoutine("Add UI Component Routine", true) {}; + virtual void Execute(double dt) noexcept override final; + + private: + + void RecurssiveUIComponentCheck(SHSceneNode* node) noexcept; + }; + friend class AddUIComponentRoutine; + + class SH_API UpdateUIMatrixRoutine final: public SHSystemRoutine + { + public: + UpdateUIMatrixRoutine() : SHSystemRoutine("Update UI Matrix Routine", true) {}; + virtual void Execute(double dt) noexcept override final; + + }; + friend class UpdateUIMatrixRoutine; + + + void Init(); + void Exit(); + + + private: + void UpdateUIComponent(SHUIComponent& comp) noexcept; + + + + + }; + + +} From d997cd4da661771a56f57f27a54f4ce96d8c9ffd Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 18 Nov 2022 09:38:31 +0800 Subject: [PATCH 3/3] Added canvas ID to UI Component --- SHADE_Engine/src/UI/SHUISystem.cpp | 10 +++++++--- SHADE_Engine/src/UI/SHUISystem.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 3f7559b5..d7dfe3c5 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -31,7 +31,7 @@ namespace SHADE auto& children = sceneGraph.GetChildren(canvas.GetEID()); for (auto& child : children) { - RecurssiveUIComponentCheck(child); + RecurssiveUIComponentCheck(child, canvas); } } @@ -53,7 +53,7 @@ namespace SHADE } - void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node) noexcept + void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node, SHCanvasComponent& canvas) noexcept { if (node == nullptr) return; @@ -62,11 +62,15 @@ namespace SHADE if(SHComponentManager::HasComponent(eid) == false) SHComponentManager::AddComponent(eid); + else + { + SHComponentManager::GetComponent(eid)->canvasID = canvas.GetEID(); + } auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eid); for (auto& child : children) { - RecurssiveUIComponentCheck(child); + RecurssiveUIComponentCheck(child, canvas); } } diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index 99891348..3da7efb3 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -25,7 +25,7 @@ namespace SHADE private: - void RecurssiveUIComponentCheck(SHSceneNode* node) noexcept; + void RecurssiveUIComponentCheck(SHSceneNode* node, SHCanvasComponent& canvas) noexcept; }; friend class AddUIComponentRoutine;