CommonHostInterface: Fix controller inputs not binding

Fixes #46.
This commit is contained in:
Connor McLaughlin 2020-03-06 21:55:25 +10:00
parent 2004de7170
commit ef2796b780
2 changed files with 14 additions and 7 deletions

View file

@ -1,4 +1,5 @@
#pragma once
#include "types.h"
#include <cstdarg>
#include <cstddef>
#include <cstring>
@ -24,7 +25,7 @@ bool WildcardMatch(const char* subject, const char* mask, bool case_sensitive =
std::size_t Strlcpy(char* dst, const char* src, std::size_t size);
/// Platform-independent strcasecmp
inline int Strcasecmp(const char* s1, const char* s2)
static inline int Strcasecmp(const char* s1, const char* s2)
{
#ifdef _MSC_VER
return _stricmp(s1, s2);
@ -35,7 +36,7 @@ inline int Strcasecmp(const char* s1, const char* s2)
/// Wrapper arond std::from_chars
template<typename T>
std::optional<T> FromChars(const std::string_view str)
static inline std::optional<T> FromChars(std::string_view str)
{
T value;
@ -54,4 +55,10 @@ std::optional<T> FromChars(const std::string_view str)
return value;
}
/// starts_with from C++20
ALWAYS_INLINE static bool StartsWith(std::string_view str, const char* prefix)
{
return (str.compare(0, std::strlen(prefix), prefix) == 0);
}
} // namespace StringUtil

View file

@ -214,7 +214,7 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const
}
#ifdef WITH_SDL2
if (device == "Controller")
if (StringUtil::StartsWith(device, "Controller"))
{
const std::optional<int> controller_index = StringUtil::FromChars<int>(device.substr(10));
if (!controller_index || *controller_index < 0)
@ -223,7 +223,7 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const
return false;
}
if (button.find_first_of("Button") == 0)
if (StringUtil::StartsWith(button, "Button"))
{
const std::optional<int> button_index = StringUtil::FromChars<int>(button.substr(6));
if (!button_index ||
@ -235,7 +235,7 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const
return true;
}
else if (button.find_first_of("+Axis") == 0 || button.find_first_of("-Axis"))
else if (StringUtil::StartsWith(button, "+Axis") || StringUtil::StartsWith(button, "-Axis"))
{
const std::optional<int> axis_index = StringUtil::FromChars<int>(button.substr(5));
const bool positive = (button[0] == '+');
@ -262,7 +262,7 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st
const std::string_view& axis, InputAxisHandler handler)
{
#ifdef WITH_SDL2
if (device == "Controller")
if (StringUtil::StartsWith(device, "Controller"))
{
const std::optional<int> controller_index = StringUtil::FromChars<int>(device.substr(10));
if (!controller_index || *controller_index < 0)
@ -271,7 +271,7 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st
return false;
}
if (axis.find_first_of("Axis") == 0)
if (StringUtil::StartsWith(axis, "Axis"))
{
const std::optional<int> axis_index = StringUtil::FromChars<int>(axis.substr(4));
if (!axis_index ||