EventDispatcher.cpp 11 KB

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