Touch.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright (c) 2006-2015 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 "common/config.h"
  22. #include "common/Exception.h"
  23. #include "Touch.h"
  24. namespace love
  25. {
  26. namespace touch
  27. {
  28. namespace sdl
  29. {
  30. std::vector<int64> Touch::getTouchIDs() const
  31. {
  32. std::vector<int64> ids;
  33. ids.reserve(touches.size());
  34. for (const auto &touch : touches)
  35. ids.push_back(touch.first);
  36. return ids;
  37. }
  38. void Touch::getPosition(int64 id, double &x, double &y) const
  39. {
  40. const auto it = touches.find(id);
  41. if (it == touches.end())
  42. throw love::Exception("Invalid active touch ID: %d", id);
  43. x = it->second.x;
  44. y = it->second.y;
  45. }
  46. const char *Touch::getName() const
  47. {
  48. return "love.touch.sdl";
  49. }
  50. void Touch::onEvent(Uint32 eventtype, const TouchInfo &info)
  51. {
  52. switch (eventtype)
  53. {
  54. case SDL_FINGERDOWN:
  55. case SDL_FINGERMOTION:
  56. touches[info.id] = info;
  57. break;
  58. case SDL_FINGERUP:
  59. touches.erase(info.id);
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. } // sdl
  66. } // touch
  67. } // love