diff --git a/src/modules/joystick/wrap_Joystick.cpp b/src/modules/joystick/wrap_Joystick.cpp index f02df8257..32791d1cc 100644 --- a/src/modules/joystick/wrap_Joystick.cpp +++ b/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 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 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 buttons; - buttons.reserve(lua_gettop(L) - 1); + buttons.reserve(num); - luaL_checkstring(L, 2); + Joystick::GamepadButton button; - for (int i = 2; i <= lua_gettop(L); i++) + if (istable) { - const char *str = luaL_checkstring(L, i); - Joystick::GamepadButton button; + for (int i = 0; i < num; i++) + { + lua_rawgeti(L, 2, i + 1); + const char *str = luaL_checkstring(L, -1); - 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); + + lua_pop(L, 1); + } + } + else + { + 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); + + buttons.push_back(button); + } } luax_pushboolean(L, j->isGamepadDown(buttons)); diff --git a/src/modules/keyboard/wrap_Keyboard.cpp b/src/modules/keyboard/wrap_Keyboard.cpp index 3b5b9ed1f..647905b19 100644 --- a/src/modules/keyboard/wrap_Keyboard.cpp +++ b/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 keylist; keylist.reserve(num); - for (int i = 0; i < num; i++) + if (istable) { - if (Keyboard::getConstant(luaL_checkstring(L, i+1), k)) - keylist.push_back(k); + 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 + { + 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 scancodelist; scancodelist.reserve(num); - for (int i = 0; i < num; i++) + if (istable) { - if (Keyboard::getConstant(luaL_checkstring(L, i+1), scancode)) - scancodelist.push_back(scancode); + 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 + { + 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)); diff --git a/src/modules/mouse/wrap_Mouse.cpp b/src/modules/mouse/wrap_Mouse.cpp index 2d7f7dc21..90b8892a6 100644 --- a/src/modules/mouse/wrap_Mouse.cpp +++ b/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 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;