EventDispatcher.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "../../lib/Point.h"
  17. template<typename Functor>
  18. void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
  19. {
  20. auto processList = [&](ui16 mask, EventReceiversList & lst)
  21. {
  22. if(mask & activityFlag)
  23. cb(lst);
  24. };
  25. processList(AEventsReceiver::LCLICK, lclickable);
  26. processList(AEventsReceiver::RCLICK, rclickable);
  27. processList(AEventsReceiver::HOVER, hoverable);
  28. processList(AEventsReceiver::MOVE, motioninterested);
  29. processList(AEventsReceiver::KEYBOARD, keyinterested);
  30. processList(AEventsReceiver::TIME, timeinterested);
  31. processList(AEventsReceiver::WHEEL, wheelInterested);
  32. processList(AEventsReceiver::DOUBLECLICK, doubleClickInterested);
  33. processList(AEventsReceiver::TEXTINPUT, textInterested);
  34. processList(AEventsReceiver::GESTURE_PANNING, panningInterested);
  35. }
  36. void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag)
  37. {
  38. processLists(activityFlag,[&](EventReceiversList & lst){
  39. lst.push_front(elem);
  40. });
  41. elem->activeState |= activityFlag;
  42. }
  43. void EventDispatcher::deactivateElement(AEventsReceiver * elem, ui16 activityFlag)
  44. {
  45. processLists(activityFlag,[&](EventReceiversList & lst){
  46. auto hlp = std::find(lst.begin(),lst.end(),elem);
  47. assert(hlp != lst.end());
  48. lst.erase(hlp);
  49. });
  50. elem->activeState &= ~activityFlag;
  51. }
  52. void EventDispatcher::dispatchTimer(uint32_t msPassed)
  53. {
  54. EventReceiversList hlp = timeinterested;
  55. for (auto & elem : hlp)
  56. {
  57. if(!vstd::contains(timeinterested,elem)) continue;
  58. (elem)->tick(msPassed);
  59. }
  60. }
  61. void EventDispatcher::dispatchShortcutPressed(const std::vector<EShortcut> & shortcutsVector)
  62. {
  63. bool keysCaptured = false;
  64. for(auto & i : keyinterested)
  65. for(EShortcut shortcut : shortcutsVector)
  66. if(i->captureThisKey(shortcut))
  67. keysCaptured = true;
  68. EventReceiversList miCopy = keyinterested;
  69. for(auto & i : miCopy)
  70. {
  71. for(EShortcut shortcut : shortcutsVector)
  72. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  73. {
  74. i->keyPressed(shortcut);
  75. if (keysCaptured)
  76. return;
  77. }
  78. }
  79. }
  80. void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector)
  81. {
  82. bool keysCaptured = false;
  83. for(auto & i : keyinterested)
  84. for(EShortcut shortcut : shortcutsVector)
  85. if(i->captureThisKey(shortcut))
  86. keysCaptured = true;
  87. EventReceiversList miCopy = keyinterested;
  88. for(auto & i : miCopy)
  89. {
  90. for(EShortcut shortcut : shortcutsVector)
  91. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  92. {
  93. i->keyReleased(shortcut);
  94. if (keysCaptured)
  95. return;
  96. }
  97. }
  98. }
  99. EventDispatcher::EventReceiversList & EventDispatcher::getListForMouseButton(MouseButton button)
  100. {
  101. switch (button)
  102. {
  103. case MouseButton::LEFT:
  104. return lclickable;
  105. case MouseButton::RIGHT:
  106. return rclickable;
  107. }
  108. throw std::runtime_error("Invalid mouse button in getListForMouseButton");
  109. }
  110. void EventDispatcher::dispatchMouseDoubleClick(const Point & position)
  111. {
  112. bool doubleClicked = false;
  113. auto hlp = doubleClickInterested;
  114. for(auto & i : hlp)
  115. {
  116. if(!vstd::contains(doubleClickInterested, i))
  117. continue;
  118. if(i->receiveEvent(position, AEventsReceiver::DOUBLECLICK))
  119. {
  120. i->clickDouble();
  121. doubleClicked = true;
  122. }
  123. }
  124. if(!doubleClicked)
  125. dispatchMouseButtonPressed(MouseButton::LEFT, position);
  126. }
  127. void EventDispatcher::dispatchMouseButtonPressed(const MouseButton & button, const Point & position)
  128. {
  129. handleMouseButtonClick(getListForMouseButton(button), button, true);
  130. }
  131. void EventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, const Point & position)
  132. {
  133. handleMouseButtonClick(getListForMouseButton(button), button, false);
  134. }
  135. void EventDispatcher::handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed)
  136. {
  137. auto hlp = interestedObjs;
  138. for(auto & i : hlp)
  139. {
  140. if(!vstd::contains(interestedObjs, i))
  141. continue;
  142. auto prev = i->isMouseButtonPressed(btn);
  143. if(!isPressed)
  144. i->currentMouseState[btn] = isPressed;
  145. if( btn == MouseButton::LEFT && i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::LCLICK))
  146. {
  147. if(isPressed)
  148. i->currentMouseState[btn] = isPressed;
  149. i->clickLeft(isPressed, prev);
  150. }
  151. else if( btn == MouseButton::RIGHT && i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::RCLICK))
  152. {
  153. if(isPressed)
  154. i->currentMouseState[btn] = isPressed;
  155. i->clickRight(isPressed, prev);
  156. }
  157. else if(!isPressed)
  158. {
  159. if (btn == MouseButton::LEFT)
  160. i->clickLeft(boost::logic::indeterminate, prev);
  161. if (btn == MouseButton::RIGHT)
  162. i->clickRight(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. for(auto it : panningInterested)
  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. for(auto it : panningInterested)
  205. {
  206. if (it->isPanning())
  207. {
  208. it->panning(false, initialPosition, finalPosition);
  209. it->panningState = false;
  210. }
  211. }
  212. }
  213. void EventDispatcher::dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  214. {
  215. auto copied = panningInterested;
  216. for(auto it : copied)
  217. {
  218. if (it->isPanning())
  219. it->gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  220. }
  221. }
  222. void EventDispatcher::dispatchGesturePinch(const Point & initialPosition, double distance)
  223. {
  224. for(auto it : panningInterested)
  225. {
  226. if (it->isPanning())
  227. it->gesturePinch(initialPosition, distance);
  228. }
  229. }
  230. void EventDispatcher::dispatchMouseMoved(const Point & position)
  231. {
  232. EventReceiversList newlyHovered;
  233. auto hoverableCopy = hoverable;
  234. for(auto & elem : hoverableCopy)
  235. {
  236. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  237. {
  238. if (!elem->isHovered())
  239. {
  240. newlyHovered.push_back((elem));
  241. }
  242. }
  243. else
  244. {
  245. if (elem->isHovered())
  246. {
  247. (elem)->hover(false);
  248. (elem)->hoveredState = false;
  249. }
  250. }
  251. }
  252. for(auto & elem : newlyHovered)
  253. {
  254. elem->hover(true);
  255. elem->hoveredState = true;
  256. }
  257. //sending active, MotionInterested objects mouseMoved() call
  258. EventReceiversList miCopy = motioninterested;
  259. for(auto & elem : miCopy)
  260. {
  261. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  262. {
  263. (elem)->mouseMoved(position);
  264. }
  265. }
  266. }