Changed how cursor behaves, added Overlay event

This commit is contained in:
Alessandro Proto 2023-01-22 21:11:14 +01:00
parent 3a7914ee6d
commit c0fe129729
5 changed files with 54 additions and 38 deletions

View file

@ -23,7 +23,7 @@ public class Capy64 : Game, IGame
Environment.SpecialFolder.ApplicationData, Environment.SpecialFolder.ApplicationData,
Environment.SpecialFolderOption.Create), Environment.SpecialFolderOption.Create),
"Capy64"); "Capy64");
public Game Game => this; public Capy64 Game => this;
public IList<IPlugin> NativePlugins { get; private set; } public IList<IPlugin> NativePlugins { get; private set; }
public IList<IPlugin> Plugins { get; private set; } public IList<IPlugin> Plugins { get; private set; }
public int Width { get; set; } = 400; public int Width { get; set; } = 400;
@ -39,11 +39,12 @@ public class Capy64 : Game, IGame
Left = 0, Left = 0,
Right = 0, Right = 0,
}; };
public SpriteBatch SpriteBatch;
private readonly InputManager _inputManager; private readonly InputManager _inputManager;
private RenderTarget2D renderTarget; private RenderTarget2D renderTarget;
private readonly GraphicsDeviceManager _graphics; private readonly GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private IServiceProvider _serviceProvider; private IServiceProvider _serviceProvider;
private ulong _totalTicks = 0; private ulong _totalTicks = 0;
@ -139,36 +140,41 @@ public class Capy64 : Game, IGame
protected override void LoadContent() protected override void LoadContent()
{ {
_spriteBatch = new SpriteBatch(GraphicsDevice); SpriteBatch = new SpriteBatch(GraphicsDevice);
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
{ {
Drawing.Begin(); Drawing.Begin();
// Register user input
_inputManager.Update(IsActive);
EventEmitter.RaiseTick(new() EventEmitter.RaiseTick(new()
{ {
GameTime = gameTime, GameTime = gameTime,
TotalTicks = _totalTicks TotalTicks = _totalTicks
}); });
// resume here
Drawing.End(); Drawing.End();
// Register user input _totalTicks++;
_inputManager.Update(IsActive);
base.Update(gameTime); base.Update(gameTime);
_totalTicks++;
} }
protected override void Draw(GameTime gameTime) protected override void Draw(GameTime gameTime)
{ {
_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp); SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp);
//GraphicsDevice.Clear(Color.Blue); SpriteBatch.Draw(renderTarget, new(Borders.Left, Borders.Top), null, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
_spriteBatch.Draw(renderTarget, new(Borders.Left, Borders.Top), null, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
_spriteBatch.End(); EventEmitter.RaiseOverlay(new()
{
GameTime = gameTime,
TotalTicks = _totalTicks,
});
SpriteBatch.End();
base.Draw(gameTime); base.Draw(gameTime);
} }

View file

@ -22,6 +22,7 @@ public class EventEmitter
public event EventHandler<TickEvent> OnTick; public event EventHandler<TickEvent> OnTick;
public event EventHandler OnInit; public event EventHandler OnInit;
public event EventHandler OnScreenSizeChange; public event EventHandler OnScreenSizeChange;
public event EventHandler<OverlayEvent> OnOverlay;
public void RaiseMouseMove(MouseMoveEvent ev) public void RaiseMouseMove(MouseMoveEvent ev)
@ -106,4 +107,11 @@ public class EventEmitter
OnScreenSizeChange(this, EventArgs.Empty); OnScreenSizeChange(this, EventArgs.Empty);
} }
} }
public void RaiseOverlay(OverlayEvent ev)
{
if(OnOverlay is not null) {
OnOverlay(this, ev);
}
}
} }

View file

@ -0,0 +1,11 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace Capy64.Eventing.Events;
public class OverlayEvent : EventArgs
{
public GameTime GameTime { get; set; }
public ulong TotalTicks { get; set; }
}

View file

@ -10,8 +10,7 @@ namespace Capy64;
public interface IGame public interface IGame
{ {
Capy64 Game { get; }
Game Game { get; }
IList<IPlugin> NativePlugins { get; } IList<IPlugin> NativePlugins { get; }
IList<IPlugin> Plugins { get; } IList<IPlugin> Plugins { get; }
GameWindow Window { get; } GameWindow Window { get; }

View file

@ -2,7 +2,11 @@
using Capy64.Eventing.Events; using Capy64.Eventing.Events;
using KeraLua; using KeraLua;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using System; using System;
using static Capy64.Utils;
using static System.Formats.Asn1.AsnWriter;
namespace Capy64.LuaRuntime.Libraries; namespace Capy64.LuaRuntime.Libraries;
@ -34,6 +38,7 @@ internal class Term : IPlugin
private static IGame _game; private static IGame _game;
private static bool cursorState = false; private static bool cursorState = false;
private static bool enableCursor = true; private static bool enableCursor = true;
private static Texture2D cursorTexture;
public Term(IGame game) public Term(IGame game)
{ {
_game = game; _game = game;
@ -42,9 +47,14 @@ internal class Term : IPlugin
ForegroundColor = Color.White; ForegroundColor = Color.White;
BackgroundColor = Color.Black; BackgroundColor = Color.Black;
cursorTexture = new(_game.Game.GraphicsDevice, 1, CharHeight);
var textureData = new Color[CharHeight];
Array.Fill(textureData, Color.White);
cursorTexture.SetData(textureData);
UpdateSize(); UpdateSize();
_game.EventEmitter.OnTick += OnTick; _game.EventEmitter.OnOverlay += OnOverlay;
} }
private LuaRegister[] TermLib = new LuaRegister[] private LuaRegister[] TermLib = new LuaRegister[]
@ -227,13 +237,13 @@ internal class Term : IPlugin
Console.WriteLine("|\n \\{0}", new string('-', Width)); Console.WriteLine("|\n \\{0}", new string('-', Width));
} }
private void OnTick(object sender, TickEvent e) private void OnOverlay(object sender, OverlayEvent e)
{ {
if (((int)e.TotalTicks % CursorDelay) == 0) if (((int)e.TotalTicks % CursorDelay) == 0)
{ {
cursorState = !cursorState; cursorState = !cursorState;
UpdateCursor();
} }
UpdateCursor();
} }
private static void UpdateCursor() private static void UpdateCursor()
@ -244,28 +254,12 @@ internal class Term : IPlugin
if (_cursorPosition.X < 0 || _cursorPosition.Y < 0 || _cursorPosition.X >= Width || _cursorPosition.Y >= Height) if (_cursorPosition.X < 0 || _cursorPosition.Y < 0 || _cursorPosition.X >= Width || _cursorPosition.Y >= Height)
return; return;
var ch = CharGrid[(int)_cursorPosition.X + ((int)_cursorPosition.Y * Width)] ??
new Char
{
Character = ' ',
Foreground = ForegroundColor,
Background = BackgroundColor,
};
Color fg, bg;
if (cursorState) if (cursorState)
{ {
fg = ch.Background; var realpos = ToRealPos(CursorPosition - Vector2.One);
bg = ch.Foreground; var charpos = realpos * _game.Scale + CharOffset;
_game.Game.SpriteBatch.Draw(cursorTexture, charpos, null, ForegroundColor, 0f, Vector2.Zero, _game.Scale, SpriteEffects.None, 0);
} }
else
{
fg = ch.Foreground;
bg = ch.Background;
}
PlotChar(_cursorPosition, ch.Character, fg, bg, false);
} }
private static void ClearGrid() private static void ClearGrid()
@ -297,10 +291,8 @@ internal class Term : IPlugin
public static void SetCursorPosition(int x, int y) public static void SetCursorPosition(int x, int y)
{ {
RedrawPos(_cursorPosition);
cursorState = true; cursorState = true;
_cursorPosition = new(x - 1, y - 1); _cursorPosition = new(x - 1, y - 1);
UpdateCursor();
} }
private static int L_GetPos(IntPtr state) private static int L_GetPos(IntPtr state)