EventDispatcher.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. auto hlp = lclickable;
  148. for(auto & i : hlp)
  149. {
  150. if(!vstd::contains(lclickable, i))
  151. continue;
  152. if( i->receiveEvent(position, AEventsReceiver::LCLICK))
  153. {
  154. if(isPressed)
  155. i->clickPressed(position);
  156. if (i->mouseClickedState && !isPressed)
  157. i->clickReleased(position);
  158. i->mouseClickedState = isPressed;
  159. }
  160. else
  161. {
  162. if(i->mouseClickedState && !isPressed)
  163. {
  164. i->mouseClickedState = isPressed;
  165. i->clickCancel(position);
  166. }
  167. }
  168. }
  169. }
  170. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  171. {
  172. EventReceiversList hlp = wheelInterested;
  173. for(auto & i : hlp)
  174. {
  175. if(!vstd::contains(wheelInterested,i))
  176. continue;
  177. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  178. i->wheelScrolled(distance.y);
  179. }
  180. }
  181. void EventDispatcher::dispatchTextInput(const std::string & text)
  182. {
  183. for(auto it : textInterested)
  184. {
  185. it->textInputed(text);
  186. }
  187. }
  188. void EventDispatcher::dispatchTextEditing(const std::string & text)
  189. {
  190. for(auto it : textInterested)
  191. {
  192. it->textEdited(text);
  193. }
  194. }
  195. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  196. {
  197. auto copied = panningInterested;
  198. for(auto it : copied)
  199. {
  200. if (it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  201. {
  202. it->gesture(true, initialPosition, initialPosition);
  203. it->panningState = true;
  204. }
  205. }
  206. }
  207. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  208. {
  209. auto copied = panningInterested;
  210. for(auto it : copied)
  211. {
  212. if (it->isGesturing())
  213. {
  214. it->gesture(false, initialPosition, finalPosition);
  215. it->panningState = false;
  216. }
  217. }
  218. }
  219. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  220. {
  221. auto copied = panningInterested;
  222. for(auto it : copied)
  223. {
  224. if (it->isGesturing())
  225. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  226. }
  227. }
  228. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  229. {
  230. for(auto it : panningInterested)
  231. {
  232. if (it->isGesturing())
  233. it->gesturePinch(initialPosition, distance);
  234. }
  235. }
  236. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  237. {
  238. EventReceiversList newlyHovered;
  239. auto hoverableCopy = hoverable;
  240. for(auto & elem : hoverableCopy)
  241. {
  242. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  243. {
  244. if (!elem->isHovered())
  245. {
  246. newlyHovered.push_back((elem));
  247. }
  248. }
  249. else
  250. {
  251. if (elem->isHovered())
  252. {
  253. elem->hover(false);
  254. elem->hoveredState = false;
  255. }
  256. }
  257. }
  258. for(auto & elem : newlyHovered)
  259. {
  260. elem->hover(true);
  261. elem->hoveredState = true;
  262. }
  263. //sending active, MotionInterested objects mouseMoved() call
  264. EventReceiversList miCopy = motioninterested;
  265. for(auto & elem : miCopy)
  266. {
  267. if (!vstd::contains(motioninterested, elem))
  268. continue;
  269. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  270. elem->mouseMoved(position, distance);
  271. }
  272. }
  273. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  274. {
  275. EventReceiversList diCopy = draginterested;
  276. for(auto & elem : diCopy)
  277. {
  278. if (elem->mouseClickedState)
  279. elem->mouseDragged(currentPosition, lastUpdateDistance);
  280. }
  281. }