Mouse.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "Mouse.h"
  21. // SDL
  22. #include <SDL.h>
  23. namespace love
  24. {
  25. namespace mouse
  26. {
  27. namespace sdl
  28. {
  29. const char * Mouse::getName() const
  30. {
  31. return "love.mouse.sdl";
  32. }
  33. int Mouse::getX() const
  34. {
  35. int x;
  36. SDL_GetMouseState(&x, 0);
  37. return x;
  38. }
  39. int Mouse::getY() const
  40. {
  41. int y;
  42. SDL_GetMouseState(0, &y);
  43. return y;
  44. }
  45. void Mouse::getPosition(int & x, int & y) const
  46. {
  47. SDL_GetMouseState(&x, &y);
  48. }
  49. void Mouse::setPosition(int x, int y)
  50. {
  51. SDL_WarpMouse(x, y);
  52. }
  53. void Mouse::setVisible(bool visible)
  54. {
  55. SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
  56. }
  57. bool Mouse::isDown(Button button) const
  58. {
  59. unsigned b = 0;
  60. if(!buttons.find(button, b))
  61. return false;
  62. return (SDL_GetMouseState(0, 0) & SDL_BUTTON(b)) != 0;
  63. }
  64. bool Mouse::isVisible() const
  65. {
  66. return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE) ? true : false;
  67. }
  68. void Mouse::setGrab(bool grab)
  69. {
  70. SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
  71. }
  72. bool Mouse::isGrabbed() const
  73. {
  74. return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON ? true : false);
  75. }
  76. EnumMap<Mouse::Button, unsigned, Mouse::BUTTON_MAX_ENUM>::Entry Mouse::buttonEntries[] =
  77. {
  78. { Mouse::BUTTON_LEFT, SDL_BUTTON_LEFT},
  79. { Mouse::BUTTON_MIDDLE, SDL_BUTTON_MIDDLE},
  80. { Mouse::BUTTON_RIGHT, SDL_BUTTON_RIGHT},
  81. { Mouse::BUTTON_WHEELUP, SDL_BUTTON_WHEELUP},
  82. { Mouse::BUTTON_WHEELDOWN, SDL_BUTTON_WHEELDOWN},
  83. { Mouse::BUTTON_X1, SDL_BUTTON_X1},
  84. { Mouse::BUTTON_X2, SDL_BUTTON_X2},
  85. };
  86. EnumMap<Mouse::Button, unsigned, Mouse::BUTTON_MAX_ENUM> Mouse::buttons(Mouse::buttonEntries, sizeof(Mouse::buttonEntries));
  87. } // sdl
  88. } // mouse
  89. } // love