EventDispatcher.cpp 11 KB

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