reformat
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DoomDeathmatch\DoomDeathmatch.csproj" />
|
||||
<ProjectReference Include="..\DoomDeathmatch\DoomDeathmatch.csproj"/>
|
||||
<ProjectReference Include="..\Engine\Engine.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<Application x:Class="PresenterWpf.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:PresenterWpf">
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
</Application>
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
using Engine;
|
||||
using Engine.Graphics;
|
||||
using Engine.Graphics.Texture;
|
||||
@@ -12,7 +13,7 @@ using Serilog.Events;
|
||||
namespace PresenterWpf;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
@@ -35,11 +36,11 @@ public partial class App : Application
|
||||
// Since engine claims current thread for rendering, we need to create a new thread to run WPF
|
||||
var thread = new Thread(() =>
|
||||
{
|
||||
var window = new MainWindow(engine);
|
||||
var window = new MainWindow();
|
||||
presenter.Presenter = window;
|
||||
inputHandler.InputHandler = new WpfInputHandler(window);
|
||||
window.Show();
|
||||
System.Windows.Threading.Dispatcher.Run();
|
||||
Dispatcher.Run();
|
||||
});
|
||||
|
||||
thread.SetApartmentState(ApartmentState.STA);
|
||||
@@ -56,50 +57,41 @@ public partial class App : Application
|
||||
|
||||
private class InputHandlerWrapper : IInputHandler
|
||||
{
|
||||
public CultureInfo CurrentInputLanguage => _inputHandler?.CurrentInputLanguage ?? new CultureInfo(1033);
|
||||
public IInputHandler? InputHandler { get; set; }
|
||||
|
||||
public Vector2 MousePosition => _inputHandler?.MousePosition ?? Vector2.Zero;
|
||||
public CultureInfo CurrentInputLanguage => InputHandler?.CurrentInputLanguage ?? new CultureInfo(1033);
|
||||
|
||||
private IInputHandler? _inputHandler;
|
||||
|
||||
public IInputHandler? InputHandler
|
||||
{
|
||||
get => _inputHandler;
|
||||
set
|
||||
{
|
||||
_inputHandler = value;
|
||||
}
|
||||
}
|
||||
public Vector2 MousePosition => InputHandler?.MousePosition ?? Vector2.Zero;
|
||||
|
||||
public bool IsKeyPressed(KeyboardButtonCode parKeyboardButtonCode)
|
||||
{
|
||||
return _inputHandler?.IsKeyPressed(parKeyboardButtonCode) ?? false;
|
||||
return InputHandler?.IsKeyPressed(parKeyboardButtonCode) ?? false;
|
||||
}
|
||||
|
||||
public bool IsKeyJustPressed(KeyboardButtonCode parKeyboardButtonCode)
|
||||
{
|
||||
return _inputHandler?.IsKeyJustPressed(parKeyboardButtonCode) ?? false;
|
||||
return InputHandler?.IsKeyJustPressed(parKeyboardButtonCode) ?? false;
|
||||
}
|
||||
|
||||
public bool IsMouseButtonPressed(MouseButtonCode parButtonCode)
|
||||
{
|
||||
return _inputHandler?.IsMouseButtonPressed(parButtonCode) ?? false;
|
||||
return InputHandler?.IsMouseButtonPressed(parButtonCode) ?? false;
|
||||
}
|
||||
|
||||
public bool IsMouseButtonJustPressed(MouseButtonCode parButtonCode)
|
||||
{
|
||||
return _inputHandler?.IsMouseButtonJustPressed(parButtonCode) ?? false;
|
||||
return InputHandler?.IsMouseButtonJustPressed(parButtonCode) ?? false;
|
||||
}
|
||||
|
||||
public void Update(double parDeltaTime)
|
||||
{
|
||||
_inputHandler?.Update(parDeltaTime);
|
||||
InputHandler?.Update(parDeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
private class PresenterWrapper : IPresenter
|
||||
{
|
||||
private IPresenter? _presenter;
|
||||
public event Action<ResizeEventArgs>? Resize;
|
||||
|
||||
public IPresenter? Presenter
|
||||
{
|
||||
@@ -120,10 +112,11 @@ public partial class App : Application
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsExiting => Presenter?.IsExiting ?? false;
|
||||
public int Width => Presenter?.Width ?? 0;
|
||||
public int Height => Presenter?.Height ?? 0;
|
||||
public event Action<ResizeEventArgs>? Resize;
|
||||
public bool IsExiting => Presenter?.IsExiting ?? false;
|
||||
|
||||
private IPresenter? _presenter;
|
||||
|
||||
public void Present(IConstTexture parTexture)
|
||||
{
|
||||
@@ -140,14 +133,14 @@ public partial class App : Application
|
||||
Presenter?.Render();
|
||||
}
|
||||
|
||||
private void PresenterResize(ResizeEventArgs e)
|
||||
{
|
||||
Resize?.Invoke(e);
|
||||
}
|
||||
|
||||
public void Exit()
|
||||
{
|
||||
Presenter?.Exit();
|
||||
}
|
||||
|
||||
private void PresenterResize(ResizeEventArgs e)
|
||||
{
|
||||
Resize?.Invoke(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:PresenterWpf"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800" Closing="MainWindow_OnClosing">
|
||||
<Grid>
|
||||
|
||||
@@ -12,23 +12,19 @@ namespace PresenterWpf;
|
||||
|
||||
public partial class MainWindow : Window, IPresenter
|
||||
{
|
||||
public bool IsExiting { get; private set; }
|
||||
public new int Width { get; private set; }
|
||||
public new int Height { get; private set; }
|
||||
|
||||
private bool _scheduledResize;
|
||||
|
||||
public event Action<ResizeEventArgs>? Resize;
|
||||
|
||||
private readonly Engine.Engine _engine;
|
||||
private Image<Rgb8>? _image;
|
||||
private WriteableBitmap? _bitmap;
|
||||
public new int Width { get; private set; }
|
||||
public new int Height { get; private set; }
|
||||
public bool IsExiting { get; private set; }
|
||||
|
||||
public MainWindow(Engine.Engine parEngine)
|
||||
private WriteableBitmap? _bitmap;
|
||||
private Image<Rgb8>? _image;
|
||||
private bool _scheduledResize;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_engine = parEngine;
|
||||
}
|
||||
|
||||
public void Update(double parDeltaTime)
|
||||
|
||||
@@ -9,9 +9,7 @@ namespace PresenterWpf;
|
||||
public class WpfInputHandler : IInputHandler
|
||||
{
|
||||
public CultureInfo CurrentInputLanguage { get; private set; }
|
||||
public Vector2 MousePosition => _mousePosition;
|
||||
|
||||
private readonly Window _window;
|
||||
public Vector2 MousePosition { get; private set; } = Vector2.Zero;
|
||||
|
||||
private readonly bool[] _actualKeys = new bool[(int)KeyboardButtonCode.TotalCount];
|
||||
private readonly bool[] _currentKeys = new bool[(int)KeyboardButtonCode.TotalCount];
|
||||
@@ -21,8 +19,7 @@ public class WpfInputHandler : IInputHandler
|
||||
private readonly bool[] _currentMouseButtons = new bool[(int)MouseButtonCode.TotalCount];
|
||||
private readonly bool[] _previousMouseButtons = new bool[(int)MouseButtonCode.TotalCount];
|
||||
|
||||
|
||||
private Vector2 _mousePosition = Vector2.Zero;
|
||||
private readonly Window _window;
|
||||
|
||||
public WpfInputHandler(Window parWindow)
|
||||
{
|
||||
@@ -35,6 +32,14 @@ public class WpfInputHandler : IInputHandler
|
||||
_window.MouseMove += Window_MouseMove;
|
||||
}
|
||||
|
||||
~WpfInputHandler()
|
||||
{
|
||||
_window.PreviewKeyDown -= Window_PreviewKeyDown;
|
||||
_window.PreviewKeyUp -= Window_PreviewKeyUp;
|
||||
_window.PreviewMouseDown -= Window_PreviewMouseDown;
|
||||
_window.PreviewMouseUp -= Window_PreviewMouseUp;
|
||||
}
|
||||
|
||||
public void Update(double parDeltaTime)
|
||||
{
|
||||
_window.Dispatcher.Invoke(() =>
|
||||
@@ -55,6 +60,30 @@ public class WpfInputHandler : IInputHandler
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsKeyPressed(KeyboardButtonCode parKeyboardButtonCode)
|
||||
{
|
||||
var keyCode = (int)parKeyboardButtonCode;
|
||||
return _currentKeys[keyCode];
|
||||
}
|
||||
|
||||
public bool IsKeyJustPressed(KeyboardButtonCode parKeyboardButtonCode)
|
||||
{
|
||||
var keyCode = (int)parKeyboardButtonCode;
|
||||
return _currentKeys[keyCode] && !_previousKeys[keyCode];
|
||||
}
|
||||
|
||||
public bool IsMouseButtonPressed(MouseButtonCode parButtonCode)
|
||||
{
|
||||
var buttonCode = (int)parButtonCode;
|
||||
return _currentMouseButtons[buttonCode];
|
||||
}
|
||||
|
||||
public bool IsMouseButtonJustPressed(MouseButtonCode parButtonCode)
|
||||
{
|
||||
var buttonCode = (int)parButtonCode;
|
||||
return _currentMouseButtons[buttonCode] && !_previousMouseButtons[buttonCode];
|
||||
}
|
||||
|
||||
private void Window_PreviewKeyDown(object parSender, KeyEventArgs parEventArgs)
|
||||
{
|
||||
var keyCode = ConvertToKeyboardButtonCode(parEventArgs.Key);
|
||||
@@ -94,109 +123,82 @@ public class WpfInputHandler : IInputHandler
|
||||
private void Window_MouseMove(object parSender, MouseEventArgs parEventArgs)
|
||||
{
|
||||
var position = parEventArgs.GetPosition(null);
|
||||
_mousePosition = new Vector2((float)position.X, (float)position.Y);
|
||||
MousePosition = new Vector2((float)position.X, (float)position.Y);
|
||||
}
|
||||
|
||||
public bool IsKeyPressed(KeyboardButtonCode parKeyboardButtonCode)
|
||||
private static int ConvertToKeyboardButtonCode(Key parKey)
|
||||
{
|
||||
var keyCode = (int)parKeyboardButtonCode;
|
||||
return _currentKeys[keyCode];
|
||||
return parKey switch
|
||||
{
|
||||
Key.A => (int)KeyboardButtonCode.A,
|
||||
Key.B => (int)KeyboardButtonCode.B,
|
||||
Key.C => (int)KeyboardButtonCode.C,
|
||||
Key.D => (int)KeyboardButtonCode.D,
|
||||
Key.E => (int)KeyboardButtonCode.E,
|
||||
Key.F => (int)KeyboardButtonCode.F,
|
||||
Key.G => (int)KeyboardButtonCode.G,
|
||||
Key.H => (int)KeyboardButtonCode.H,
|
||||
Key.I => (int)KeyboardButtonCode.I,
|
||||
Key.J => (int)KeyboardButtonCode.J,
|
||||
Key.K => (int)KeyboardButtonCode.K,
|
||||
Key.L => (int)KeyboardButtonCode.L,
|
||||
Key.M => (int)KeyboardButtonCode.M,
|
||||
Key.N => (int)KeyboardButtonCode.N,
|
||||
Key.O => (int)KeyboardButtonCode.O,
|
||||
Key.P => (int)KeyboardButtonCode.P,
|
||||
Key.Q => (int)KeyboardButtonCode.Q,
|
||||
Key.R => (int)KeyboardButtonCode.R,
|
||||
Key.S => (int)KeyboardButtonCode.S,
|
||||
Key.T => (int)KeyboardButtonCode.T,
|
||||
Key.U => (int)KeyboardButtonCode.U,
|
||||
Key.V => (int)KeyboardButtonCode.V,
|
||||
Key.W => (int)KeyboardButtonCode.W,
|
||||
Key.X => (int)KeyboardButtonCode.X,
|
||||
Key.Y => (int)KeyboardButtonCode.Y,
|
||||
Key.Z => (int)KeyboardButtonCode.Z,
|
||||
Key.LeftCtrl => (int)KeyboardButtonCode.Ctrl,
|
||||
Key.LeftAlt => (int)KeyboardButtonCode.Alt,
|
||||
Key.LeftShift => (int)KeyboardButtonCode.Shift,
|
||||
Key.RightCtrl => (int)KeyboardButtonCode.Ctrl,
|
||||
Key.RightAlt => (int)KeyboardButtonCode.Alt,
|
||||
Key.RightShift => (int)KeyboardButtonCode.Shift,
|
||||
Key.Space => (int)KeyboardButtonCode.Space,
|
||||
Key.Tab => (int)KeyboardButtonCode.Tab,
|
||||
Key.Back => (int)KeyboardButtonCode.Backspace,
|
||||
Key.Return => (int)KeyboardButtonCode.Enter,
|
||||
Key.Escape => (int)KeyboardButtonCode.Escape,
|
||||
Key.PageUp => (int)KeyboardButtonCode.PageUp,
|
||||
Key.PageDown => (int)KeyboardButtonCode.PageDown,
|
||||
Key.Up => (int)KeyboardButtonCode.Up,
|
||||
Key.Down => (int)KeyboardButtonCode.Down,
|
||||
Key.Left => (int)KeyboardButtonCode.Left,
|
||||
Key.Right => (int)KeyboardButtonCode.Right,
|
||||
Key.Delete => (int)KeyboardButtonCode.Delete,
|
||||
Key.Insert => (int)KeyboardButtonCode.Insert,
|
||||
Key.Home => (int)KeyboardButtonCode.Home,
|
||||
Key.End => (int)KeyboardButtonCode.End,
|
||||
Key.D0 => (int)KeyboardButtonCode.D0,
|
||||
Key.D1 => (int)KeyboardButtonCode.D1,
|
||||
Key.D2 => (int)KeyboardButtonCode.D2,
|
||||
Key.D3 => (int)KeyboardButtonCode.D3,
|
||||
Key.D4 => (int)KeyboardButtonCode.D4,
|
||||
Key.D5 => (int)KeyboardButtonCode.D5,
|
||||
Key.D6 => (int)KeyboardButtonCode.D6,
|
||||
Key.D7 => (int)KeyboardButtonCode.D7,
|
||||
Key.D8 => (int)KeyboardButtonCode.D8,
|
||||
Key.D9 => (int)KeyboardButtonCode.D9,
|
||||
_ => -1
|
||||
};
|
||||
}
|
||||
|
||||
public bool IsKeyJustPressed(KeyboardButtonCode parKeyboardButtonCode)
|
||||
private static int ConvertToMouseButtonCode(MouseButton parButton)
|
||||
{
|
||||
var keyCode = (int)parKeyboardButtonCode;
|
||||
return _currentKeys[keyCode] && !_previousKeys[keyCode];
|
||||
}
|
||||
|
||||
public bool IsMouseButtonPressed(MouseButtonCode parButtonCode)
|
||||
{
|
||||
var buttonCode = (int)parButtonCode;
|
||||
return _currentMouseButtons[buttonCode];
|
||||
}
|
||||
|
||||
public bool IsMouseButtonJustPressed(MouseButtonCode parButtonCode)
|
||||
{
|
||||
var buttonCode = (int)parButtonCode;
|
||||
return _currentMouseButtons[buttonCode] && !_previousMouseButtons[buttonCode];
|
||||
}
|
||||
|
||||
private static int ConvertToKeyboardButtonCode(Key parKey) => parKey switch
|
||||
{
|
||||
Key.A => (int)KeyboardButtonCode.A,
|
||||
Key.B => (int)KeyboardButtonCode.B,
|
||||
Key.C => (int)KeyboardButtonCode.C,
|
||||
Key.D => (int)KeyboardButtonCode.D,
|
||||
Key.E => (int)KeyboardButtonCode.E,
|
||||
Key.F => (int)KeyboardButtonCode.F,
|
||||
Key.G => (int)KeyboardButtonCode.G,
|
||||
Key.H => (int)KeyboardButtonCode.H,
|
||||
Key.I => (int)KeyboardButtonCode.I,
|
||||
Key.J => (int)KeyboardButtonCode.J,
|
||||
Key.K => (int)KeyboardButtonCode.K,
|
||||
Key.L => (int)KeyboardButtonCode.L,
|
||||
Key.M => (int)KeyboardButtonCode.M,
|
||||
Key.N => (int)KeyboardButtonCode.N,
|
||||
Key.O => (int)KeyboardButtonCode.O,
|
||||
Key.P => (int)KeyboardButtonCode.P,
|
||||
Key.Q => (int)KeyboardButtonCode.Q,
|
||||
Key.R => (int)KeyboardButtonCode.R,
|
||||
Key.S => (int)KeyboardButtonCode.S,
|
||||
Key.T => (int)KeyboardButtonCode.T,
|
||||
Key.U => (int)KeyboardButtonCode.U,
|
||||
Key.V => (int)KeyboardButtonCode.V,
|
||||
Key.W => (int)KeyboardButtonCode.W,
|
||||
Key.X => (int)KeyboardButtonCode.X,
|
||||
Key.Y => (int)KeyboardButtonCode.Y,
|
||||
Key.Z => (int)KeyboardButtonCode.Z,
|
||||
Key.LeftCtrl => (int)KeyboardButtonCode.Ctrl,
|
||||
Key.LeftAlt => (int)KeyboardButtonCode.Alt,
|
||||
Key.LeftShift => (int)KeyboardButtonCode.Shift,
|
||||
Key.RightCtrl => (int)KeyboardButtonCode.Ctrl,
|
||||
Key.RightAlt => (int)KeyboardButtonCode.Alt,
|
||||
Key.RightShift => (int)KeyboardButtonCode.Shift,
|
||||
Key.Space => (int)KeyboardButtonCode.Space,
|
||||
Key.Tab => (int)KeyboardButtonCode.Tab,
|
||||
Key.Back => (int)KeyboardButtonCode.Backspace,
|
||||
Key.Return => (int)KeyboardButtonCode.Enter,
|
||||
Key.Escape => (int)KeyboardButtonCode.Escape,
|
||||
Key.PageUp => (int)KeyboardButtonCode.PageUp,
|
||||
Key.PageDown => (int)KeyboardButtonCode.PageDown,
|
||||
Key.Up => (int)KeyboardButtonCode.Up,
|
||||
Key.Down => (int)KeyboardButtonCode.Down,
|
||||
Key.Left => (int)KeyboardButtonCode.Left,
|
||||
Key.Right => (int)KeyboardButtonCode.Right,
|
||||
Key.Delete => (int)KeyboardButtonCode.Delete,
|
||||
Key.Insert => (int)KeyboardButtonCode.Insert,
|
||||
Key.Home => (int)KeyboardButtonCode.Home,
|
||||
Key.End => (int)KeyboardButtonCode.End,
|
||||
Key.D0 => (int)KeyboardButtonCode.D0,
|
||||
Key.D1 => (int)KeyboardButtonCode.D1,
|
||||
Key.D2 => (int)KeyboardButtonCode.D2,
|
||||
Key.D3 => (int)KeyboardButtonCode.D3,
|
||||
Key.D4 => (int)KeyboardButtonCode.D4,
|
||||
Key.D5 => (int)KeyboardButtonCode.D5,
|
||||
Key.D6 => (int)KeyboardButtonCode.D6,
|
||||
Key.D7 => (int)KeyboardButtonCode.D7,
|
||||
Key.D8 => (int)KeyboardButtonCode.D8,
|
||||
Key.D9 => (int)KeyboardButtonCode.D9,
|
||||
_ => -1
|
||||
};
|
||||
|
||||
private static int ConvertToMouseButtonCode(MouseButton parButton) =>
|
||||
parButton switch
|
||||
return parButton switch
|
||||
{
|
||||
MouseButton.Left => (int)MouseButtonCode.Left,
|
||||
MouseButton.Right => (int)MouseButtonCode.Right,
|
||||
MouseButton.Middle => (int)MouseButtonCode.Middle,
|
||||
_ => -1
|
||||
};
|
||||
|
||||
~WpfInputHandler()
|
||||
{
|
||||
_window.PreviewKeyDown -= Window_PreviewKeyDown;
|
||||
_window.PreviewKeyUp -= Window_PreviewKeyUp;
|
||||
_window.PreviewMouseDown -= Window_PreviewMouseDown;
|
||||
_window.PreviewMouseUp -= Window_PreviewMouseUp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user