From 7c36886669172c2a61eb0dfcc945dced1b6d6c3e Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 22 Nov 2022 21:03:53 +0800 Subject: [PATCH] Fix for Canvas scaler --- Assets/Scenes/UI Test.shade | 5 +++++ Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/Camera/SHCameraSystem.cpp | 20 ++++++++++++++++++ SHADE_Engine/src/Camera/SHCameraSystem.h | 3 +++ SHADE_Engine/src/UI/SHUISystem.cpp | 24 ++++------------------ 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Assets/Scenes/UI Test.shade b/Assets/Scenes/UI Test.shade index b6aab1a1..e8ee4df2 100644 --- a/Assets/Scenes/UI Test.shade +++ b/Assets/Scenes/UI Test.shade @@ -22,6 +22,11 @@ Mesh: 141771688 Material: 129340704 IsActive: true + Button Component: + Default Texture: 0 + Hovered Texture: 0 + Clicked Texture: 0 + IsActive: true Scripts: ~ - EID: 1 Name: Camera diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index 0e80f988..cf38916e 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 96139961 +ID: 87707373 Type: 5 diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index 30bdec29..a2e83767 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -8,6 +8,8 @@ #include "Math/Transform/SHTransformComponent.h" #include #include "Scene/SHSceneManager.h" +#include "ECS_Base/Managers/SHSystemManager.h" +#include "Editor/SHEditor.h" namespace SHADE { @@ -440,4 +442,22 @@ namespace SHADE SetCameraViewMatrix(camera, viewMtx); } + SHVec2 SHCameraSystem::GetCameraWidthHeight(size_t index) noexcept + { +#ifdef SHEDITOR + auto editor = SHSystemManager::GetSystem(); + if (editor->editorState != SHEditor::State::PLAY) + { + return SHVec2{ GetEditorCamera()->GetWidth(), GetEditorCamera()->GetHeight() }; + } + else + { + return SHVec2{ GetDirector(index)->GetWidth(),GetDirector(index)->GetHeight() }; + } + +#else + return SHVec2{ GetDirector(index)->GetWidth(),GetDirector(index)->GetHeight() }; +#endif + } + } diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h index 9c8157f4..d40f9a6c 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.h +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -27,6 +27,7 @@ namespace SHADE void UpdateCameraComponent(SHCameraComponent& camera) noexcept; void UpdatePivotArmComponent(SHCameraArmComponent& pivot) noexcept; + public: @@ -57,6 +58,8 @@ namespace SHADE void SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept; void CameraLookAt(SHCameraComponent& camera, SHVec3 target) noexcept; void UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept; + + SHVec2 GetCameraWidthHeight(size_t index) noexcept; }; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 247eb6cc..1387bc6b 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -119,26 +119,10 @@ namespace SHADE void SHUISystem::UpdateCanvasComponent(SHCanvasComponent& comp) noexcept { auto cameraSystem = SHSystemManager::GetSystem(); -#ifdef SHEDITOR - auto editor = SHSystemManager::GetSystem(); - if (editor->editorState != SHEditor::State::PLAY) - { - comp.canvasMatrix = SHMatrix::Identity; - comp.canvasMatrix(0, 0) = cameraSystem->GetEditorCamera()->GetWidth() * 0.5f / (comp.GetCanvasWidth() * 0.5f); - comp.canvasMatrix(1, 1) = cameraSystem->GetEditorCamera()->GetHeight() * 0.5f / (comp.GetCanvasHeight() * 0.5f); - } - else - { - comp.canvasMatrix = SHMatrix::Identity; - comp.canvasMatrix(0, 0) = cameraSystem->GetDirector(0)->GetWidth() * 0.5f / (comp.GetCanvasWidth() * 0.5f); - comp.canvasMatrix(1, 1) = cameraSystem->GetDirector(0)->GetHeight() * 0.5f / (comp.GetCanvasHeight() * 0.5f); - } - -#else + SHVec2 camSize = cameraSystem->GetCameraWidthHeight(0); comp.canvasMatrix = SHMatrix::Identity; - comp.canvasMatrix(0, 0) = cameraSystem->GetDirector(0)->GetWidth() * 0.5f / (comp.GetCanvasWidth() * 0.5f ); - comp.canvasMatrix(1, 1) = cameraSystem->GetDirector(0)->GetHeight() * 0.5f / (comp.GetCanvasHeight() * 0.5f ); -#endif + comp.canvasMatrix(0, 0) = camSize.x * 0.5f / (comp.GetCanvasWidth() * 0.5f); + comp.canvasMatrix(1, 1) = camSize.y * 0.5f / (comp.GetCanvasHeight() * 0.5f); } void SHUISystem::UpdateCanvasMatrixRoutine::Execute(double dt) noexcept @@ -183,7 +167,7 @@ namespace SHADE - SHVec2 camSize{ cameraSystem->GetDirector(0)->GetWidth() , cameraSystem->GetDirector(0)->GetHeight() }; + SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0).x , cameraSystem->GetCameraWidthHeight(0).y }; topExtent += camSize * 0.5f; btmExtent += camSize * 0.5f; -- 2.40.1