From a3112f9c60d1cb8232bf9ec4ef437c219ac7a8b5 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 21 Feb 2023 10:48:07 +0800 Subject: [PATCH] Fixed Canvas Scaler to keep AR of the UI Elements --- Assets/Scenes/MainMenu.shade | 12 ++++++++++-- SHADE_Engine/src/UI/SHCanvasComponent.cpp | 7 ++++++- SHADE_Engine/src/UI/SHCanvasComponent.h | 5 ++++- SHADE_Engine/src/UI/SHUISystem.cpp | 13 +++++++++++-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Assets/Scenes/MainMenu.shade b/Assets/Scenes/MainMenu.shade index 54208e8b..30ef368a 100644 --- a/Assets/Scenes/MainMenu.shade +++ b/Assets/Scenes/MainMenu.shade @@ -6,6 +6,7 @@ Canvas Component: Canvas Width: 1920 Canvas Height: 1080 + Scale by canvas width: false IsActive: true Scripts: ~ - EID: 1 @@ -24,6 +25,8 @@ IsActive: true UI Component: Canvas ID: 0 + Hovered: false + Clicked: false IsActive: true Scripts: ~ - EID: 5 @@ -47,6 +50,8 @@ IsActive: true UI Component: Canvas ID: 0 + Hovered: false + Clicked: false IsActive: true Scripts: - Type: ChangeSceneButton @@ -73,6 +78,8 @@ IsActive: true UI Component: Canvas ID: 0 + Hovered: false + Clicked: false IsActive: true Scripts: - Type: QuitButton @@ -106,11 +113,12 @@ Pitch: 0 Yaw: 0 Roll: 0 - Width: 1920 - Height: 1080 + Width: 1319 + Height: 622 Near: 0.00999999978 Far: 10000 Perspective: true + FOV: 90 IsActive: true Scripts: ~ - EID: 4 diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.cpp b/SHADE_Engine/src/UI/SHCanvasComponent.cpp index 1ffc7a19..5ee93bc1 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), canvasMatrix() + :width(1), height(1), dirtyMatrix(false), canvasMatrix(), scaleByCanvasWidth(false) { } @@ -27,6 +27,8 @@ namespace SHADE height = val; } + + SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasWidth() const noexcept { @@ -43,6 +45,8 @@ namespace SHADE return canvasMatrix; } + + } @@ -54,6 +58,7 @@ RTTR_REGISTRATION registration::class_("Canvas Component") .property("Canvas Width", &SHCanvasComponent::GetCanvasWidth, &SHCanvasComponent::SetCanvasWidth) .property("Canvas Height", &SHCanvasComponent::GetCanvasHeight, &SHCanvasComponent::SetCanvasHeight) + .property("Scale by canvas width", &SHCanvasComponent::scaleByCanvasWidth) ; diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.h b/SHADE_Engine/src/UI/SHCanvasComponent.h index 145b3cb3..5b6c1781 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.h +++ b/SHADE_Engine/src/UI/SHCanvasComponent.h @@ -21,21 +21,24 @@ namespace SHADE SHCanvasComponent(); ~SHCanvasComponent() = default; + bool scaleByCanvasWidth; void SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept; void SetCanvasWidth(CanvasSizeType width) noexcept; void SetCanvasHeight(CanvasSizeType height) noexcept; + CanvasSizeType GetCanvasWidth() const noexcept; CanvasSizeType GetCanvasHeight() const noexcept; SHMatrix const& GetMatrix() const noexcept; + private: CanvasSizeType width; CanvasSizeType height; bool dirtyMatrix; SHMatrix canvasMatrix; - + RTTR_ENABLE() }; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 69481b5e..3552e47b 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -131,8 +131,17 @@ namespace SHADE auto cameraSystem = SHSystemManager::GetSystem(); SHVec2 camSize = cameraSystem->GetCameraWidthHeight(0); comp.canvasMatrix = SHMatrix::Identity; - comp.canvasMatrix(0, 0) = camSize.x * 0.5f / (comp.GetCanvasWidth() * 0.5f); - comp.canvasMatrix(1, 1) = camSize.y * 0.5f / (comp.GetCanvasHeight() * 0.5f); + float scale = camSize.y / comp.GetCanvasHeight(); + if (comp.scaleByCanvasWidth) + { + scale = camSize.x / comp.GetCanvasWidth(); + } + + comp.canvasMatrix(0, 0) = scale; + comp.canvasMatrix(1, 1) = scale; + + //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