From baaeb9ee102d2b80b1b32d007f91d095cb5d0aaa Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:05:09 +0800 Subject: [PATCH 1/7] Input Binding File I/O --- Assets/Bindings.SHConfig | 52 +++++++ SHADE_Engine/src/Input/SHInputManager.cpp | 169 ++++++++++++++++++++++ SHADE_Engine/src/Input/SHInputManager.h | 11 ++ 3 files changed, 232 insertions(+) create mode 100644 Assets/Bindings.SHConfig diff --git a/Assets/Bindings.SHConfig b/Assets/Bindings.SHConfig new file mode 100644 index 00000000..e0023603 --- /dev/null +++ b/Assets/Bindings.SHConfig @@ -0,0 +1,52 @@ +4 +Horizontal +0 +0 +5 +0.2 +5 +0 +2 +39 +68 +2 +37 +65 +2 +3 +16 +1 +2 +Mouse Wheel +3 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 +Mouse X +1 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 +Mouse Y +2 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index cec78648..a94b66d4 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -11,6 +11,7 @@ #pragma once #include +#include #include "SHInputManager.h" #include "../Tools/SHException.h" @@ -99,6 +100,174 @@ namespace SHADE } } + //The Binding File format presently goes as such: + /* + * Name + Binding Type Enum + Inverted Bool + Gravity Double + Dead Double + Sensitivity Double + Snap Bool + PositiveKeyCode count + PositiveKeyCodes + NegativeKeyCode count + NegativeKeyCodes + PositiveControllerCode Count + PositiveControllerCodes + NegativeControllerCode Count + NegativeControllerCodes + */ + void SHInputManager::SaveBindings(std::string const& targetFile) noexcept + { + std::ofstream file; + file.open("../../Assets/Bindings.SHConfig"); + + //File cannot be written to + if (!file) return; + + //First write the number of bindings + file << bindings.size() << std::endl; + + for (auto& b : bindings) + { + //Name + file << b.first << std::endl; + + //Data + auto& lbd = b.second; + + file << static_cast(lbd.bindingType) << std::endl; + file << static_cast(lbd.inverted) << std::endl; + file << lbd.gravity << std::endl; + file << lbd.dead << std::endl; + file << lbd.sensitivity << std::endl; + file << static_cast(lbd.snap) << std::endl; + + //Bindings + file << lbd.positiveKeyCodes.size() << std::endl; + for (auto kc : lbd.positiveKeyCodes) + file << static_cast(kc) << std::endl; + file << lbd.negativeKeyCodes.size() << std::endl; + for (auto kc : lbd.negativeKeyCodes) + file << static_cast(kc) << std::endl; + file << lbd.positiveControllerCodes.size() << std::endl; + for (auto cc : lbd.positiveControllerCodes) + file << static_cast(cc) << std::endl; + file << lbd.negativeControllerCodes.size() << std::endl; + for (auto cc : lbd.negativeControllerCodes) + file << static_cast(cc) << std::endl; + } + + file.close(); + } + + void SHInputManager::LoadBindings(std::string const& sourceFile) noexcept + { + std::ifstream file; + file.open("../../Assets/Bindings.SHConfig"); + + //Check + if (!file) return; + + //Erase + ClearBindings(); + + //Read + std::string read; + + int count = 0; + std::getline(file, read); + count = std::stoi(read); + + std::string bindingName; + for (int b = 0; b < count; ++b) + { + //Name + std::getline(file, read); + SHLOGV_CRITICAL("Binding Name: {}", read); + bindingName = read; + AddBinding(bindingName); + + //Type + std::getline(file, read); + SHLOGV_CRITICAL("Binding Type: {}", read); + SetBindingType(bindingName, static_cast(std::stoi(read))); + + //Inversion + std::getline(file, read); + SHLOGV_CRITICAL("Inversion: {}", read); + SetBindingInverted(bindingName, static_cast(std::stoi(read))); + + //Gravity + std::getline(file, read); + SHLOGV_CRITICAL("Gravity: {}", read); + SetBindingGravity(bindingName, std::stod(read)); + + //Dead + std::getline(file, read); + SHLOGV_CRITICAL("Dead: {}", read); + SetBindingDead(bindingName, std::stod(read)); + + //Sensitivity + std::getline(file, read); + SHLOGV_CRITICAL("Sensitivity: {}", read); + SetBindingSensitivity(bindingName, std::stod(read)); + + //Snap + std::getline(file, read); + SHLOGV_CRITICAL("Snap: {}", read); + SetBindingSnap(bindingName, static_cast(std::stoi(read))); + + int count = 0; + //Positive Key Codes + std::getline(file, read); + SHLOGV_CRITICAL("Positive Key Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Positive Key: {}", read); + AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Key Codes + std::getline(file, read); + SHLOGV_CRITICAL("Negative Key Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Negative Key: {}", read); + AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Positive Controller Codes + std::getline(file, read); + SHLOGV_CRITICAL("Positive Controller Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Positive Controller: {}", read); + AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Controller Codes + std::getline(file, read); + SHLOGV_CRITICAL("Negative Controller Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Negative Controller: {}", read); + AddBindingNegativeControllerCode(bindingName, static_cast(std::stoi(read))); + } + } + + file.close(); + } + void SHInputManager::UpdateInput(double dt) noexcept { //Keyboard and Mouse Buttons//////////////////////////////////////////////// diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 3f708124..977a2d08 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -681,6 +681,17 @@ namespace SHADE return controllersReleasedTime[controllerNum][static_cast(code)]; } + /*------------------------------------------------------------------------*/ + /* Binding I/O */ + /*------------------------------------------------------------------------*/ + + //Save bindings registered into a file + static void SaveBindings(std::string const& targetFile = "Assets/InputBindings.SHConfig") noexcept; + + //Load and register bindings from a file + //The current list of bindings will be overwritten, so save them somewhere else before loading + static void LoadBindings(std::string const& sourceFile = "Assets/InputBindings.SHConfig") noexcept; + /*------------------------------------------------------------------------*/ /* Binding Functions */ /*------------------------------------------------------------------------*/ From 4123e76a7d991add7f03f57b2b6ba9512b5f92b0 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:36:19 +0800 Subject: [PATCH 2/7] Checking Input Binding I/O --- SHADE_Engine/src/Input/SHInputManager.cpp | 21 ++++----------------- SHADE_Engine/src/Input/SHInputManager.h | 4 ++-- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index a94b66d4..b8f329b9 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -102,6 +102,8 @@ namespace SHADE //The Binding File format presently goes as such: /* + * Binding count + * (For each binding:) * Name Binding Type Enum Inverted Bool @@ -121,7 +123,7 @@ namespace SHADE void SHInputManager::SaveBindings(std::string const& targetFile) noexcept { std::ofstream file; - file.open("../../Assets/Bindings.SHConfig"); + file.open(targetFile); //File cannot be written to if (!file) return; @@ -165,7 +167,7 @@ namespace SHADE void SHInputManager::LoadBindings(std::string const& sourceFile) noexcept { std::ifstream file; - file.open("../../Assets/Bindings.SHConfig"); + file.open(sourceFile); //Check if (!file) return; @@ -185,82 +187,67 @@ namespace SHADE { //Name std::getline(file, read); - SHLOGV_CRITICAL("Binding Name: {}", read); bindingName = read; AddBinding(bindingName); //Type std::getline(file, read); - SHLOGV_CRITICAL("Binding Type: {}", read); SetBindingType(bindingName, static_cast(std::stoi(read))); //Inversion std::getline(file, read); - SHLOGV_CRITICAL("Inversion: {}", read); SetBindingInverted(bindingName, static_cast(std::stoi(read))); //Gravity std::getline(file, read); - SHLOGV_CRITICAL("Gravity: {}", read); SetBindingGravity(bindingName, std::stod(read)); //Dead std::getline(file, read); - SHLOGV_CRITICAL("Dead: {}", read); SetBindingDead(bindingName, std::stod(read)); //Sensitivity std::getline(file, read); - SHLOGV_CRITICAL("Sensitivity: {}", read); SetBindingSensitivity(bindingName, std::stod(read)); //Snap std::getline(file, read); - SHLOGV_CRITICAL("Snap: {}", read); SetBindingSnap(bindingName, static_cast(std::stoi(read))); int count = 0; //Positive Key Codes std::getline(file, read); - SHLOGV_CRITICAL("Positive Key Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Positive Key: {}", read); AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); } //Negative Key Codes std::getline(file, read); - SHLOGV_CRITICAL("Negative Key Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Negative Key: {}", read); AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); } //Positive Controller Codes std::getline(file, read); - SHLOGV_CRITICAL("Positive Controller Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Positive Controller: {}", read); AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); } //Negative Controller Codes std::getline(file, read); - SHLOGV_CRITICAL("Negative Controller Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Negative Controller: {}", read); AddBindingNegativeControllerCode(bindingName, static_cast(std::stoi(read))); } } diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 977a2d08..01ef6c42 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -686,11 +686,11 @@ namespace SHADE /*------------------------------------------------------------------------*/ //Save bindings registered into a file - static void SaveBindings(std::string const& targetFile = "Assets/InputBindings.SHConfig") noexcept; + static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; //Load and register bindings from a file //The current list of bindings will be overwritten, so save them somewhere else before loading - static void LoadBindings(std::string const& sourceFile = "Assets/InputBindings.SHConfig") noexcept; + static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ /* Binding Functions */ From 7dbd0b93b3cff3bb85bb48565e7a85ee68cb0caf Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:39:48 +0800 Subject: [PATCH 3/7] Minor comment fix --- SHADE_Engine/src/Input/SHInputManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 01ef6c42..30ee00e8 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -689,7 +689,7 @@ namespace SHADE static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; //Load and register bindings from a file - //The current list of bindings will be overwritten, so save them somewhere else before loading + //If specified file exists, the current list of bindings will be overwritten, so save them somewhere else before loading static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ From 4f63558f4021d2fea154b59f83cfa0ec77022bae Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 10:44:36 +0800 Subject: [PATCH 4/7] Added GetComponents to Component Manager --- .../ECS_Base/Managers/SHComponentManager.h | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h index 8921fbce..9fcbf6f8 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h @@ -23,7 +23,7 @@ #include "SH_API.h" #include "Events/SHEventManager.hpp" - +#include #include namespace SHADE @@ -151,6 +151,32 @@ namespace SHADE return (componentSet.GetSparseSet()->GetElement_s(EntityHandleGenerator::GetIndex(entityID))); } + /*!************************************************************************* + * \brief + * Gets the Component of the entity with the specified entityID + * + * This is the safe version of GetComponent_s which does a HasComponent to make + * sure that the entity has such a component and returns nullptr if it doesn't + * + * This safe version also checks if the sparse set of this component type + * has been created in SHComponentManager and creates one if it doesn't + * + * @tparam T... + * Pack of Types for all the Components to get. + * \param entityID + * EntityID of the entity that we are trying to get the component of. + * \return + * A tuple of pointers to all the components specified. + * Returns nullptr if the entity does not contain such a component. + ***************************************************************************/ + + template + static std::enable_if_t<(... && std::is_base_of_v), std::tuple> GetComponents(EntityID entityID) noexcept + { + return std::make_tuple(GetComponent_s(entityID)...); + } + + /*!************************************************************************* * \brief * Gets the Component of the entity with the specified entityID From 356ec24cc20f93693cef18aa6ff165c3f1cfa299 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Mon, 9 Jan 2023 17:21:24 +0800 Subject: [PATCH 5/7] Change default pathing for binding file I/O --- Assets/Bindings.SHConfig | 51 ------------------------- SHADE_Engine/src/Input/SHInputManager.h | 5 ++- 2 files changed, 3 insertions(+), 53 deletions(-) diff --git a/Assets/Bindings.SHConfig b/Assets/Bindings.SHConfig index e0023603..573541ac 100644 --- a/Assets/Bindings.SHConfig +++ b/Assets/Bindings.SHConfig @@ -1,52 +1 @@ -4 -Horizontal -0 -0 -5 -0.2 -5 -0 -2 -39 -68 -2 -37 -65 -2 -3 -16 -1 -2 -Mouse Wheel -3 -0 -1 -0.2 -1 -0 -0 -0 -0 -0 -Mouse X -1 -0 -1 -0.2 -1 -0 -0 -0 -0 -0 -Mouse Y -2 -0 -1 -0.2 -1 -0 -0 -0 -0 0 diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 30ee00e8..680035c3 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -14,6 +14,7 @@ #include #include #include "../../SHADE_Managed/src/SHpch.h" +#include "../../SHADE_Engine/src/Assets/SHAssetMacros.h" #include "SH_API.h" #pragma comment(lib, "xinput.lib") @@ -686,11 +687,11 @@ namespace SHADE /*------------------------------------------------------------------------*/ //Save bindings registered into a file - static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; + static void SaveBindings(std::string const& targetFile = std::string(ASSET_ROOT) + "/Bindings.SHConfig") noexcept; //Load and register bindings from a file //If specified file exists, the current list of bindings will be overwritten, so save them somewhere else before loading - static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; + static void LoadBindings(std::string const& sourceFile = std::string(ASSET_ROOT) + "/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ /* Binding Functions */ From 5aa7bfe03e5b1937d43f570db51cef6786862e01 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 07:34:44 +0800 Subject: [PATCH 6/7] button fixed --- Assets/Editor/Editor.SHConfig | 4 ++++ Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 Assets/Editor/Editor.SHConfig diff --git a/Assets/Editor/Editor.SHConfig b/Assets/Editor/Editor.SHConfig new file mode 100644 index 00000000..51425027 --- /dev/null +++ b/Assets/Editor/Editor.SHConfig @@ -0,0 +1,4 @@ +Start Maximized: true +Working Scene ID: 97161771 +Window Size: {x: 1920, y: 1080} +Style: 0 \ No newline at end of file diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index ad821810..9b0a85f5 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 88543249 +ID: 92992815 Type: 5 diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 95663bc0..cd4ed79a 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -158,7 +158,7 @@ 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) From 5190c490c9e3a6805cd9b27126c07d1e4852c6fb Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 11:36:12 +0800 Subject: [PATCH 7/7] 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;