WindowViewportAdapter.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace MonoGame.Extended.ViewportAdapters
  5. {
  6. public class WindowViewportAdapter : ViewportAdapter
  7. {
  8. protected readonly GameWindow Window;
  9. public WindowViewportAdapter(GameWindow window, GraphicsDevice graphicsDevice)
  10. : base(graphicsDevice)
  11. {
  12. Window = window;
  13. window.ClientSizeChanged += OnClientSizeChanged;
  14. }
  15. public override int ViewportWidth => Window.ClientBounds.Width;
  16. public override int ViewportHeight => Window.ClientBounds.Height;
  17. public override int VirtualWidth => Window.ClientBounds.Width;
  18. public override int VirtualHeight => Window.ClientBounds.Height;
  19. public override Matrix GetScaleMatrix()
  20. {
  21. return Matrix.Identity;
  22. }
  23. private void OnClientSizeChanged(object sender, EventArgs eventArgs)
  24. {
  25. var x = Window.ClientBounds.Width;
  26. var y = Window.ClientBounds.Height;
  27. GraphicsDevice.Viewport = new Viewport(0, 0, x, y);
  28. }
  29. }
  30. }