InputHandler.cpp 6.3 KB

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