wrap_Mouse.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Copyright (c) 2006-2009 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 "wrap_Mouse.h"
  21. namespace love
  22. {
  23. namespace mouse
  24. {
  25. namespace sdl
  26. {
  27. static Mouse * instance = 0;
  28. int w_getX(lua_State * L)
  29. {
  30. lua_pushnumber(L, instance->getX());
  31. return 1;
  32. }
  33. int w_getY(lua_State * L)
  34. {
  35. lua_pushnumber(L, instance->getY());
  36. return 1;
  37. }
  38. int w_getPosition(lua_State * L)
  39. {
  40. int x, y;
  41. instance->getPosition(&x, &y);
  42. lua_pushinteger(L, x);
  43. lua_pushinteger(L, y);
  44. return 2;
  45. }
  46. int w_setPosition(lua_State * L)
  47. {
  48. int x = luaL_checkint(L, 1);
  49. int y = luaL_checkint(L, 2);
  50. instance->setPosition(x, y);
  51. return 0;
  52. }
  53. int w_isDown(lua_State * L)
  54. {
  55. int b = luaL_checkint(L, 1);
  56. luax_pushboolean(L, instance->isDown(b));
  57. return 1;
  58. }
  59. int w_setVisible(lua_State * L)
  60. {
  61. bool b = luax_toboolean(L, 1);
  62. instance->setVisible(b);
  63. return 0;
  64. }
  65. int w_isVisible(lua_State * L)
  66. {
  67. luax_pushboolean(L, instance->isVisible());
  68. return 1;
  69. }
  70. int w_setGrap(lua_State * L)
  71. {
  72. bool b = luax_toboolean(L, 1);
  73. instance->setGrab(b);
  74. return 0;
  75. }
  76. int w_isGrabbed(lua_State * L)
  77. {
  78. luax_pushboolean(L, instance->isGrabbed());
  79. return 1;
  80. }
  81. int w_Mouse_open(lua_State * L)
  82. {
  83. // List of functions to wrap.
  84. static const luaL_Reg functions[] = {
  85. { "getX", w_getX },
  86. { "getY", w_getY },
  87. { "setPosition", w_setPosition },
  88. { "isDown", w_isDown },
  89. { "setVisible", w_setVisible },
  90. { "isVisible", w_isVisible },
  91. { "getPosition", w_getPosition },
  92. { "setGrab", w_setGrap },
  93. { "isGrabbed", w_isGrabbed },
  94. { 0, 0 }
  95. };
  96. // List of constants.
  97. static const LuaConstant constants[] = {
  98. { "mouse_left", Mouse::MOUSE_LEFT },
  99. { "mouse_middle", Mouse::MOUSE_MIDDLE },
  100. { "mouse_right", Mouse::MOUSE_RIGHT },
  101. { "mouse_wheelup", Mouse::MOUSE_WHEELUP },
  102. { "mouse_wheeldown", Mouse::MOUSE_WHEELDOWN },
  103. { 0, 0 }
  104. };
  105. if(instance == 0)
  106. {
  107. try
  108. {
  109. instance = new Mouse();
  110. }
  111. catch(Exception & e)
  112. {
  113. return luaL_error(L, e.what());
  114. }
  115. }
  116. luax_register_gc(L, instance);
  117. return luax_register_module(L, functions, 0, constants, "mouse");
  118. }
  119. } // sdl
  120. } // mouse
  121. } // love
  122. int luaopen_love_mouse(lua_State * L)
  123. {
  124. return love::mouse::sdl::w_Mouse_open(L);
  125. }