EventDispatcher.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * EventDispatcher.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 "EventDispatcher.h"
  12. #include "EventsReceiver.h"
  13. #include "FramerateManager.h"
  14. #include "GameEngine.h"
  15. #include "MouseButton.h"
  16. #include "WindowHandler.h"
  17. #include "gui/Shortcut.h"
  18. #include "../../lib/CConfigHandler.h"
  19. #include "../../lib/Rect.h"
  20. #include "../eventsSDL/InputHandler.h"
  21. template<typename Functor>
  22. void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
  23. {
  24. auto processList = [&](ui16 mask, EventReceiversList & lst)
  25. {
  26. if(mask & activityFlag)
  27. cb(lst);
  28. };
  29. processList(AEventsReceiver::LCLICK, lclickable);
  30. processList(AEventsReceiver::SHOW_POPUP, rclickable);
  31. processList(AEventsReceiver::HOVER, hoverable);
  32. processList(AEventsReceiver::MOVE, motioninterested);
  33. processList(AEventsReceiver::DRAG, draginterested);
  34. processList(AEventsReceiver::DRAG_POPUP, dragPopupInterested);
  35. processList(AEventsReceiver::KEYBOARD, keyinterested);
  36. processList(AEventsReceiver::TIME, timeinterested);
  37. processList(AEventsReceiver::WHEEL, wheelInterested);
  38. processList(AEventsReceiver::DOUBLECLICK, doubleClickInterested);
  39. processList(AEventsReceiver::TEXTINPUT, textInterested);
  40. processList(AEventsReceiver::GESTURE, panningInterested);
  41. processList(AEventsReceiver::INPUT_MODE_CHANGE, inputModeChangeInterested);
  42. processList(AEventsReceiver::KEY_NAME, keyNameInterested);
  43. }
  44. void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag)
  45. {
  46. processLists(activityFlag,[&](EventReceiversList & lst){
  47. lst.push_front(elem);
  48. });
  49. elem->activeState |= activityFlag;
  50. }
  51. void EventDispatcher::deactivateElement(AEventsReceiver * elem, ui16 activityFlag)
  52. {
  53. processLists(activityFlag,[&](EventReceiversList & lst){
  54. auto hlp = std::find(lst.begin(),lst.end(),elem);
  55. assert(hlp != lst.end());
  56. lst.erase(hlp);
  57. });
  58. elem->activeState &= ~activityFlag;
  59. }
  60. void EventDispatcher::dispatchTimer(uint32_t msPassed)
  61. {
  62. EventReceiversList hlp = timeinterested;
  63. for (auto & elem : hlp)
  64. {
  65. if(!vstd::contains(timeinterested,elem))
  66. continue;
  67. elem->tick(msPassed);
  68. }
  69. }
  70. void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & shortcutsVector)
  71. {
  72. bool keysCaptured = false;
  73. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_LEFT))
  74. dispatchMouseLeftButtonPressed(ENGINE->getCursorPosition(), settings["input"]["shortcutToleranceDistance"].Integer());
  75. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_RIGHT))
  76. dispatchShowPopup(ENGINE->getCursorPosition(), settings["input"]["shortcutToleranceDistance"].Integer());
  77. for(auto & i : keyinterested)
  78. for(EShortcut shortcut : shortcutsVector)
  79. if(i->captureThisKey(shortcut))
  80. keysCaptured = true;
  81. EventReceiversList miCopy = keyinterested;
  82. for(auto & i : miCopy)
  83. {
  84. for(EShortcut shortcut : shortcutsVector)
  85. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  86. {
  87. i->keyPressed(shortcut);
  88. if (keysCaptured)
  89. return;
  90. }
  91. }
  92. }
  93. void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector)
  94. {
  95. bool keysCaptured = false;
  96. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_LEFT))
  97. dispatchMouseLeftButtonReleased(ENGINE->getCursorPosition(), settings["input"]["shortcutToleranceDistance"].Integer());
  98. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_RIGHT))
  99. dispatchClosePopup(ENGINE->getCursorPosition());
  100. for(auto & i : keyinterested)
  101. for(EShortcut shortcut : shortcutsVector)
  102. if(i->captureThisKey(shortcut))
  103. keysCaptured = true;
  104. EventReceiversList miCopy = keyinterested;
  105. for(auto & i : miCopy)
  106. {
  107. for(EShortcut shortcut : shortcutsVector)
  108. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  109. {
  110. i->keyReleased(shortcut);
  111. if (keysCaptured)
  112. return;
  113. }
  114. }
  115. }
  116. void EventDispatcher::dispatchKeyPressed(const std::string & keyName)
  117. {
  118. EventReceiversList miCopy = keyNameInterested;
  119. for(auto & i : miCopy)
  120. i->keyPressed(keyName);
  121. }
  122. void EventDispatcher::dispatchKeyReleased(const std::string & keyName)
  123. {
  124. EventReceiversList miCopy = keyNameInterested;
  125. for(auto & i : miCopy)
  126. i->keyReleased(keyName);
  127. }
  128. void EventDispatcher::dispatchMouseDoubleClick(const Point & position, int tolerance)
  129. {
  130. handleDoubleButtonClick(position, tolerance);
  131. }
  132. void EventDispatcher::dispatchMouseLeftButtonPressed(const Point & position, int tolerance)
  133. {
  134. handleLeftButtonClick(position, tolerance, true);
  135. }
  136. void EventDispatcher::dispatchMouseLeftButtonReleased(const Point & position, int tolerance)
  137. {
  138. handleLeftButtonClick(position, tolerance, false);
  139. }
  140. AEventsReceiver * EventDispatcher::findElementInToleranceRange(const EventReceiversList & list, const Point & position, int eventToTest, int tolerance)
  141. {
  142. AEventsReceiver * bestElement = nullptr;
  143. int bestDistance = std::numeric_limits<int>::max();
  144. for(auto & i : list)
  145. {
  146. // if there is element that can actually receive event then tolerance clicking is disabled
  147. if( i->receiveEvent(position, eventToTest))
  148. return nullptr;
  149. if (i->getPosition().distanceTo(position) > bestDistance)
  150. continue;
  151. Point center = i->getPosition().center();
  152. Point distance = center - position;
  153. if (distance.lengthSquared() == 0)
  154. continue;
  155. Point moveDelta = distance * std::min(1.0, static_cast<double>(tolerance) / distance.length());
  156. Point testPosition = position + moveDelta;
  157. if( !i->receiveEvent(testPosition, eventToTest))
  158. continue;
  159. bestElement = i;
  160. bestDistance = i->getPosition().distanceTo(position);
  161. }
  162. return bestElement;
  163. }
  164. void EventDispatcher::dispatchShowPopup(const Point & position, int tolerance)
  165. {
  166. AEventsReceiver * nearestElement = findElementInToleranceRange(rclickable, position, AEventsReceiver::SHOW_POPUP, tolerance);
  167. auto hlp = rclickable;
  168. for(auto & i : hlp)
  169. {
  170. if(!vstd::contains(rclickable, i))
  171. continue;
  172. if( !i->receiveEvent(position, AEventsReceiver::SHOW_POPUP) && i != nearestElement)
  173. continue;
  174. i->showPopupWindow(position);
  175. }
  176. }
  177. void EventDispatcher::dispatchClosePopup(const Point & position)
  178. {
  179. bool popupOpen = ENGINE->windows().isTopWindowPopup(); // popup can already be closed for mouse dragging with RMB
  180. if(popupOpen)
  181. {
  182. ENGINE->windows().popWindows(1);
  183. }
  184. auto hlp = rclickable;
  185. for(auto & i : hlp)
  186. {
  187. if(!vstd::contains(rclickable, i))
  188. continue;
  189. i->closePopupWindow(!popupOpen);
  190. }
  191. }
  192. void EventDispatcher::handleLeftButtonClick(const Point & position, int tolerance, bool isPressed)
  193. {
  194. // WARNING: this approach is NOT SAFE
  195. // 1) We allow (un)registering elements when list itself is being processed/iterated
  196. // 2) To avoid iterator invalidation we create a copy of this list for processing
  197. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  198. // 1) element gets unregistered and deleted from lclickable
  199. // 2) element is completely deleted, as in - destructor called, memory freed
  200. // 3) new element is created *with exactly same address(!)
  201. // 4) new element is registered and code will incorrectly assume that this element is still registered
  202. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  203. AEventsReceiver * nearestElement = findElementInToleranceRange(lclickable, position, AEventsReceiver::LCLICK, tolerance);
  204. auto hlp = lclickable;
  205. bool lastActivated = true;
  206. for(auto & i : hlp)
  207. {
  208. if(!vstd::contains(lclickable, i))
  209. continue;
  210. if( i->receiveEvent(position, AEventsReceiver::LCLICK) || i == nearestElement)
  211. {
  212. if(isPressed)
  213. {
  214. i->mouseClickedState = isPressed;
  215. i->clickPressed(position, lastActivated);
  216. }
  217. else
  218. {
  219. if (i->mouseClickedState)
  220. {
  221. i->mouseClickedState = isPressed;
  222. i->clickReleased(position, lastActivated);
  223. }
  224. else
  225. i->mouseClickedState = isPressed;
  226. }
  227. lastActivated = false;
  228. }
  229. else
  230. {
  231. if(i->mouseClickedState && !isPressed)
  232. {
  233. i->mouseClickedState = isPressed;
  234. i->clickCancel(position);
  235. }
  236. else if(isPressed)
  237. {
  238. i->notFocusedClick();
  239. }
  240. }
  241. }
  242. }
  243. void EventDispatcher::handleDoubleButtonClick(const Point & position, int tolerance)
  244. {
  245. // WARNING: this approach is NOT SAFE
  246. // 1) We allow (un)registering elements when list itself is being processed/iterated
  247. // 2) To avoid iterator invalidation we create a copy of this list for processing
  248. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  249. // 1) element gets unregistered and deleted from lclickable
  250. // 2) element is completely deleted, as in - destructor called, memory freed
  251. // 3) new element is created *with exactly same address(!)
  252. // 4) new element is registered and code will incorrectly assume that this element is still registered
  253. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  254. AEventsReceiver * nearestElement = findElementInToleranceRange(doubleClickInterested, position, AEventsReceiver::DOUBLECLICK, tolerance);
  255. bool doubleClicked = false;
  256. auto hlp = doubleClickInterested;
  257. for(auto & i : hlp)
  258. {
  259. if(!vstd::contains(doubleClickInterested, i))
  260. continue;
  261. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK) || i == nearestElement)
  262. {
  263. i->clickDouble(position);
  264. doubleClicked = true;
  265. }
  266. }
  267. if(!doubleClicked)
  268. handleLeftButtonClick(position, tolerance, true);
  269. }
  270. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  271. {
  272. EventReceiversList hlp = wheelInterested;
  273. for(auto & i : hlp)
  274. {
  275. if(!vstd::contains(wheelInterested,i))
  276. continue;
  277. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  278. i->wheelScrolled(distance.y);
  279. }
  280. }
  281. void EventDispatcher::dispatchTextInput(const std::string & text)
  282. {
  283. for(auto it : textInterested)
  284. {
  285. it->textInputted(text);
  286. }
  287. }
  288. void EventDispatcher::dispatchTextEditing(const std::string & text)
  289. {
  290. for(auto it : textInterested)
  291. {
  292. it->textEdited(text);
  293. }
  294. }
  295. void EventDispatcher::dispatchInputModeChanged(const InputMode & modi)
  296. {
  297. for(auto it : inputModeChangeInterested)
  298. {
  299. it->inputModeChanged(modi);
  300. }
  301. }
  302. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  303. {
  304. auto copied = panningInterested;
  305. for(auto it : copied)
  306. {
  307. if (!vstd::contains(panningInterested, it))
  308. continue;
  309. if (!it->isGesturing() && it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  310. {
  311. it->panningState = true;
  312. it->gesture(true, initialPosition, initialPosition);
  313. }
  314. }
  315. }
  316. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  317. {
  318. dispatchGesturePanningStarted(initialPosition);
  319. auto copied = panningInterested;
  320. for(auto it : copied)
  321. {
  322. if (it->isGesturing())
  323. {
  324. it->panningState = false;
  325. it->gesture(false, initialPosition, finalPosition);
  326. }
  327. }
  328. }
  329. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  330. {
  331. dispatchGesturePanningStarted(initialPosition);
  332. auto copied = panningInterested;
  333. for(auto it : copied)
  334. {
  335. if (!vstd::contains(panningInterested, it))
  336. continue;
  337. if (it->isGesturing())
  338. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  339. }
  340. }
  341. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  342. {
  343. for(auto it : panningInterested)
  344. {
  345. if (it->isGesturing())
  346. it->gesturePinch(initialPosition, distance);
  347. }
  348. }
  349. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  350. {
  351. EventReceiversList newlyHovered;
  352. auto hoverableCopy = hoverable;
  353. for(auto & elem : hoverableCopy)
  354. {
  355. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  356. {
  357. if (!elem->isHovered())
  358. {
  359. newlyHovered.push_back((elem));
  360. }
  361. }
  362. else
  363. {
  364. if (elem->isHovered())
  365. {
  366. elem->hoveredState = false;
  367. elem->hover(false);
  368. }
  369. }
  370. }
  371. for(auto & elem : newlyHovered)
  372. {
  373. elem->hoveredState = true;
  374. elem->hover(true);
  375. }
  376. //sending active, MotionInterested objects mouseMoved() call
  377. EventReceiversList miCopy = motioninterested;
  378. for(auto & elem : miCopy)
  379. {
  380. if (!vstd::contains(motioninterested, elem))
  381. continue;
  382. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  383. elem->mouseMoved(position, distance);
  384. }
  385. }
  386. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  387. {
  388. EventReceiversList diCopy = draginterested;
  389. for(auto & elem : diCopy)
  390. {
  391. if (elem->mouseClickedState)
  392. elem->mouseDragged(currentPosition, lastUpdateDistance);
  393. }
  394. }
  395. void EventDispatcher::dispatchMouseDraggedPopup(const Point & currentPosition, const Point & lastUpdateDistance)
  396. {
  397. EventReceiversList diCopy = dragPopupInterested;
  398. for(auto & elem : diCopy)
  399. elem->mouseDraggedPopup(currentPosition, lastUpdateDistance);
  400. }