EventDispatcher.cpp 11 KB

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