Skip to content

Commit 406175b

Browse files
authored
Zoom (#83)
* Very crude zoom is functional. * Zoom now has min and max bounds. * Got rid of those pesky lines caused by imperfect rendering coordinates. * Stopped the GUI from being zoomed. * Code quality tweaks. * Limited zoom to only in. The project isn't far enough along to be able to correctly solve the problems that would be caused by being able to zoom out.
1 parent c0bf223 commit 406175b

6 files changed

Lines changed: 94 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SneakyTactician <SneakyTactician@outlook.com>
2525
* The placeholder texture for the mine action button has been replaced
2626
* Stone rubble texture has been replaced
2727
* Pickaxe icon in map when tile is queue up to be mined has changed
28+
* Added the ability to zoom in
2829

2930
#### Sound
3031

MagicalLifeGUIWindows/Game1.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,21 @@ protected override void Draw(GameTime gameTime)
131131
{
132132
this.DisplayInGame();
133133
FMODUtil.System.update();
134-
this.GraphicsDevice.SetRenderTarget(null);
134+
135+
//Used to render things to a buffer that will have a zoom multiplier applied before rendering.
136+
SpriteBatch zoomBatch = new SpriteBatch(this.GraphicsDevice);
137+
RenderTarget2D target = new RenderTarget2D(this.GraphicsDevice, RenderingPipe.FullScreenWindow.Width, RenderingPipe.FullScreenWindow.Height);
138+
this.GraphicsDevice.SetRenderTarget(target);
139+
135140
this.GraphicsDevice.Clear(Color.Black);
136141

142+
143+
137144
if (Game1.SplashDone)
138145
{
139-
this.SpriteBatch.Begin();
140-
RenderingPipe.DrawScreen(ref this.SpriteBatch);
141-
this.SpriteBatch.End();
146+
zoomBatch.Begin();
147+
RenderingPipe.DrawScreen(zoomBatch);
148+
zoomBatch.End();
142149
}
143150
else
144151
{
@@ -148,7 +155,7 @@ protected override void Draw(GameTime gameTime)
148155
LogoScreen item = Game1.SplashScreens[i];
149156
if (!item.Done())
150157
{
151-
item.Draw(ref this.SpriteBatch);
158+
item.Draw(ref zoomBatch);
152159
break;
153160
}
154161

@@ -163,6 +170,19 @@ protected override void Draw(GameTime gameTime)
163170
}
164171
}
165172

173+
//set rendering back to the back buffer
174+
this.GraphicsDevice.SetRenderTarget(null);
175+
176+
//render target to back buffer
177+
zoomBatch.Begin();
178+
179+
int width = (int)(this.GraphicsDevice.DisplayMode.Width * RenderingPipe.Zoom);
180+
int height = (int)(this.GraphicsDevice.DisplayMode.Height * RenderingPipe.Zoom);
181+
182+
zoomBatch.Draw(target, new Rectangle(0, 0, width, height), Color.White);
183+
RenderingPipe.DrawGUI(zoomBatch);
184+
zoomBatch.End();
185+
166186
base.Draw(gameTime);
167187
}
168188

MagicalLifeGUIWindows/Input/InputHandlers.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ public static class InputHandlers
1313

1414
public static MiningActionHandler MiningAction;
1515

16-
public static StrafeHandler StrafingHandler = new StrafeHandler();
16+
public static StrafeHandler StrafingHandler;
1717

18-
public static EscapeHandler EscHandler = new EscapeHandler();
18+
public static EscapeHandler EscHandler;
19+
20+
public static ZoomHandler ZooomHandler;
1921

2022
public static void Initialize()
2123
{
2224
LivingMove = new LivingMoveOrderInputHandler();
2325
LogoSkipper = new LogoSkip();
2426
MiningAction = new MiningActionHandler();
27+
StrafingHandler = new StrafeHandler();
28+
EscHandler = new EscapeHandler();
29+
ZooomHandler = new ZoomHandler();
2530
}
2631
}
2732
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using MagicalLifeAPI.Filing.Logging;
2+
using MagicalLifeGUIWindows.Input.History;
3+
using MagicalLifeGUIWindows.Rendering;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace MagicalLifeGUIWindows.Input.Specialized_Handlers
11+
{
12+
public class ZoomHandler
13+
{
14+
/// <summary>
15+
/// The most zoomed out the player can get.
16+
/// </summary>
17+
private static readonly float MinZoom = 1F;
18+
19+
/// <summary>
20+
/// The most zoomed in the player can get.
21+
/// </summary>
22+
private static readonly float MaxZoom = 4F;
23+
24+
25+
public ZoomHandler()
26+
{
27+
BoundHandler.MouseListner.MouseWheelMoved += this.MouseListner_MouseWheelMoved;
28+
}
29+
30+
private void MouseListner_MouseWheelMoved(object sender, MonoGame.Extended.Input.InputListeners.MouseEventArgs e)
31+
{
32+
float previous = e.PreviousState.ScrollWheelValue;
33+
float current = e.ScrollWheelValue;
34+
35+
float change = (previous - current) / 1000;
36+
37+
//Ensure that zoom is above the minimum.
38+
float aboveMin = Math.Max(ZoomHandler.MinZoom, RenderingPipe.Zoom - change);
39+
40+
//Ensure that zoom is below the maximum.
41+
float belowMaxAndAboveMin = Math.Min(ZoomHandler.MaxZoom, aboveMin);
42+
43+
RenderingPipe.Zoom = belowMaxAndAboveMin;
44+
MasterLog.DebugWriteLine("Zoom: " + RenderingPipe.Zoom.ToString());
45+
}
46+
}
47+
}

MagicalLifeGUIWindows/MagicalLifeGUIWindows.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
<Compile Include="Input\Specialized Handlers\LogoSkip.cs" />
176176
<Compile Include="Input\Specialized Handlers\MiningActionHandler.cs" />
177177
<Compile Include="Input\Specialized Handlers\StrafeHandler.cs" />
178+
<Compile Include="Input\Specialized Handlers\ZoomHandler.cs" />
178179
<Compile Include="Load\Initializer.cs" />
179180
<Compile Include="Load\InputLoader.cs" />
180181
<Compile Include="Load\WindowConfig.cs" />

MagicalLifeGUIWindows/Rendering/RenderingPipe.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,36 @@ public static class RenderingPipe
3232
/// <summary>
3333
/// The x offset of the view due to the player moving the camera around the map.
3434
/// </summary>
35-
public static int XViewOffset = 0;
35+
public static int XViewOffset { get; set; } = 0;
3636

3737
/// <summary>
3838
/// The y offset of the view due to the player moving the camera around the map.
3939
/// </summary>
40-
public static int YViewOffset = 0;
40+
public static int YViewOffset { get; set; } = 0;
41+
42+
/// <summary>
43+
/// The zoom level of the map.
44+
/// </summary>
45+
public static float Zoom { get; set; } = 1F;
4146

4247
/// <summary>
4348
/// The currently viewed dimension.
4449
/// </summary>
45-
public static int Dimension = 0;
50+
public static int Dimension { get; set; } = 0;
4651

4752
/// <summary>
4853
/// Draws the screen.
4954
/// </summary>
5055
/// <param name="spBatch"></param>
51-
public static void DrawScreen(ref SpriteBatch spBatch)
56+
public static void DrawScreen(SpriteBatch spBatch)
5257
{
5358
if (World.Dimensions.Count > 0)
5459
{
5560
MapRenderer.DrawMap(ref spBatch, RenderingPipe.Dimension);
5661
}
57-
58-
DrawGUI(ref spBatch);
5962
}
6063

61-
public static void DrawMouseLocation(ref SpriteBatch spBatch)
64+
public static void DrawMouseLocation(SpriteBatch spBatch)
6265
{
6366
int x = Mouse.GetState().X;
6467
int y = Mouse.GetState().Y;
@@ -70,12 +73,12 @@ public static void DrawMouseLocation(ref SpriteBatch spBatch)
7073
/// Draws the GUI onto the screen.
7174
/// </summary>
7275
/// <param name="spBatch"></param>
73-
private static void DrawGUI(ref SpriteBatch spBatch)
76+
public static void DrawGUI(SpriteBatch spBatch)
7477
{
75-
DrawContainers(ref spBatch);
78+
DrawContainers(spBatch);
7679
}
7780

78-
private static void DrawContainers(ref SpriteBatch spBatch)
81+
private static void DrawContainers(SpriteBatch spBatch)
7982
{
8083
foreach (GUIContainer item in Enumerable.Reverse(BoundHandler.GUIWindows))
8184
{

0 commit comments

Comments
 (0)