EventDispatcher.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/Point.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)
  103. {
  104. bool doubleClicked = false;
  105. auto hlp = doubleClickInterested;
  106. for(auto & i : hlp)
  107. {
  108. if(!vstd::contains(doubleClickInterested, i))
  109. continue;
  110. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK))
  111. {
  112. i->clickDouble(position);
  113. doubleClicked = true;
  114. }
  115. }
  116. if(!doubleClicked)
  117. handleLeftButtonClick(position, true);
  118. }
  119. void EventDispatcher::dispatchMouseLeftButtonPressed(const Point & position)
  120. {
  121. handleLeftButtonClick(position, true);
  122. }
  123. void EventDispatcher::dispatchMouseLeftButtonReleased(const Point & position)
  124. {
  125. handleLeftButtonClick(position, false);
  126. }
  127. void EventDispatcher::dispatchShowPopup(const Point & position)
  128. {
  129. auto hlp = rclickable;
  130. for(auto & i : hlp)
  131. {
  132. if(!vstd::contains(rclickable, i))
  133. continue;
  134. if( !i->receiveEvent(position, AEventsReceiver::LCLICK))
  135. continue;
  136. i->showPopupWindow(position);
  137. }
  138. }
  139. void EventDispatcher::dispatchClosePopup(const Point & position)
  140. {
  141. if (GH.windows().isTopWindowPopup())
  142. GH.windows().popWindows(1);
  143. assert(!GH.windows().isTopWindowPopup());
  144. }
  145. void EventDispatcher::handleLeftButtonClick(const Point & position, bool isPressed)
  146. {
  147. // WARNING: this approach is NOT SAFE
  148. // 1) We allow (un)registering elements when list itself is being processed/iterated
  149. // 2) To avoid iterator invalidation we create a copy of this list for processing
  150. // HOWEVER it is completely possible (as in, actually happen, no just theory) to:
  151. // 1) element gets unregistered and deleted from lclickable
  152. // 2) element is completely deleted, as in - destructor called, memory freed
  153. // 3) new element is created *with exactly same address(!)
  154. // 4) new element is registered and code will incorrectly assume that this element is still registered
  155. // POSSIBLE SOLUTION: make EventReceivers inherit from create_shared_from this and store weak_ptr's in lists
  156. auto hlp = lclickable;
  157. for(auto & i : hlp)
  158. {
  159. if(!vstd::contains(lclickable, i))
  160. continue;
  161. if( i->receiveEvent(position, AEventsReceiver::LCLICK))
  162. {
  163. if(isPressed)
  164. i->clickPressed(position);
  165. if (i->mouseClickedState && !isPressed)
  166. i->clickReleased(position);
  167. i->mouseClickedState = isPressed;
  168. }
  169. else
  170. {
  171. if(i->mouseClickedState && !isPressed)
  172. {
  173. i->mouseClickedState = isPressed;
  174. i->clickCancel(position);
  175. }
  176. }
  177. }
  178. }
  179. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  180. {
  181. EventReceiversList hlp = wheelInterested;
  182. for(auto & i : hlp)
  183. {
  184. if(!vstd::contains(wheelInterested,i))
  185. continue;
  186. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  187. i->wheelScrolled(distance.y);
  188. }
  189. }
  190. void EventDispatcher::dispatchTextInput(const std::string & text)
  191. {
  192. for(auto it : textInterested)
  193. {
  194. it->textInputed(text);
  195. }
  196. }
  197. void EventDispatcher::dispatchTextEditing(const std::string & text)
  198. {
  199. for(auto it : textInterested)
  200. {
  201. it->textEdited(text);
  202. }
  203. }
  204. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  205. {
  206. auto copied = panningInterested;
  207. for(auto it : copied)
  208. {
  209. if (it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  210. {
  211. it->gesture(true, initialPosition, initialPosition);
  212. it->panningState = true;
  213. }
  214. }
  215. }
  216. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  217. {
  218. auto copied = panningInterested;
  219. for(auto it : copied)
  220. {
  221. if (it->isGesturing())
  222. {
  223. it->gesture(false, initialPosition, finalPosition);
  224. it->panningState = false;
  225. }
  226. }
  227. }
  228. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  229. {
  230. auto copied = panningInterested;
  231. for(auto it : copied)
  232. {
  233. if (it->isGesturing())
  234. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  235. }
  236. }
  237. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  238. {
  239. for(auto it : panningInterested)
  240. {
  241. if (it->isGesturing())
  242. it->gesturePinch(initialPosition, distance);
  243. }
  244. }
  245. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  246. {
  247. EventReceiversList newlyHovered;
  248. auto hoverableCopy = hoverable;
  249. for(auto & elem : hoverableCopy)
  250. {
  251. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  252. {
  253. if (!elem->isHovered())
  254. {
  255. newlyHovered.push_back((elem));
  256. }
  257. }
  258. else
  259. {
  260. if (elem->isHovered())
  261. {
  262. elem->hover(false);
  263. elem->hoveredState = false;
  264. }
  265. }
  266. }
  267. for(auto & elem : newlyHovered)
  268. {
  269. elem->hover(true);
  270. elem->hoveredState = true;
  271. }
  272. //sending active, MotionInterested objects mouseMoved() call
  273. EventReceiversList miCopy = motioninterested;
  274. for(auto & elem : miCopy)
  275. {
  276. if (!vstd::contains(motioninterested, elem))
  277. continue;
  278. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  279. elem->mouseMoved(position, distance);
  280. }
  281. }
  282. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  283. {
  284. EventReceiversList diCopy = draginterested;
  285. for(auto & elem : diCopy)
  286. {
  287. if (elem->mouseClickedState)
  288. elem->mouseDragged(currentPosition, lastUpdateDistance);
  289. }
  290. }