EventDispatcher.cpp 7.3 KB

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