EventDispatcher.cpp 12 KB

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