EventDispatcher.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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::MCLICK, mclickable);
  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. }
  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. case MouseButton::MIDDLE:
  108. return mclickable;
  109. }
  110. throw std::runtime_error("Invalid mouse button in getListForMouseButton");
  111. }
  112. void EventDispatcher::dispatchMouseDoubleClick(const Point & position)
  113. {
  114. bool doubleClicked = false;
  115. auto hlp = doubleClickInterested;
  116. for(auto & i : hlp)
  117. {
  118. if(!vstd::contains(doubleClickInterested, i))
  119. continue;
  120. if(i->isInside(position))
  121. {
  122. i->onDoubleClick();
  123. doubleClicked = true;
  124. }
  125. }
  126. if(!doubleClicked)
  127. dispatchMouseButtonPressed(MouseButton::LEFT, position);
  128. }
  129. void EventDispatcher::dispatchMouseButtonPressed(const MouseButton & button, const Point & position)
  130. {
  131. handleMouseButtonClick(getListForMouseButton(button), button, true);
  132. }
  133. void EventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, const Point & position)
  134. {
  135. handleMouseButtonClick(getListForMouseButton(button), button, false);
  136. }
  137. void EventDispatcher::handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed)
  138. {
  139. auto hlp = interestedObjs;
  140. for(auto & i : hlp)
  141. {
  142. if(!vstd::contains(interestedObjs, i))
  143. continue;
  144. auto prev = i->isMouseButtonPressed(btn);
  145. if(!isPressed)
  146. i->currentMouseState[btn] = isPressed;
  147. if(i->isInside(GH.getCursorPosition()))
  148. {
  149. if(isPressed)
  150. i->currentMouseState[btn] = isPressed;
  151. i->click(btn, isPressed, prev);
  152. }
  153. else if(!isPressed)
  154. i->click(btn, boost::logic::indeterminate, prev);
  155. }
  156. }
  157. void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  158. {
  159. EventReceiversList hlp = wheelInterested;
  160. for(auto & i : hlp)
  161. {
  162. if(!vstd::contains(wheelInterested,i))
  163. continue;
  164. i->wheelScrolled(distance.y < 0, i->isInside(position));
  165. }
  166. }
  167. void EventDispatcher::dispatchTextInput(const std::string & text)
  168. {
  169. for(auto it : textInterested)
  170. {
  171. it->textInputed(text);
  172. }
  173. }
  174. void EventDispatcher::dispatchTextEditing(const std::string & text)
  175. {
  176. for(auto it : textInterested)
  177. {
  178. it->textEdited(text);
  179. }
  180. }
  181. void EventDispatcher::dispatchMouseMoved(const Point & position)
  182. {
  183. EventReceiversList newlyHovered;
  184. auto hoverableCopy = hoverable;
  185. for(auto & elem : hoverableCopy)
  186. {
  187. if(elem->isInside(position))
  188. {
  189. if (!elem->isHovered())
  190. {
  191. newlyHovered.push_back((elem));
  192. }
  193. }
  194. else
  195. {
  196. if (elem->isHovered())
  197. {
  198. (elem)->hover(false);
  199. (elem)->hoveredState = false;
  200. }
  201. }
  202. }
  203. for(auto & elem : newlyHovered)
  204. {
  205. elem->hover(true);
  206. elem->hoveredState = true;
  207. }
  208. //sending active, MotionInterested objects mouseMoved() call
  209. EventReceiversList miCopy = motioninterested;
  210. for(auto & elem : miCopy)
  211. {
  212. if(elem->strongInterestState || elem->isInside(position)) //checking bounds including border fixes bug #2476
  213. {
  214. (elem)->mouseMoved(position);
  215. }
  216. }
  217. }