MapViewActions.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if(!settings["adventure"]["rightButtonDrag"].Bool())
  91. return;
  92. dragActive = true;
  93. owner.onMapSwiped(lastUpdateDistance);
  94. }
  95. void MapViewActions::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  96. {
  97. owner.onMapSwiped(lastUpdateDistance);
  98. }
  99. void MapViewActions::gesturePinch(const Point & centerPosition, double lastUpdateFactor)
  100. {
  101. double newZoom = pinchZoomFactor * lastUpdateFactor;
  102. int newZoomSteps = std::round(std::log(newZoom) / std::log(1.01));
  103. int oldZoomSteps = std::round(std::log(pinchZoomFactor) / std::log(1.01));
  104. if (newZoomSteps != oldZoomSteps)
  105. adventureInt->hotkeyZoom(newZoomSteps - oldZoomSteps, true);
  106. pinchZoomFactor = newZoom;
  107. }
  108. void MapViewActions::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  109. {
  110. dragActive = on;
  111. pinchZoomFactor = 1.0;
  112. }
  113. void MapViewActions::handleHover(const Point & cursorPosition)
  114. {
  115. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  116. if(!context->isInMap(tile))
  117. {
  118. ENGINE->cursor().set(Cursor::Map::POINTER);
  119. return;
  120. }
  121. adventureInt->onTileHovered(tile);
  122. }
  123. void MapViewActions::hover(bool on)
  124. {
  125. if(!on)
  126. {
  127. ENGINE->statusbar()->clear();
  128. ENGINE->cursor().set(Cursor::Map::POINTER);
  129. }
  130. }