mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 03:56:31 +00:00
inject the game window to the boxing viewport adapter and remove the dependency on calling an method manually when client size changes
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Tests
|
||||
{
|
||||
public class MockGameWindow : GameWindow
|
||||
{
|
||||
public void RaiseOnClientSizeChangedEvent()
|
||||
{
|
||||
OnClientSizeChanged();
|
||||
}
|
||||
|
||||
public override void BeginScreenDeviceChange(bool willBeFullScreen)
|
||||
{
|
||||
}
|
||||
|
||||
public override void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void SetSupportedOrientations(DisplayOrientation orientations)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void SetTitle(string title)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool AllowUserResizing { get; set; }
|
||||
public override Rectangle ClientBounds { get; }
|
||||
public override Point Position { get; set; }
|
||||
public override DisplayOrientation CurrentOrientation { get; }
|
||||
public override IntPtr Handle { get; }
|
||||
public override string ScreenDeviceName { get; }
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="MockGameWindow.cs" />
|
||||
<Compile Include="Shapes\CircleTests.cs" />
|
||||
<Compile Include="Sprites\SpriteTests.cs" />
|
||||
<Compile Include="TextureAtlases\TextureAtlasTests.cs" />
|
||||
|
||||
@@ -10,11 +10,12 @@ namespace MonoGame.Extended.Tests.ViewportAdapters
|
||||
[Test]
|
||||
public void BoxingViewportAdapter_Letterbox_Test()
|
||||
{
|
||||
var gameWindow = new MockGameWindow();
|
||||
var graphicsDevice = TestHelper.CreateGraphicsDevice();
|
||||
var viewportAdapter = new BoxingViewportAdapter(graphicsDevice, 800, 480);
|
||||
var viewportAdapter = new BoxingViewportAdapter(gameWindow, graphicsDevice, 800, 480);
|
||||
|
||||
graphicsDevice.Viewport = new Viewport(0, 0, 1024, 768);
|
||||
viewportAdapter.OnClientSizeChanged();
|
||||
gameWindow.RaiseOnClientSizeChangedEvent();
|
||||
|
||||
Assert.AreEqual(1024, graphicsDevice.Viewport.Width);
|
||||
Assert.AreEqual(614, graphicsDevice.Viewport.Height);
|
||||
@@ -24,11 +25,12 @@ namespace MonoGame.Extended.Tests.ViewportAdapters
|
||||
[Test]
|
||||
public void BoxingViewportAdapter_Pillarbox_Test()
|
||||
{
|
||||
var gameWindow = new MockGameWindow();
|
||||
var graphicsDevice = TestHelper.CreateGraphicsDevice();
|
||||
var viewportAdapter = new BoxingViewportAdapter(graphicsDevice, 800, 480);
|
||||
var viewportAdapter = new BoxingViewportAdapter(gameWindow, graphicsDevice, 800, 480);
|
||||
|
||||
graphicsDevice.Viewport = new Viewport(0, 0, 900, 500);
|
||||
viewportAdapter.OnClientSizeChanged();
|
||||
gameWindow.RaiseOnClientSizeChangedEvent();
|
||||
|
||||
Assert.AreEqual(833, graphicsDevice.Viewport.Width);
|
||||
Assert.AreEqual(500, graphicsDevice.Viewport.Height);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
@@ -11,14 +12,15 @@ namespace MonoGame.Extended.ViewportAdapters
|
||||
|
||||
public class BoxingViewportAdapter : ScalingViewportAdapter
|
||||
{
|
||||
public BoxingViewportAdapter(GraphicsDevice graphicsDevice, int virtualWidth, int virtualHeight)
|
||||
public BoxingViewportAdapter(GameWindow window, GraphicsDevice graphicsDevice, int virtualWidth, int virtualHeight)
|
||||
: base(graphicsDevice, virtualWidth, virtualHeight)
|
||||
{
|
||||
window.ClientSizeChanged += OnClientSizeChanged;
|
||||
}
|
||||
|
||||
public BoxingMode BoxingMode { get; private set; }
|
||||
|
||||
public override void OnClientSizeChanged()
|
||||
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
|
||||
{
|
||||
var viewport = GraphicsDevice.Viewport;
|
||||
var aspectRatio = (float) VirtualWidth / VirtualHeight;
|
||||
|
||||
@@ -33,10 +33,6 @@ namespace MonoGame.Extended.ViewportAdapters
|
||||
get { return _graphicsDevice.Viewport.Height; }
|
||||
}
|
||||
|
||||
public override void OnClientSizeChanged()
|
||||
{
|
||||
}
|
||||
|
||||
public override Matrix GetScaleMatrix()
|
||||
{
|
||||
return Matrix.Identity;
|
||||
|
||||
@@ -37,10 +37,6 @@ namespace MonoGame.Extended.ViewportAdapters
|
||||
get { return GraphicsDevice.Viewport.Height; }
|
||||
}
|
||||
|
||||
public override void OnClientSizeChanged()
|
||||
{
|
||||
}
|
||||
|
||||
public override Matrix GetScaleMatrix()
|
||||
{
|
||||
var scaleX = (float)ViewportWidth / VirtualWidth;
|
||||
|
||||
@@ -13,7 +13,6 @@ namespace MonoGame.Extended.ViewportAdapters
|
||||
public abstract int VirtualHeight { get; }
|
||||
public abstract int ViewportWidth { get; }
|
||||
public abstract int ViewportHeight { get; }
|
||||
public abstract void OnClientSizeChanged();
|
||||
public abstract Matrix GetScaleMatrix();
|
||||
|
||||
public Point PointToScreen(Point point)
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace Sandbox
|
||||
protected override void Initialize()
|
||||
{
|
||||
_fpsCounter = new FramesPerSecondCounter();
|
||||
_viewportAdapter = new BoxingViewportAdapter(GraphicsDevice, 800, 480);
|
||||
_viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 480);
|
||||
_camera = new Camera2D(_viewportAdapter)
|
||||
{
|
||||
MinimumZoom = 0.1f,
|
||||
@@ -48,7 +48,6 @@ namespace Sandbox
|
||||
};
|
||||
|
||||
Window.AllowUserResizing = true;
|
||||
Window.ClientSizeChanged += (s, e) => _viewportAdapter.OnClientSizeChanged();
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user