EventDispatcher.cpp 6.9 KB

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