From ad30f150d13e7f54dd1677c73d5ab0024ef29bed Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 07:12:38 +0800 Subject: [PATCH] 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; + + + + + }; + + +}