Browse Source

Add support for passing a table to love.mouse.isDown, love.keyboard.isDown, love.keyboard.isScancodeDown, Joystick:isDown, and Joystick:isGamepadDown.

Alex Szpakowski 9 years ago
parent
commit
327682d07f

+ 54 - 13
src/modules/joystick/wrap_Joystick.cpp

@@ -105,8 +105,8 @@ int w_Joystick_getAxes(lua_State *L)
 	Joystick *j = luax_checkjoystick(L, 1);
 	std::vector<float> axes = j->getAxes();
 
-	for (size_t i = 0; i < axes.size(); i++)
-		lua_pushnumber(L, axes[i]);
+	for (float value : axes)
+		lua_pushnumber(L, value);
 
 	return (int) axes.size();
 }
@@ -129,11 +129,29 @@ int w_Joystick_isDown(lua_State *L)
 {
 	Joystick *j = luax_checkjoystick(L, 1);
 
-	luaL_checkinteger(L, 2);
+	bool istable = lua_istable(L, 2);
+	int num = istable ? (int) luax_objlen(L, 2) : (lua_gettop(L) - 1);
+
+	if (num == 0)
+		luaL_checkinteger(L, 2);
 
 	std::vector<int> buttons;
-	for (int i = 2; i <= lua_gettop(L); i++)
-		buttons.push_back((int) luaL_checknumber(L, i) - 1);
+	buttons.reserve(num);
+
+	if (istable)
+	{
+		for (int i = 0; i < num; i++)
+		{
+			lua_rawgeti(L, 2, i + 1);
+			buttons.push_back((int) luaL_checknumber(L, -1) - 1);
+			lua_pop(L, 1);
+		}
+	}
+	else
+	{
+		for (int i = 0; i < num; i++)
+			buttons.push_back((int) luaL_checknumber(L, i + 2) - 1);
+	}
 
 	luax_pushboolean(L, j->isDown(buttons));
 	return 1;
@@ -164,20 +182,43 @@ int w_Joystick_isGamepadDown(lua_State *L)
 {
 	Joystick *j = luax_checkjoystick(L, 1);
 
+	bool istable = lua_istable(L, 2);
+	int num = istable ? (int) luax_objlen(L, 2) : (lua_gettop(L) - 1);
+
+	if (num == 0)
+		luaL_checkstring(L, 2);
+
 	std::vector<Joystick::GamepadButton> buttons;
-	buttons.reserve(lua_gettop(L) - 1);
+	buttons.reserve(num);
+
+	Joystick::GamepadButton button;
+
+	if (istable)
+	{
+		for (int i = 0; i < num; i++)
+		{
+			lua_rawgeti(L, 2, i + 1);
+			const char *str = luaL_checkstring(L, -1);
 
-	luaL_checkstring(L, 2);
+			if (!joystick::Joystick::getConstant(str, button))
+				return luaL_error(L, "Invalid gamepad button: %s", str);
 
-	for (int i = 2; i <= lua_gettop(L); i++)
+			buttons.push_back(button);
+
+			lua_pop(L, 1);
+		}
+	}
+	else
 	{
-		const char *str = luaL_checkstring(L, i);
-		Joystick::GamepadButton button;
+		for (int i = 0; i < num; i++)
+		{
+			const char *str = luaL_checkstring(L, i + 2);
 
-		if (!joystick::Joystick::getConstant(str, button))
-			return luaL_error(L, "Invalid gamepad button: %s", str);
+			if (!joystick::Joystick::getConstant(str, button))
+				return luaL_error(L, "Invalid gamepad button: %s", str);
 
-		buttons.push_back(button);
+			buttons.push_back(button);
+		}
 	}
 
 	luax_pushboolean(L, j->isGamepadDown(buttons));

+ 40 - 8
src/modules/keyboard/wrap_Keyboard.cpp

@@ -46,14 +46,30 @@ int w_hasKeyRepeat(lua_State *L)
 int w_isDown(lua_State *L)
 {
 	Keyboard::Key k;
-	int num = lua_gettop(L);
+
+	bool istable = lua_istable(L, 1);
+	int num = istable ? (int) luax_objlen(L, 1) : lua_gettop(L);
+
 	std::vector<Keyboard::Key> keylist;
 	keylist.reserve(num);
 
-	for (int i = 0; i < num; i++)
+	if (istable)
+	{
+		for (int i = 0; i < num; i++)
+		{
+			lua_rawgeti(L, 1, i + 1);
+			if (Keyboard::getConstant(luaL_checkstring(L, -1), k))
+				keylist.push_back(k);
+			lua_pop(L, 1);
+		}
+	}
+	else
 	{
-		if (Keyboard::getConstant(luaL_checkstring(L, i+1), k))
-			keylist.push_back(k);
+		for (int i = 0; i < num; i++)
+		{
+			if (Keyboard::getConstant(luaL_checkstring(L, i+1), k))
+				keylist.push_back(k);
+		}
 	}
 
 	luax_pushboolean(L, instance()->isDown(keylist));
@@ -63,14 +79,30 @@ int w_isDown(lua_State *L)
 int w_isScancodeDown(lua_State *L)
 {
 	Keyboard::Scancode scancode;
-	int num = lua_gettop(L);
+
+	bool istable = lua_istable(L, 1);
+	int num = istable ? (int) luax_objlen(L, 1) : lua_gettop(L);
+
 	std::vector<Keyboard::Scancode> scancodelist;
 	scancodelist.reserve(num);
 
-	for (int i = 0; i < num; i++)
+	if (istable)
+	{
+		for (int i = 0; i < num; i++)
+		{
+			lua_rawgeti(L, 1, i + 1);
+			if (Keyboard::getConstant(luaL_checkstring(L, -1), scancode))
+				scancodelist.push_back(scancode);
+			lua_pop(L, 1);
+		}
+	}
+	else
 	{
-		if (Keyboard::getConstant(luaL_checkstring(L, i+1), scancode))
-			scancodelist.push_back(scancode);
+		for (int i = 0; i < num; i++)
+		{
+			if (Keyboard::getConstant(luaL_checkstring(L, i+1), scancode))
+				scancodelist.push_back(scancode);
+		}
 	}
 
 	luax_pushboolean(L, instance()->isScancodeDown(scancodelist));

+ 17 - 3
src/modules/mouse/wrap_Mouse.cpp

@@ -142,12 +142,26 @@ int w_setPosition(lua_State *L)
 
 int w_isDown(lua_State *L)
 {
-	int num = lua_gettop(L);
+	bool istable = lua_istable(L, 1);
+	int num = istable ? (int) luax_objlen(L, 1) : lua_gettop(L);
+
 	std::vector<int> buttons;
 	buttons.reserve(num);
 
-	for (int i = 0; i < num; i++)
-		buttons.push_back((int) luaL_checknumber(L, i + 1));
+	if (istable)
+	{
+		for (int i = 0; i < num; i++)
+		{
+			lua_rawgeti(L, 1, i + 1);
+			buttons.push_back((int) luaL_checknumber(L, -1));
+			lua_pop(L, 1);
+		}
+	}
+	else
+	{
+		for (int i = 0; i < num; i++)
+			buttons.push_back((int) luaL_checknumber(L, i + 1));
+	}
 
 	luax_pushboolean(L, instance()->isDown(buttons));
 	return 1;