MapViewActions.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "../CPlayerInterface.h"
  21. #include "../adventureMap/CInGameConsole.h"
  22. #include "../../lib/CConfigHandler.h"
  23. MapViewActions::MapViewActions(MapView & owner, const std::shared_ptr<MapViewModel> & model)
  24. : model(model)
  25. , owner(owner)
  26. , pinchZoomFactor(1.0)
  27. , dragActive(false)
  28. {
  29. pos.w = model->getPixelsVisibleDimensions().x;
  30. pos.h = model->getPixelsVisibleDimensions().y;
  31. addUsedEvents(LCLICK | SHOW_POPUP | DRAG | DRAG_POPUP | GESTURE | HOVER | MOVE | WHEEL);
  32. }
  33. void MapViewActions::setContext(const std::shared_ptr<IMapRendererContext> & context)
  34. {
  35. this->context = context;
  36. }
  37. void MapViewActions::clickPressed(const Point & cursorPosition)
  38. {
  39. if (!settings["adventure"]["leftButtonDrag"].Bool())
  40. {
  41. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  42. if(context->isInMap(tile))
  43. adventureInt->onTileLeftClicked(tile);
  44. }
  45. }
  46. void MapViewActions::clickReleased(const Point & cursorPosition)
  47. {
  48. if (!dragActive && settings["adventure"]["leftButtonDrag"].Bool())
  49. {
  50. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  51. if(context->isInMap(tile))
  52. adventureInt->onTileLeftClicked(tile);
  53. }
  54. dragActive = false;
  55. dragDistance = Point(0,0);
  56. }
  57. void MapViewActions::clickCancel(const Point & cursorPosition)
  58. {
  59. dragActive = false;
  60. dragDistance = Point(0,0);
  61. }
  62. void MapViewActions::showPopupWindow(const Point & cursorPosition)
  63. {
  64. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  65. if(context->isInMap(tile))
  66. adventureInt->onTileRightClicked(tile);
  67. }
  68. void MapViewActions::closePopupWindow(bool alreadyClosed)
  69. {
  70. if(alreadyClosed)
  71. dragActive = false;
  72. }
  73. void MapViewActions::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  74. {
  75. handleHover(cursorPosition);
  76. }
  77. void MapViewActions::wheelScrolled(int distance)
  78. {
  79. adventureInt->hotkeyZoom(distance * 4, true);
  80. }
  81. void MapViewActions::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
  82. {
  83. dragDistance += lastUpdateDistance;
  84. if (dragDistance.length() > 16)
  85. dragActive = true;
  86. if (dragActive && settings["adventure"]["leftButtonDrag"].Bool())
  87. owner.onMapSwiped(lastUpdateDistance);
  88. }
  89. void MapViewActions::mouseDraggedPopup(const Point & cursorPosition, const Point & lastUpdateDistance)
  90. {
  91. if(!settings["adventure"]["rightButtonDrag"].Bool())
  92. return;
  93. dragActive = true;
  94. owner.onMapSwiped(lastUpdateDistance);
  95. }
  96. void MapViewActions::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  97. {
  98. owner.onMapSwiped(lastUpdateDistance);
  99. }
  100. void MapViewActions::gesturePinch(const Point & centerPosition, double lastUpdateFactor)
  101. {
  102. double newZoom = pinchZoomFactor * lastUpdateFactor;
  103. int newZoomSteps = std::round(std::log(newZoom) / std::log(1.01));
  104. int oldZoomSteps = std::round(std::log(pinchZoomFactor) / std::log(1.01));
  105. if (newZoomSteps != oldZoomSteps)
  106. adventureInt->hotkeyZoom(newZoomSteps - oldZoomSteps, true);
  107. pinchZoomFactor = newZoom;
  108. }
  109. void MapViewActions::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  110. {
  111. dragActive = on;
  112. pinchZoomFactor = 1.0;
  113. }
  114. void MapViewActions::handleHover(const Point & cursorPosition)
  115. {
  116. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  117. if(!context->isInMap(tile))
  118. {
  119. CCS->curh->set(Cursor::Map::POINTER);
  120. return;
  121. }
  122. adventureInt->onTileHovered(tile);
  123. }
  124. void MapViewActions::hover(bool on)
  125. {
  126. if(!on)
  127. {
  128. GH.statusbar()->clear();
  129. CCS->curh->set(Cursor::Map::POINTER);
  130. }
  131. }