diff --git a/Assets/Application.SHConfig b/Assets/Application.SHConfig index 3cf8846b..5673556d 100644 --- a/Assets/Application.SHConfig +++ b/Assets/Application.SHConfig @@ -1,4 +1,4 @@ Start in Fullscreen: false -Starting Scene ID: 97161771 +Starting Scene ID: 97158628 Window Size: {x: 1920, y: 1080} Window Title: SHADE Engine \ No newline at end of file diff --git a/Assets/Bindings.SHConfig b/Assets/Bindings.SHConfig new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/Assets/Bindings.SHConfig @@ -0,0 +1 @@ +0 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 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 f32277e0..0ce3b040 100644 --- a/Assets/Scenes/UI_Test.shade.shmeta +++ b/Assets/Scenes/UI_Test.shade.shmeta @@ -1,3 +1,3 @@ -Name: UI_Test -ID: 97161771 +Name: UI Test +ID: 92642496 Type: 5 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 diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index 8077fcc1..c090cd69 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -23,4 +23,5 @@ constexpr SHEventIdentifier SH_SCENE_INIT_POST { 14 }; constexpr SHEventIdentifier SH_SCENE_EXIT_PRE { 15 }; constexpr SHEventIdentifier SH_SCENE_EXIT_POST { 16 }; constexpr SHEventIdentifier SH_GRAPHICS_LIGHT_ENABLE_SHADOW_EVENT { 17 }; +constexpr SHEventIdentifier SH_BUTTON_CLICK_EVENT { 18 }; diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index cec78648..b8f329b9 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,161 @@ namespace SHADE } } + //The Binding File format presently goes as such: + /* + * Binding count + * (For each binding:) + * 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(targetFile); + + //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(sourceFile); + + //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); + bindingName = read; + AddBinding(bindingName); + + //Type + std::getline(file, read); + SetBindingType(bindingName, static_cast(std::stoi(read))); + + //Inversion + std::getline(file, read); + SetBindingInverted(bindingName, static_cast(std::stoi(read))); + + //Gravity + std::getline(file, read); + SetBindingGravity(bindingName, std::stod(read)); + + //Dead + std::getline(file, read); + SetBindingDead(bindingName, std::stod(read)); + + //Sensitivity + std::getline(file, read); + SetBindingSensitivity(bindingName, std::stod(read)); + + //Snap + std::getline(file, read); + SetBindingSnap(bindingName, static_cast(std::stoi(read))); + + int count = 0; + //Positive Key Codes + std::getline(file, read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Key Codes + std::getline(file, read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Positive Controller Codes + std::getline(file, read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Controller Codes + std::getline(file, read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, 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..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") @@ -681,6 +682,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 = 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 = std::string(ASSET_ROOT) + "/Bindings.SHConfig") noexcept; + /*------------------------------------------------------------------------*/ /* Binding Functions */ /*------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index a055d91c..cc081413 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -350,7 +350,6 @@ namespace SHADE SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); - //SHSerializationHelper::ConvertNodeToComponent(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); } 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 cd2bef82..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 { @@ -20,7 +27,6 @@ namespace SHADE SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); - SHComponentManager::CreateComponentSparseSet(); } void SHUISystem::Exit() @@ -163,35 +169,62 @@ namespace SHADE mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) - SHLOG_INFO("window size: {}, {}", windowSize.x, windowSize.y) - //mousePos /= windowSize; - //SHLOG_INFO("mouse pos normalized: {}, {}", 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") } @@ -224,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 @@ -234,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; @@ -245,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 { @@ -286,7 +353,7 @@ namespace SHADE } } - SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept + SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept { SHVec2 result{canvasPoint}; @@ -298,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;