InputHandler.cpp 7.2 KB

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