From d50de67f82bef0d05b5652ccdc68c92a335ef417 Mon Sep 17 00:00:00 2001 From: jme9 Date: Tue, 18 Mar 2025 11:51:39 -0700 Subject: [PATCH] Updated RenderHandler zoom functionality. --- RenderHandler.cs | 30 +++++++++++++++++++----------- UpdateNotes/RenderHandler.md | 4 ++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/RenderHandler.cs b/RenderHandler.cs index 0a0026d..7506bf7 100644 --- a/RenderHandler.cs +++ b/RenderHandler.cs @@ -11,14 +11,14 @@ namespace LunaLightXMG private SpriteBatch spriteBatch; private RenderTarget2D renderTarget; private Rectangle renderDestination; - - private int retroWidth; - private int retroHeight; + + private float retroWidth; + private float retroHeight; private float zoomLevel = 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 zoomMax = 1.25f; + private readonly float zoomMax = 1.50f; private readonly float zoomSpeed = 0.1f; public float ZoomLevel {get => zoomLevel;} @@ -49,24 +49,32 @@ namespace LunaLightXMG { zoomTarget = zoomMax; } - + public void UpdateZoom() { + // interpolate to target 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); + // calc new zoomed render destination CalculateRenderDestination(); } - + public void CalculateRenderDestination() { Point screenSize = graphicsDevice.Viewport.Bounds.Size; // Calculate the integer scale factor - int scaleX = screenSize.X / (int)(retroWidth * zoomLevel); - int scaleY = screenSize.Y / (int)(retroHeight * zoomLevel); + float scaleX = screenSize.X / (retroWidth * zoomLevel); + float scaleY = screenSize.Y / (retroHeight * zoomLevel); // Set renderDestination - renderDestination.Width = retroWidth * scaleX; - renderDestination.Height = retroHeight * scaleY; + renderDestination.Width = (int)(retroWidth * scaleX); + renderDestination.Height = (int)(retroHeight * scaleY); renderDestination.X = (screenSize.X - renderDestination.Width) / 2; renderDestination.Y = (screenSize.Y - renderDestination.Height) / 2; } diff --git a/UpdateNotes/RenderHandler.md b/UpdateNotes/RenderHandler.md index b1a14ca..d820841 100644 --- a/UpdateNotes/RenderHandler.md +++ b/UpdateNotes/RenderHandler.md @@ -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). -- 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 \ No newline at end of file +- ZoomOut() should smoothly zoom to 150% retroWidth x retroHeight, 480x270 \ No newline at end of file