InputHandler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * InputHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "InputHandler.h"
  12. #include "NotificationHandler.h"
  13. #include "InputSourceMouse.h"
  14. #include "InputSourceKeyboard.h"
  15. #include "InputSourceTouch.h"
  16. #include "InputSourceText.h"
  17. #include "UserEventHandler.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../gui/CursorHandler.h"
  20. #include "../gui/EventDispatcher.h"
  21. #include "../gui/MouseButton.h"
  22. #include "../CMT.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../CGameInfo.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include <SDL_events.h>
  27. #include <SDL_hints.h>
  28. InputHandler::InputHandler()
  29. : mouseHandler(std::make_unique<InputSourceMouse>())
  30. , keyboardHandler(std::make_unique<InputSourceKeyboard>())
  31. , fingerHandler(std::make_unique<InputSourceTouch>())
  32. , textHandler(std::make_unique<InputSourceText>())
  33. , userHandler(std::make_unique<UserEventHandler>())
  34. {
  35. }
  36. InputHandler::~InputHandler() = default;
  37. void InputHandler::handleCurrentEvent(const SDL_Event & current)
  38. {
  39. switch (current.type)
  40. {
  41. case SDL_KEYDOWN:
  42. return keyboardHandler->handleEventKeyDown(current.key);
  43. case SDL_KEYUP:
  44. return keyboardHandler->handleEventKeyUp(current.key);
  45. case SDL_MOUSEMOTION:
  46. return mouseHandler->handleEventMouseMotion(current.motion);
  47. case SDL_MOUSEBUTTONDOWN:
  48. return mouseHandler->handleEventMouseButtonDown(current.button);
  49. case SDL_MOUSEWHEEL:
  50. return mouseHandler->handleEventMouseWheel(current.wheel);
  51. case SDL_TEXTINPUT:
  52. return textHandler->handleEventTextInput(current.text);
  53. case SDL_TEXTEDITING:
  54. return textHandler->handleEventTextEditing(current.edit);
  55. case SDL_MOUSEBUTTONUP:
  56. return mouseHandler->handleEventMouseButtonUp(current.button);
  57. case SDL_FINGERMOTION:
  58. return fingerHandler->handleEventFingerMotion(current.tfinger);
  59. case SDL_FINGERDOWN:
  60. return fingerHandler->handleEventFingerDown(current.tfinger);
  61. case SDL_FINGERUP:
  62. return fingerHandler->handleEventFingerUp(current.tfinger);
  63. }
  64. }
  65. void InputHandler::processEvents()
  66. {
  67. boost::unique_lock<boost::mutex> lock(eventsMutex);
  68. for (auto const & currentEvent : eventsQueue)
  69. handleCurrentEvent(currentEvent);
  70. eventsQueue.clear();
  71. fingerHandler->handleUpdate();
  72. }
  73. bool InputHandler::ignoreEventsUntilInput()
  74. {
  75. bool inputFound = false;
  76. boost::unique_lock<boost::mutex> lock(eventsMutex);
  77. for (auto const & event : eventsQueue)
  78. {
  79. switch(event.type)
  80. {
  81. case SDL_MOUSEBUTTONDOWN:
  82. case SDL_FINGERDOWN:
  83. case SDL_KEYDOWN:
  84. inputFound = true;
  85. }
  86. }
  87. eventsQueue.clear();
  88. return inputFound;
  89. }
  90. void InputHandler::preprocessEvent(const SDL_Event & ev)
  91. {
  92. if((ev.type==SDL_QUIT) ||(ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT)))
  93. {
  94. #ifdef VCMI_ANDROID
  95. handleQuit(false);
  96. #else
  97. handleQuit();
  98. #endif
  99. return;
  100. }
  101. #ifdef VCMI_ANDROID
  102. else if (ev.type == SDL_KEYDOWN && ev.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
  103. {
  104. handleQuit(true);
  105. }
  106. #endif
  107. else if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4)
  108. {
  109. Settings full = settings.write["video"]["fullscreen"];
  110. full->Bool() = !full->Bool();
  111. return;
  112. }
  113. else if(ev.type == SDL_USEREVENT)
  114. {
  115. userHandler->handleUserEvent(ev.user);
  116. return;
  117. }
  118. else if(ev.type == SDL_WINDOWEVENT)
  119. {
  120. switch (ev.window.event) {
  121. case SDL_WINDOWEVENT_RESTORED:
  122. #ifndef VCMI_IOS
  123. {
  124. boost::unique_lock<boost::recursive_mutex> lock(*CPlayerInterface::pim);
  125. GH.onScreenResize();
  126. }
  127. #endif
  128. break;
  129. }
  130. return;
  131. }
  132. else if(ev.type == SDL_SYSWMEVENT)
  133. {
  134. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  135. {
  136. NotificationHandler::handleSdlEvent(ev);
  137. }
  138. }
  139. //preprocessing
  140. if(ev.type == SDL_MOUSEMOTION)
  141. {
  142. if (CCS && CCS->curh)
  143. CCS->curh->cursorMove(ev.motion.x, ev.motion.y);
  144. }
  145. {
  146. boost::unique_lock<boost::mutex> lock(eventsMutex);
  147. if(ev.type == SDL_MOUSEMOTION && !eventsQueue.empty() && eventsQueue.back().type == SDL_MOUSEMOTION)
  148. {
  149. // In a sequence of mouse motion events, skip all but the last one.
  150. // This prevents freezes when every motion event takes longer to handle than interval at which
  151. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  152. // so that the events would start piling up faster than they can be processed.
  153. eventsQueue.back() = ev;
  154. return;
  155. }
  156. eventsQueue.push_back(ev);
  157. }
  158. }
  159. void InputHandler::fetchEvents()
  160. {
  161. SDL_Event ev;
  162. while(1 == SDL_PollEvent(&ev))
  163. {
  164. preprocessEvent(ev);
  165. }
  166. }
  167. bool InputHandler::isKeyboardCtrlDown() const
  168. {
  169. return keyboardHandler->isKeyboardCtrlDown();
  170. }
  171. bool InputHandler::isKeyboardAltDown() const
  172. {
  173. return keyboardHandler->isKeyboardAltDown();
  174. }
  175. bool InputHandler::isKeyboardShiftDown() const
  176. {
  177. return keyboardHandler->isKeyboardShiftDown();
  178. }
  179. void InputHandler::moveCursorPosition(const Point & distance)
  180. {
  181. setCursorPosition(getCursorPosition() + distance);
  182. }
  183. void InputHandler::setCursorPosition(const Point & position)
  184. {
  185. cursorPosition = position;
  186. GH.events().dispatchMouseMoved(position);
  187. }
  188. void InputHandler::startTextInput(const Rect & where)
  189. {
  190. textHandler->startTextInput(where);
  191. }
  192. void InputHandler::stopTextInput()
  193. {
  194. textHandler->stopTextInput();
  195. }
  196. bool InputHandler::isMouseButtonPressed(MouseButton button) const
  197. {
  198. return mouseHandler->isMouseButtonPressed(button) || fingerHandler->isMouseButtonPressed(button);
  199. }
  200. void InputHandler::pushUserEvent(EUserEvent usercode, void * userdata)
  201. {
  202. SDL_Event event;
  203. event.type = SDL_USEREVENT;
  204. event.user.code = static_cast<int32_t>(usercode);
  205. event.user.data1 = userdata;
  206. SDL_PushEvent(&event);
  207. }
  208. const Point & InputHandler::getCursorPosition() const
  209. {
  210. return cursorPosition;
  211. }