Add zooming and panning to wxDC Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Add zooming and panning to wxDC

Post by TheGreatRambler »

I intend to add zooming and panning to a wxDC a la Google Maps. I have no compilation problems or runtime problems, but I need some help on the math I should implement. I am handling EVT_MOUSEWHEEL and EVT_MOTION right now:

Code: Select all

void DrawingCanvas::OnMousewheel(wxMouseEvent& event) {
	int ticks = event.GetWheelRotation() / event.GetWheelDelta();
	zoomScale += (double)ticks / 20;
	if(zoomScale < 0.01) {
		zoomScale = 0.01;
	}
	zoomPoint = event.GetPosition();
	Refresh();
}

void DrawingCanvas::OnMouseMove(wxMouseEvent& event) {
	if(event.Dragging()) {
		zoomPoint = event.GetPosition();
		Refresh();
	}
}
I suspect the mouse move event could be changed to do panning. In my drawing code, I do this:

Code: Select all

void DrawingCanvasBitmap::draw(wxDC& dc) {
	if(bitmap != NULL) {
		int width;
		int height;
		GetSize(&width, &height);

		// Scale for width and height are the same
		double scale = (double)width / bitmap->GetWidth();

		// https://forums.wxwidgets.org/viewtopic.php?t=21080
		scale *= zoomScale;
		dc.SetUserScale(scale, scale);
		if(zoomPoint != wxDefaultPosition) {
			wxPoint adjustedZoomPoint;
			// TODO fix this crap
			adjustedZoomPoint.x = dc.DeviceToLogicalX(zoomPoint.x);
			adjustedZoomPoint.y = dc.DeviceToLogicalY(zoomPoint.y);
			wxPoint middlePoint = zoomPoint - adjustedZoomPoint;
			dc.SetDeviceOrigin(middlePoint.x, middlePoint.y);
		}

		dc.DrawBitmap(*bitmap, 0, 0, false);
	}
}
The canvas itself is in a sizer with wxSHAPED and the wxBitmap here has the same aspect ratio as the canvas. It is the bitmap I want to implement google maps style navigation on. The scale variable is self explanatory, bigger than one means closer in on the image, smaller than one but above zero means farther away from the image. The zoomPoint is the point where the user's mouse is when they use the scrollwheel. As an aside, how could I make the scaling constant, so it doesn't take more scrollwheel revolutions to zoom in when very zoomed in? Thank you so much for your help or pointers!
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Add zooming and panning to wxDC

Post by New Pagodi »

I wrote about this here.
TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: Add zooming and panning to wxDC

Post by TheGreatRambler »

Thank you, that's fantastic!
Post Reply