InputHandler.cpp 7.1 KB

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