Allow resizable screen

This commit is contained in:
Alessandro Proto 2023-02-03 22:59:20 +01:00
parent 15e700008e
commit bf6424c0d1
4 changed files with 47 additions and 21 deletions

View file

@ -33,7 +33,7 @@ public class Capy64 : Game, IGame
public float Scale { get; set; } = 2f;
public Drawing Drawing { get; private set; }
public LuaState LuaRuntime { get; set; }
public EventEmitter EventEmitter { get; private set; }
public Eventing.EventEmitter EventEmitter { get; private set; }
public DiscordIntegration Discord { get; set; }
public Borders Borders = new()
@ -71,11 +71,14 @@ public class Capy64 : Game, IGame
_serviceProvider = serviceProvider;
}
public void UpdateSize()
public void UpdateSize(bool resize = true)
{
if (resize)
{
_graphics.PreferredBackBufferWidth = (int)(Width * Scale) + Borders.Left + Borders.Right;
_graphics.PreferredBackBufferHeight = (int)(Height * Scale) + Borders.Top + Borders.Right;
_graphics.ApplyChanges();
}
renderTarget = new RenderTarget2D(
GraphicsDevice,
@ -96,17 +99,27 @@ public class Capy64 : Game, IGame
private void OnWindowSizeChange(object sender, EventArgs e)
{
var bounds = Window.ClientBounds;
Console.WriteLine(bounds);
if (Window.IsMaximized())
{
}
Width = (int)(bounds.Width / Scale);
Height = (int)(bounds.Height / Scale);
UpdateSize();
if (Window.IsMaximized())
{
var vertical = bounds.Height - Height * Scale;
var horizontal = bounds.Width - Width * Scale;
Borders.Top = (int)Math.Floor(vertical / 2d);
Borders.Bottom = (int)Math.Ceiling(vertical / 2d);
Borders.Left = (int)Math.Floor(horizontal / 2d);
Borders.Right = (int)Math.Ceiling(horizontal / 2d);
}
else
{
Borders = new Borders();
}
UpdateSize(false);
}
protected override void Initialize()
@ -115,7 +128,7 @@ public class Capy64 : Game, IGame
UpdateSize();
Window.AllowUserResizing = false;
Window.AllowUserResizing = true;
Window.ClientSizeChanged += OnWindowSizeChange;
NativePlugins = GetNativePlugins();

View file

@ -17,13 +17,13 @@ public interface IGame
GameWindow Window { get; }
Drawing Drawing { get; }
LuaState LuaRuntime { get; set; }
EventEmitter EventEmitter { get; }
Eventing.EventEmitter EventEmitter { get; }
void ConfigureServices(IServiceProvider serviceProvider);
int Width { get; set; }
int Height { get; set; }
float Scale { get; set; }
void UpdateSize();
void UpdateSize(bool resize = true);
event EventHandler<EventArgs> Exiting;
void Run();

View file

@ -56,6 +56,7 @@ internal class Term : IPlugin
UpdateSize();
_game.EventEmitter.OnOverlay += OnOverlay;
_game.EventEmitter.OnScreenSizeChange += OnScreenSizeChange;
}
private LuaRegister[] TermLib = new LuaRegister[]
@ -160,12 +161,16 @@ internal class Term : IPlugin
return 1;
}
public static void UpdateSize()
public static void UpdateSize(bool resize = true)
{
Array.Resize(ref CharGrid, Width * Height);
if (resize)
{
_game.Width = RealWidth;
_game.Height = RealHeight;
_game.UpdateSize();
CharGrid = new Char?[Width * Height];
}
}
public static Vector2 ToRealPos(Vector2 termPos)
@ -252,6 +257,14 @@ internal class Term : IPlugin
UpdateCursor();
}
private void OnScreenSizeChange(object sender, EventArgs e)
{
Width = _game.Width / CharWidth;
Height = _game.Height / CharHeight;
UpdateSize(false);
}
private static void UpdateCursor()
{
if (!enableCursor)

View file

@ -16,7 +16,7 @@ namespace Capy64.Runtime;
internal class RuntimeManager : IPlugin
{
private LuaState luaState;
private InputEmitter emitter;
private EventEmitter emitter;
private int step = 0;
private static bool close = false;