EventDispatcher.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #include "../eventsSDL/InputHandler.h"
  21. template<typename Functor>
  22. void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
  23. {
  24. auto processList = [&](ui16 mask, EventReceiversList & lst)
  25. {
  26. if(mask & activityFlag)
  27. cb(lst);
  28. };
  29. processList(AEventsReceiver::LCLICK, lclickable);
  30. processList(AEventsReceiver::SHOW_POPUP, rclickable);
  31. processList(AEventsReceiver::HOVER, hoverable);
  32. processList(AEventsReceiver::MOVE, motioninterested);
  33. processList(AEventsReceiver::DRAG, draginterested);
  34. processList(AEventsReceiver::DRAG_POPUP, dragPopupInterested);
  35. processList(AEventsReceiver::KEYBOARD, keyinterested);
  36. processList(AEventsReceiver::TIME, timeinterested);
  37. processList(AEventsReceiver::WHEEL, wheelInterested);
  38. processList(AEventsReceiver::DOUBLECLICK, doubleClickInterested);
  39. processList(AEventsReceiver::TEXTINPUT, textInterested);
  40. processList(AEventsReceiver::GESTURE, panningInterested);
  41. processList(AEventsReceiver::INPUT_MODE_CHANGE, inputModeChangeInterested);
  42. }
  43. void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag)
  44. {
  45. processLists(activityFlag,[&](EventReceiversList & lst){
  46. lst.push_front(elem);
  47. });
  48. elem->activeState |= activityFlag;
  49. }
  50. void EventDispatcher::deactivateElement(AEventsReceiver * elem, ui16 activityFlag)
  51. {
  52. processLists(activityFlag,[&](EventReceiversList & lst){
  53. auto hlp = std::find(lst.begin(),lst.end(),elem);
  54. assert(hlp != lst.end());
  55. lst.erase(hlp);
  56. });
  57. elem->activeState &= ~activityFlag;
  58. }
  59. void EventDispatcher::dispatchTimer(uint32_t msPassed)
  60. {
  61. EventReceiversList hlp = timeinterested;
  62. for (auto & elem : hlp)
  63. {
  64. if(!vstd::contains(timeinterested,elem))
  65. continue;
  66. elem->tick(msPassed);
  67. }
  68. }
  69. void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & shortcutsVector)
  70. {
  71. bool keysCaptured = false;
  72. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_LEFT))
  73. dispatchMouseLeftButtonPressed(GH.getCursorPosition(), settings["input"]["shortcutToleranceDistance"].Integer());
  74. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_RIGHT))
  75. dispatchShowPopup(GH.getCursorPosition(), settings["input"]["shortcutToleranceDistance"].Integer());
  76. for(auto & i : keyinterested)
  77. for(EShortcut shortcut : shortcutsVector)
  78. if(i->captureThisKey(shortcut))
  79. keysCaptured = true;
  80. EventReceiversList miCopy = keyinterested;
  81. for(auto & i : miCopy)
  82. {
  83. for(EShortcut shortcut : shortcutsVector)
  84. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  85. {
  86. i->keyPressed(shortcut);
  87. if (keysCaptured)
  88. return;
  89. }
  90. }
  91. }
  92. void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector)
  93. {
  94. bool keysCaptured = false;
  95. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_LEFT))
  96. dispatchMouseLeftButtonReleased(GH.getCursorPosition(), settings["input"]["shortcutToleranceDistance"].Integer());
  97. if (vstd::contains(shortcutsVector, EShortcut::MOUSE_RIGHT))
  98. dispatchClosePopup(GH.getCursorPosition());
  99. for(auto & i : keyinterested)
  100. for(EShortcut shortcut : shortcutsVector)
  101. if(i->captureThisKey(shortcut))
  102. keysCaptured = true;
  103. EventReceiversList miCopy = keyinterested;
  104. for(auto & i : miCopy)
  105. {
  106. for(EShortcut shortcut : shortcutsVector)
  107. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  108. {
  109. i->keyReleased(shortcut);
  110. if (keysCaptured)
  111. return;
  112. }
  113. }
  114. }
  115. void EventDispatcher::dispatchMouseDoubleClick(const Point & position, int tolerance)
  116. {
  117. handleDoubleButtonClick(position, tolerance);
  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 * std::min(1.0, static_cast<double>(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. bool lastActivated = true;
  184. for(auto & i : hlp)
  185. {
  186. if(!vstd::contains(lclickable, i))
  187. continue;
  188. if( i->receiveEvent(position, AEventsReceiver::LCLICK) || i == nearestElement)
  189. {
  190. if(isPressed)
  191. {
  192. i->mouseClickedState = isPressed;
  193. i->clickPressed(position, lastActivated);
  194. }
  195. else
  196. {
  197. if (i->mouseClickedState)
  198. {
  199. i->mouseClickedState = isPressed;
  200. i->clickReleased(position, lastActivated);
  201. }
  202. else
  203. i->mouseClickedState = isPressed;
  204. }
  205. lastActivated = false;
  206. }
  207. else
  208. {
  209. if(i->mouseClickedState && !isPressed)
  210. {
  211. i->mouseClickedState = isPressed;
  212. i->clickCancel(position);
  213. }
  214. else if(isPressed)
  215. {
  216. i->notFocusedClick();
  217. }
  218. }
  219. }
  220. }
  221. void EventDispatcher::handleDoubleButtonClick(const Point & position, int tolerance)
  222. {
  223. // WARNING: this approach is NOT SAFE
  224. // 1) We allow (un)registering elements when list itself is being processed/iterated
  225. // 2) To avoid iterator invalidation we create a copy of this list for processing
  226. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  227. // 1) element gets unregistered and deleted from lclickable
  228. // 2) element is completely deleted, as in - destructor called, memory freed
  229. // 3) new element is created *with exactly same address(!)
  230. // 4) new element is registered and code will incorrectly assume that this element is still registered
  231. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  232. AEventsReceiver * nearestElement = findElementInToleranceRange(doubleClickInterested, position, AEventsReceiver::DOUBLECLICK, tolerance);
  233. bool doubleClicked = false;
  234. auto hlp = doubleClickInterested;
  235. for(auto & i : hlp)
  236. {
  237. if(!vstd::contains(doubleClickInterested, i))
  238. continue;
  239. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK) || i == nearestElement)
  240. {
  241. i->clickDouble(position);
  242. doubleClicked = true;
  243. }
  244. }
  245. if(!doubleClicked)
  246. handleLeftButtonClick(position, tolerance, true);
  247. }
  248. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  249. {
  250. EventReceiversList hlp = wheelInterested;
  251. for(auto & i : hlp)
  252. {
  253. if(!vstd::contains(wheelInterested,i))
  254. continue;
  255. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  256. i->wheelScrolled(distance.y);
  257. }
  258. }
  259. void EventDispatcher::dispatchTextInput(const std::string & text)
  260. {
  261. for(auto it : textInterested)
  262. {
  263. it->textInputted(text);
  264. }
  265. }
  266. void EventDispatcher::dispatchTextEditing(const std::string & text)
  267. {
  268. for(auto it : textInterested)
  269. {
  270. it->textEdited(text);
  271. }
  272. }
  273. void EventDispatcher::dispatchInputModeChanged(const InputMode & modi)
  274. {
  275. for(auto it : inputModeChangeInterested)
  276. {
  277. it->inputModeChanged(modi);
  278. }
  279. }
  280. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  281. {
  282. auto copied = panningInterested;
  283. for(auto it : copied)
  284. {
  285. if (!vstd::contains(panningInterested, it))
  286. continue;
  287. if (!it->isGesturing() && it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  288. {
  289. it->panningState = true;
  290. it->gesture(true, initialPosition, initialPosition);
  291. }
  292. }
  293. }
  294. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  295. {
  296. dispatchGesturePanningStarted(initialPosition);
  297. auto copied = panningInterested;
  298. for(auto it : copied)
  299. {
  300. if (it->isGesturing())
  301. {
  302. it->panningState = false;
  303. it->gesture(false, initialPosition, finalPosition);
  304. }
  305. }
  306. }
  307. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  308. {
  309. dispatchGesturePanningStarted(initialPosition);
  310. auto copied = panningInterested;
  311. for(auto it : copied)
  312. {
  313. if (!vstd::contains(panningInterested, it))
  314. continue;
  315. if (it->isGesturing())
  316. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  317. }
  318. }
  319. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  320. {
  321. for(auto it : panningInterested)
  322. {
  323. if (it->isGesturing())
  324. it->gesturePinch(initialPosition, distance);
  325. }
  326. }
  327. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  328. {
  329. EventReceiversList newlyHovered;
  330. auto hoverableCopy = hoverable;
  331. for(auto & elem : hoverableCopy)
  332. {
  333. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  334. {
  335. if (!elem->isHovered())
  336. {
  337. newlyHovered.push_back((elem));
  338. }
  339. }
  340. else
  341. {
  342. if (elem->isHovered())
  343. {
  344. elem->hoveredState = false;
  345. elem->hover(false);
  346. }
  347. }
  348. }
  349. for(auto & elem : newlyHovered)
  350. {
  351. elem->hoveredState = true;
  352. elem->hover(true);
  353. }
  354. //sending active, MotionInterested objects mouseMoved() call
  355. EventReceiversList miCopy = motioninterested;
  356. for(auto & elem : miCopy)
  357. {
  358. if (!vstd::contains(motioninterested, elem))
  359. continue;
  360. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  361. elem->mouseMoved(position, distance);
  362. }
  363. }
  364. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  365. {
  366. EventReceiversList diCopy = draginterested;
  367. for(auto & elem : diCopy)
  368. {
  369. if (elem->mouseClickedState)
  370. elem->mouseDragged(currentPosition, lastUpdateDistance);
  371. }
  372. }
  373. void EventDispatcher::dispatchMouseDraggedPopup(const Point & currentPosition, const Point & lastUpdateDistance)
  374. {
  375. EventReceiversList diCopy = dragPopupInterested;
  376. for(auto & elem : diCopy)
  377. elem->mouseDraggedPopup(currentPosition, lastUpdateDistance);
  378. }