EventDispatcher.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // ignore distance value and only provide its sign - we expect one scroll "event" to move sliders and such by 1 point,
  174. // and not by system-specific "number of lines to scroll", which is what 'distance' represents
  175. if (i->receiveEvent(position, AEventsReceiver::WHEEL))
  176. i->wheelScrolled( std::clamp(distance.y, -1, 1));
  177. }
  178. }
  179. void EventDispatcher::dispatchTextInput(const std::string & text)
  180. {
  181. for(auto it : textInterested)
  182. {
  183. it->textInputed(text);
  184. }
  185. }
  186. void EventDispatcher::dispatchTextEditing(const std::string & text)
  187. {
  188. for(auto it : textInterested)
  189. {
  190. it->textEdited(text);
  191. }
  192. }
  193. void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
  194. {
  195. for(auto it : panningInterested)
  196. {
  197. if (it->receiveEvent(initialPosition, AEventsReceiver::GESTURE_PANNING))
  198. {
  199. assert(it->panningState == false);
  200. it->panning(true);
  201. it->panningState = true;
  202. }
  203. }
  204. }
  205. void EventDispatcher::dispatchGesturePanningEnded()
  206. {
  207. for(auto it : panningInterested)
  208. {
  209. if (it->isPanning())
  210. {
  211. it->panning(false);
  212. it->panningState = false;
  213. }
  214. }
  215. }
  216. void EventDispatcher::dispatchGesturePanning(const Point & distance)
  217. {
  218. auto copied = panningInterested;
  219. for(auto it : copied)
  220. {
  221. if (it->isPanning())
  222. it->gesturePanning(distance);
  223. }
  224. }
  225. void EventDispatcher::dispatchMouseMoved(const Point & position)
  226. {
  227. EventReceiversList newlyHovered;
  228. auto hoverableCopy = hoverable;
  229. for(auto & elem : hoverableCopy)
  230. {
  231. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  232. {
  233. if (!elem->isHovered())
  234. {
  235. newlyHovered.push_back((elem));
  236. }
  237. }
  238. else
  239. {
  240. if (elem->isHovered())
  241. {
  242. (elem)->hover(false);
  243. (elem)->hoveredState = false;
  244. }
  245. }
  246. }
  247. for(auto & elem : newlyHovered)
  248. {
  249. elem->hover(true);
  250. elem->hoveredState = true;
  251. }
  252. //sending active, MotionInterested objects mouseMoved() call
  253. EventReceiversList miCopy = motioninterested;
  254. for(auto & elem : miCopy)
  255. {
  256. if(elem->receiveEvent(position, AEventsReceiver::HOVER))
  257. {
  258. (elem)->mouseMoved(position);
  259. }
  260. }
  261. }