wrap_Mouse.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Mouse::Button button;
  56. if(!Mouse::getConstant(luaL_checkstring(L, 1), button))
  57. luax_pushboolean(L, false);
  58. else
  59. luax_pushboolean(L, instance->isDown(button));
  60. return 1;
  61. }
  62. int w_setVisible(lua_State * L)
  63. {
  64. bool b = luax_toboolean(L, 1);
  65. instance->setVisible(b);
  66. return 0;
  67. }
  68. int w_isVisible(lua_State * L)
  69. {
  70. luax_pushboolean(L, instance->isVisible());
  71. return 1;
  72. }
  73. int w_setGrap(lua_State * L)
  74. {
  75. bool b = luax_toboolean(L, 1);
  76. instance->setGrab(b);
  77. return 0;
  78. }
  79. int w_isGrabbed(lua_State * L)
  80. {
  81. luax_pushboolean(L, instance->isGrabbed());
  82. return 1;
  83. }
  84. // List of functions to wrap.
  85. static const luaL_Reg functions[] = {
  86. { "getX", w_getX },
  87. { "getY", w_getY },
  88. { "setPosition", w_setPosition },
  89. { "isDown", w_isDown },
  90. { "setVisible", w_setVisible },
  91. { "isVisible", w_isVisible },
  92. { "getPosition", w_getPosition },
  93. { "setGrab", w_setGrap },
  94. { "isGrabbed", w_isGrabbed },
  95. { 0, 0 }
  96. };
  97. int luaopen_love_mouse(lua_State * L)
  98. {
  99. if(instance == 0)
  100. {
  101. try
  102. {
  103. instance = new Mouse();
  104. }
  105. catch(Exception & e)
  106. {
  107. return luaL_error(L, e.what());
  108. }
  109. }
  110. WrappedModule w;
  111. w.module = instance;
  112. w.name = "mouse";
  113. w.flags = MODULE_T;
  114. w.functions = functions;
  115. w.types = 0;
  116. return luax_register_module(L, w);
  117. }
  118. } // sdl
  119. } // mouse
  120. } // love