Browse Source

Fixed map view centering during scaling change

Ivan Savenko 2 years ago
parent
commit
7504ceb256
2 changed files with 20 additions and 7 deletions
  1. 12 2
      client/mapView/MapViewController.cpp
  2. 8 5
      client/mapView/MapViewModel.cpp

+ 12 - 2
client/mapView/MapViewController.cpp

@@ -69,10 +69,19 @@ void MapViewController::setViewCenter(const Point & position, int level)
 
 void MapViewController::setTileSize(const Point & tileSize)
 {
+	Point oldSize = model->getSingleTileSize();
 	model->setTileSize(tileSize);
 
+	double scaleChangeX = 1.0 * tileSize.x / oldSize.x;
+	double scaleChangeY = 1.0 * tileSize.y / oldSize.y;
+
+	Point newViewCenter {
+		static_cast<int>(std::round(model->getMapViewCenter().x * scaleChangeX)),
+		static_cast<int>(std::round(model->getMapViewCenter().y * scaleChangeY))
+	};
+
 	// force update of view center since changing tile size may invalidated it
-	setViewCenter(model->getMapViewCenter(), model->getLevel());
+	setViewCenter(newViewCenter, model->getLevel());
 }
 
 void MapViewController::modifyTileSize(int stepsChange)
@@ -101,7 +110,8 @@ void MapViewController::modifyTileSize(int stepsChange)
 		std::clamp(desiredZoom.y, minimal.y, maximal.y)
 	};
 
-	setTileSize(actualZoom);
+	if (actualZoom != currentZoom)
+		setTileSize(actualZoom);
 }
 
 MapViewController::MapViewController(std::shared_ptr<MapViewModel> model, std::shared_ptr<MapViewCache> view)

+ 8 - 5
client/mapView/MapViewModel.cpp

@@ -35,12 +35,15 @@ void MapViewModel::setLevel(int newLevel)
 
 Point MapViewModel::getSingleTileSizeUpperLimit() const
 {
-	return Point(128, 128); // arbitrary-seleted upscaling limit
+	// arbitrary-seleted upscaling limit
+	// TODO: figure out reason behind graphical artifacts on scaling higher than 120px (375%)
+	return Point(120, 120);
 }
 
 Point MapViewModel::getSingleTileSizeLowerLimit() const
 {
-	return Point(4, 4); // arbitrary-seleted upscaling limit
+	// arbitrary-seleted downscaling limit
+	return Point(4, 4);
 }
 
 Point MapViewModel::getSingleTileSize() const
@@ -114,14 +117,14 @@ Rect MapViewModel::getCacheTileArea(const int3 & coordinates) const
 		(getTilesVisibleDimensions().y + coordinates.y) % getTilesVisibleDimensions().y
 	};
 
-	return Rect(tileIndex * tileSize, tileSize);
+	return Rect(tileIndex * getSingleTileSize(), getSingleTileSize());
 }
 
 Rect MapViewModel::getTargetTileArea(const int3 & coordinates) const
 {
 	Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
-	Point tilePosAbsolute = Point(coordinates) * tileSize;
+	Point tilePosAbsolute = Point(coordinates) * getSingleTileSize();
 	Point tilePosRelative = tilePosAbsolute - topLeftOffset;
 
-	return Rect(tilePosRelative, tileSize);
+	return Rect(tilePosRelative, getSingleTileSize());
 }