EventDispatcher.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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)) continue;
  60. elem->tick(msPassed);
  61. }
  62. }
  63. void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & shortcutsVector)
  64. {
  65. bool keysCaptured = false;
  66. for(auto & i : keyinterested)
  67. for(EShortcut shortcut : shortcutsVector)
  68. if(i->captureThisKey(shortcut))
  69. keysCaptured = true;
  70. EventReceiversList miCopy = keyinterested;
  71. for(auto & i : miCopy)
  72. {
  73. for(EShortcut shortcut : shortcutsVector)
  74. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  75. {
  76. i->keyPressed(shortcut);
  77. if (keysCaptured)
  78. return;
  79. }
  80. }
  81. }
  82. void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector)
  83. {
  84. bool keysCaptured = false;
  85. for(auto & i : keyinterested)
  86. for(EShortcut shortcut : shortcutsVector)
  87. if(i->captureThisKey(shortcut))
  88. keysCaptured = true;
  89. EventReceiversList miCopy = keyinterested;
  90. for(auto & i : miCopy)
  91. {
  92. for(EShortcut shortcut : shortcutsVector)
  93. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  94. {
  95. i->keyReleased(shortcut);
  96. if (keysCaptured)
  97. return;
  98. }
  99. }
  100. }
  101. void EventDispatcher::dispatchMouseDoubleClick(const Point & position)
  102. {
  103. bool doubleClicked = false;
  104. auto hlp = doubleClickInterested;
  105. for(auto & i : hlp)
  106. {
  107. if(!vstd::contains(doubleClickInterested, i))
  108. continue;
  109. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK))
  110. {
  111. i->clickDouble();
  112. doubleClicked = true;
  113. }
  114. }
  115. if(!doubleClicked)
  116. handleLeftButtonClick(true);
  117. }
  118. void EventDispatcher::dispatchMouseLeftButtonPressed(const Point & position)
  119. {
  120. handleLeftButtonClick(true);
  121. }
  122. void EventDispatcher::dispatchMouseLeftButtonReleased(const Point & position)
  123. {
  124. handleLeftButtonClick(false);
  125. }
  126. void EventDispatcher::dispatchShowPopup(const Point & position)
  127. {
  128. auto hlp = rclickable;
  129. for(auto & i : hlp)
  130. {
  131. if(!vstd::contains(rclickable, i))
  132. continue;
  133. if( !i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::LCLICK))
  134. continue;
  135. i->showPopupWindow();
  136. }
  137. }
  138. void EventDispatcher::dispatchClosePopup(const Point & position)
  139. {
  140. if (GH.windows().isTopWindowPopup())
  141. GH.windows().popWindows(1);
  142. assert(!GH.windows().isTopWindowPopup());
  143. }
  144. void EventDispatcher::handleLeftButtonClick(bool isPressed)
  145. {
  146. auto hlp = lclickable;
  147. for(auto & i : hlp)
  148. {
  149. if(!vstd::contains(lclickable, i))
  150. continue;
  151. auto prev = i->isMouseLeftButtonPressed();
  152. if(!isPressed)
  153. i->mouseClickedState = isPressed;
  154. if( i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::LCLICK))
  155. {
  156. if(isPressed)
  157. i->mouseClickedState = isPressed;
  158. i->clickLeft(isPressed, prev);
  159. }
  160. else if(!isPressed)
  161. {
  162. i->clickLeft(boost::logic::indeterminate, prev);
  163. }
  164. }
  165. }
  166. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  167. {
  168. EventReceiversList hlp = wheelInterested;
  169. for(auto & i : hlp)
  170. {
  171. if(!vstd::contains(wheelInterested,i))
  172. continue;
  173. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  174. i->wheelScrolled(distance.y);
  175. }
  176. }
  177. void EventDispatcher::dispatchTextInput(const std::string & text)
  178. {
  179. for(auto it : textInterested)
  180. {
  181. it->textInputed(text);
  182. }
  183. }
  184. void EventDispatcher::dispatchTextEditing(const std::string & text)
  185. {
  186. for(auto it : textInterested)
  187. {
  188. it->textEdited(text);
  189. }
  190. }
  191. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  192. {
  193. auto copied = panningInterested;
  194. for(auto it : copied)
  195. {
  196. if (it->receiveEvent(initialPosition, AEventsReceiver::GESTURE))
  197. {
  198. it->gesture(true, initialPosition, initialPosition);
  199. it->panningState = true;
  200. }
  201. }
  202. }
  203. void EventDispatcher::dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition)
  204. {
  205. auto copied = panningInterested;
  206. for(auto it : copied)
  207. {
  208. if (it->isGesturing())
  209. {
  210. it->gesture(false, initialPosition, finalPosition);
  211. it->panningState = false;
  212. }
  213. }
  214. }
  215. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  216. {
  217. auto copied = panningInterested;
  218. for(auto it : copied)
  219. {
  220. if (it->isGesturing())
  221. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  222. }
  223. }
  224. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  225. {
  226. for(auto it : panningInterested)
  227. {
  228. if (it->isGesturing())
  229. it->gesturePinch(initialPosition, distance);
  230. }
  231. }
  232. void EventDispatcher::dispatchMouseMoved(const Point & distance, const Point & position)
  233. {
  234. EventReceiversList newlyHovered;
  235. auto hoverableCopy = hoverable;
  236. for(auto & elem : hoverableCopy)
  237. {
  238. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  239. {
  240. if (!elem->isHovered())
  241. {
  242. newlyHovered.push_back((elem));
  243. }
  244. }
  245. else
  246. {
  247. if (elem->isHovered())
  248. {
  249. elem->hover(false);
  250. elem->hoveredState = false;
  251. }
  252. }
  253. }
  254. for(auto & elem : newlyHovered)
  255. {
  256. elem->hover(true);
  257. elem->hoveredState = true;
  258. }
  259. //sending active, MotionInterested objects mouseMoved() call
  260. EventReceiversList miCopy = motioninterested;
  261. for(auto & elem : miCopy)
  262. {
  263. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  264. elem->mouseMoved(position, distance);
  265. }
  266. }
  267. void EventDispatcher::dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance)
  268. {
  269. EventReceiversList diCopy = draginterested;
  270. for(auto & elem : diCopy)
  271. {
  272. if (elem->mouseClickedState)
  273. elem->mouseDragged(currentPosition, lastUpdateDistance);
  274. }
  275. }