From 5190c490c9e3a6805cd9b27126c07d1e4852c6fb Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 11:36:12 +0800 Subject: [PATCH] added events --- Assets/Scenes/UI Test.shade | 4 +- Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/Events/SHEventDefines.h | 1 + .../src/UI/Events/SHButtonClickEvent.h | 16 +++ SHADE_Engine/src/UI/SHSliderComponent.cpp | 39 ++++++++ SHADE_Engine/src/UI/SHSliderComponent.h | 41 ++++++++ SHADE_Engine/src/UI/SHUISystem.cpp | 98 ++++++++++++++++--- SHADE_Engine/src/UI/SHUISystem.h | 16 ++- 8 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 SHADE_Engine/src/UI/Events/SHButtonClickEvent.h create mode 100644 SHADE_Engine/src/UI/SHSliderComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHSliderComponent.h diff --git a/Assets/Scenes/UI Test.shade b/Assets/Scenes/UI Test.shade index 0aea6f72..d9ce5026 100644 --- a/Assets/Scenes/UI Test.shade +++ b/Assets/Scenes/UI Test.shade @@ -34,7 +34,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 153.399994, y: 0, z: 0} + Translate: {x: 0, y: -3.9000001, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} IsActive: true @@ -45,7 +45,7 @@ Toggle Button Component: Non Toggled Texture: 0 Toggled Texture: 0 - Value: false + Value: true IsActive: true Scripts: ~ - EID: 1 diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index 9b0a85f5..0ce3b040 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 92992815 +ID: 92642496 Type: 5 diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index d7bbf5f0..1ad458c4 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -18,4 +18,5 @@ constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_REMOVED_EVENT { 9 }; constexpr SHEventIdentifier SH_EDITOR_ON_PLAY_EVENT { 10 }; constexpr SHEventIdentifier SH_EDITOR_ON_PAUSE_EVENT { 11 }; constexpr SHEventIdentifier SH_EDITOR_ON_STOP_EVENT { 12 }; +constexpr SHEventIdentifier SH_BUTTON_CLICK_EVENT { 13 }; diff --git a/SHADE_Engine/src/UI/Events/SHButtonClickEvent.h b/SHADE_Engine/src/UI/Events/SHButtonClickEvent.h new file mode 100644 index 00000000..35bcdc61 --- /dev/null +++ b/SHADE_Engine/src/UI/Events/SHButtonClickEvent.h @@ -0,0 +1,16 @@ +#pragma once + + +#include "ECS_Base/SHECSMacros.h" + +namespace SHADE +{ + struct SHButtonClickEvent + { + EntityID EID; + // value of the toggle button, default to false if its a button and not a toggle button + bool value{false}; + }; + + +} diff --git a/SHADE_Engine/src/UI/SHSliderComponent.cpp b/SHADE_Engine/src/UI/SHSliderComponent.cpp new file mode 100644 index 00000000..56d1d89b --- /dev/null +++ b/SHADE_Engine/src/UI/SHSliderComponent.cpp @@ -0,0 +1,39 @@ +#include "SHpch.h" +#include "SHSliderComponent.h" + +namespace SHADE +{ + SHSliderComponent::SHSliderComponent() + :size(1.0f), isHovered(false), isClicked(false), value(0.0f) + { + + } + + + float SHSliderComponent::GetValue() const noexcept + { + return value; + } + + void SHSliderComponent::SetValue(float value) noexcept + { + this->value = value; + } + + + +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Slider Component") + .property("Slider Value", &SHSliderComponent::GetValue, &SHSliderComponent::SetValue) + + ; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHSliderComponent.h b/SHADE_Engine/src/UI/SHSliderComponent.h new file mode 100644 index 00000000..bdc57c7e --- /dev/null +++ b/SHADE_Engine/src/UI/SHSliderComponent.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" +#include "Math/Vector/SHVec3.h" +#include "Math/Vector/SHVec2.h" +#include "Assets/SHAssetMacros.h" + +namespace SHADE +{ + + class SH_API SHSliderComponent final: public SHComponent + { + public: + SHSliderComponent(); + virtual ~SHSliderComponent() = default; + + SHVec2 size; + + + float GetValue() const noexcept; + + + void SetValue(float value) noexcept; + + + friend class SHUISystem; + private: + + bool isHovered; + bool isClicked; + + float value; + + RTTR_ENABLE() + }; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index cd4ed79a..bb98939b 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -10,6 +10,13 @@ #include "Editor/SHEditor.h" #include "Resource/SHResourceManager.h" #include "Input/SHInputManager.h" +#include "SHUIComponent.h" +#include "SHButtonComponent.h" +#include "SHToggleButtonComponent.h" +#include "SHSliderComponent.h" +#include "SHCanvasComponent.h" +#include "Events/SHEventManager.hpp" +#include "Events/SHButtonClickEvent.h" namespace SHADE { @@ -164,32 +171,60 @@ namespace SHADE 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 = { ws.first,ws.second }; + mousePos /= windowSize; #endif SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) - topExtent = CanvasToScreenPoint(topExtent); - btmExtent = CanvasToScreenPoint(btmExtent); + topExtent = CanvasToScreenPoint(topExtent,true); + btmExtent = CanvasToScreenPoint(btmExtent,true); //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - topExtent /= camSize; - btmExtent /= camSize; + 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::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + } +#else if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } - SHLOG_INFO("HOVERED") +#endif + + //SHLOG_INFO("HOVERED") } else { comp.isHovered = false; - SHLOG_INFO("NOT HOVERED") + //SHLOG_INFO("NOT HOVERED") } @@ -222,9 +257,25 @@ namespace SHADE SHVec2 mousePos; SHVec2 windowSize; + #ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; + 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 = { ws.first,ws.second }; mousePos /= windowSize; #endif @@ -232,10 +283,9 @@ namespace SHADE - topExtent = CanvasToScreenPoint(topExtent); - btmExtent = CanvasToScreenPoint(btmExtent); - topExtent /= camSize; - btmExtent /= camSize; + topExtent = CanvasToScreenPoint(topExtent,true); + btmExtent = CanvasToScreenPoint(btmExtent, true); + comp.isClicked = false; @@ -243,11 +293,30 @@ namespace SHADE && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) { comp.isHovered = true; +#ifdef SHEDITOR + if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) + { + if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + comp.value = !comp.value; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + clickEvent.value = comp.value; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + } +#else if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; comp.value = !comp.value; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + clickEvent.value = comp.value; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } +#endif } else { @@ -284,7 +353,7 @@ namespace SHADE } } - SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept + SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept { SHVec2 result{canvasPoint}; @@ -296,7 +365,10 @@ namespace SHADE result.y *= -1.0f; result += camSize * 0.5f; - return result; + if (normalized) + return result / camSize; + else + return result; } diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index a5ba8c4f..ae1091ec 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -3,15 +3,20 @@ #include "SH_API.h" #include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystemRoutine.h" -#include "SHUIComponent.h" -#include "SHButtonComponent.h" -#include "SHToggleButtonComponent.h" -#include "SHCanvasComponent.h" + #include "Scene/SHSceneGraph.h" #include "Scene/SHSceneManager.h" +#include "Math/Vector/SHVec2.h" namespace SHADE { + + class SHButtonComponent; + class SHUIComponent; + class SHToggleButtonComponent; + class SHSliderComponent; + class SHCanvasComponent; + class SH_API SHUISystem final: public SHSystem { public: @@ -67,7 +72,8 @@ namespace SHADE void UpdateButtonComponent(SHButtonComponent& comp) noexcept; void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; - SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept; + + SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept;