Canvas and UI Component #222

Merged
maverickdgg merged 5 commits from SP3-20-UI-System into main 2022-11-18 12:29:40 +08:00
7 changed files with 196 additions and 10 deletions
Showing only changes of commit ad30f150d1 - Show all commits

View File

@ -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<SHAudioSystem>();
SHSystemManager::CreateSystem<SHCameraSystem>();
SHSystemManager::CreateSystem<SHUISystem>();
SHSystemManager::CreateSystem<SHGraphicsSystem>();
@ -117,6 +119,8 @@ namespace Sandbox
SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BeginRoutine>();
//SHSystemManager::RegisterRoutine<SHCameraSystem, SHCameraSystem::EditorCameraUpdate>();
SHSystemManager::RegisterRoutine<SHUISystem, SHUISystem::AddUIComponentRoutine>();
SHSystemManager::RegisterRoutine<SHUISystem, SHUISystem::UpdateUIMatrixRoutine>();
SHSystemManager::RegisterRoutine<SHCameraSystem, SHCameraSystem::CameraSystemUpdate>();
#ifdef SHEDITOR

View File

@ -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;
}
}

View File

@ -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()

View File

@ -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_<SHUIComponent>("Canvas Component")
;
}

View File

@ -21,12 +21,12 @@ namespace SHADE
private:
SHMatrix LocalToCanvasMatrix;
SHMatrix localToCanvasMatrix;
EntityID canvasID;
RTTR_ENABLE()
}
};
}

View File

@ -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<SHUISystem>();
SHComponentManager::CreateComponentSparseSet<SHCanvasComponent>();
SHComponentManager::CreateComponentSparseSet<SHUIComponent>();
}
void SHUISystem::Exit()
{
}
void SHUISystem::AddUIComponentRoutine::Execute(double dt) noexcept
{
auto& dense = SHComponentManager::GetDense<SHCanvasComponent>();
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<EntityID> entitiesToRemove;
auto& UIDense = SHComponentManager::GetDense<SHUIComponent>();
for (auto& ui : UIDense)
{
SHSceneNode* parentNode = sceneGraph.GetParent(ui.GetEID());
if (parentNode == nullptr || !(SHComponentManager::HasComponent<SHUIComponent>(parentNode->GetEntityID()) || SHComponentManager::HasComponent<SHCanvasComponent>(parentNode->GetEntityID())))
entitiesToRemove.push_back(ui.GetEID());
}
for (auto& id : entitiesToRemove)
{
SHComponentManager::RemoveComponent<SHUIComponent>(id);
}
}
void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node) noexcept
{
if (node == nullptr)
return;
EntityID eid = node->GetEntityID();
if(SHComponentManager::HasComponent<SHUIComponent>(eid) == false)
SHComponentManager::AddComponent<SHUIComponent>(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<SHUIComponent>();
for (auto& comp : dense)
{
system->UpdateUIComponent(comp);
}
}
void SHUISystem::UpdateUIComponent(SHUIComponent& comp) noexcept
{
auto canvasComp = SHComponentManager::GetComponent_s<SHCanvasComponent>(comp.canvasID);
if (SHComponentManager::HasComponent<SHTransformComponent>(comp.GetEID()))
{
auto transform = SHComponentManager::GetComponent<SHTransformComponent>(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

View File

@ -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;
};
}