EventDispatcher.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. auto hlp = rclickable;
  181. for(auto & i : hlp)
  182. {
  183. if(!vstd::contains(rclickable, i))
  184. continue;
  185. i->closePopupWindow(!popupOpen);
  186. }
  187. if(popupOpen)
  188. ENGINE->windows().popWindows(1);
  189. }
  190. void EventDispatcher::handleLeftButtonClick(const Point & position, int tolerance, bool isPressed)
  191. {
  192. // WARNING: this approach is NOT SAFE
  193. // 1) We allow (un)registering elements when list itself is being processed/iterated
  194. // 2) To avoid iterator invalidation we create a copy of this list for processing
  195. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  196. // 1) element gets unregistered and deleted from lclickable
  197. // 2) element is completely deleted, as in - destructor called, memory freed
  198. // 3) new element is created *with exactly same address(!)
  199. // 4) new element is registered and code will incorrectly assume that this element is still registered
  200. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  201. AEventsReceiver * nearestElement = findElementInToleranceRange(lclickable, position, AEventsReceiver::LCLICK, tolerance);
  202. auto hlp = lclickable;
  203. bool lastActivated = true;
  204. for(auto & i : hlp)
  205. {
  206. if(!vstd::contains(lclickable, i))
  207. continue;
  208. if( i->receiveEvent(position, AEventsReceiver::LCLICK) || i == nearestElement)
  209. {
  210. if(isPressed)
  211. {
  212. i->mouseClickedState = isPressed;
  213. i->clickPressed(position, lastActivated);
  214. }
  215. else
  216. {
  217. if (i->mouseClickedState)
  218. {
  219. i->mouseClickedState = isPressed;
  220. i->clickReleased(position, lastActivated);
  221. }
  222. else
  223. i->mouseClickedState = isPressed;
  224. }
  225. lastActivated = false;
  226. }
  227. else
  228. {
  229. if(i->mouseClickedState && !isPressed)
  230. {
  231. i->mouseClickedState = isPressed;
  232. i->clickCancel(position);
  233. }
  234. else if(isPressed)
  235. {
  236. i->notFocusedClick();
  237. }
  238. }
  239. }
  240. }
  241. void EventDispatcher::handleDoubleButtonClick(const Point & position, int tolerance)
  242. {
  243. // WARNING: this approach is NOT SAFE
  244. // 1) We allow (un)registering elements when list itself is being processed/iterated
  245. // 2) To avoid iterator invalidation we create a copy of this list for processing
  246. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  247. // 1) element gets unregistered and deleted from lclickable
  248. // 2) element is completely deleted, as in - destructor called, memory freed
  249. // 3) new element is created *with exactly same address(!)
  250. // 4) new element is registered and code will incorrectly assume that this element is still registered
  251. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  252. AEventsReceiver * nearestElement = findElementInToleranceRange(doubleClickInterested, position, AEventsReceiver::DOUBLECLICK, tolerance);
  253. bool doubleClicked = false;
  254. auto hlp = doubleClickInterested;
  255. for(auto & i : hlp)
  256. {
  257. if(!vstd::contains(doubleClickInterested, i))
  258. continue;
  259. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK) || i == nearestElement)
  260. {
  261. i->clickDouble(position);
  262. doubleClicked = true;
  263. }
  264. }
  265. if(!doubleClicked)
  266. handleLeftButtonClick(position, tolerance, true);
  267. }
  268. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  269. {
  270. EventReceiversList hlp = wheelInterested;
  271. for(auto & i : hlp)
  272. {
  273. if(!vstd::contains(wheelInterested,i))
  274. continue;
  275. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  276. i->wheelScrolled(distance.y);
  277. }
  278. }
  279. void EventDispatcher::dispatchTextInput(const std::string & text)
  280. {
  281. for(auto it : textInterested)
  282. {
  283. it->textInputted(text);
  284. }
  285. }
  286. void EventDispatcher::dispatchTextEditing(const std::string & text)
  287. {
  288. for(auto it : textInterested)
  289. {
  290. it->textEdited(text);
  291. }
  292. }
  293. void EventDispatcher::dispatchInputModeChanged(const InputMode & modi)
  294. {
  295. for(auto it : inputModeChangeInterested)
  296. {
  297. it->inputModeChanged(modi);
  298. }
  299. }
  300. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  301. {
  302. auto copied = panningInterested;
  303. for(auto it : copied)
  304. {
  305. if (!vstd::contains(panningInterested, it))
  306. continue;
  307. if (!it->isGesturing() && it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  308. {
  309. it->panningState = true;
  310. it->gesture(true, initialPosition, initialPosition);
  311. }
  312. }
  313. }
  314. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  315. {
  316. dispatchGesturePanningStarted(initialPosition);
  317. auto copied = panningInterested;
  318. for(auto it : copied)
  319. {
  320. if (it->isGesturing())
  321. {
  322. it->panningState = false;
  323. it->gesture(false, initialPosition, finalPosition);
  324. }
  325. }
  326. }
  327. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  328. {
  329. dispatchGesturePanningStarted(initialPosition);
  330. auto copied = panningInterested;
  331. for(auto it : copied)
  332. {
  333. if (!vstd::contains(panningInterested, it))
  334. continue;
  335. if (it->isGesturing())
  336. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  337. }
  338. }
  339. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  340. {
  341. for(auto it : panningInterested)
  342. {
  343. if (it->isGesturing())
  344. it->gesturePinch(initialPosition, distance);
  345. }
  346. }
  347. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  348. {
  349. EventReceiversList newlyHovered;
  350. auto hoverableCopy = hoverable;
  351. for(auto & elem : hoverableCopy)
  352. {
  353. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  354. {
  355. if (!elem->isHovered())
  356. {
  357. newlyHovered.push_back((elem));
  358. }
  359. }
  360. else
  361. {
  362. if (elem->isHovered())
  363. {
  364. elem->hoveredState = false;
  365. elem->hover(false);
  366. }
  367. }
  368. }
  369. for(auto & elem : newlyHovered)
  370. {
  371. elem->hoveredState = true;
  372. elem->hover(true);
  373. }
  374. //sending active, MotionInterested objects mouseMoved() call
  375. EventReceiversList miCopy = motioninterested;
  376. for(auto & elem : miCopy)
  377. {
  378. if (!vstd::contains(motioninterested, elem))
  379. continue;
  380. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  381. elem->mouseMoved(position, distance);
  382. }
  383. }
  384. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  385. {
  386. EventReceiversList diCopy = draginterested;
  387. for(auto & elem : diCopy)
  388. {
  389. if (elem->mouseClickedState)
  390. elem->mouseDragged(currentPosition, lastUpdateDistance);
  391. }
  392. }
  393. void EventDispatcher::dispatchMouseDraggedPopup(const Point & currentPosition, const Point & lastUpdateDistance)
  394. {
  395. EventReceiversList diCopy = dragPopupInterested;
  396. for(auto & elem : diCopy)
  397. elem->mouseDraggedPopup(currentPosition, lastUpdateDistance);
  398. }