Fully working zoom relative to cursor
This commit is contained in:
@@ -47,41 +47,43 @@ namespace DepthsBelow
|
|||||||
MouseState ms = Mouse.GetState();
|
MouseState ms = Mouse.GetState();
|
||||||
|
|
||||||
if (ms.X < 20)
|
if (ms.X < 20)
|
||||||
Position.X -= Speed * elapsed;
|
|
||||||
if (ms.X > core.GraphicsDevice.Viewport.Width - 20)
|
|
||||||
Position.X += Speed * elapsed;
|
Position.X += Speed * elapsed;
|
||||||
|
if (ms.X > core.GraphicsDevice.Viewport.Width - 20)
|
||||||
|
Position.X -= Speed * elapsed;
|
||||||
|
|
||||||
if (ms.Y < 20)
|
if (ms.Y < 20)
|
||||||
Position.Y -= Speed * elapsed;
|
|
||||||
if (ms.Y > core.GraphicsDevice.Viewport.Height - 20)
|
|
||||||
Position.Y += Speed * elapsed;
|
Position.Y += Speed * elapsed;
|
||||||
|
if (ms.Y > core.GraphicsDevice.Viewport.Height - 20)
|
||||||
|
Position.Y -= Speed * elapsed;
|
||||||
|
|
||||||
int scrollChange = ms.ScrollWheelValue - lastMouseState.ScrollWheelValue;
|
int scrollChange = ms.ScrollWheelValue - lastMouseState.ScrollWheelValue;
|
||||||
|
|
||||||
float preZoom = Zoom;
|
|
||||||
|
|
||||||
if (scrollChange > 0)
|
if (scrollChange > 0)
|
||||||
Zoom *= 1.15f;
|
Zoom *= 1.15f;
|
||||||
else if (scrollChange < 0)
|
else if (scrollChange < 0)
|
||||||
Zoom /= 1.15f;
|
Zoom /= 1.15f;
|
||||||
|
|
||||||
|
// Store the current screen and world position of the mouse for zoom compensation
|
||||||
|
var mousePosBefore = new Vector2(ms.X, ms.Y);
|
||||||
|
var mousePosBeforeWorld = this.ScreenToWorld(mousePosBefore);
|
||||||
|
|
||||||
|
// Apply the matrix transformation
|
||||||
Transform = Matrix.Identity
|
Transform = Matrix.Identity
|
||||||
* Matrix.CreateRotationZ(Rotation)
|
* Matrix.CreateRotationZ(Rotation)
|
||||||
* Matrix.CreateScale(Zoom)
|
* Matrix.CreateScale(Zoom)
|
||||||
* Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));
|
* Matrix.CreateTranslation(new Vector3(Position.X, Position.Y, 0));
|
||||||
|
|
||||||
/*var mouseOffset = core.camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
|
|
||||||
|
|
||||||
|
// If we've zoomed
|
||||||
if (scrollChange != 0)
|
if (scrollChange != 0)
|
||||||
{
|
{
|
||||||
Transform *= Matrix.CreateTranslation(new Vector3(-mouseOffset.X, -mouseOffset.Y, 0))
|
// Translate the stored world position back to new screen coordinates
|
||||||
* Matrix.CreateScale(Zoom)
|
var mousePosAfter = this.WorldToScreen(mousePosBeforeWorld);
|
||||||
* Matrix.CreateTranslation(new Vector3(mouseOffset.X, mouseOffset.Y, 0));
|
var change = mousePosBefore - mousePosAfter;
|
||||||
|
|
||||||
|
// Update position and matrix transformation
|
||||||
|
Position += change;
|
||||||
|
Transform *= Matrix.CreateTranslation(new Vector3(change.X, change.Y, 0));
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Transform *= Matrix.CreateScale(Zoom) * Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)); ;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
lastMouseState = ms;
|
lastMouseState = ms;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user