MapViewActions.cpp 3.9 KB

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