This commit is contained in:
2025-01-02 05:15:16 +03:00
parent 3c66a65b40
commit ac00eb18a9
96 changed files with 384134 additions and 230 deletions

View File

@@ -1,10 +1,12 @@
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;
using Engine;
using Engine.Graphics;
using Engine.Graphics.Texture;
using Engine.Input;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using Serilog.Events;
@@ -27,6 +29,7 @@ public partial class App : Application
.LogToConsole()
.LogToFile(true, "log.txt")
.LogLevel(LogEventLevel.Debug)
.AssetFolder(Path.GetFullPath("../DoomDeathmatch/asset"))
.Build();
// Since engine claims current thread for rendering, we need to create a new thread to run WPF
@@ -53,6 +56,8 @@ public partial class App : Application
private class InputHandlerWrapper : IInputHandler
{
public Vector2 MousePosition => _inputHandler?.MousePosition ?? Vector2.Zero;
private IInputHandler? _inputHandler;
public IInputHandler? InputHandler
@@ -137,5 +142,10 @@ public partial class App : Application
{
Resize?.Invoke(e);
}
public void Exit()
{
Presenter?.Exit();
}
}
}

View File

@@ -74,6 +74,11 @@ public partial class MainWindow : Window, IPresenter
});
}
public void Exit()
{
Dispatcher.Invoke(Close);
}
private void DrawImage(Image<Rgb8> parImage)
{
try

View File

@@ -1,18 +1,27 @@
using System.Windows;
using System.Windows.Input;
using Engine.Input;
using OpenTK.Mathematics;
namespace PresenterWpf;
public class WpfInputHandler : IInputHandler
{
public Vector2 MousePosition => _mousePosition;
private readonly Window _window;
private readonly bool[] _actualKeys = new bool[(int)KeyboardButtonCode.TotalCount];
private readonly bool[] _currentKeys = new bool[(int)KeyboardButtonCode.TotalCount];
private readonly bool[] _previousKeys = new bool[(int)KeyboardButtonCode.TotalCount];
private readonly bool[] _actualMouseButtons = new bool[(int)MouseButtonCode.TotalCount];
private readonly bool[] _currentMouseButtons = new bool[(int)MouseButtonCode.TotalCount];
private readonly bool[] _previousMouseButtons = new bool[(int)MouseButtonCode.TotalCount];
private Vector2 _mousePosition = Vector2.Zero;
public WpfInputHandler(Window parWindow)
{
_window = parWindow;
@@ -21,6 +30,7 @@ public class WpfInputHandler : IInputHandler
_window.PreviewKeyUp += Window_PreviewKeyUp;
_window.PreviewMouseDown += Window_PreviewMouseDown;
_window.PreviewMouseUp += Window_PreviewMouseUp;
_window.MouseMove += Window_MouseMove;
}
public void Update(double parDeltaTime)
@@ -28,50 +38,58 @@ public class WpfInputHandler : IInputHandler
for (var i = 0; i < _currentKeys.Length; i++)
{
_previousKeys[i] = _currentKeys[i];
_currentKeys[i] = _actualKeys[i];
}
for (var i = 0; i < _currentMouseButtons.Length; i++)
{
_previousMouseButtons[i] = _currentMouseButtons[i];
_currentMouseButtons[i] = _actualMouseButtons[i];
}
}
private void Window_PreviewKeyDown(object parSender, KeyEventArgs parEventArgs)
{
var keyCode = ConvertToKeyboardButtonCode(parEventArgs.Key);
if (keyCode >= 0 && keyCode < _currentKeys.Length)
if (keyCode >= 0 && keyCode < _actualKeys.Length)
{
_currentKeys[keyCode] = true;
_actualKeys[keyCode] = true;
}
}
private void Window_PreviewKeyUp(object parSender, KeyEventArgs parEventArgs)
{
var keyCode = ConvertToKeyboardButtonCode(parEventArgs.Key);
if (keyCode >= 0 && keyCode < _currentKeys.Length)
if (keyCode >= 0 && keyCode < _actualKeys.Length)
{
_currentKeys[keyCode] = false;
_actualKeys[keyCode] = false;
}
}
private void Window_PreviewMouseDown(object parSender, MouseButtonEventArgs parEventArgs)
{
var buttonCode = ConvertToMouseButtonCode(parEventArgs.ChangedButton);
if (buttonCode >= 0 && buttonCode < _currentMouseButtons.Length)
if (buttonCode >= 0 && buttonCode < _actualMouseButtons.Length)
{
_currentMouseButtons[buttonCode] = true;
_actualMouseButtons[buttonCode] = true;
}
}
private void Window_PreviewMouseUp(object parSender, MouseButtonEventArgs parEventArgs)
{
var buttonCode = ConvertToMouseButtonCode(parEventArgs.ChangedButton);
if (buttonCode >= 0 && buttonCode < _currentMouseButtons.Length)
if (buttonCode >= 0 && buttonCode < _actualMouseButtons.Length)
{
_currentMouseButtons[buttonCode] = false;
_actualMouseButtons[buttonCode] = false;
}
}
private void Window_MouseMove(object parSender, MouseEventArgs parEventArgs)
{
var position = parEventArgs.GetPosition(null);
_mousePosition = new Vector2((float)position.X, (float)position.Y);
}
public bool IsKeyPressed(KeyboardButtonCode parKeyboardButtonCode)
{
var keyCode = (int)parKeyboardButtonCode;