InputHandler.cpp 6.2 KB

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