Pārlūkot izejas kodu

Allow zooming with mouse wheel

Ivan Savenko 2 gadi atpakaļ
vecāks
revīzija
9e3bc87a6b

+ 1 - 1
client/adventureMap/AdventureMapInterface.cpp

@@ -800,7 +800,7 @@ void AdventureMapInterface::hotkeySwitchMapLevel()
 
 void AdventureMapInterface::hotkeyZoom(int delta)
 {
-	widget->getMapView()->onMapZoomLevelChanged( 1.0 + delta / 10.0);
+	widget->getMapView()->onMapZoomLevelChanged(delta);
 }
 
 void AdventureMapInterface::onScreenResize()

+ 2 - 2
client/mapView/MapView.cpp

@@ -154,9 +154,9 @@ void MapView::onViewWorldActivated(uint32_t tileSize)
 	controller->setTileSize(Point(tileSize, tileSize));
 }
 
-void MapView::onMapZoomLevelChanged(double zoomFactor)
+void MapView::onMapZoomLevelChanged(int stepsChange)
 {
-	controller->modifyTileSize(zoomFactor);
+	controller->modifyTileSize(stepsChange);
 }
 
 void MapView::onViewMapActivated()

+ 1 - 1
client/mapView/MapView.h

@@ -80,7 +80,7 @@ public:
 	void onViewWorldActivated(uint32_t tileSize);
 
 	/// Changes zoom level / tile size of current view by specified factor
-	void onMapZoomLevelChanged(double zoomFactor);
+	void onMapZoomLevelChanged(int stepsChange);
 
 	/// Switches view from View World mode back to standard view
 	void onViewMapActivated();

+ 8 - 1
client/mapView/MapViewActions.cpp

@@ -29,7 +29,7 @@ MapViewActions::MapViewActions(MapView & owner, const std::shared_ptr<MapViewMod
 	pos.w = model->getPixelsVisibleDimensions().x;
 	pos.h = model->getPixelsVisibleDimensions().y;
 
-	addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
+	addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE | WHEEL);
 }
 
 bool MapViewActions::swipeEnabled() const
@@ -92,6 +92,13 @@ void MapViewActions::mouseMoved(const Point & cursorPosition)
 	handleSwipeMove(cursorPosition);
 }
 
+void MapViewActions::wheelScrolled(bool down, bool in)
+{
+	if (!in)
+		return;
+	adventureInt->hotkeyZoom(down ? -1 : +1);
+}
+
 void MapViewActions::handleSwipeMove(const Point & cursorPosition)
 {
 	// unless swipe is enabled, swipe move only works with middle mouse button

+ 1 - 0
client/mapView/MapViewActions.h

@@ -42,4 +42,5 @@ public:
 	void clickMiddle(tribool down, bool previousState) override;
 	void hover(bool on) override;
 	void mouseMoved(const Point & cursorPosition) override;
+	void wheelScrolled(bool down, bool in) override;
 };

+ 12 - 4
client/mapView/MapViewController.cpp

@@ -75,15 +75,23 @@ void MapViewController::setTileSize(const Point & tileSize)
 	setViewCenter(model->getMapViewCenter(), model->getLevel());
 }
 
-void MapViewController::modifyTileSize(double zoomFactor)
+void MapViewController::modifyTileSize(int stepsChange)
 {
+	// we want to zoom in/out in fixed 10% steps, to allow player to return back to exactly 100% zoom just by scrolling
+	// so, zooming in for 5 steps will put game at 1.1^5 = 1.61 scale
+	// try to determine current zooming level and change it by requested number of steps
+	double currentZoomFactor = model->getSingleTileSize().x / 32.0;
+	double currentZoomSteps = std::round(std::log(currentZoomFactor) / std::log(1.1));
+	double newZoomSteps = currentZoomSteps + stepsChange;
+	double newZoomFactor = std::pow(1.1, newZoomSteps);
+
 	Point currentZoom = model->getSingleTileSize();
-	Point desiredZoom = currentZoom * zoomFactor;
+	Point desiredZoom = Point(32,32) * newZoomFactor;
 
-	if (desiredZoom == currentZoom && zoomFactor < 1.0)
+	if (desiredZoom == currentZoom && stepsChange < 0)
 		desiredZoom -= Point(1,1);
 
-	if (desiredZoom == currentZoom && zoomFactor > 1.0)
+	if (desiredZoom == currentZoom && stepsChange > 0)
 		desiredZoom += Point(1,1);
 
 	Point minimal = model->getSingleTileSizeLowerLimit();

+ 1 - 1
client/mapView/MapViewController.h

@@ -83,7 +83,7 @@ public:
 	void setViewCenter(const int3 & position);
 	void setViewCenter(const Point & position, int level);
 	void setTileSize(const Point & tileSize);
-	void modifyTileSize(double zoomFactor);
+	void modifyTileSize(int stepsChange);
 	void tick(uint32_t timePassed);
 	void afterRender();