wrap_Keyboard.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2006-2011 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include <common/config.h>
  21. #include "wrap_Keyboard.h"
  22. #include "sdl/Keyboard.h"
  23. namespace love
  24. {
  25. namespace keyboard
  26. {
  27. static Keyboard * instance;
  28. int w_isDown(lua_State * L)
  29. {
  30. Keyboard::Key k;
  31. unsigned int num = lua_gettop(L);
  32. Keyboard::Key * keylist = new Keyboard::Key[num+1];
  33. unsigned int counter = 0;
  34. for (unsigned int i = 0; i < num; i++)
  35. {
  36. if (Keyboard::getConstant(luaL_checkstring(L, i+1), k))
  37. keylist[counter++] = k;
  38. }
  39. keylist[counter] = Keyboard::KEY_MAX_ENUM;
  40. luax_pushboolean(L, instance->isDown(keylist));
  41. delete[] keylist;
  42. return 1;
  43. }
  44. int w_setKeyRepeat(lua_State * L)
  45. {
  46. if (lua_gettop(L) == 0)
  47. {
  48. // Disables key repeat.
  49. instance->setKeyRepeat(0, 0);
  50. return 0;
  51. }
  52. int delay = lua_isnumber(L, 1) ? (int) (lua_tonumber(L, 1) * 1000 + 0.5) : Keyboard::DEFAULT;
  53. int interval = lua_isnumber(L, 2) ? (int) (lua_tonumber(L, 2) * 1000 + 0.5) : Keyboard::DEFAULT;
  54. instance->setKeyRepeat(delay, interval);
  55. return 0;
  56. }
  57. int w_getKeyRepeat(lua_State * L)
  58. {
  59. lua_pushnumber(L, (lua_Number) instance->getKeyRepeatDelay() * 0.001);
  60. lua_pushnumber(L, (lua_Number) instance->getKeyRepeatInterval() * 0.001);
  61. return 2;
  62. }
  63. // List of functions to wrap.
  64. static const luaL_Reg functions[] = {
  65. { "isDown", w_isDown },
  66. { "setKeyRepeat", w_setKeyRepeat },
  67. { "getKeyRepeat", w_getKeyRepeat },
  68. { 0, 0 }
  69. };
  70. extern "C" int luaopen_love_keyboard(lua_State * L)
  71. {
  72. if (instance == 0)
  73. {
  74. try
  75. {
  76. instance = new love::keyboard::sdl::Keyboard();
  77. }
  78. catch (Exception & e)
  79. {
  80. return luaL_error(L, e.what());
  81. }
  82. }
  83. else
  84. instance->retain();
  85. WrappedModule w;
  86. w.module = instance;
  87. w.name = "keyboard";
  88. w.flags = MODULE_T;
  89. w.functions = functions;
  90. w.types = 0;
  91. return luax_register_module(L, w);
  92. }
  93. } // keyboard
  94. } // love