Updated RenderHandler zoom functionality.

This commit is contained in:
jme9
2025-03-18 11:51:39 -07:00
parent 0ba801ccff
commit d50de67f82
2 changed files with 21 additions and 13 deletions
+19 -11
View File
@@ -11,14 +11,14 @@ namespace LunaLightXMG
private SpriteBatch spriteBatch; private SpriteBatch spriteBatch;
private RenderTarget2D renderTarget; private RenderTarget2D renderTarget;
private Rectangle renderDestination; private Rectangle renderDestination;
private int retroWidth; private float retroWidth;
private int retroHeight; private float retroHeight;
private float zoomLevel = 1.0f; private float zoomLevel = 1.0f;
private float zoomTarget = 1.0f; private float zoomTarget = 1.0f;
private readonly float zoomMin = 0.75f; private readonly float zoomMin = 0.50f;
private readonly float zoomNorm = 1.0f; private readonly float zoomNorm = 1.0f;
private readonly float zoomMax = 1.25f; private readonly float zoomMax = 1.50f;
private readonly float zoomSpeed = 0.1f; private readonly float zoomSpeed = 0.1f;
public float ZoomLevel {get => zoomLevel;} public float ZoomLevel {get => zoomLevel;}
@@ -49,24 +49,32 @@ namespace LunaLightXMG
{ {
zoomTarget = zoomMax; zoomTarget = zoomMax;
} }
public void UpdateZoom() public void UpdateZoom()
{ {
// interpolate to target
zoomLevel = MathHelper.Lerp(zoomLevel, zoomTarget, zoomSpeed); zoomLevel = MathHelper.Lerp(zoomLevel, zoomTarget, zoomSpeed);
// snap to target when we're close enough
if (Math.Abs(zoomLevel - zoomTarget) < 0.001f)
{
zoomLevel = zoomTarget;
}
// stay in bounds (just in case)
zoomLevel = MathHelper.Clamp(zoomLevel, zoomMin, zoomMax); zoomLevel = MathHelper.Clamp(zoomLevel, zoomMin, zoomMax);
// calc new zoomed render destination
CalculateRenderDestination(); CalculateRenderDestination();
} }
public void CalculateRenderDestination() public void CalculateRenderDestination()
{ {
Point screenSize = graphicsDevice.Viewport.Bounds.Size; Point screenSize = graphicsDevice.Viewport.Bounds.Size;
// Calculate the integer scale factor // Calculate the integer scale factor
int scaleX = screenSize.X / (int)(retroWidth * zoomLevel); float scaleX = screenSize.X / (retroWidth * zoomLevel);
int scaleY = screenSize.Y / (int)(retroHeight * zoomLevel); float scaleY = screenSize.Y / (retroHeight * zoomLevel);
// Set renderDestination // Set renderDestination
renderDestination.Width = retroWidth * scaleX; renderDestination.Width = (int)(retroWidth * scaleX);
renderDestination.Height = retroHeight * scaleY; renderDestination.Height = (int)(retroHeight * scaleY);
renderDestination.X = (screenSize.X - renderDestination.Width) / 2; renderDestination.X = (screenSize.X - renderDestination.Width) / 2;
renderDestination.Y = (screenSize.Y - renderDestination.Height) / 2; renderDestination.Y = (screenSize.Y - renderDestination.Height) / 2;
} }
+2 -2
View File
@@ -15,6 +15,6 @@ Here's what the render handler can do.
Trying out zoom using the render target. The render target is set by retroWidth and retroHeight (320x180). Trying out zoom using the render target. The render target is set by retroWidth and retroHeight (320x180).
- ZoomIn() should smoothly zoom to 75% retroWidth x retroHeight, 240x135 - ZoomIn() should smoothly zoom to 50% retroWidth x retroHeight, 160x90
- ZoomOut() should smoothly zoom to 125% retroWidth x retroHeight, 400x225 - ZoomOut() should smoothly zoom to 150% retroWidth x retroHeight, 480x270