MapViewActions.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. {
  28. pos.w = model->getPixelsVisibleDimensions().x;
  29. pos.h = model->getPixelsVisibleDimensions().y;
  30. addUsedEvents(LCLICK | SHOW_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. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  39. if(context->isInMap(tile))
  40. adventureInt->onTileLeftClicked(tile);
  41. }
  42. void MapViewActions::showPopupWindow(const Point & cursorPosition)
  43. {
  44. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  45. if(context->isInMap(tile))
  46. adventureInt->onTileRightClicked(tile);
  47. }
  48. void MapViewActions::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  49. {
  50. handleHover(cursorPosition);
  51. }
  52. void MapViewActions::wheelScrolled(int distance)
  53. {
  54. adventureInt->hotkeyZoom(distance * 4);
  55. }
  56. void MapViewActions::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  57. {
  58. owner.onMapSwiped(lastUpdateDistance);
  59. }
  60. void MapViewActions::gesturePinch(const Point & centerPosition, double lastUpdateFactor)
  61. {
  62. double newZoom = pinchZoomFactor * lastUpdateFactor;
  63. int newZoomSteps = std::round(std::log(newZoom) / std::log(1.01));
  64. int oldZoomSteps = std::round(std::log(pinchZoomFactor) / std::log(1.01));
  65. if (newZoomSteps != oldZoomSteps)
  66. adventureInt->hotkeyZoom(newZoomSteps - oldZoomSteps);
  67. pinchZoomFactor = newZoom;
  68. }
  69. void MapViewActions::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  70. {
  71. pinchZoomFactor = 1.0;
  72. }
  73. void MapViewActions::handleHover(const Point & cursorPosition)
  74. {
  75. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  76. if(!context->isInMap(tile))
  77. {
  78. CCS->curh->set(Cursor::Map::POINTER);
  79. return;
  80. }
  81. adventureInt->onTileHovered(tile);
  82. }
  83. void MapViewActions::hover(bool on)
  84. {
  85. if(!on)
  86. {
  87. GH.statusbar()->clear();
  88. CCS->curh->set(Cursor::Map::POINTER);
  89. }
  90. }