InterfaceEventDispatcher.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * CGuiHandler.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 "InterfaceEventDispatcher.h"
  12. #include "CIntObject.h"
  13. #include "CGuiHandler.h"
  14. #include "FramerateManager.h"
  15. void InterfaceEventDispatcher::allowEventHandling(bool enable)
  16. {
  17. eventHandlingAllowed = enable;
  18. }
  19. void InterfaceEventDispatcher::processList(const ui16 mask, const ui16 flag, CIntObjectList *lst, std::function<void (CIntObjectList *)> cb)
  20. {
  21. if (mask & flag)
  22. cb(lst);
  23. }
  24. void InterfaceEventDispatcher::processLists(ui16 activityFlag, std::function<void (CIntObjectList *)> cb)
  25. {
  26. processList(AEventsReceiver::LCLICK,activityFlag,&lclickable,cb);
  27. processList(AEventsReceiver::RCLICK,activityFlag,&rclickable,cb);
  28. processList(AEventsReceiver::MCLICK,activityFlag,&mclickable,cb);
  29. processList(AEventsReceiver::HOVER,activityFlag,&hoverable,cb);
  30. processList(AEventsReceiver::MOVE,activityFlag,&motioninterested,cb);
  31. processList(AEventsReceiver::KEYBOARD,activityFlag,&keyinterested,cb);
  32. processList(AEventsReceiver::TIME,activityFlag,&timeinterested,cb);
  33. processList(AEventsReceiver::WHEEL,activityFlag,&wheelInterested,cb);
  34. processList(AEventsReceiver::DOUBLECLICK,activityFlag,&doubleClickInterested,cb);
  35. processList(AEventsReceiver::TEXTINPUT,activityFlag,&textInterested,cb);
  36. }
  37. void InterfaceEventDispatcher::handleElementActivate(AEventsReceiver * elem, ui16 activityFlag)
  38. {
  39. processLists(activityFlag,[&](CIntObjectList * lst){
  40. lst->push_front(elem);
  41. });
  42. elem->activeState |= activityFlag;
  43. }
  44. void InterfaceEventDispatcher::handleElementDeActivate(AEventsReceiver * elem, ui16 activityFlag)
  45. {
  46. processLists(activityFlag,[&](CIntObjectList * 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 InterfaceEventDispatcher::dispatchTimer(uint32_t msPassed)
  54. {
  55. CIntObjectList hlp = timeinterested;
  56. for (auto & elem : hlp)
  57. {
  58. if(!vstd::contains(timeinterested,elem)) continue;
  59. (elem)->tick(msPassed);
  60. }
  61. }
  62. void InterfaceEventDispatcher::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. CIntObjectList miCopy = keyinterested;
  70. for(auto & i : miCopy)
  71. {
  72. if (!eventHandlingAllowed)
  73. break;
  74. for(EShortcut shortcut : shortcutsVector)
  75. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  76. i->keyPressed(shortcut);
  77. }
  78. }
  79. void InterfaceEventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & shortcutsVector)
  80. {
  81. bool keysCaptured = false;
  82. for(auto & i : keyinterested)
  83. for(EShortcut shortcut : shortcutsVector)
  84. if(i->captureThisKey(shortcut))
  85. keysCaptured = true;
  86. CIntObjectList miCopy = keyinterested;
  87. for(auto & i : miCopy)
  88. {
  89. if (!eventHandlingAllowed)
  90. break;
  91. for(EShortcut shortcut : shortcutsVector)
  92. if(vstd::contains(keyinterested, i) && (!keysCaptured || i->captureThisKey(shortcut)))
  93. i->keyReleased(shortcut);
  94. }
  95. }
  96. InterfaceEventDispatcher::CIntObjectList & InterfaceEventDispatcher::getListForMouseButton(MouseButton button)
  97. {
  98. switch (button)
  99. {
  100. case MouseButton::LEFT:
  101. return lclickable;
  102. case MouseButton::RIGHT:
  103. return rclickable;
  104. case MouseButton::MIDDLE:
  105. return mclickable;
  106. }
  107. throw std::runtime_error("Invalid mouse button in getListForMouseButton");
  108. }
  109. void InterfaceEventDispatcher::dispatchMouseDoubleClick(const Point & position)
  110. {
  111. bool doubleClicked = false;
  112. auto hlp = doubleClickInterested;
  113. for(auto & i : hlp)
  114. {
  115. if(!vstd::contains(doubleClickInterested, i))
  116. continue;
  117. if (!eventHandlingAllowed)
  118. break;
  119. if(i->isInside(position))
  120. {
  121. i->onDoubleClick();
  122. doubleClicked = true;
  123. }
  124. }
  125. if(!doubleClicked)
  126. dispatchMouseButtonPressed(MouseButton::LEFT, position);
  127. }
  128. void InterfaceEventDispatcher::dispatchMouseButtonPressed(const MouseButton & button, const Point & position)
  129. {
  130. handleMouseButtonClick(getListForMouseButton(button), button, true);
  131. }
  132. void InterfaceEventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, const Point & position)
  133. {
  134. handleMouseButtonClick(getListForMouseButton(button), button, false);
  135. }
  136. void InterfaceEventDispatcher::handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed)
  137. {
  138. auto hlp = interestedObjs;
  139. for(auto & i : hlp)
  140. {
  141. if(!vstd::contains(interestedObjs, i))
  142. continue;
  143. if (!eventHandlingAllowed)
  144. break;
  145. auto prev = i->isMouseButtonPressed(btn);
  146. if(!isPressed)
  147. i->currentMouseState[btn] = isPressed;
  148. if(i->isInside(GH.getCursorPosition()))
  149. {
  150. if(isPressed)
  151. i->currentMouseState[btn] = isPressed;
  152. i->click(btn, isPressed, prev);
  153. }
  154. else if(!isPressed)
  155. i->click(btn, boost::logic::indeterminate, prev);
  156. }
  157. }
  158. void InterfaceEventDispatcher::dispatchMouseScrolled(const Point & distance, const Point & position)
  159. {
  160. CIntObjectList hlp = wheelInterested;
  161. for(auto i = hlp.begin(); i != hlp.end() && eventHandlingAllowed; i++)
  162. {
  163. if(!vstd::contains(wheelInterested,*i))
  164. continue;
  165. (*i)->wheelScrolled(distance.y < 0, (*i)->isInside(position));
  166. }
  167. }
  168. void InterfaceEventDispatcher::dispatchTextInput(const std::string & text)
  169. {
  170. for(auto it : textInterested)
  171. {
  172. it->textInputed(text);
  173. }
  174. }
  175. void InterfaceEventDispatcher::dispatchTextEditing(const std::string & text)
  176. {
  177. for(auto it : textInterested)
  178. {
  179. it->textEdited(text);
  180. }
  181. }
  182. void InterfaceEventDispatcher::dispatchMouseMoved(const Point & position)
  183. {
  184. //sending active, hovered hoverable objects hover() call
  185. CIntObjectList hlp;
  186. auto hoverableCopy = hoverable;
  187. for(auto & elem : hoverableCopy)
  188. {
  189. if(elem->isInside(GH.getCursorPosition()))
  190. {
  191. if (!(elem)->isHovered())
  192. hlp.push_back((elem));
  193. }
  194. else if ((elem)->isHovered())
  195. {
  196. (elem)->hover(false);
  197. (elem)->hoveredState = false;
  198. }
  199. }
  200. for(auto & elem : hlp)
  201. {
  202. elem->hover(true);
  203. elem->hoveredState = true;
  204. }
  205. //sending active, MotionInterested objects mouseMoved() call
  206. CIntObjectList miCopy = motioninterested;
  207. for(auto & elem : miCopy)
  208. {
  209. if(elem->strongInterestState || elem->isInside(position)) //checking bounds including border fixes bug #2476
  210. {
  211. (elem)->mouseMoved(position);
  212. }
  213. }
  214. }