From ee4ec83f7a33a9e235043f613c72240e909b21e4 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 25 Dec 2022 14:13:21 +0800 Subject: [PATCH] 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