Mouse.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2006-2013 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. // LOVE
  21. #include "Mouse.h"
  22. #include "window/sdl/Window.h"
  23. // SDL
  24. #include <SDL_mouse.h>
  25. namespace love
  26. {
  27. namespace mouse
  28. {
  29. namespace sdl
  30. {
  31. const char *Mouse::getName() const
  32. {
  33. return "love.mouse.sdl";
  34. }
  35. Mouse::Mouse()
  36. : curCursor(0)
  37. {
  38. }
  39. Mouse::~Mouse()
  40. {
  41. if (curCursor)
  42. setCursor();
  43. }
  44. love::mouse::Cursor *Mouse::newCursor(love::image::ImageData *data, int hotx, int hoty)
  45. {
  46. return new Cursor(data, hotx, hoty);
  47. }
  48. love::mouse::Cursor *Mouse::newCursor(love::mouse::Cursor::SystemCursor cursortype)
  49. {
  50. return new Cursor(cursortype);
  51. }
  52. void Mouse::setCursor(love::mouse::Cursor *cursor)
  53. {
  54. Object::AutoRelease cursorrelease(curCursor);
  55. curCursor = cursor;
  56. curCursor->retain();
  57. SDL_SetCursor((SDL_Cursor *) cursor->getHandle());
  58. }
  59. void Mouse::setCursor()
  60. {
  61. Object::AutoRelease cursorrelease(curCursor);
  62. curCursor = NULL;
  63. SDL_SetCursor(SDL_GetDefaultCursor());
  64. }
  65. love::mouse::Cursor *Mouse::getCursor() const
  66. {
  67. return curCursor;
  68. }
  69. int Mouse::getX() const
  70. {
  71. int x;
  72. SDL_GetMouseState(&x, 0);
  73. return x;
  74. }
  75. int Mouse::getY() const
  76. {
  77. int y;
  78. SDL_GetMouseState(0, &y);
  79. return y;
  80. }
  81. void Mouse::getPosition(int &x, int &y) const
  82. {
  83. SDL_GetMouseState(&x, &y);
  84. }
  85. void Mouse::setPosition(int x, int y)
  86. {
  87. love::window::Window *window = love::window::sdl::Window::getSingleton();
  88. SDL_Window *handle = NULL;
  89. if (window)
  90. handle = (SDL_Window *) window->getHandle();
  91. SDL_WarpMouseInWindow(handle, x, y);
  92. }
  93. void Mouse::setX(int x)
  94. {
  95. int y = getY();
  96. setPosition(x, y);
  97. }
  98. void Mouse::setY(int y)
  99. {
  100. int x = getX();
  101. setPosition(x, y);
  102. }
  103. void Mouse::setVisible(bool visible)
  104. {
  105. SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
  106. }
  107. bool Mouse::isDown(Button *buttonlist) const
  108. {
  109. Uint8 buttonstate = SDL_GetMouseState(0, 0);
  110. for (Button button = *buttonlist; button != BUTTON_MAX_ENUM; button = *(++buttonlist))
  111. {
  112. if (buttonstate & SDL_BUTTON(button))
  113. return true;
  114. }
  115. return false;
  116. }
  117. bool Mouse::isVisible() const
  118. {
  119. return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
  120. }
  121. void Mouse::setGrab(bool grab)
  122. {
  123. love::window::Window *window = love::window::sdl::Window::getSingleton();
  124. if (window)
  125. window->setMouseGrab(grab);
  126. }
  127. bool Mouse::isGrabbed() const
  128. {
  129. love::window::Window *window = love::window::sdl::Window::getSingleton();
  130. if (window)
  131. return window->isMouseGrabbed();
  132. else
  133. return false;
  134. }
  135. } // sdl
  136. } // mouse
  137. } // love