EventDispatcher.cpp 12 KB

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