MapViewActions.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 | 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::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  69. {
  70. handleHover(cursorPosition);
  71. }
  72. void MapViewActions::wheelScrolled(int distance)
  73. {
  74. adventureInt->hotkeyZoom(distance * 4);
  75. }
  76. void MapViewActions::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
  77. {
  78. dragDistance += lastUpdateDistance;
  79. if (dragDistance.length() > 16)
  80. dragActive = true;
  81. if (dragActive && settings["adventure"]["leftButtonDrag"].Bool())
  82. owner.onMapSwiped(lastUpdateDistance);
  83. }
  84. void MapViewActions::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  85. {
  86. owner.onMapSwiped(lastUpdateDistance);
  87. }
  88. void MapViewActions::gesturePinch(const Point & centerPosition, double lastUpdateFactor)
  89. {
  90. double newZoom = pinchZoomFactor * lastUpdateFactor;
  91. int newZoomSteps = std::round(std::log(newZoom) / std::log(1.01));
  92. int oldZoomSteps = std::round(std::log(pinchZoomFactor) / std::log(1.01));
  93. if (newZoomSteps != oldZoomSteps)
  94. adventureInt->hotkeyZoom(newZoomSteps - oldZoomSteps);
  95. pinchZoomFactor = newZoom;
  96. }
  97. void MapViewActions::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  98. {
  99. dragActive = on;
  100. pinchZoomFactor = 1.0;
  101. }
  102. void MapViewActions::handleHover(const Point & cursorPosition)
  103. {
  104. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  105. if(!context->isInMap(tile))
  106. {
  107. CCS->curh->set(Cursor::Map::POINTER);
  108. return;
  109. }
  110. adventureInt->onTileHovered(tile);
  111. }
  112. void MapViewActions::hover(bool on)
  113. {
  114. if(!on)
  115. {
  116. GH.statusbar()->clear();
  117. CCS->curh->set(Cursor::Map::POINTER);
  118. }
  119. }