Mouse.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Copyright (c) 2006-2014 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. // SDL reports mouse coordinates in the window coordinate system in OS X, but
  32. // we want them in pixel coordinates (may be different with high-DPI enabled.)
  33. static void windowToPixelCoords(int *x, int *y)
  34. {
  35. window::Window *window = Module::getInstance<window::Window>(Module::M_WINDOW);
  36. if (window && x)
  37. *x = (int) window->toPixels(*x);
  38. if (window && y)
  39. *y = (int) window->toPixels(*y);
  40. }
  41. // And vice versa for setting mouse coordinates.
  42. static void pixelToWindowCoords(int *x, int *y)
  43. {
  44. window::Window *window = Module::getInstance<window::Window>(Module::M_WINDOW);
  45. if (window && x)
  46. *x = (int) window->fromPixels(*x);
  47. if (window && y)
  48. *y = (int) window->fromPixels(*y);
  49. }
  50. const char *Mouse::getName() const
  51. {
  52. return "love.mouse.sdl";
  53. }
  54. Mouse::Mouse()
  55. : curCursor(nullptr)
  56. {
  57. }
  58. Mouse::~Mouse()
  59. {
  60. if (curCursor.get())
  61. setCursor();
  62. for (auto &c : systemCursors)
  63. c.second->release();
  64. }
  65. love::mouse::Cursor *Mouse::newCursor(love::image::ImageData *data, int hotx, int hoty)
  66. {
  67. return new Cursor(data, hotx, hoty);
  68. }
  69. love::mouse::Cursor *Mouse::getSystemCursor(Cursor::SystemCursor cursortype)
  70. {
  71. Cursor *cursor = nullptr;
  72. auto it = systemCursors.find(cursortype);
  73. if (it != systemCursors.end())
  74. cursor = it->second;
  75. else
  76. {
  77. cursor = new Cursor(cursortype);
  78. systemCursors[cursortype] = cursor;
  79. }
  80. return cursor;
  81. }
  82. void Mouse::setCursor(love::mouse::Cursor *cursor)
  83. {
  84. curCursor.set(cursor);
  85. SDL_SetCursor((SDL_Cursor *) cursor->getHandle());
  86. }
  87. void Mouse::setCursor()
  88. {
  89. curCursor.set(nullptr);
  90. SDL_SetCursor(SDL_GetDefaultCursor());
  91. }
  92. love::mouse::Cursor *Mouse::getCursor() const
  93. {
  94. return curCursor.get();
  95. }
  96. int Mouse::getX() const
  97. {
  98. int x;
  99. SDL_GetMouseState(&x, nullptr);
  100. windowToPixelCoords(&x, nullptr);
  101. return x;
  102. }
  103. int Mouse::getY() const
  104. {
  105. int y;
  106. SDL_GetMouseState(nullptr, &y);
  107. windowToPixelCoords(nullptr, &y);
  108. return y;
  109. }
  110. void Mouse::getPosition(int &x, int &y) const
  111. {
  112. int mx, my;
  113. SDL_GetMouseState(&mx, &my);
  114. windowToPixelCoords(&mx, &my);
  115. x = mx;
  116. y = my;
  117. }
  118. void Mouse::setPosition(int x, int y)
  119. {
  120. love::window::Window *window = love::window::sdl::Window::getSingleton();
  121. SDL_Window *handle = nullptr;
  122. if (window)
  123. handle = (SDL_Window *) window->getHandle();
  124. pixelToWindowCoords(&x, &y);
  125. SDL_WarpMouseInWindow(handle, x, y);
  126. }
  127. void Mouse::setX(int x)
  128. {
  129. int y = getY();
  130. setPosition(x, y);
  131. }
  132. void Mouse::setY(int y)
  133. {
  134. int x = getX();
  135. setPosition(x, y);
  136. }
  137. void Mouse::setVisible(bool visible)
  138. {
  139. SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
  140. }
  141. bool Mouse::isDown(Button *buttonlist) const
  142. {
  143. Uint32 buttonstate = SDL_GetMouseState(nullptr, nullptr);
  144. for (Button button = *buttonlist; button != BUTTON_MAX_ENUM; button = *(++buttonlist))
  145. {
  146. if (buttonstate & SDL_BUTTON(button))
  147. return true;
  148. }
  149. return false;
  150. }
  151. bool Mouse::isVisible() const
  152. {
  153. return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
  154. }
  155. void Mouse::setGrabbed(bool grab)
  156. {
  157. love::window::Window *window = love::window::sdl::Window::getSingleton();
  158. if (window)
  159. window->setMouseGrab(grab);
  160. }
  161. bool Mouse::isGrabbed() const
  162. {
  163. love::window::Window *window = love::window::sdl::Window::getSingleton();
  164. if (window)
  165. return window->isMouseGrabbed();
  166. else
  167. return false;
  168. }
  169. } // sdl
  170. } // mouse
  171. } // love