mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
Add Joystick:getGamepadType.
Return value is "unknown", "xbox360", "xboxone", "ps3", "ps4", "ps5", "switchpro", or "virtual".
This commit is contained in:
@@ -42,102 +42,71 @@ float Joystick::clampval(float x)
|
||||
return x;
|
||||
}
|
||||
|
||||
bool Joystick::getConstant(const char *in, Joystick::Hat &out)
|
||||
STRINGMAP_CLASS_BEGIN(Joystick, Joystick::Hat, Joystick::HAT_MAX_ENUM, hat)
|
||||
{
|
||||
return hats.find(in, out);
|
||||
{ "c", Joystick::HAT_CENTERED },
|
||||
{ "u", Joystick::HAT_UP },
|
||||
{ "r", Joystick::HAT_RIGHT },
|
||||
{ "d", Joystick::HAT_DOWN },
|
||||
{ "l", Joystick::HAT_LEFT },
|
||||
{ "ru", Joystick::HAT_RIGHTUP },
|
||||
{ "rd", Joystick::HAT_RIGHTDOWN },
|
||||
{ "lu", Joystick::HAT_LEFTUP },
|
||||
{ "ld", Joystick::HAT_LEFTDOWN },
|
||||
}
|
||||
STRINGMAP_CLASS_END(Joystick, Joystick::Hat, Joystick::HAT_MAX_ENUM, hat)
|
||||
|
||||
bool Joystick::getConstant(Joystick::Hat in, const char *&out)
|
||||
STRINGMAP_CLASS_BEGIN(Joystick, Joystick::GamepadType, Joystick::GAMEPAD_TYPE_MAX_ENUM, gamepadType)
|
||||
{
|
||||
return hats.find(in, out);
|
||||
{ "unknown", Joystick::GAMEPAD_TYPE_UNKNOWN },
|
||||
{ "xbox360", Joystick::GAMEPAD_TYPE_XBOX360 },
|
||||
{ "xboxone", Joystick::GAMEPAD_TYPE_XBOXONE },
|
||||
{ "ps3", Joystick::GAMEPAD_TYPE_PS3 },
|
||||
{ "ps4", Joystick::GAMEPAD_TYPE_PS4 },
|
||||
{ "ps5", Joystick::GAMEPAD_TYPE_PS5 },
|
||||
{ "switchpro", Joystick::GAMEPAD_TYPE_NINTENDO_SWITCH_PRO },
|
||||
{ "virtual", Joystick::GAMEPAD_TYPE_VIRTUAL },
|
||||
}
|
||||
STRINGMAP_CLASS_END(Joystick, Joystick::GamepadType, Joystick::GAMEPAD_TYPE_MAX_ENUM, gamepadType)
|
||||
|
||||
bool Joystick::getConstant(const char *in, Joystick::GamepadAxis &out)
|
||||
STRINGMAP_CLASS_BEGIN(Joystick, Joystick::GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM, gpAxis)
|
||||
{
|
||||
return gpAxes.find(in, out);
|
||||
{ "leftx", Joystick::GAMEPAD_AXIS_LEFTX },
|
||||
{ "lefty", Joystick::GAMEPAD_AXIS_LEFTY },
|
||||
{ "rightx", Joystick::GAMEPAD_AXIS_RIGHTX },
|
||||
{ "righty", Joystick::GAMEPAD_AXIS_RIGHTY },
|
||||
{ "triggerleft", Joystick::GAMEPAD_AXIS_TRIGGERLEFT },
|
||||
{ "triggerright", Joystick::GAMEPAD_AXIS_TRIGGERRIGHT },
|
||||
}
|
||||
STRINGMAP_CLASS_END(Joystick, Joystick::GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM, gpAxis)
|
||||
|
||||
bool Joystick::getConstant(Joystick::GamepadAxis in, const char *&out)
|
||||
STRINGMAP_CLASS_BEGIN(Joystick, Joystick::GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM, gpButton)
|
||||
{
|
||||
return gpAxes.find(in, out);
|
||||
{ "a", Joystick::GAMEPAD_BUTTON_A },
|
||||
{ "b", Joystick::GAMEPAD_BUTTON_B },
|
||||
{ "x", Joystick::GAMEPAD_BUTTON_X },
|
||||
{ "y", Joystick::GAMEPAD_BUTTON_Y },
|
||||
{ "back", Joystick::GAMEPAD_BUTTON_BACK },
|
||||
{ "guide", Joystick::GAMEPAD_BUTTON_GUIDE },
|
||||
{ "start", Joystick::GAMEPAD_BUTTON_START },
|
||||
{ "leftstick", Joystick::GAMEPAD_BUTTON_LEFTSTICK },
|
||||
{ "rightstick", Joystick::GAMEPAD_BUTTON_RIGHTSTICK },
|
||||
{ "leftshoulder", Joystick::GAMEPAD_BUTTON_LEFTSHOULDER },
|
||||
{ "rightshoulder", Joystick::GAMEPAD_BUTTON_RIGHTSHOULDER },
|
||||
{ "dpup", Joystick::GAMEPAD_BUTTON_DPAD_UP },
|
||||
{ "dpdown", Joystick::GAMEPAD_BUTTON_DPAD_DOWN },
|
||||
{ "dpleft", Joystick::GAMEPAD_BUTTON_DPAD_LEFT },
|
||||
{ "dpright", Joystick::GAMEPAD_BUTTON_DPAD_RIGHT },
|
||||
}
|
||||
STRINGMAP_CLASS_END(Joystick, Joystick::GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM, gpButton)
|
||||
|
||||
bool Joystick::getConstant(const char *in, Joystick::GamepadButton &out)
|
||||
STRINGMAP_CLASS_BEGIN(Joystick, Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM, inputType)
|
||||
{
|
||||
return gpButtons.find(in, out);
|
||||
{ "axis", Joystick::INPUT_TYPE_AXIS },
|
||||
{ "button", Joystick::INPUT_TYPE_BUTTON },
|
||||
{ "hat", Joystick::INPUT_TYPE_HAT },
|
||||
}
|
||||
|
||||
bool Joystick::getConstant(Joystick::GamepadButton in, const char *&out)
|
||||
{
|
||||
return gpButtons.find(in, out);
|
||||
}
|
||||
|
||||
bool Joystick::getConstant(const char *in, Joystick::InputType &out)
|
||||
{
|
||||
return inputTypes.find(in, out);
|
||||
}
|
||||
|
||||
bool Joystick::getConstant(Joystick::InputType in, const char *&out)
|
||||
{
|
||||
return inputTypes.find(in, out);
|
||||
}
|
||||
|
||||
StringMap<Joystick::Hat, Joystick::HAT_MAX_ENUM>::Entry Joystick::hatEntries[] =
|
||||
{
|
||||
{"c", Joystick::HAT_CENTERED},
|
||||
{"u", Joystick::HAT_UP},
|
||||
{"r", Joystick::HAT_RIGHT},
|
||||
{"d", Joystick::HAT_DOWN},
|
||||
{"l", Joystick::HAT_LEFT},
|
||||
{"ru", Joystick::HAT_RIGHTUP},
|
||||
{"rd", Joystick::HAT_RIGHTDOWN},
|
||||
{"lu", Joystick::HAT_LEFTUP},
|
||||
{"ld", Joystick::HAT_LEFTDOWN},
|
||||
};
|
||||
|
||||
StringMap<Joystick::Hat, Joystick::HAT_MAX_ENUM> Joystick::hats(Joystick::hatEntries, sizeof(Joystick::hatEntries));
|
||||
|
||||
StringMap<Joystick::GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM>::Entry Joystick::gpAxisEntries[] =
|
||||
{
|
||||
{"leftx", GAMEPAD_AXIS_LEFTX},
|
||||
{"lefty", GAMEPAD_AXIS_LEFTY},
|
||||
{"rightx", GAMEPAD_AXIS_RIGHTX},
|
||||
{"righty", GAMEPAD_AXIS_RIGHTY},
|
||||
{"triggerleft", GAMEPAD_AXIS_TRIGGERLEFT},
|
||||
{"triggerright", GAMEPAD_AXIS_TRIGGERRIGHT},
|
||||
};
|
||||
|
||||
StringMap<Joystick::GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM> Joystick::gpAxes(Joystick::gpAxisEntries, sizeof(Joystick::gpAxisEntries));
|
||||
|
||||
StringMap<Joystick::GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM>::Entry Joystick::gpButtonEntries[] =
|
||||
{
|
||||
{"a", GAMEPAD_BUTTON_A},
|
||||
{"b", GAMEPAD_BUTTON_B},
|
||||
{"x", GAMEPAD_BUTTON_X},
|
||||
{"y", GAMEPAD_BUTTON_Y},
|
||||
{"back", GAMEPAD_BUTTON_BACK},
|
||||
{"guide", GAMEPAD_BUTTON_GUIDE},
|
||||
{"start", GAMEPAD_BUTTON_START},
|
||||
{"leftstick", GAMEPAD_BUTTON_LEFTSTICK},
|
||||
{"rightstick", GAMEPAD_BUTTON_RIGHTSTICK},
|
||||
{"leftshoulder", GAMEPAD_BUTTON_LEFTSHOULDER},
|
||||
{"rightshoulder", GAMEPAD_BUTTON_RIGHTSHOULDER},
|
||||
{"dpup", GAMEPAD_BUTTON_DPAD_UP},
|
||||
{"dpdown", GAMEPAD_BUTTON_DPAD_DOWN},
|
||||
{"dpleft", GAMEPAD_BUTTON_DPAD_LEFT},
|
||||
{"dpright", GAMEPAD_BUTTON_DPAD_RIGHT},
|
||||
};
|
||||
|
||||
StringMap<Joystick::GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM> Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries));
|
||||
|
||||
StringMap<Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM>::Entry Joystick::inputTypeEntries[] =
|
||||
{
|
||||
{"axis", Joystick::INPUT_TYPE_AXIS},
|
||||
{"button", Joystick::INPUT_TYPE_BUTTON},
|
||||
{"hat", Joystick::INPUT_TYPE_HAT},
|
||||
};
|
||||
|
||||
StringMap<Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM> Joystick::inputTypes(Joystick::inputTypeEntries, sizeof(Joystick::inputTypeEntries));
|
||||
STRINGMAP_CLASS_END(Joystick, Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM, inputType)
|
||||
|
||||
} // joystick
|
||||
} // love
|
||||
|
||||
@@ -56,6 +56,19 @@ public:
|
||||
HAT_MAX_ENUM = 16
|
||||
};
|
||||
|
||||
enum GamepadType
|
||||
{
|
||||
GAMEPAD_TYPE_UNKNOWN,
|
||||
GAMEPAD_TYPE_XBOX360,
|
||||
GAMEPAD_TYPE_XBOXONE,
|
||||
GAMEPAD_TYPE_PS3,
|
||||
GAMEPAD_TYPE_PS4,
|
||||
GAMEPAD_TYPE_PS5,
|
||||
GAMEPAD_TYPE_NINTENDO_SWITCH_PRO,
|
||||
GAMEPAD_TYPE_VIRTUAL,
|
||||
GAMEPAD_TYPE_MAX_ENUM
|
||||
};
|
||||
|
||||
// Valid Gamepad axes.
|
||||
enum GamepadAxis
|
||||
{
|
||||
@@ -149,6 +162,8 @@ public:
|
||||
virtual bool openGamepad(int deviceindex) = 0;
|
||||
virtual bool isGamepad() const = 0;
|
||||
|
||||
virtual GamepadType getGamepadType() const = 0;
|
||||
|
||||
virtual float getGamepadAxis(GamepadAxis axis) const = 0;
|
||||
virtual bool isGamepadDown(const std::vector<GamepadButton> &blist) const = 0;
|
||||
|
||||
@@ -168,34 +183,14 @@ public:
|
||||
virtual bool setVibration() = 0;
|
||||
virtual void getVibration(float &left, float &right) = 0;
|
||||
|
||||
static bool getConstant(const char *in, Hat &out);
|
||||
static bool getConstant(Hat in, const char *&out);
|
||||
|
||||
static bool getConstant(const char *in, GamepadAxis &out);
|
||||
static bool getConstant(GamepadAxis in, const char *&out);
|
||||
|
||||
static bool getConstant(const char *in, GamepadButton &out);
|
||||
static bool getConstant(GamepadButton in, const char *&out);
|
||||
|
||||
static bool getConstant(const char *in, InputType &out);
|
||||
static bool getConstant(InputType in, const char *&out);
|
||||
STRINGMAP_CLASS_DECLARE(Hat);
|
||||
STRINGMAP_CLASS_DECLARE(GamepadType);
|
||||
STRINGMAP_CLASS_DECLARE(GamepadAxis);
|
||||
STRINGMAP_CLASS_DECLARE(GamepadButton);
|
||||
STRINGMAP_CLASS_DECLARE(InputType);
|
||||
|
||||
static float clampval(float x);
|
||||
|
||||
private:
|
||||
|
||||
static StringMap<Hat, HAT_MAX_ENUM>::Entry hatEntries[];
|
||||
static StringMap<Hat, HAT_MAX_ENUM> hats;
|
||||
|
||||
static StringMap<GamepadAxis, GAMEPAD_AXIS_MAX_ENUM>::Entry gpAxisEntries[];
|
||||
static StringMap<GamepadAxis, GAMEPAD_AXIS_MAX_ENUM> gpAxes;
|
||||
|
||||
static StringMap<GamepadButton, GAMEPAD_BUTTON_MAX_ENUM>::Entry gpButtonEntries[];
|
||||
static StringMap<GamepadButton, GAMEPAD_BUTTON_MAX_ENUM> gpButtons;
|
||||
|
||||
static StringMap<InputType, INPUT_TYPE_MAX_ENUM>::Entry inputTypeEntries[];
|
||||
static StringMap<InputType, INPUT_TYPE_MAX_ENUM> inputTypes;
|
||||
|
||||
}; // Joystick
|
||||
|
||||
} // joystick
|
||||
|
||||
@@ -215,6 +215,30 @@ bool Joystick::isGamepad() const
|
||||
return controller != nullptr;
|
||||
}
|
||||
|
||||
Joystick::GamepadType Joystick::getGamepadType() const
|
||||
{
|
||||
if (controller == nullptr)
|
||||
return GAMEPAD_TYPE_UNKNOWN;
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 12)
|
||||
switch (SDL_GameControllerGetType(controller))
|
||||
{
|
||||
case SDL_CONTROLLER_TYPE_UNKNOWN: return GAMEPAD_TYPE_UNKNOWN;
|
||||
case SDL_CONTROLLER_TYPE_XBOX360: return GAMEPAD_TYPE_XBOX360;
|
||||
case SDL_CONTROLLER_TYPE_XBOXONE: return GAMEPAD_TYPE_XBOXONE;
|
||||
case SDL_CONTROLLER_TYPE_PS3: return GAMEPAD_TYPE_PS3;
|
||||
case SDL_CONTROLLER_TYPE_PS4: return GAMEPAD_TYPE_PS4;
|
||||
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO: return GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 14)
|
||||
case SDL_CONTROLLER_TYPE_VIRTUAL: return GAMEPAD_TYPE_VIRTUAL;
|
||||
case SDL_CONTROLLER_TYPE_PS5: return GAMEPAD_TYPE_PS5;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
return GAMEPAD_TYPE_UNKNOWN;
|
||||
}
|
||||
|
||||
float Joystick::getGamepadAxis(love::joystick::Joystick::GamepadAxis axis) const
|
||||
{
|
||||
if (!isConnected() || !isGamepad())
|
||||
|
||||
@@ -64,6 +64,8 @@ public:
|
||||
bool openGamepad(int deviceindex) override;
|
||||
bool isGamepad() const override;
|
||||
|
||||
GamepadType getGamepadType() const override;
|
||||
|
||||
float getGamepadAxis(GamepadAxis axis) const override;
|
||||
bool isGamepadDown(const std::vector<GamepadButton> &blist) const override;
|
||||
|
||||
|
||||
@@ -178,6 +178,15 @@ int w_Joystick_isGamepad(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_getGamepadType(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
const char *str = "unknown";
|
||||
Joystick::getConstant(j->getGamepadType(), str);
|
||||
lua_pushstring(L, str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_getGamepadAxis(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
@@ -359,6 +368,7 @@ static const luaL_Reg w_Joystick_functions[] =
|
||||
{ "isDown", w_Joystick_isDown },
|
||||
|
||||
{ "isGamepad", w_Joystick_isGamepad },
|
||||
{ "getGamepadType", w_Joystick_getGamepadType },
|
||||
{ "getGamepadAxis", w_Joystick_getGamepadAxis },
|
||||
{ "isGamepadDown", w_Joystick_isGamepadDown },
|
||||
{ "getGamepadMapping", w_Joystick_getGamepadMapping },
|
||||
|
||||
Reference in New Issue
Block a user