2
0

MapViewActions.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_PANNING | HOVER | MOVE | WHEEL);
  31. }
  32. void MapViewActions::setContext(const std::shared_ptr<IMapRendererContext> & context)
  33. {
  34. this->context = context;
  35. }
  36. void MapViewActions::clickLeft(tribool down, bool previousState)
  37. {
  38. if(indeterminate(down))
  39. return;
  40. if(down == false)
  41. return;
  42. int3 tile = model->getTileAtPoint(GH.getCursorPosition() - pos.topLeft());
  43. if(context->isInMap(tile))
  44. adventureInt->onTileLeftClicked(tile);
  45. }
  46. void MapViewActions::showPopupWindow()
  47. {
  48. int3 tile = model->getTileAtPoint(GH.getCursorPosition() - pos.topLeft());
  49. if(context->isInMap(tile))
  50. adventureInt->onTileRightClicked(tile);
  51. }
  52. void MapViewActions::mouseMoved(const Point & cursorPosition)
  53. {
  54. handleHover(cursorPosition);
  55. }
  56. void MapViewActions::wheelScrolled(int distance)
  57. {
  58. adventureInt->hotkeyZoom(distance * 4);
  59. }
  60. void MapViewActions::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  61. {
  62. owner.onMapSwiped(lastUpdateDistance);
  63. }
  64. void MapViewActions::gesturePinch(const Point & centerPosition, double lastUpdateFactor)
  65. {
  66. double newZoom = pinchZoomFactor * lastUpdateFactor;
  67. int newZoomSteps = std::round(std::log(newZoom) / std::log(1.01));
  68. int oldZoomSteps = std::round(std::log(pinchZoomFactor) / std::log(1.01));
  69. if (newZoomSteps != oldZoomSteps)
  70. adventureInt->hotkeyZoom(newZoomSteps - oldZoomSteps);
  71. pinchZoomFactor = newZoom;
  72. }
  73. void MapViewActions::panning(bool on, const Point & initialPosition, const Point & finalPosition)
  74. {
  75. pinchZoomFactor = 1.0;
  76. }
  77. void MapViewActions::handleHover(const Point & cursorPosition)
  78. {
  79. int3 tile = model->getTileAtPoint(cursorPosition - pos.topLeft());
  80. if(!context->isInMap(tile))
  81. {
  82. CCS->curh->set(Cursor::Map::POINTER);
  83. return;
  84. }
  85. adventureInt->onTileHovered(tile);
  86. }
  87. void MapViewActions::hover(bool on)
  88. {
  89. if(!on)
  90. {
  91. GH.statusbar()->clear();
  92. CCS->curh->set(Cursor::Map::POINTER);
  93. }
  94. }