From 9a5dc52d776f47aaf77478289a6b2d185dc58ad5 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Mon, 5 Dec 2022 23:25:43 +0800 Subject: [PATCH 01/42] Minor fixes to input manager - Pass binding names into functions by const reference instead of by value - Fixed oversight of not being able to modify or read mouse Y positive multiplier for a binding --- SHADE_Engine/src/Input/SHInputManager.cpp | 24 ++++---- SHADE_Engine/src/Input/SHInputManager.h | 70 +++++++++++------------ 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index 4849a772..f6b58a94 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -575,7 +575,7 @@ namespace SHADE } //Only get of largest magnitude - double SHInputManager::GetBindingAxis(std::string bindingName, size_t cNum) noexcept + double SHInputManager::GetBindingAxis(std::string const& bindingName, size_t cNum) noexcept { //Over keycodes, prioritise positive for (SH_KEYCODE k : bindings[bindingName].positiveKeyCodes) @@ -606,7 +606,7 @@ namespace SHADE return largestMagnitude; } - bool SHInputManager::GetBindingPositiveButton(std::string bindingName, size_t cNum) noexcept + bool SHInputManager::GetBindingPositiveButton(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return false; @@ -625,7 +625,7 @@ namespace SHADE return false; } - bool SHInputManager::GetBindingNegativeButton(std::string bindingName, size_t cNum) noexcept + bool SHInputManager::GetBindingNegativeButton(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return false; @@ -644,7 +644,7 @@ namespace SHADE return false; } - bool SHInputManager::GetBindingPositiveButtonDown(std::string bindingName, size_t cNum) noexcept + bool SHInputManager::GetBindingPositiveButtonDown(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return false; @@ -663,7 +663,7 @@ namespace SHADE return false; } - bool SHInputManager::GetBindingNegativeButtonDown(std::string bindingName, size_t cNum) noexcept + bool SHInputManager::GetBindingNegativeButtonDown(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return false; @@ -682,7 +682,7 @@ namespace SHADE return false; } - bool SHInputManager::GetBindingPositiveButtonUp(std::string bindingName, size_t cNum) noexcept + bool SHInputManager::GetBindingPositiveButtonUp(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return false; @@ -701,7 +701,7 @@ namespace SHADE return false; } - bool SHInputManager::GetBindingNegativeButtonUp(std::string bindingName, size_t cNum) noexcept + bool SHInputManager::GetBindingNegativeButtonUp(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return false; @@ -721,7 +721,7 @@ namespace SHADE } //Fetches longest hold time - double SHInputManager::GetBindingPositiveHeldTime(std::string bindingName, size_t cNum) noexcept + double SHInputManager::GetBindingPositiveHeldTime(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return 0.0; @@ -742,7 +742,7 @@ namespace SHADE return maxHeldTime; } - double SHInputManager::GetBindingNegativeHeldTime(std::string bindingName, size_t cNum) noexcept + double SHInputManager::GetBindingNegativeHeldTime(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return 0.0; @@ -764,7 +764,7 @@ namespace SHADE } //Fetches shortest release time - double SHInputManager::GetBindingPositiveReleasedTime(std::string bindingName, size_t cNum) noexcept + double SHInputManager::GetBindingPositiveReleasedTime(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return 0.0; @@ -785,7 +785,7 @@ namespace SHADE return minReleaseTime; } - double SHInputManager::GetBindingNegativeReleasedTime(std::string bindingName, size_t cNum) noexcept + double SHInputManager::GetBindingNegativeReleasedTime(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return 0.0; @@ -808,7 +808,7 @@ namespace SHADE //Only for mouse movement //Get largest delta - double SHInputManager::GetBindingMouseVelocity(std::string bindingName, size_t cNum) noexcept + double SHInputManager::GetBindingMouseVelocity(std::string const& bindingName, size_t cNum) noexcept { if (cNum >= XUSER_MAX_COUNT) return 0.0; diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 04e5871d..ce3e69aa 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -319,8 +319,8 @@ namespace SHADE std::set negativeControllerCodes; //Mouse movement mapped to axes? - double mouseXPositiveMultiplier; - double mouseYPositiveMultiplier; + double mouseXPositiveMultiplier = 0.0f; + double mouseYPositiveMultiplier = 0.0f; }; public: @@ -484,7 +484,7 @@ namespace SHADE } /*------------------------------------------------------------------------*/ - /* Input state accessors (KB & M) */ + /* Input state accessors (Controller) */ /*------------------------------------------------------------------------*/ //How many controller inputs of any kind are being used now @@ -622,14 +622,14 @@ namespace SHADE /*------------------------------------------------------------------------*/ //Add a new binding to the map - static inline void BindingsAdd(std::string newBindingName) noexcept + static inline void BindingsAdd(std::string const& newBindingName) noexcept { bindings.insert({ newBindingName, SHLogicalBindingData() }); } //Remove a binding from the map //Returns 1 if found and removed, 0 if not found - static inline size_t BindingsRemove(std::string targetBindingName) noexcept + static inline size_t BindingsRemove(std::string const& targetBindingName) noexcept { return bindings.erase(targetBindingName); } @@ -647,13 +647,13 @@ namespace SHADE } //Check positive keycodes to binding - static inline std::set const& BindingsGetPositiveKeyCodes(std::string bindingName) noexcept + static inline std::set const& BindingsGetPositiveKeyCodes(std::string const& bindingName) noexcept { return bindings[bindingName].positiveKeyCodes; } //Add positive SH_KEYCODE to binding - static inline void BindingsAddPositiveKeyCode(std::string targetBindingName, + static inline void BindingsAddPositiveKeyCode(std::string const& targetBindingName, SH_KEYCODE toAdd) noexcept { bindings[targetBindingName].positiveKeyCodes.insert(toAdd); @@ -661,20 +661,20 @@ namespace SHADE //Remove positive SH_KEYCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemovePositiveKeyCode(std::string targetBindingName, + static inline size_t BindingsRemovePositiveKeyCode(std::string const& targetBindingName, SH_KEYCODE toRemove) noexcept { return bindings[targetBindingName].positiveKeyCodes.erase(toRemove); } //Check negative keycodes to binding - static inline std::set const& BindingsGetNegativeKeyCodes(std::string bindingName) noexcept + static inline std::set const& BindingsGetNegativeKeyCodes(std::string const& bindingName) noexcept { return bindings[bindingName].negativeKeyCodes; } //Add negative SH_KEYCODE to binding - static inline void BindingsAddNegativeKeyCode(std::string targetBindingName, + static inline void BindingsAddNegativeKeyCode(std::string const& targetBindingName, SH_KEYCODE toAdd) noexcept { bindings[targetBindingName].negativeKeyCodes.insert(toAdd); @@ -682,20 +682,20 @@ namespace SHADE //Remove negative SH_KEYCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemoveNegativeKeyCode(std::string targetBindingName, + static inline size_t BindingsRemoveNegativeKeyCode(std::string const& targetBindingName, SH_KEYCODE toRemove) noexcept { return bindings[targetBindingName].negativeKeyCodes.erase(toRemove); } //Check positive controllercodes to binding - static inline std::set const& BindingsGetPositiveControllerCodes(std::string bindingName) noexcept + static inline std::set const& BindingsGetPositiveControllerCodes(std::string const& bindingName) noexcept { return bindings[bindingName].positiveControllerCodes; } //Add positive SH_CONTROLLERCODE to binding - static inline void BindingsAddPositiveControllerCode(std::string targetBindingName, + static inline void BindingsAddPositiveControllerCode(std::string const& targetBindingName, SH_CONTROLLERCODE toAdd) noexcept { bindings[targetBindingName].positiveControllerCodes.insert(toAdd); @@ -703,20 +703,20 @@ namespace SHADE //Remove positive SH_CONTROLLERCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemovePositiveControllerCode(std::string targetBindingName, + static inline size_t BindingsRemovePositiveControllerCode(std::string const& targetBindingName, SH_CONTROLLERCODE toRemove) noexcept { return bindings[targetBindingName].positiveControllerCodes.erase(toRemove); } //Check negative controllercodes to binding - static inline std::set const& BindingsGetNegativeControllerCodes(std::string bindingName) noexcept + static inline std::set const& BindingsGetNegativeControllerCodes(std::string const& bindingName) noexcept { return bindings[bindingName].negativeControllerCodes; } //Add negative SH_CONTROLLERCODE to binding - static inline void BindingsAddNegativeControllerCode(std::string targetBindingName, + static inline void BindingsAddNegativeControllerCode(std::string const& targetBindingName, SH_CONTROLLERCODE toAdd) noexcept { bindings[targetBindingName].negativeControllerCodes.insert(toAdd); @@ -724,7 +724,7 @@ namespace SHADE //Remove negative SH_CONTROLLERCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemoveNegativeControllerCode(std::string targetBindingName, + static inline size_t BindingsRemoveNegativeControllerCode(std::string const& targetBindingName, SH_CONTROLLERCODE toRemove) noexcept { return bindings[targetBindingName].negativeControllerCodes.erase(toRemove); @@ -732,57 +732,57 @@ namespace SHADE //Mouse movement bindings - static inline double const BindingsGetMouseXPositiveMultiplier(std::string bindingName) noexcept + static inline double const BindingsGetMouseXPositiveMultiplier(std::string const& bindingName) noexcept { return bindings[bindingName].mouseXPositiveMultiplier; } - static inline void BindingsSetMouseXPositiveMultiplier(std::string bindingName, double newValue) noexcept + static inline void BindingsSetMouseXPositiveMultiplier(std::string const& bindingName, double newValue) noexcept { bindings[bindingName].mouseXPositiveMultiplier = newValue; } - static inline double const BindingsGetMouseYPositiveMultiplier(std::string bindingName) noexcept + static inline double const BindingsGetMouseYPositiveMultiplier(std::string const& bindingName) noexcept { - return bindings[bindingName].mouseXPositiveMultiplier; + return bindings[bindingName].mouseYPositiveMultiplier; } - static inline void BindingsSetMouseYPositiveMultiplier(std::string bindingName, double newValue) noexcept + static inline void BindingsSetMouseYPositiveMultiplier(std::string const& bindingName, double newValue) noexcept { - bindings[bindingName].mouseXPositiveMultiplier = newValue; + bindings[bindingName].mouseYPositiveMultiplier = newValue; } //Get the axis value of binding, between -1 and 1 - static double GetBindingAxis(std::string bindingName, size_t controllerNumber = 0) noexcept; + static double GetBindingAxis(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Whether binding is being held or not //Does not work for mouse movement - static bool GetBindingPositiveButton(std::string bindingName, size_t controllerNumber = 0) noexcept; - static bool GetBindingNegativeButton(std::string bindingName, size_t controllerNumber = 0) noexcept; + static bool GetBindingPositiveButton(std::string const& bindingName, size_t controllerNumber = 0) noexcept; + static bool GetBindingNegativeButton(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Whether binding is pressed down IN THIS FRAME ONLY //Does not work for mouse movement - static bool GetBindingPositiveButtonDown(std::string bindingName, size_t controllerNumber = 0) noexcept; - static bool GetBindingNegativeButtonDown(std::string bindingName, size_t controllerNumber = 0) noexcept; + static bool GetBindingPositiveButtonDown(std::string const& bindingName, size_t controllerNumber = 0) noexcept; + static bool GetBindingNegativeButtonDown(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Whether binding is released IN THIS FRAME ONLY //Does not work for mouse movement - static bool GetBindingPositiveButtonUp(std::string bindingName, size_t controllerNumber = 0) noexcept; - static bool GetBindingNegativeButtonUp(std::string bindingName, size_t controllerNumber = 0) noexcept; + static bool GetBindingPositiveButtonUp(std::string const& bindingName, size_t controllerNumber = 0) noexcept; + static bool GetBindingNegativeButtonUp(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Binding times //Does not work for mouse movement - static double GetBindingPositiveHeldTime(std::string bindingName, size_t controllerNumber = 0) noexcept; - static double GetBindingNegativeHeldTime(std::string bindingName, size_t controllerNumber = 0) noexcept; + static double GetBindingPositiveHeldTime(std::string const& bindingName, size_t controllerNumber = 0) noexcept; + static double GetBindingNegativeHeldTime(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Does not work for mouse movement - static double GetBindingPositiveReleasedTime(std::string bindingName, size_t controllerNumber = 0) noexcept; - static double GetBindingNegativeReleasedTime(std::string bindingName, size_t controllerNumber = 0) noexcept; + static double GetBindingPositiveReleasedTime(std::string const& bindingName, size_t controllerNumber = 0) noexcept; + static double GetBindingNegativeReleasedTime(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Binding mouse velocity //Only for mouse movement - static double GetBindingMouseVelocity(std::string bindingName, size_t controllerNumber = 0) noexcept; + static double GetBindingMouseVelocity(std::string const& bindingName, size_t controllerNumber = 0) noexcept; /*------------------------------------------------------------------------*/ /* Other Functions */ From 8212ed2280396a7f7383e96cd57a24a8c1f6eac4 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 20 Dec 2022 20:29:28 +0800 Subject: [PATCH 02/42] Application::Quit() no longer kills the application if in editor --- SHADE_Managed/src/Engine/Application.cxx | 9 ++++++++- SHADE_Managed/src/Engine/Application.hxx | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Engine/Application.cxx b/SHADE_Managed/src/Engine/Application.cxx index c19bafa6..06ad632f 100644 --- a/SHADE_Managed/src/Engine/Application.cxx +++ b/SHADE_Managed/src/Engine/Application.cxx @@ -44,6 +44,10 @@ namespace SHADE return false; } + bool Application::IsEditor::get() + { + return SHSystemManager::GetSystem() != nullptr; + } int Application::WindowWidth::get() { return SHGraphicsSystemInterface::GetWindowWidth(); @@ -66,6 +70,9 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void Application::Quit() { - SHGraphicsSystemInterface::CloseWindow(); + if (!IsEditor) + { + SHGraphicsSystemInterface::CloseWindow(); + } } } diff --git a/SHADE_Managed/src/Engine/Application.hxx b/SHADE_Managed/src/Engine/Application.hxx index 8629cf75..4467ec3b 100644 --- a/SHADE_Managed/src/Engine/Application.hxx +++ b/SHADE_Managed/src/Engine/Application.hxx @@ -43,6 +43,13 @@ namespace SHADE bool get(); } /// + /// True if the engine is running in the editor. + /// + static property bool IsEditor + { + bool get(); + } + /// /// Retrieves the designated width of the current window. /// static property int WindowWidth @@ -71,6 +78,7 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /// /// Marks the application to stop at the end of the current frame. + /// If running in the editor, this function does nothing. /// static void Quit(); }; From 88e89a226a895c2ee0d62ac6db50e50301ac706f Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 20 Dec 2022 22:35:47 +0800 Subject: [PATCH 03/42] Added the option to open the script csproj via menu bar --- .../EditorWindow/MenuBar/SHEditorMenuBar.cpp | 5 ++ SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 73 ++++++++++++------- SHADE_Engine/src/Scripting/SHScriptEngine.h | 5 ++ 3 files changed, 58 insertions(+), 25 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp index a1335e19..22d78173 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp @@ -120,6 +120,11 @@ namespace SHADE auto* scriptEngine = static_cast(SHSystemManager::GetSystem()); scriptEngine->GenerateScriptsCsProjFile(); } + if (ImGui::Selectable("Open Visual Studio Project")) + { + auto* scriptEngine = static_cast(SHSystemManager::GetSystem()); + scriptEngine->OpenSolution(); + } ImGui::BeginDisabled(SHSystemManager::GetSystem()->editorState != SHEditor::State::STOP); if (ImGui::Selectable("Build Scripts - Debug")) { diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 3746d1d0..76a57242 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -306,6 +306,22 @@ namespace SHADE file.close(); } + void SHScriptEngine::OpenSolution() + { + // Generate csproj file if it doesn't exist + if (!std::filesystem::exists(CSPROJ_PATH)) + { + GenerateScriptsCsProjFile(CSPROJ_PATH); + } + + // Open it + execProcessNoBlock + ( + L"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe", + L"/Edit " + SHStringUtilities::StrToWstr(CSPROJ_PATH) + ); + } + /*-----------------------------------------------------------------------------------*/ /* Event Handler Functions */ /*-----------------------------------------------------------------------------------*/ @@ -612,31 +628,7 @@ namespace SHADE DWORD SHScriptEngine::execProcess(const std::wstring& path, const std::wstring& args) { - STARTUPINFOW startInfo; - PROCESS_INFORMATION procInfo; - ZeroMemory(&startInfo, sizeof(startInfo)); - ZeroMemory(&procInfo, sizeof(procInfo)); - startInfo.cb = sizeof(startInfo); - - std::wstring argsWstr = args; - - // Start Process - const auto SUCCESS = CreateProcess - ( - path.data(), argsWstr.data(), - nullptr, nullptr, false, NULL, nullptr, nullptr, - &startInfo, &procInfo - ); - - // Error Check - if (!SUCCESS) - { - auto err = GetLastError(); - std::ostringstream oss; - oss << "[ScriptEngine] Failed to launch process. Error code: " << std::hex << err - << " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")"; - throw std::runtime_error(oss.str()); - } + PROCESS_INFORMATION procInfo = execProcessNoBlock(path, args); // Wait for execution to end DWORD status; @@ -662,6 +654,37 @@ namespace SHADE } } + PROCESS_INFORMATION SHScriptEngine::execProcessNoBlock(const std::wstring& path, const std::wstring& args) + { + STARTUPINFOW startInfo; + PROCESS_INFORMATION procInfo; + ZeroMemory(&startInfo, sizeof(startInfo)); + ZeroMemory(&procInfo, sizeof(procInfo)); + startInfo.cb = sizeof(startInfo); + + std::wstring argsWstr = args; + + // Start Process + const auto SUCCESS = CreateProcess + ( + path.data(), argsWstr.data(), + nullptr, nullptr, false, NULL, nullptr, nullptr, + &startInfo, &procInfo + ); + + // Error Check + if (!SUCCESS) + { + auto err = GetLastError(); + std::ostringstream oss; + oss << "[ScriptEngine] Failed to launch process. Error code: " << std::hex << err + << " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")"; + throw std::runtime_error(oss.str()); + } + + return procInfo; + } + std::wstring SHScriptEngine::generateBuildCommand(bool debug) { std::wostringstream oss; diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 1a38a678..40efa042 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -217,6 +217,10 @@ namespace SHADE /// /// File path to the generated file. void GenerateScriptsCsProjFile(const std::filesystem::path& path = CSPROJ_PATH) const; + /// + /// Opens the script solution in Visual Studio 2019. + /// + void OpenSolution(); private: /*-----------------------------------------------------------------------------*/ @@ -321,6 +325,7 @@ namespace SHADE static bool fileExists(const std::filesystem::path& filePath); static bool copyFile(const std::filesystem::path& from, const std::filesystem::path& to, const std::filesystem::copy_options options) noexcept; static DWORD execProcess(const std::wstring& path, const std::wstring& args); + static PROCESS_INFORMATION execProcessNoBlock(const std::wstring& path, const std::wstring& args); static std::wstring generateBuildCommand(bool debug); }; } From 360b362b7b4500052790f2d61ac8b367812ad8c2 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Wed, 21 Dec 2022 16:47:10 +0800 Subject: [PATCH 04/42] Moved command and process execution helpers to SHExecUtilities --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 68 +----------- SHADE_Engine/src/Scripting/SHScriptEngine.h | 4 +- .../src/Tools/Utilities/SHExecUtilities.cpp | 103 ++++++++++++++++++ .../src/Tools/Utilities/SHExecUtilities.h | 84 ++++++++++++++ 4 files changed, 191 insertions(+), 68 deletions(-) create mode 100644 SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp create mode 100644 SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 76a57242..b4f893d7 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -30,6 +30,7 @@ of DigiPen Institute of Technology is prohibited. #include "Scene/SHSceneEvents.h" #include "Assets/SHAssetMacros.h" +#include "Tools/Utilities/SHExecUtilities.h" namespace SHADE { @@ -189,11 +190,7 @@ namespace SHADE oss << "[ScriptEngine] Building " << (debug ? " debug " : "") << "Managed Script Assembly (" << MANAGED_SCRIPT_LIB_NAME << ")!"; SHLOG_INFO(oss.str()); oss.str(""); - const bool BUILD_SUCCESS = execProcess - ( - L"C:\\Windows\\system32\\cmd.exe", - L"/K \"" + generateBuildCommand(debug) + L" & exit\"" - ) == 0; + const bool BUILD_SUCCESS = SHExecUtilties::ExecBlockingCommand(generateBuildCommand(debug)) == 0; if (BUILD_SUCCESS) { // Copy to built dll to the working directory and replace @@ -315,7 +312,7 @@ namespace SHADE } // Open it - execProcessNoBlock + SHExecUtilties::ExecProcess ( L"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe", L"/Edit " + SHStringUtilities::StrToWstr(CSPROJ_PATH) @@ -626,65 +623,6 @@ namespace SHADE } } - DWORD SHScriptEngine::execProcess(const std::wstring& path, const std::wstring& args) - { - PROCESS_INFORMATION procInfo = execProcessNoBlock(path, args); - - // Wait for execution to end - DWORD status; - while (true) - { - const auto EXEC_SUCCESS = GetExitCodeProcess(procInfo.hProcess, &status); - if (!EXEC_SUCCESS) - { - auto err = GetLastError(); - std::ostringstream oss; - oss << "[ScriptEngine] Failed to query process. Error code: " << std::hex << err - << " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")"; - throw std::runtime_error(oss.str()); - } - - // Break only if process ends - if (status != STILL_ACTIVE) - { - CloseHandle(procInfo.hProcess); - CloseHandle(procInfo.hThread); - return status; - } - } - } - - PROCESS_INFORMATION SHScriptEngine::execProcessNoBlock(const std::wstring& path, const std::wstring& args) - { - STARTUPINFOW startInfo; - PROCESS_INFORMATION procInfo; - ZeroMemory(&startInfo, sizeof(startInfo)); - ZeroMemory(&procInfo, sizeof(procInfo)); - startInfo.cb = sizeof(startInfo); - - std::wstring argsWstr = args; - - // Start Process - const auto SUCCESS = CreateProcess - ( - path.data(), argsWstr.data(), - nullptr, nullptr, false, NULL, nullptr, nullptr, - &startInfo, &procInfo - ); - - // Error Check - if (!SUCCESS) - { - auto err = GetLastError(); - std::ostringstream oss; - oss << "[ScriptEngine] Failed to launch process. Error code: " << std::hex << err - << " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")"; - throw std::runtime_error(oss.str()); - } - - return procInfo; - } - std::wstring SHScriptEngine::generateBuildCommand(bool debug) { std::wostringstream oss; diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 40efa042..d26b360e 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -1,5 +1,5 @@ /************************************************************************************//*! -\file ScriptEngine.h +\file SHScriptEngine.h \author Tng Kah Wei, kahwei.tng, 390009620 \par email: kahwei.tng\@digipen.edu \date Sep 17, 2021 @@ -324,8 +324,6 @@ namespace SHADE /// True if the file exists static bool fileExists(const std::filesystem::path& filePath); static bool copyFile(const std::filesystem::path& from, const std::filesystem::path& to, const std::filesystem::copy_options options) noexcept; - static DWORD execProcess(const std::wstring& path, const std::wstring& args); - static PROCESS_INFORMATION execProcessNoBlock(const std::wstring& path, const std::wstring& args); static std::wstring generateBuildCommand(bool debug); }; } diff --git a/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp new file mode 100644 index 00000000..c1bae764 --- /dev/null +++ b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp @@ -0,0 +1,103 @@ +/************************************************************************************//*! +\file SHExecUtilities.cpp +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Dec 21, 2022 +\brief Contains the implementation for SHExecUtilities static class. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include +// Primary Header +#include "SHExecUtilities.h" +// Project Includes +#include "SHStringUtilities.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Process Execution Functions */ + /*-----------------------------------------------------------------------------------*/ + PROCESS_INFORMATION SHExecUtilties::ExecProcess(const std::wstring& path, const std::wstring& args) + { + STARTUPINFOW startInfo; + PROCESS_INFORMATION procInfo; + ZeroMemory(&startInfo, sizeof(startInfo)); + ZeroMemory(&procInfo, sizeof(procInfo)); + startInfo.cb = sizeof(startInfo); + + std::wstring argsWstr = args; + + // Start Process + const auto SUCCESS = CreateProcess + ( + path.data(), argsWstr.data(), + nullptr, nullptr, false, NULL, nullptr, nullptr, + &startInfo, &procInfo + ); + + // Error Check + if (!SUCCESS) + { + auto err = GetLastError(); + std::ostringstream oss; + oss << "[SHExecUtilties] Failed to launch process. Error code: " << std::hex << err + << " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")"; + throw std::runtime_error(oss.str()); + } + + return procInfo; + } + + DWORD SHExecUtilties::ExecBlockingProcess(const std::wstring& path, const std::wstring& args) + { + PROCESS_INFORMATION procInfo = ExecProcess(path, args); + + // Wait for execution to end + DWORD status; + while (true) + { + const auto EXEC_SUCCESS = GetExitCodeProcess(procInfo.hProcess, &status); + if (!EXEC_SUCCESS) + { + auto err = GetLastError(); + std::ostringstream oss; + oss << "[SHExecUtilties] Failed to query process. Error code: " << std::hex << err + << " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")"; + throw std::runtime_error(oss.str()); + } + + // Break only if process ends + if (status != STILL_ACTIVE) + { + CloseHandle(procInfo.hProcess); + CloseHandle(procInfo.hThread); + return status; + } + } + } + + /*-----------------------------------------------------------------------------------*/ + /* Command Execution Functions */ + /*-----------------------------------------------------------------------------------*/ + PROCESS_INFORMATION SHExecUtilties::ExecCommand(const std::wstring& command) + { + return ExecProcess + ( + L"C:\\Windows\\system32\\cmd.exe", + L"/K \"" + command + L" & exit\"" + ); + } + + DWORD SHExecUtilties::ExecBlockingCommand(const std::wstring& command) + { + return ExecBlockingProcess + ( + L"C:\\Windows\\system32\\cmd.exe", + L"/K \"" + command + L" & exit\"" + ); + } +} diff --git a/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h new file mode 100644 index 00000000..838cee41 --- /dev/null +++ b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h @@ -0,0 +1,84 @@ +/************************************************************************************//*! +\file SHExecUtilties.h +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Dec 21, 2022 +\brief Contains the interface for SHExecUtilities class. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ + +// STL Includes +#include +// External Dependencies +#include + +namespace SHADE +{ + /// + /// Static class containing functions for executing external processes or commands. + /// + class SH_API SHExecUtilties final + { + public: + /*---------------------------------------------------------------------------------*/ + /* Process Execution Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Executes a process at the specified path with the specified arguments. This call + /// does not wait for the process to finish executing. + /// + /// Path to the processs to start. + /// Arguments to pass to the process. + /// Information about the started process. + /// + /// Thrown if failed to start the process. + /// + static PROCESS_INFORMATION ExecProcess(const std::wstring& path, const std::wstring& args); + /// + /// Executes a process at the specified path with the specified arguments and waits + /// for that process to finish executing. + /// + /// Path to the processs to start. + /// Arguments to pass to the process. + /// Return value of the process. + /// + /// Thrown if failed to start the process. + /// + static DWORD ExecBlockingProcess(const std::wstring& path, const std::wstring& args); + + /*---------------------------------------------------------------------------------*/ + /* Command Execution Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Executes a specified command in cmd. + /// This call does not wait for the command to finish executing. + /// + /// Command to execute. + /// + /// Information about the started cmd process that executes the command. + /// + /// + /// Thrown if failed to start the process. + /// + static PROCESS_INFORMATION ExecCommand(const std::wstring& command); + /// + /// Executes a specified command in cmd and waits for that process to finish + /// executing. + /// + /// Command to execute. + /// Return value of the process. + /// + /// Thrown if failed to start the process. + /// + static DWORD ExecBlockingCommand(const std::wstring& command); + + private: + /*-------------------------------------------------------------------------------*/ + /* Constructors/Destructors */ + /*-------------------------------------------------------------------------------*/ + SHExecUtilties() = delete; + }; +} From 861e47812f40b64ad711e02988b04b511520e5af Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 22 Dec 2022 15:06:52 +0800 Subject: [PATCH 05/42] Fixed bug where StrToWstr and WstrToStr may contain invalid characters from a previous call --- .../src/Tools/Utilities/SHStringUtilities.cpp | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/SHADE_Engine/src/Tools/Utilities/SHStringUtilities.cpp b/SHADE_Engine/src/Tools/Utilities/SHStringUtilities.cpp index b1e4aa92..b9698071 100644 --- a/SHADE_Engine/src/Tools/Utilities/SHStringUtilities.cpp +++ b/SHADE_Engine/src/Tools/Utilities/SHStringUtilities.cpp @@ -26,27 +26,29 @@ namespace SHADE std::vector SHStringUtilities::Split(const std::wstring& str, const wchar_t& delim) { return Split(str, delim); - } - std::string SHStringUtilities::WstrToStr(const std::wstring& wstr) - { - static std::vector buffer; - const int STR_SIZE = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast(wstr.size()), nullptr, 0, nullptr, nullptr) + 1 /* Null Terminator */; - buffer.resize(STR_SIZE); - WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast(wstr.size()), buffer.data(), MAX_PATH, nullptr, nullptr); - return std::string(buffer.data()); - } - std::wstring SHStringUtilities::StrToWstr(const std::string& str) - { - static std::vector buffer; - const int WSTR_SIZE = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.size()), nullptr, 0) + 1 /* Null Terminator */; - buffer.resize(WSTR_SIZE); - MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.size()), buffer.data(), WSTR_SIZE); - return std::wstring(buffer.data()); - } + } + std::string SHStringUtilities::WstrToStr(const std::wstring& wstr) + { + static std::vector buffer; + buffer.clear(); + const int STR_SIZE = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast(wstr.size()), nullptr, 0, nullptr, nullptr) + 1 /* Null Terminator */; + buffer.resize(STR_SIZE, '\0'); + WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast(wstr.size()), buffer.data(), MAX_PATH, nullptr, nullptr); + return std::string(buffer.data()); + } + std::wstring SHStringUtilities::StrToWstr(const std::string& str) + { + static std::vector buffer; + buffer.clear(); + const int WSTR_SIZE = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.size()), nullptr, 0) + 1 /* Null Terminator */; + buffer.resize(WSTR_SIZE, '\0'); + MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.size()), buffer.data(), WSTR_SIZE); + return std::wstring(buffer.data()); + } - std::string SHStringUtilities::GetWin32ErrorMessage(unsigned long errorCode) - { - return std::system_category().message(errorCode); - } + std::string SHStringUtilities::GetWin32ErrorMessage(unsigned long errorCode) + { + return std::system_category().message(errorCode); + } } \ No newline at end of file From 605d408a3aafb975a0e13c2dcfcf7e6fffdf8574 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Fri, 23 Dec 2022 15:24:12 +0800 Subject: [PATCH 06/42] Binding types, scroll wheel support, bind clears --- SHADE_Engine/src/Input/SHInputManager.cpp | 46 ++++---- SHADE_Engine/src/Input/SHInputManager.h | 126 +++++++++++++++++----- 2 files changed, 126 insertions(+), 46 deletions(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index f6b58a94..01bd4c13 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -577,18 +577,42 @@ namespace SHADE //Only get of largest magnitude double SHInputManager::GetBindingAxis(std::string const& bindingName, size_t cNum) noexcept { + if (cNum >= XUSER_MAX_COUNT) return 0.0f; + + //Over mouse movement, if used for this axis + if (bindings[bindingName].bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_X) + { + double velX = 0.0f; + GetMouseVelocity(&velX, nullptr); + return velX; + } + + if (bindings[bindingName].bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_Y) + { + double velY = 0.0f; + GetMouseVelocity(nullptr, &velY); + return velY; + } + + //Over mouse scroll, if used for this axis + if (bindings[bindingName].bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_SCROLL) + { + return mouseWheelVerticalDelta; + } + + //The largest magnitude recorded so far + double largestMagnitude = 0.0; + //Over keycodes, prioritise positive for (SH_KEYCODE k : bindings[bindingName].positiveKeyCodes) { - if (GetKey(k)) return 1.0; + if (GetKey(k)) if (std::abs(1.0f) > std::abs(largestMagnitude)) largestMagnitude = 1.0f; } for (SH_KEYCODE k : bindings[bindingName].negativeKeyCodes) { - if (GetKey(k)) return -1.0; + if (GetKey(k)) if (std::abs(-1.0f) > std::abs(largestMagnitude)) largestMagnitude = -1.0f; } - double largestMagnitude = 0.0; - //Over controllerCodes for (SH_CONTROLLERCODE c : bindings[bindingName].positiveControllerCodes) { @@ -806,18 +830,4 @@ namespace SHADE return minReleaseTime; } - //Only for mouse movement - //Get largest delta - double SHInputManager::GetBindingMouseVelocity(std::string const& bindingName, size_t cNum) noexcept - { - if (cNum >= XUSER_MAX_COUNT) return 0.0; - - //Mouse velocity - double velX = 0.0; - double velY = 0.0; - GetMouseVelocity(&velX, &velY); - - return bindings[bindingName].mouseXPositiveMultiplier * velX + bindings[bindingName].mouseYPositiveMultiplier * velY; - } - } //namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index ce3e69aa..3c503336 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -306,6 +306,20 @@ namespace SHADE /*------------------------------------------------------------------------*/ struct SH_API SHLogicalBindingData { + //BINDING TYPES/////////////////////////////////////////////////////////// + enum class SH_BINDINGTYPE + { + KB_MB_CONTROLLER, + MOUSE_X, + MOUSE_Y, + MOUSE_SCROLL + }; + + //BINDINGS//////////////////////////////////////////////////////////////// + + //The type of the binding + SH_BINDINGTYPE bindingType = SH_BINDINGTYPE::KB_MB_CONTROLLER; + //Key codes mapped to positive std::set positiveKeyCodes; @@ -318,9 +332,32 @@ namespace SHADE //Controller Codes mapped to negative std::set negativeControllerCodes; - //Mouse movement mapped to axes? - double mouseXPositiveMultiplier = 0.0f; - double mouseYPositiveMultiplier = 0.0f; + //VALUES////////////////////////////////////////////////////////////////// + + //The current value of the axis binding + double value = 0.0f; + + //Whether the input is inverted, + //If so, positive bindings will make the value negative, + // negative bindings will make the value positive, + // moving the mouse up will make the value negative, + // scrolling the mousewheel up will make the value negative, + bool inverted = false; + + //When no input is present, how fast does the value fall back to neutral? + double gravity = 1.0f; + + //How far the user needs to move an analog stick before application + //registers the movement + double dead = 0.1f; + + //Speed in units per second that the axis will move toward target value + //For digital inputs only + double sensitivity = 1.0f; + + //If enabled, axis value will reset to zero when pressing a button + //that corresponds in the opposite direction + bool snap = false; }; public: @@ -350,6 +387,12 @@ namespace SHADE mouseWheelVerticalDeltaPoll += GET_WHEEL_DELTA_WPARAM(wParam); } + //Get if controller or KB/M is presently being used + static inline bool const GetControllerInUse() noexcept + { + return controllerInUse; + } + //For testing purposes //static void PrintCurrentState() noexcept; @@ -621,13 +664,19 @@ namespace SHADE /* Binding Functions */ /*------------------------------------------------------------------------*/ + //Get a read-only map of the bindings + static inline std::map const& getBindings() noexcept + { + return bindings; + } + //Add a new binding to the map static inline void BindingsAdd(std::string const& newBindingName) noexcept { bindings.insert({ newBindingName, SHLogicalBindingData() }); } - //Remove a binding from the map + //Remove a binding and all its associated inputs from the list //Returns 1 if found and removed, 0 if not found static inline size_t BindingsRemove(std::string const& targetBindingName) noexcept { @@ -646,6 +695,20 @@ namespace SHADE return bindings.size(); } + //BINDING TYPE////////////////////////////////////////////////////////////// + + static inline void BindingsSetType(std::string const& targetBindingName, SHLogicalBindingData::SH_BINDINGTYPE const newType) + { + bindings[targetBindingName].bindingType = newType; + } + + static inline SHLogicalBindingData::SH_BINDINGTYPE const BindingsGetType(std::string const& targetBindingName) + { + return bindings[targetBindingName].bindingType; + } + + //POSITIVE KEYCODES///////////////////////////////////////////////////////// + //Check positive keycodes to binding static inline std::set const& BindingsGetPositiveKeyCodes(std::string const& bindingName) noexcept { @@ -667,6 +730,14 @@ namespace SHADE return bindings[targetBindingName].positiveKeyCodes.erase(toRemove); } + //Clear all positive SH_KEYCODEs from binding + static inline void BindingsClearPositiveKeyCodes(std::string const& targetBindingName) noexcept + { + bindings[targetBindingName].positiveKeyCodes.clear(); + } + + //NEGATIVE KEYCODES///////////////////////////////////////////////////////// + //Check negative keycodes to binding static inline std::set const& BindingsGetNegativeKeyCodes(std::string const& bindingName) noexcept { @@ -688,6 +759,14 @@ namespace SHADE return bindings[targetBindingName].negativeKeyCodes.erase(toRemove); } + //Clear all negative SH_KEYCODEs from binding + static inline void BindingsClearNegativeKeyCodes(std::string const& targetBindingName) noexcept + { + bindings[targetBindingName].negativeKeyCodes.clear(); + } + + //POSITIVE CONTROLLERCODES////////////////////////////////////////////////// + //Check positive controllercodes to binding static inline std::set const& BindingsGetPositiveControllerCodes(std::string const& bindingName) noexcept { @@ -709,6 +788,14 @@ namespace SHADE return bindings[targetBindingName].positiveControllerCodes.erase(toRemove); } + //Clear all positive SH_CONTROLLERCODEs from binding + static inline void BindingsClearPositiveControllerCodes(std::string const& targetBindingName) noexcept + { + bindings[targetBindingName].positiveControllerCodes.clear(); + } + + //NEGATIVE CONTROLLERCODES////////////////////////////////////////////////// + //Check negative controllercodes to binding static inline std::set const& BindingsGetNegativeControllerCodes(std::string const& bindingName) noexcept { @@ -730,29 +817,16 @@ namespace SHADE return bindings[targetBindingName].negativeControllerCodes.erase(toRemove); } - //Mouse movement bindings - - static inline double const BindingsGetMouseXPositiveMultiplier(std::string const& bindingName) noexcept + //Clear all negative SH_CONTROLLERCODEs from binding + static inline void BindingsClearNegativeControllerCodes(std::string const& targetBindingName) noexcept { - return bindings[bindingName].mouseXPositiveMultiplier; + bindings[targetBindingName].negativeControllerCodes.clear(); } - static inline void BindingsSetMouseXPositiveMultiplier(std::string const& bindingName, double newValue) noexcept - { - bindings[bindingName].mouseXPositiveMultiplier = newValue; - } - - static inline double const BindingsGetMouseYPositiveMultiplier(std::string const& bindingName) noexcept - { - return bindings[bindingName].mouseYPositiveMultiplier; - } - - static inline void BindingsSetMouseYPositiveMultiplier(std::string const& bindingName, double newValue) noexcept - { - bindings[bindingName].mouseYPositiveMultiplier = newValue; - } - - //Get the axis value of binding, between -1 and 1 + //Get the axis value of binding, between -1 and 1 for non-mouse + //For mouse, it won't be between -1 and 1. It will also be multiplied by sensitivity + //To avoid interference between mouse movement/wheel and keyboard/mouse/controller input, + //Set mouseXBound, mouseYBound and mouseScrollBound to false static double GetBindingAxis(std::string const& bindingName, size_t controllerNumber = 0) noexcept; //Whether binding is being held or not @@ -780,10 +854,6 @@ namespace SHADE static double GetBindingPositiveReleasedTime(std::string const& bindingName, size_t controllerNumber = 0) noexcept; static double GetBindingNegativeReleasedTime(std::string const& bindingName, size_t controllerNumber = 0) noexcept; - //Binding mouse velocity - //Only for mouse movement - static double GetBindingMouseVelocity(std::string const& bindingName, size_t controllerNumber = 0) noexcept; - /*------------------------------------------------------------------------*/ /* Other Functions */ /*------------------------------------------------------------------------*/ From ee4ec83f7a33a9e235043f613c72240e909b21e4 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 25 Dec 2022 14:13:21 +0800 Subject: [PATCH 07/42] Progress on Input Manager Fixes --- SHADE_Engine/src/Input/SHInputManager.cpp | 188 +++++++++++++++------- SHADE_Engine/src/Input/SHInputManager.h | 129 ++++++++++----- 2 files changed, 225 insertions(+), 92 deletions(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index 01bd4c13..a1ebe1a6 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -159,7 +159,7 @@ namespace SHADE } } - //Mouse Positioning///////////////////////////////////// + //Mouse Positioning///////////////////////////////////////////////////////// //https://stackoverflow.com/a/6423739 //Set last positioning @@ -181,7 +181,7 @@ namespace SHADE mouseWheelVerticalDelta = mouseWheelVerticalDeltaPoll; mouseWheelVerticalDeltaPoll = 0; - //Controllers////////////////////////////////////////////////////////////// + //Controllers/////////////////////////////////////////////////////////////// controllersConnectedCount = 0; @@ -447,6 +447,137 @@ namespace SHADE } } } + + //Bindings////////////////////////////////////////////////////////////////// + for (std::pair binding : bindings) + { + SHLogicalBindingData& data = binding.second; + + if (data.bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_X) + { + double velX = 0.0; + GetMouseVelocity(&velX, nullptr); + data.value = velX * data.sensitivity * (data.inverted ? -1.0 : 1.0); + } + else if (data.bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_Y) + { + double velY = 0.0; + GetMouseVelocity(nullptr, &velY); + data.value = velY * data.sensitivity * (data.inverted ? -1.0 : 1.0); + } + else if (data.bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_SCROLL) + { + data.value = mouseWheelVerticalDelta * data.sensitivity * (data.inverted ? -1.0 : 1.0); + } + else if (data.bindingType == SHLogicalBindingData::SH_BINDINGTYPE::KB_MB_CONTROLLER) + { + //Prioritise the largest magnitude + double largestMagnitude = 0.0; + + //If digital input was in, use sensitivity + bool digitalInput = false; + + //If data was read + bool positiveInputRead = false; + bool negativeInputRead = false; + + //Over keycodes + for (SH_KEYCODE k : data.positiveKeyCodes) + { + if (GetKey(k)) + { + if (std::abs(1.0) > std::abs(largestMagnitude)) largestMagnitude = 1.0; + digitalInput = true; + positiveInputRead = true; + } + } + for (SH_KEYCODE k : data.negativeKeyCodes) + { + if (GetKey(k)) + { + if (std::abs(-1.0) > std::abs(largestMagnitude)) largestMagnitude = -1.0; + digitalInput = true; + negativeInputRead = true; + } + } + + //Over controllerCodes + for (SH_CONTROLLERCODE c : data.positiveControllerCodes) + { + double newValue = 0.0; + if (GetControllerInput(c, &newValue)) + { + positiveInputRead = true; + if (static_cast(c) < NUM_CONTROLLER_BUTTON) + { + digitalInput = true; + } + if (std::abs(newValue) > std::abs(largestMagnitude)) + largestMagnitude = newValue * data.sensitivity * (data.inverted ? -1.0 : 1.0); + } + } + + for (SH_CONTROLLERCODE c : data.negativeControllerCodes) + { + double newValue = 0.0; + if (GetControllerInput(c, &newValue)) + { + negativeInputRead = true; + if (static_cast(c) < NUM_CONTROLLER_BUTTON) + { + digitalInput = true; + } + if (std::abs(newValue) > std::abs(largestMagnitude)) + largestMagnitude = -newValue * data.sensitivity * (data.inverted ? -1.0 : 1.0); + } + } + + //If both positive and negative inputs read, do not modify value + if (positiveInputRead && negativeInputRead) + { + data.value = data.value; + } + else + { + //If no data received, use gravity + if (!positiveInputRead && !negativeInputRead) + { + if (data.value > 0.0) + { + data.value -= data.gravity * dt; + if (data.value < 0.0) data.value = 0.0; + } + if (data.value < 0.0) + { + data.value += data.gravity * dt; + if (data.value > 0.0) data.value = 0.0; + } + } + else //Either positive OR negative input was read + { + //If digital input was in, use sensitivity + if (digitalInput) + { + data.value += data.sensitivity * largestMagnitude * dt; + if (data.value > 1.0) data.value = 1.0; + else if (data.value < -1.0) data.value = -1.0; + } + else + { + data.value = largestMagnitude; + } + + if (data.snap) //Snapping + { + if (data.value > 0.0 && negativeInputRead) + data.value = 0.0; + if (data.value < 0.0 && positiveInputRead) + data.value = 0.0; + } + } + } + } + } } bool SHInputManager::AnyKeyDown(SH_KEYCODE* firstDetected) noexcept @@ -574,60 +705,9 @@ namespace SHADE return false; } - //Only get of largest magnitude double SHInputManager::GetBindingAxis(std::string const& bindingName, size_t cNum) noexcept { - if (cNum >= XUSER_MAX_COUNT) return 0.0f; - - //Over mouse movement, if used for this axis - if (bindings[bindingName].bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_X) - { - double velX = 0.0f; - GetMouseVelocity(&velX, nullptr); - return velX; - } - - if (bindings[bindingName].bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_Y) - { - double velY = 0.0f; - GetMouseVelocity(nullptr, &velY); - return velY; - } - - //Over mouse scroll, if used for this axis - if (bindings[bindingName].bindingType == SHLogicalBindingData::SH_BINDINGTYPE::MOUSE_SCROLL) - { - return mouseWheelVerticalDelta; - } - - //The largest magnitude recorded so far - double largestMagnitude = 0.0; - - //Over keycodes, prioritise positive - for (SH_KEYCODE k : bindings[bindingName].positiveKeyCodes) - { - if (GetKey(k)) if (std::abs(1.0f) > std::abs(largestMagnitude)) largestMagnitude = 1.0f; - } - for (SH_KEYCODE k : bindings[bindingName].negativeKeyCodes) - { - if (GetKey(k)) if (std::abs(-1.0f) > std::abs(largestMagnitude)) largestMagnitude = -1.0f; - } - - //Over controllerCodes - for (SH_CONTROLLERCODE c : bindings[bindingName].positiveControllerCodes) - { - double newValue = 0.0; - if (GetControllerInput(c, &newValue, nullptr, nullptr, cNum)) - if (std::abs(newValue) > std::abs(largestMagnitude)) largestMagnitude = newValue; - } - for (SH_CONTROLLERCODE c : bindings[bindingName].negativeControllerCodes) - { - double newValue = 0.0; - if (GetControllerInput(c, &newValue, nullptr, nullptr, cNum)) - if (std::abs(newValue) > std::abs(largestMagnitude)) largestMagnitude = -newValue; - } - - return largestMagnitude; + return bindings[bindingName].value; } bool SHInputManager::GetBindingPositiveButton(std::string const& bindingName, size_t cNum) noexcept diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 3c503336..df996647 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -335,7 +335,7 @@ namespace SHADE //VALUES////////////////////////////////////////////////////////////////// //The current value of the axis binding - double value = 0.0f; + double value = 0.0; //Whether the input is inverted, //If so, positive bindings will make the value negative, @@ -345,15 +345,16 @@ namespace SHADE bool inverted = false; //When no input is present, how fast does the value fall back to neutral? - double gravity = 1.0f; + //Best to be non-negative + double gravity = 1.0; //How far the user needs to move an analog stick before application //registers the movement - double dead = 0.1f; + double dead = 0.1; - //Speed in units per second that the axis will move toward target value - //For digital inputs only - double sensitivity = 1.0f; + //Speed in units per second that the axis will move toward target value for digital + //For mouse movement / scrolling, serves as multiplier + double sensitivity = 1.0; //If enabled, axis value will reset to zero when pressing a button //that corresponds in the opposite direction @@ -388,7 +389,7 @@ namespace SHADE } //Get if controller or KB/M is presently being used - static inline bool const GetControllerInUse() noexcept + static inline bool GetControllerInUse() noexcept { return controllerInUse; } @@ -678,9 +679,9 @@ namespace SHADE //Remove a binding and all its associated inputs from the list //Returns 1 if found and removed, 0 if not found - static inline size_t BindingsRemove(std::string const& targetBindingName) noexcept + static inline size_t BindingsRemove(std::string const& bindingName) noexcept { - return bindings.erase(targetBindingName); + return bindings.erase(bindingName); } //Clears all bindings from the list @@ -695,16 +696,68 @@ namespace SHADE return bindings.size(); } - //BINDING TYPE////////////////////////////////////////////////////////////// + //BINDING VALUES//////////////////////////////////////////////////////////// - static inline void BindingsSetType(std::string const& targetBindingName, SHLogicalBindingData::SH_BINDINGTYPE const newType) + static inline bool BindingsGetInverted(std::string const& bindingName) { - bindings[targetBindingName].bindingType = newType; + return bindings[bindingName].inverted; } - static inline SHLogicalBindingData::SH_BINDINGTYPE const BindingsGetType(std::string const& targetBindingName) + static inline void BindingsSetInverted(std::string const& bindingName, bool const newValue) { - return bindings[targetBindingName].bindingType; + bindings[bindingName].inverted = newValue; + } + + static inline double BindingsGetGravity(std::string const& bindingName) + { + return bindings[bindingName].gravity; + } + + static inline void BindingsSetGravity(std::string const& bindingName, double const newValue) + { + bindings[bindingName].gravity = newValue; + } + + static inline double BindingsGetDead(std::string const& bindingName) + { + return bindings[bindingName].dead; + } + + static inline void BindingsSetDead(std::string const& bindingName, double const newValue) + { + bindings[bindingName].dead = newValue; + } + + static inline double BindingsGetSensitivity(std::string const& bindingName) + { + return bindings[bindingName].sensitivity; + } + + static inline void BindingsSetSensitivity(std::string const& bindingName, double const newValue) + { + bindings[bindingName].sensitivity = newValue; + } + + static inline bool BindingsGetSnap(std::string const& bindingName) + { + return bindings[bindingName].snap; + } + + static inline void BindingsSetSnap(std::string const& bindingName, bool const newValue) + { + bindings[bindingName].snap = newValue; + } + + //BINDING TYPE////////////////////////////////////////////////////////////// + + static inline SHLogicalBindingData::SH_BINDINGTYPE BindingsGetType(std::string const& bindingName) + { + return bindings[bindingName].bindingType; + } + + static inline void BindingsSetType(std::string const& bindingName, SHLogicalBindingData::SH_BINDINGTYPE const newType) + { + bindings[bindingName].bindingType = newType; } //POSITIVE KEYCODES///////////////////////////////////////////////////////// @@ -716,24 +769,24 @@ namespace SHADE } //Add positive SH_KEYCODE to binding - static inline void BindingsAddPositiveKeyCode(std::string const& targetBindingName, + static inline void BindingsAddPositiveKeyCode(std::string const& bindingName, SH_KEYCODE toAdd) noexcept { - bindings[targetBindingName].positiveKeyCodes.insert(toAdd); + bindings[bindingName].positiveKeyCodes.insert(toAdd); } //Remove positive SH_KEYCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemovePositiveKeyCode(std::string const& targetBindingName, + static inline size_t BindingsRemovePositiveKeyCode(std::string const& bindingName, SH_KEYCODE toRemove) noexcept { - return bindings[targetBindingName].positiveKeyCodes.erase(toRemove); + return bindings[bindingName].positiveKeyCodes.erase(toRemove); } //Clear all positive SH_KEYCODEs from binding - static inline void BindingsClearPositiveKeyCodes(std::string const& targetBindingName) noexcept + static inline void BindingsClearPositiveKeyCodes(std::string const& bindingName) noexcept { - bindings[targetBindingName].positiveKeyCodes.clear(); + bindings[bindingName].positiveKeyCodes.clear(); } //NEGATIVE KEYCODES///////////////////////////////////////////////////////// @@ -745,24 +798,24 @@ namespace SHADE } //Add negative SH_KEYCODE to binding - static inline void BindingsAddNegativeKeyCode(std::string const& targetBindingName, + static inline void BindingsAddNegativeKeyCode(std::string const& bindingName, SH_KEYCODE toAdd) noexcept { - bindings[targetBindingName].negativeKeyCodes.insert(toAdd); + bindings[bindingName].negativeKeyCodes.insert(toAdd); } //Remove negative SH_KEYCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemoveNegativeKeyCode(std::string const& targetBindingName, + static inline size_t BindingsRemoveNegativeKeyCode(std::string const& bindingName, SH_KEYCODE toRemove) noexcept { - return bindings[targetBindingName].negativeKeyCodes.erase(toRemove); + return bindings[bindingName].negativeKeyCodes.erase(toRemove); } //Clear all negative SH_KEYCODEs from binding - static inline void BindingsClearNegativeKeyCodes(std::string const& targetBindingName) noexcept + static inline void BindingsClearNegativeKeyCodes(std::string const& bindingName) noexcept { - bindings[targetBindingName].negativeKeyCodes.clear(); + bindings[bindingName].negativeKeyCodes.clear(); } //POSITIVE CONTROLLERCODES////////////////////////////////////////////////// @@ -774,24 +827,24 @@ namespace SHADE } //Add positive SH_CONTROLLERCODE to binding - static inline void BindingsAddPositiveControllerCode(std::string const& targetBindingName, + static inline void BindingsAddPositiveControllerCode(std::string const& bindingName, SH_CONTROLLERCODE toAdd) noexcept { - bindings[targetBindingName].positiveControllerCodes.insert(toAdd); + bindings[bindingName].positiveControllerCodes.insert(toAdd); } //Remove positive SH_CONTROLLERCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemovePositiveControllerCode(std::string const& targetBindingName, + static inline size_t BindingsRemovePositiveControllerCode(std::string const& bindingName, SH_CONTROLLERCODE toRemove) noexcept { - return bindings[targetBindingName].positiveControllerCodes.erase(toRemove); + return bindings[bindingName].positiveControllerCodes.erase(toRemove); } //Clear all positive SH_CONTROLLERCODEs from binding - static inline void BindingsClearPositiveControllerCodes(std::string const& targetBindingName) noexcept + static inline void BindingsClearPositiveControllerCodes(std::string const& bindingName) noexcept { - bindings[targetBindingName].positiveControllerCodes.clear(); + bindings[bindingName].positiveControllerCodes.clear(); } //NEGATIVE CONTROLLERCODES////////////////////////////////////////////////// @@ -803,24 +856,24 @@ namespace SHADE } //Add negative SH_CONTROLLERCODE to binding - static inline void BindingsAddNegativeControllerCode(std::string const& targetBindingName, + static inline void BindingsAddNegativeControllerCode(std::string const& bindingName, SH_CONTROLLERCODE toAdd) noexcept { - bindings[targetBindingName].negativeControllerCodes.insert(toAdd); + bindings[bindingName].negativeControllerCodes.insert(toAdd); } //Remove negative SH_CONTROLLERCODE from binding //If toRemove found and removed, returns 1. Otherwise, 0. - static inline size_t BindingsRemoveNegativeControllerCode(std::string const& targetBindingName, + static inline size_t BindingsRemoveNegativeControllerCode(std::string const& bindingName, SH_CONTROLLERCODE toRemove) noexcept { - return bindings[targetBindingName].negativeControllerCodes.erase(toRemove); + return bindings[bindingName].negativeControllerCodes.erase(toRemove); } //Clear all negative SH_CONTROLLERCODEs from binding - static inline void BindingsClearNegativeControllerCodes(std::string const& targetBindingName) noexcept + static inline void BindingsClearNegativeControllerCodes(std::string const& bindingName) noexcept { - bindings[targetBindingName].negativeControllerCodes.clear(); + bindings[bindingName].negativeControllerCodes.clear(); } //Get the axis value of binding, between -1 and 1 for non-mouse From 51c9058ab8779e1fdc3a60a725c294e998e14adb Mon Sep 17 00:00:00 2001 From: SHAM-DP Date: Wed, 28 Dec 2022 17:00:54 +0800 Subject: [PATCH 08/42] Window now maximized by default Application now loads working scene if run with editor Added editor config to save: - Window size - Window Maximized - Working Scene - Editor Style --- .../src/Application/SBApplication.cpp | 8 ++++++- .../EditorWindow/MenuBar/SHEditorMenuBar.cpp | 2 +- SHADE_Engine/src/Editor/SHEditor.cpp | 23 +++++++++++++++++-- SHADE_Engine/src/Editor/SHEditor.h | 5 +++- SHADE_Engine/src/Editor/SHEditorWidgets.hpp | 2 +- .../src/Graphics/Windowing/SHWindow.cpp | 9 ++++++++ .../src/Graphics/Windowing/SHWindow.h | 2 ++ .../Configurations/SHConfigurationManager.cpp | 22 ++++++++++++++++-- .../Configurations/SHConfigurationManager.h | 9 +++++--- 9 files changed, 71 insertions(+), 11 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 5aa1eb00..fcceacab 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -67,6 +67,9 @@ namespace Sandbox SHFileUtilities::SetWorkDirToExecDir(); WindowData wndData{}; auto& appConfig = SHConfigurationManager::LoadApplicationConfig(&wndData); +#if SHEDITOR + auto& editorConfig = SHConfigurationManager::LoadEditorConfig(&wndData); +#endif window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow, wndData); // Create Systems @@ -158,8 +161,11 @@ namespace Sandbox SHSystemManager::Init(); +#if SHEDITOR + SHSceneManager::InitSceneManager(editorConfig.workingSceneID); +#else SHSceneManager::InitSceneManager(appConfig.startingSceneID); - +#endif SHFrameRateController::UpdateFRC(); // Link up SHDebugDraw diff --git a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp index a1335e19..a364ea83 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp @@ -183,7 +183,7 @@ namespace SHADE //ImGui::InputScalar("Starting Scene", ImGuiDataType_U32, &appConfig.startingSceneID); auto sceneAsset = SHAssetManager::GetData(appConfig.startingSceneID); - if(ImGui::BeginCombo("Starting Scne", sceneAsset ? sceneAsset->name.data() : "")) + if(ImGui::BeginCombo("Starting Scene", sceneAsset ? sceneAsset->name.data() : "")) { auto scenes = SHAssetManager::GetAllRecordOfType(AssetType::SCENE); for(auto const& scene : scenes) diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index 6466533f..7d713ff2 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -93,6 +93,8 @@ namespace SHADE SHLOG_CRITICAL("Failed to create ImGui Context") } } + + editorConfig = &SHConfigurationManager::LoadEditorConfig(); //Add editor windows SHEditorWindowManager::CreateEditorWindow(); @@ -122,7 +124,7 @@ namespace SHADE InitBackend(); - SetStyle(Style::SHADE); + SetStyle(static_cast