2
0

MapViewActions.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * MapViewActions.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 "MapViewActions.h"
  12. #include "IMapRendererContext.h"
  13. #include "MapView.h"
  14. #include "MapViewModel.h"
  15. #include "../CGameInfo.h"
  16. #include "../adventureMap/AdventureMapInterface.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/CursorHandler.h"
  19. #include "../gui/MouseButton.h"
  20. #include "../../lib/CConfigHandler.h"
  21. MapViewActions::MapViewActions(MapView & owner, const std::shared_ptr<MapViewModel> & model)
  22. : model(model)
  23. , owner(owner)
  24. , isSwiping(false)
  25. {
  26. pos.w = model->getPixelsVisibleDimensions().x;
  27. pos.h = model->getPixelsVisibleDimensions().y;
  28. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE | WHEEL);
  29. }
  30. bool MapViewActions::swipeEnabled() const
  31. {
  32. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  33. return settings["general"]["swipe"].Bool();
  34. #else
  35. return settings["general"]["swipeDesktop"].Bool();
  36. #endif
  37. }
  38. void MapViewActions::setContext(const std::shared_ptr<IMapRendererContext> & context)
  39. {
  40. this->context = context;
  41. }
  42. void MapViewActions::clickLeft(tribool down, bool previousState)
  43. {
  44. if(indeterminate(down))
  45. return;
  46. if(swipeEnabled())
  47. {
  48. if(handleSwipeStateChange(static_cast<bool>(down)))
  49. {
  50. return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
  51. }
  52. }
  53. else
  54. {
  55. if(down == false)
  56. return;
  57. }
  58. int3 tile = model->getTileAtPoint(GH.getCursorPosition() - pos.topLeft());
  59. if(context->isInMap(tile))
  60. adventureInt->onTileLeftClicked(tile);
  61. }
  62. void MapViewActions::clickRight(tribool down, bool previousState)
  63. {
  64. if(isSwiping)
  65. return;
  66. int3 tile = model->getTileAtPoint(GH.getCursorPosition() - pos.topLeft());
  67. if(down && context->isInMap(tile))
  68. adventureInt->onTileRightClicked(tile);
  69. }
  70. void MapViewActions::clickMiddle(tribool down, bool previousState)
  71. {
  72. handleSwipeStateChange(static_cast<bool>(down));
  73. }
  74. void MapViewActions::mouseMoved(const Point & cursorPosition)
  75. {
  76. handleHover(cursorPosition);
  77. handleSwipeMove(cursorPosition);
  78. }
  79. void MapViewActions::wheelScrolled(bool down, bool in)
  80. {
  81. if (!in)
  82. return;
  83. adventureInt->hotkeyZoom(down ? -1 : +1);
  84. }
  85. void MapViewActions::handleSwipeMove(const Point & cursorPosition)
  86. {
  87. // unless swipe is enabled, swipe move only works with middle mouse button
  88. if(!swipeEnabled() && !GH.isMouseButtonPressed(MouseButton::MIDDLE))
  89. return;
  90. // on mobile platforms with enabled swipe we use left button
  91. if(swipeEnabled() && !GH.isMouseButtonPressed(MouseButton::LEFT))
  92. return;
  93. if(!isSwiping)
  94. {
  95. static constexpr int touchSwipeSlop = 16;
  96. Point distance = (cursorPosition - swipeInitialRealPos);
  97. // try to distinguish if this touch was meant to be a swipe or just fat-fingering press
  98. if(std::abs(distance.x) + std::abs(distance.y) > touchSwipeSlop)
  99. isSwiping = true;
  100. }
  101. if(isSwiping)
  102. {
  103. Point swipeTargetPosition = swipeInitialViewPos + swipeInitialRealPos - cursorPosition;
  104. owner.onMapSwiped(swipeTargetPosition);
  105. }
  106. }
  107. bool MapViewActions::handleSwipeStateChange(bool btnPressed)
  108. {
  109. if(btnPressed)
  110. {
  111. swipeInitialRealPos = GH.getCursorPosition();
  112. swipeInitialViewPos = model->getMapViewCenter();
  113. return true;
  114. }
  115. if(isSwiping) // only accept this touch if it wasn't a swipe
  116. {
  117. owner.onMapSwipeEnded();
  118. isSwiping = false;
  119. return true;
  120. }
  121. return false;
  122. }
  123. void MapViewActions::handleHover(const Point & cursorPosition)
  124. {
  125. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  126. if(!context->isInMap(tile))
  127. {
  128. CCS->curh->set(Cursor::Map::POINTER);
  129. return;
  130. }
  131. adventureInt->onTileHovered(tile);
  132. }
  133. void MapViewActions::hover(bool on)
  134. {
  135. if(!on)
  136. {
  137. GH.statusbar()->clear();
  138. CCS->curh->set(Cursor::Map::POINTER);
  139. }
  140. }