EventDispatcher.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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)
  103. {
  104. bool doubleClicked = false;
  105. auto hlp = doubleClickInterested;
  106. for(auto & i : hlp)
  107. {
  108. if(!vstd::contains(doubleClickInterested, i))
  109. continue;
  110. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK))
  111. {
  112. i->clickDouble(position);
  113. doubleClicked = true;
  114. }
  115. }
  116. if(!doubleClicked)
  117. handleLeftButtonClick(position, 0, true);
  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 * 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. if (GH.windows().isTopWindowPopup())
  167. GH.windows().popWindows(1);
  168. assert(!GH.windows().isTopWindowPopup());
  169. }
  170. void EventDispatcher::handleLeftButtonClick(const Point & position, int tolerance, bool isPressed)
  171. {
  172. // WARNING: this approach is NOT SAFE
  173. // 1) We allow (un)registering elements when list itself is being processed/iterated
  174. // 2) To avoid iterator invalidation we create a copy of this list for processing
  175. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  176. // 1) element gets unregistered and deleted from lclickable
  177. // 2) element is completely deleted, as in - destructor called, memory freed
  178. // 3) new element is created *with exactly same address(!)
  179. // 4) new element is registered and code will incorrectly assume that this element is still registered
  180. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  181. AEventsReceiver * nearestElement = findElementInToleranceRange(lclickable, position, AEventsReceiver::LCLICK, tolerance);
  182. auto hlp = lclickable;
  183. for(auto & i : hlp)
  184. {
  185. if(!vstd::contains(lclickable, i))
  186. continue;
  187. if( i->receiveEvent(position, AEventsReceiver::LCLICK) || i == nearestElement)
  188. {
  189. if(isPressed)
  190. i->clickPressed(position);
  191. if (i->mouseClickedState && !isPressed)
  192. i->clickReleased(position);
  193. i->mouseClickedState = isPressed;
  194. }
  195. else
  196. {
  197. if(i->mouseClickedState && !isPressed)
  198. {
  199. i->mouseClickedState = isPressed;
  200. i->clickCancel(position);
  201. }
  202. }
  203. }
  204. }
  205. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  206. {
  207. EventReceiversList hlp = wheelInterested;
  208. for(auto & i : hlp)
  209. {
  210. if(!vstd::contains(wheelInterested,i))
  211. continue;
  212. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  213. i->wheelScrolled(distance.y);
  214. }
  215. }
  216. void EventDispatcher::dispatchTextInput(const std::string & text)
  217. {
  218. for(auto it : textInterested)
  219. {
  220. it->textInputed(text);
  221. }
  222. }
  223. void EventDispatcher::dispatchTextEditing(const std::string & text)
  224. {
  225. for(auto it : textInterested)
  226. {
  227. it->textEdited(text);
  228. }
  229. }
  230. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  231. {
  232. auto copied = panningInterested;
  233. for(auto it : copied)
  234. {
  235. if (!vstd::contains(panningInterested, it))
  236. continue;
  237. if (!it->isGesturing() && it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  238. {
  239. it->panningState = true;
  240. it->gesture(true, initialPosition, initialPosition);
  241. }
  242. }
  243. }
  244. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  245. {
  246. dispatchGesturePanningStarted(initialPosition);
  247. auto copied = panningInterested;
  248. for(auto it : copied)
  249. {
  250. if (it->isGesturing())
  251. {
  252. it->panningState = false;
  253. it->gesture(false, initialPosition, finalPosition);
  254. }
  255. }
  256. }
  257. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  258. {
  259. dispatchGesturePanningStarted(initialPosition);
  260. auto copied = panningInterested;
  261. for(auto it : copied)
  262. {
  263. if (!vstd::contains(panningInterested, it))
  264. continue;
  265. if (it->isGesturing())
  266. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  267. }
  268. }
  269. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  270. {
  271. for(auto it : panningInterested)
  272. {
  273. if (it->isGesturing())
  274. it->gesturePinch(initialPosition, distance);
  275. }
  276. }
  277. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  278. {
  279. EventReceiversList newlyHovered;
  280. auto hoverableCopy = hoverable;
  281. for(auto & elem : hoverableCopy)
  282. {
  283. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  284. {
  285. if (!elem->isHovered())
  286. {
  287. newlyHovered.push_back((elem));
  288. }
  289. }
  290. else
  291. {
  292. if (elem->isHovered())
  293. {
  294. elem->hoveredState = false;
  295. elem->hover(false);
  296. }
  297. }
  298. }
  299. for(auto & elem : newlyHovered)
  300. {
  301. elem->hoveredState = true;
  302. elem->hover(true);
  303. }
  304. //sending active, MotionInterested objects mouseMoved() call
  305. EventReceiversList miCopy = motioninterested;
  306. for(auto & elem : miCopy)
  307. {
  308. if (!vstd::contains(motioninterested, elem))
  309. continue;
  310. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  311. elem->mouseMoved(position, distance);
  312. }
  313. }
  314. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  315. {
  316. EventReceiversList diCopy = draginterested;
  317. for(auto & elem : diCopy)
  318. {
  319. if (elem->mouseClickedState)
  320. elem->mouseDragged(currentPosition, lastUpdateDistance);
  321. }
  322. }