From febe22e4871b11012233d6ab1505f5415b9e1687 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 21 Feb 2023 09:52:23 +0800 Subject: [PATCH 1/3] Added new button events --- Assets/Editor/Editor.SHConfig | 2 +- SHADE_Engine/src/Events/SHEventDefines.h | 3 + SHADE_Engine/src/UI/SHUISystem.cpp | 233 ++++++++++------------- SHADE_Engine/src/UI/SHUISystem.h | 5 + 4 files changed, 110 insertions(+), 133 deletions(-) diff --git a/Assets/Editor/Editor.SHConfig b/Assets/Editor/Editor.SHConfig index 9bd51ca8..37edf50c 100644 --- a/Assets/Editor/Editor.SHConfig +++ b/Assets/Editor/Editor.SHConfig @@ -1,4 +1,4 @@ Start Maximized: true -Working Scene ID: 86098106 +Working Scene ID: 97158628 Window Size: {x: 1920, y: 1013} Style: 0 \ No newline at end of file diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index fa5bcafb..fffe8b5f 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -26,4 +26,7 @@ constexpr SHEventIdentifier SH_GRAPHICS_LIGHT_ENABLE_SHADOW_EVENT { 17 }; constexpr SHEventIdentifier SH_BUTTON_CLICK_EVENT { 18 }; constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_DRAW_EVENT { 19 }; constexpr SHEventIdentifier SH_WINDOW_RESIZE_EVENT { 20 }; +constexpr SHEventIdentifier SH_BUTTON_RELEASE_EVENT { 21 }; +constexpr SHEventIdentifier SH_BUTTON_HOVER_ENTER_EVENT { 22 }; +constexpr SHEventIdentifier SH_BUTTON_HOVER_EXIT_EVENT { 23 }; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 92a754bf..3ddff353 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -146,6 +146,105 @@ namespace SHADE } } + bool SHUISystem::CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept + { + + auto cameraSystem = SHSystemManager::GetSystem(); + SHVec2 mousePos; + SHVec2 windowSize; +#ifdef SHEDITOR + windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; + mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; + //mousePos.y = windowSize.y - mousePos.y; + //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + mousePos /= windowSize; + //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + + + +#else + + int x, y; + SHInputManager::GetMouseScreenPosition(&x, &y); + mousePos.x = x; + mousePos.y = y; + auto ws = SHSystemManager::GetSystem()->GetWindow()->GetWindowSize(); + windowSize = { static_cast(ws.first), static_cast(ws.second) }; + mousePos /= windowSize; +#endif + + SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0) }; + //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) + + topExtent = CanvasToScreenPoint(topExtent, true); + btmExtent = CanvasToScreenPoint(btmExtent, true); + //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) + + + //comp.isClicked = false; + if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x + && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) + { + if (isHovered == false) + { + SHButtonClickEvent clickEvent; + clickEvent.EID = eid; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_ENTER_EVENT); + } + isHovered = true; + + + +#ifdef SHEDITOR + //if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) + { + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) + { + isClicked = true; + SHButtonClickEvent clickEvent; + clickEvent.EID = eid; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + } +#else + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + SHButtonClickEvent clickEvent; + clickEvent.EID = eid; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } +#endif + + //SHLOG_INFO("HOVERED") + } + else + { + if (isHovered == true) + { + SHButtonClickEvent clickEvent; + clickEvent.EID = eid; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_EXIT_EVENT); + } + + isHovered = false; + //SHLOG_INFO("NOT HOVERED") + } + if (isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + isClicked = false; + SHButtonClickEvent clickEvent; + clickEvent.EID = eid; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_RELEASE_EVENT); + + return true; + } + + + return false; + + } + void SHUISystem::UpdateButtonComponent(SHButtonComponent& comp) noexcept { @@ -164,72 +263,7 @@ namespace SHADE SHVec2 topExtent{ topExtent4.x,topExtent4.y }; SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; - - SHVec2 mousePos; - SHVec2 windowSize; -#ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; - mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; - //mousePos.y = windowSize.y - mousePos.y; - //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) - mousePos /= windowSize; - //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) - - - -#else - - int x, y; - SHInputManager::GetMouseScreenPosition(&x, &y); - mousePos.x = x; - mousePos.y = y; - auto ws = SHSystemManager::GetSystem()->GetWindow()->GetWindowSize(); - windowSize = { static_cast(ws.first), static_cast(ws.second) }; - mousePos /= windowSize; -#endif - - SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; - //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) - - topExtent = CanvasToScreenPoint(topExtent,true); - btmExtent = CanvasToScreenPoint(btmExtent,true); - //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - - - //comp.isClicked = false; - if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x - && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) - { - comp.isHovered = true; - #ifdef SHEDITOR - //if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) - { - if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = true; - } - } - #else - if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = true; - } - #endif - - //SHLOG_INFO("HOVERED") - } - else - { - comp.isHovered = false; - //SHLOG_INFO("NOT HOVERED") - } - if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = false; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); - } + CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked); if (SHComponentManager::HasComponent(comp.GetEID())) { @@ -295,73 +329,8 @@ namespace SHADE SHVec2 topExtent{ topExtent4.x,topExtent4.y }; SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; - - SHVec2 mousePos; - SHVec2 windowSize; -#ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; - mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; - //mousePos.y = windowSize.y - mousePos.y; - //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) - mousePos /= windowSize; - //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) - - - -#else - - int x, y; - SHInputManager::GetMouseScreenPosition(&x, &y); - mousePos.x = x; - mousePos.y = y; - auto ws = SHSystemManager::GetSystem()->GetWindow()->GetWindowSize(); - windowSize = { static_cast(ws.first), static_cast(ws.second) }; - mousePos /= windowSize; -#endif - - SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0) }; - //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) - - topExtent = CanvasToScreenPoint(topExtent, true); - btmExtent = CanvasToScreenPoint(btmExtent, true); - //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - - - //comp.isClicked = false; - if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x - && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) - { - comp.isHovered = true; -#ifdef SHEDITOR - //if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) - { - if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = true; - } - } -#else - if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = true; - } -#endif - - //SHLOG_INFO("HOVERED") - } - else - { - comp.isHovered = false; - //SHLOG_INFO("NOT HOVERED") - } - if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = false; + if (CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked)) comp.value = !comp.value; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); - } if (SHComponentManager::HasComponent(comp.GetEID())) { diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index ae1091ec..1eaf883e 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -72,6 +72,11 @@ namespace SHADE void UpdateButtonComponent(SHButtonComponent& comp) noexcept; void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; + + //returns true on button release. + bool CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept; + + SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept; From 51909071e69fa1677dba73f68a5cc4ba9d75aa83 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 21 Feb 2023 10:09:58 +0800 Subject: [PATCH 2/3] Change isHovered and isClicked to be stored in UIComponent instead and made a helper function to check for clicks and hovered --- SHADE_Engine/src/UI/SHButtonComponent.cpp | 6 ++- SHADE_Engine/src/UI/SHButtonComponent.h | 8 +-- .../src/UI/SHToggleButtonComponent.cpp | 3 +- SHADE_Engine/src/UI/SHToggleButtonComponent.h | 8 +-- SHADE_Engine/src/UI/SHUIComponent.cpp | 17 +++++++ SHADE_Engine/src/UI/SHUIComponent.h | 16 ++++++ SHADE_Engine/src/UI/SHUISystem.cpp | 51 +++++++++---------- SHADE_Engine/src/UI/SHUISystem.h | 2 +- 8 files changed, 64 insertions(+), 47 deletions(-) diff --git a/SHADE_Engine/src/UI/SHButtonComponent.cpp b/SHADE_Engine/src/UI/SHButtonComponent.cpp index 35b6b3bc..cbc36ce3 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.cpp +++ b/SHADE_Engine/src/UI/SHButtonComponent.cpp @@ -6,8 +6,7 @@ namespace SHADE { SHButtonComponent::SHButtonComponent() - :size(1.0f), isHovered(false), isClicked(false), - defaultTexture(0), hoveredTexture(0), clickedTexture(0), currentTexture(0) + : defaultTexture(0), hoveredTexture(0), clickedTexture(0), currentTexture(0) { } @@ -41,6 +40,9 @@ namespace SHADE clickedTexture = texture; } + + + } diff --git a/SHADE_Engine/src/UI/SHButtonComponent.h b/SHADE_Engine/src/UI/SHButtonComponent.h index bb66d224..a89a4b9e 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.h +++ b/SHADE_Engine/src/UI/SHButtonComponent.h @@ -17,8 +17,6 @@ namespace SHADE SHButtonComponent(); virtual ~SHButtonComponent() = default; - SHVec2 size; - AssetID GetClickedTexture() const noexcept; AssetID GetDefaultTexture() const noexcept; AssetID GetHoveredTexture() const noexcept; @@ -32,11 +30,7 @@ namespace SHADE friend class SHUISystem; private: - //Set to true when mouse is hovering over the button. - bool isHovered; - //This is set to true when the mouse clicks down, and set back to false when mouse releases. - //The event for the button click will be broadcasted when mouse release. - bool isClicked; + AssetID defaultTexture; AssetID hoveredTexture; AssetID clickedTexture; diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp b/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp index 47df669c..1ae0e9e0 100644 --- a/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp @@ -5,8 +5,7 @@ namespace SHADE { SHToggleButtonComponent::SHToggleButtonComponent() - :size(1.0f), isHovered(false), isClicked(false), value(false), - defaultTexture(0), toggledTexture(0), currentTexture(0) + :value(false), defaultTexture(0), toggledTexture(0), currentTexture(0) { } diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.h b/SHADE_Engine/src/UI/SHToggleButtonComponent.h index dc678fe9..1903df93 100644 --- a/SHADE_Engine/src/UI/SHToggleButtonComponent.h +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.h @@ -17,8 +17,6 @@ namespace SHADE SHToggleButtonComponent(); virtual ~SHToggleButtonComponent() = default; - SHVec2 size; - AssetID GetToggledTexture() const noexcept; AssetID GetDefaultTexture() const noexcept; bool GetValue() const noexcept; @@ -33,11 +31,7 @@ namespace SHADE friend class SHUISystem; private: - //Set to true when mouse is hovering over the button. - bool isHovered; - //This is set to true when the mouse clicks down, and set back to false when mouse releases. - //The event for the button click will be broadcasted when mouse release. - bool isClicked; + bool value; AssetID defaultTexture; AssetID toggledTexture; diff --git a/SHADE_Engine/src/UI/SHUIComponent.cpp b/SHADE_Engine/src/UI/SHUIComponent.cpp index 8131d081..95b93375 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.cpp +++ b/SHADE_Engine/src/UI/SHUIComponent.cpp @@ -7,6 +7,7 @@ namespace SHADE { SHUIComponent::SHUIComponent() + :size(1.0f), isHovered(false), isClicked(false) { } @@ -27,6 +28,20 @@ namespace SHADE (void)id; } + bool SHUIComponent::GetIsHovered() const noexcept + { + return isHovered; + } + + bool SHUIComponent::GetIsClicked() const noexcept + { + return isClicked; + } + + void SHUIComponent::SetEmptyHoveredClick(bool value) noexcept + { + (void)value; + } } @@ -37,6 +52,8 @@ RTTR_REGISTRATION registration::class_("UI Component") .property("Canvas ID", &SHUIComponent::GetCanvasID, &SHUIComponent::SetCanvasID) + .property("Hovered", &SHUIComponent::GetIsHovered, &SHUIComponent::SetEmptyHoveredClick) + .property("Clicked", &SHUIComponent::GetIsClicked, &SHUIComponent::SetEmptyHoveredClick) ; diff --git a/SHADE_Engine/src/UI/SHUIComponent.h b/SHADE_Engine/src/UI/SHUIComponent.h index 5a9290cc..dd5bb664 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.h +++ b/SHADE_Engine/src/UI/SHUIComponent.h @@ -5,6 +5,7 @@ #include "SH_API.h" #include "ECS_Base/Components/SHComponent.h" #include "Math/SHMatrix.h" +#include "Math/Vector/SHVec2.h" namespace SHADE @@ -17,14 +18,29 @@ namespace SHADE SHUIComponent(); ~SHUIComponent() = default; + + SHVec2 size; + + SHMatrix const& GetMatrix() const noexcept; EntityID GetCanvasID() const noexcept; void SetCanvasID(EntityID id) noexcept; + bool GetIsHovered() const noexcept; + bool GetIsClicked() const noexcept; + + void SetEmptyHoveredClick(bool value)noexcept; + + private: SHMatrix localToCanvasMatrix; EntityID canvasID; + //Set to true when mouse is hovering over the button. Only set if there is a Button/Slider/ToggleButton comp. + bool isHovered; + //This is set to true when the mouse clicks down, and set back to false when mouse releases. Only set if there is a Button/Slider/ToggleButton comp. + bool isClicked; + RTTR_ENABLE() }; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 3ddff353..69481b5e 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -146,9 +146,14 @@ namespace SHADE } } - bool SHUISystem::CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept + bool SHUISystem::CheckButtonHoveredOrClicked(SHUIComponent& comp) noexcept { + SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * comp.GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * comp.GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + + SHVec2 topExtent{ topExtent4.x,topExtent4.y }; + SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; auto cameraSystem = SHSystemManager::GetSystem(); SHVec2 mousePos; SHVec2 windowSize; @@ -185,13 +190,13 @@ namespace SHADE if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) { - if (isHovered == false) + if (comp.isHovered == false) { SHButtonClickEvent clickEvent; - clickEvent.EID = eid; + clickEvent.EID = comp.GetEID(); SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_ENTER_EVENT); } - isHovered = true; + comp.isHovered = true; @@ -200,9 +205,9 @@ namespace SHADE { if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { - isClicked = true; + comp.isClicked = true; SHButtonClickEvent clickEvent; - clickEvent.EID = eid; + clickEvent.EID = comp.GetEID(); SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } } @@ -211,7 +216,7 @@ namespace SHADE { comp.isClicked = true; SHButtonClickEvent clickEvent; - clickEvent.EID = eid; + clickEvent.EID = comp.GetEID(); SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } #endif @@ -220,21 +225,21 @@ namespace SHADE } else { - if (isHovered == true) + if (comp.isHovered == true) { SHButtonClickEvent clickEvent; - clickEvent.EID = eid; + clickEvent.EID = comp.GetEID(); SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_EXIT_EVENT); } - isHovered = false; + comp.isHovered = false; //SHLOG_INFO("NOT HOVERED") } - if (isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { - isClicked = false; + comp.isClicked = false; SHButtonClickEvent clickEvent; - clickEvent.EID = eid; + clickEvent.EID = comp.GetEID(); SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_RELEASE_EVENT); return true; @@ -256,14 +261,9 @@ namespace SHADE auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); //auto canvasComp = SHComponentManager::GetComponent_s(uiComp->canvasID); - SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); - SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f,0.0f, 0.0f,1.0f); - - SHVec2 topExtent{ topExtent4.x,topExtent4.y }; - SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; - CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked); + CheckButtonHoveredOrClicked(*uiComp); if (SHComponentManager::HasComponent(comp.GetEID())) { @@ -272,7 +272,7 @@ namespace SHADE AssetID textureID = 0; - if (!comp.isHovered && !comp.isClicked) + if (!uiComp->isHovered && !uiComp->isClicked) { if (comp.GetDefaultTexture() != 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) { @@ -281,7 +281,7 @@ namespace SHADE //SHLOG_INFO("SETTING DEFAULT TEXTURE") } } - else if (comp.isClicked) + else if (uiComp->isClicked) { if (comp.GetClickedTexture() != 0 && SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) { @@ -322,14 +322,9 @@ namespace SHADE auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); //auto canvasComp = SHComponentManager::GetComponent_s(uiComp->canvasID); - SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); - SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + - - SHVec2 topExtent{ topExtent4.x,topExtent4.y }; - SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; - - if (CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked)) + if (CheckButtonHoveredOrClicked(*uiComp)) comp.value = !comp.value; if (SHComponentManager::HasComponent(comp.GetEID())) diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index 1eaf883e..3b2bb2cf 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -74,7 +74,7 @@ namespace SHADE void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; //returns true on button release. - bool CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept; + bool CheckButtonHoveredOrClicked(SHUIComponent& comp) noexcept; From a3112f9c60d1cb8232bf9ec4ef437c219ac7a8b5 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 21 Feb 2023 10:48:07 +0800 Subject: [PATCH 3/3] 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