Fixed Canvas Scaler to keep AR of the UI Elements

This commit is contained in:
maverickdgg 2023-02-21 10:48:07 +08:00
parent 51909071e6
commit a3112f9c60
4 changed files with 31 additions and 6 deletions

View File

@ -6,6 +6,7 @@
Canvas Component: Canvas Component:
Canvas Width: 1920 Canvas Width: 1920
Canvas Height: 1080 Canvas Height: 1080
Scale by canvas width: false
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 1 - EID: 1
@ -24,6 +25,8 @@
IsActive: true IsActive: true
UI Component: UI Component:
Canvas ID: 0 Canvas ID: 0
Hovered: false
Clicked: false
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 5 - EID: 5
@ -47,6 +50,8 @@
IsActive: true IsActive: true
UI Component: UI Component:
Canvas ID: 0 Canvas ID: 0
Hovered: false
Clicked: false
IsActive: true IsActive: true
Scripts: Scripts:
- Type: ChangeSceneButton - Type: ChangeSceneButton
@ -73,6 +78,8 @@
IsActive: true IsActive: true
UI Component: UI Component:
Canvas ID: 0 Canvas ID: 0
Hovered: false
Clicked: false
IsActive: true IsActive: true
Scripts: Scripts:
- Type: QuitButton - Type: QuitButton
@ -106,11 +113,12 @@
Pitch: 0 Pitch: 0
Yaw: 0 Yaw: 0
Roll: 0 Roll: 0
Width: 1920 Width: 1319
Height: 1080 Height: 622
Near: 0.00999999978 Near: 0.00999999978
Far: 10000 Far: 10000
Perspective: true Perspective: true
FOV: 90
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 4 - EID: 4

View File

@ -6,7 +6,7 @@ namespace SHADE
{ {
SHCanvasComponent::SHCanvasComponent() SHCanvasComponent::SHCanvasComponent()
:width(1),height(1), dirtyMatrix(false), canvasMatrix() :width(1), height(1), dirtyMatrix(false), canvasMatrix(), scaleByCanvasWidth(false)
{ {
} }
@ -28,6 +28,8 @@ namespace SHADE
} }
SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasWidth() const noexcept SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasWidth() const noexcept
{ {
return width; return width;
@ -43,6 +45,8 @@ namespace SHADE
return canvasMatrix; return canvasMatrix;
} }
} }
@ -54,6 +58,7 @@ RTTR_REGISTRATION
registration::class_<SHCanvasComponent>("Canvas Component") registration::class_<SHCanvasComponent>("Canvas Component")
.property("Canvas Width", &SHCanvasComponent::GetCanvasWidth, &SHCanvasComponent::SetCanvasWidth) .property("Canvas Width", &SHCanvasComponent::GetCanvasWidth, &SHCanvasComponent::SetCanvasWidth)
.property("Canvas Height", &SHCanvasComponent::GetCanvasHeight, &SHCanvasComponent::SetCanvasHeight) .property("Canvas Height", &SHCanvasComponent::GetCanvasHeight, &SHCanvasComponent::SetCanvasHeight)
.property("Scale by canvas width", &SHCanvasComponent::scaleByCanvasWidth)
; ;

View File

@ -21,15 +21,18 @@ namespace SHADE
SHCanvasComponent(); SHCanvasComponent();
~SHCanvasComponent() = default; ~SHCanvasComponent() = default;
bool scaleByCanvasWidth;
void SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept; void SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept;
void SetCanvasWidth(CanvasSizeType width) noexcept; void SetCanvasWidth(CanvasSizeType width) noexcept;
void SetCanvasHeight(CanvasSizeType height) noexcept; void SetCanvasHeight(CanvasSizeType height) noexcept;
CanvasSizeType GetCanvasWidth() const noexcept; CanvasSizeType GetCanvasWidth() const noexcept;
CanvasSizeType GetCanvasHeight() const noexcept; CanvasSizeType GetCanvasHeight() const noexcept;
SHMatrix const& GetMatrix() const noexcept; SHMatrix const& GetMatrix() const noexcept;
private: private:
CanvasSizeType width; CanvasSizeType width;
CanvasSizeType height; CanvasSizeType height;

View File

@ -131,8 +131,17 @@ namespace SHADE
auto cameraSystem = SHSystemManager::GetSystem<SHCameraSystem>(); auto cameraSystem = SHSystemManager::GetSystem<SHCameraSystem>();
SHVec2 camSize = cameraSystem->GetCameraWidthHeight(0); SHVec2 camSize = cameraSystem->GetCameraWidthHeight(0);
comp.canvasMatrix = SHMatrix::Identity; comp.canvasMatrix = SHMatrix::Identity;
comp.canvasMatrix(0, 0) = camSize.x * 0.5f / (comp.GetCanvasWidth() * 0.5f); float scale = camSize.y / comp.GetCanvasHeight();
comp.canvasMatrix(1, 1) = camSize.y * 0.5f / (comp.GetCanvasHeight() * 0.5f); 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 void SHUISystem::UpdateCanvasMatrixRoutine::Execute(double dt) noexcept