EventDispatcher.cpp 12 KB

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