Browse Source

Add Joystick:set/getPlayerIndex

Alex Szpakowski 4 years ago
parent
commit
b1b70a83db

+ 3 - 0
src/modules/joystick/Joystick.h

@@ -165,6 +165,9 @@ public:
 
 	virtual bool isDown(const std::vector<int> &buttonlist) const = 0;
 
+	virtual void setPlayerIndex(int index) = 0;
+	virtual int getPlayerIndex() const = 0;
+
 	virtual bool openGamepad(int deviceindex) = 0;
 	virtual bool isGamepad() const = 0;
 

+ 24 - 0
src/modules/joystick/sdl/Joystick.cpp

@@ -195,6 +195,30 @@ bool Joystick::isDown(const std::vector<int> &buttonlist) const
 	return false;
 }
 
+void Joystick::setPlayerIndex(int index)
+{
+	if (!isConnected())
+		return;
+
+#if SDL_VERSION_ATLEAST(2, 0, 12)
+	SDL_JoystickSetPlayerIndex(joyhandle, index);
+#else
+	LOVE_UNUSED(index);
+#endif
+}
+
+int Joystick::getPlayerIndex() const
+{
+	if (!isConnected())
+		return -1;
+
+#if SDL_VERSION_ATLEAST(2, 0, 12)
+	return SDL_JoystickGetPlayerIndex(joyhandle);
+#else
+	return -1;
+#endif
+}
+
 bool Joystick::openGamepad(int deviceindex)
 {
 	if (!SDL_IsGameController(deviceindex))

+ 3 - 0
src/modules/joystick/sdl/Joystick.h

@@ -61,6 +61,9 @@ public:
 
 	bool isDown(const std::vector<int> &buttonlist) const override;
 
+	void setPlayerIndex(int index) override;
+	int getPlayerIndex() const override;
+
 	bool openGamepad(int deviceindex) override;
 	bool isGamepad() const override;
 

+ 18 - 0
src/modules/joystick/wrap_Joystick.cpp

@@ -171,6 +171,22 @@ int w_Joystick_isDown(lua_State *L)
 	return 1;
 }
 
+int w_Joystick_setPlayerIndex(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	int index = (int) luaL_checkinteger(L, 2) - 1;
+	j->setPlayerIndex(index);
+	return 0;
+}
+
+int w_Joystick_getPlayerIndex(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	int index = j->getPlayerIndex();
+	lua_pushinteger(L, index >= 0 ? index + 1 : index);
+	return 1;
+}
+
 int w_Joystick_isGamepad(lua_State *L)
 {
 	Joystick *j = luax_checkjoystick(L, 1);
@@ -366,6 +382,8 @@ static const luaL_Reg w_Joystick_functions[] =
 	{ "getAxes", w_Joystick_getAxes },
 	{ "getHat", w_Joystick_getHat },
 	{ "isDown", w_Joystick_isDown },
+	{ "setPlayerIndex", w_Joystick_setPlayerIndex },
+	{ "getPlayerIndex", w_Joystick_getPlayerIndex },
 
 	{ "isGamepad", w_Joystick_isGamepad },
 	{ "getGamepadType", w_Joystick_getGamepadType },