EventDispatcher.cpp 11 KB

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