Binding types, scroll wheel support, bind clears

This commit is contained in:
mushgunAX 2022-12-23 15:24:12 +08:00
parent 3041761e96
commit 605d408a3a
2 changed files with 126 additions and 46 deletions

View File

@ -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

View File

@ -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<SH_KEYCODE> positiveKeyCodes;
@ -318,9 +332,32 @@ namespace SHADE
//Controller Codes mapped to negative
std::set<SH_CONTROLLERCODE> 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<std::string, SHLogicalBindingData> 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<SH_KEYCODE> 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<SH_KEYCODE> 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<SH_CONTROLLERCODE> 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<SH_CONTROLLERCODE> 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 */
/*------------------------------------------------------------------------*/