MouseModule.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * author: Patrick-Christopher Mattulat
  3. * dev-tools: JetBrains CLion
  4. * created: 20.08.2018
  5. * last updated: 25.08.2018
  6. */
  7. #include "MouseModule.h"
  8. #include <SDL.h>
  9. //////////////////////////////////////////////////
  10. //////////////////////////////////////////////////
  11. //// constructor / destructor
  12. //////////////////////////////////////////////////
  13. //////////////////////////////////////////////////
  14. MouseModule::MouseModule():
  15. leftButton(0),
  16. rightButton(0)
  17. {
  18. this->position = {0.0f, 0.0f};
  19. }
  20. MouseModule::~MouseModule() = default;
  21. //////////////////////////////////////////////////
  22. //////////////////////////////////////////////////
  23. //// public
  24. //////////////////////////////////////////////////
  25. //////////////////////////////////////////////////
  26. void MouseModule::freeKeys()
  27. {
  28. if(this->leftButton == LE_MOUSE_BUTTON_RELEASED) {
  29. this->leftButton = LE_MOUSE_BUTTON_INACTIVE;
  30. }
  31. if(this->rightButton == LE_MOUSE_BUTTON_RELEASED) {
  32. this->rightButton = LE_MOUSE_BUTTON_INACTIVE;
  33. }
  34. }
  35. void MouseModule::getMouseState()
  36. {
  37. SDL_Point point = {(int) this->position.x, (int) this->position.y};
  38. SDL_GetMouseState(&(point.x), &(point.y));
  39. }
  40. LynarMath::_Point_d MouseModule::getPosition()
  41. {
  42. return this->position;
  43. }
  44. bool MouseModule::hasEvent(uint32_t state, uint8_t button)
  45. {
  46. bool flag = false;
  47. switch(state)
  48. {
  49. case SDL_MOUSEBUTTONDOWN:
  50. {
  51. switch(button)
  52. {
  53. case SDL_BUTTON_LEFT:
  54. {
  55. flag = (this->leftButton == LE_MOUSE_BUTTON_PRESSED);
  56. } break;
  57. case SDL_BUTTON_RIGHT:
  58. {
  59. flag = (this->rightButton == LE_MOUSE_BUTTON_PRESSED);
  60. } break;
  61. default:
  62. {} break;
  63. };
  64. } break;
  65. case SDL_MOUSEBUTTONUP:
  66. {
  67. switch(button)
  68. {
  69. case SDL_BUTTON_LEFT:
  70. {
  71. flag = (this->leftButton == LE_MOUSE_BUTTON_RELEASED);
  72. } break;
  73. case SDL_BUTTON_RIGHT:
  74. {
  75. flag = (this->rightButton == LE_MOUSE_BUTTON_RELEASED);
  76. } break;
  77. default:
  78. {} break;
  79. };
  80. } break;
  81. default:
  82. {} break;
  83. };
  84. return flag;
  85. }
  86. void MouseModule::scanKeys(SDL_Event event)
  87. {
  88. switch(event.type)
  89. {
  90. case SDL_MOUSEBUTTONDOWN:
  91. {
  92. switch(event.button.button)
  93. {
  94. case SDL_BUTTON_LEFT:
  95. {
  96. this->leftButton = LE_MOUSE_BUTTON_PRESSED;
  97. } break;
  98. case SDL_BUTTON_RIGHT:
  99. {
  100. this->rightButton = LE_MOUSE_BUTTON_PRESSED;
  101. } break;
  102. default:
  103. {} break;
  104. };
  105. } break;
  106. case SDL_MOUSEBUTTONUP:
  107. {
  108. switch(event.button.button)
  109. {
  110. case SDL_BUTTON_LEFT:
  111. {
  112. this->leftButton = LE_MOUSE_BUTTON_RELEASED;
  113. } break;
  114. case SDL_BUTTON_RIGHT:
  115. {
  116. this->rightButton = LE_MOUSE_BUTTON_RELEASED;
  117. } break;
  118. default:
  119. {} break;
  120. };
  121. } break;
  122. default:
  123. {} break;
  124. };
  125. }