Skip to content

Commit

Permalink
Add input manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Aminator committed Aug 5, 2019
1 parent 113bfbd commit 0873a62
Show file tree
Hide file tree
Showing 80 changed files with 2,093 additions and 389 deletions.
38 changes: 14 additions & 24 deletions DirectX12Game/CameraController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Numerics;
using DirectX12GameEngine.Engine;
using DirectX12GameEngine.Games;
using DirectX12GameEngine.Input;

namespace DirectX12Game
{
Expand All @@ -15,26 +15,16 @@ public override void Start()
{
SceneSystem.CurrentCamera = Camera;

#if WINDOWS_UWP
if (Game.Context is CoreWindowGameContext context)
if (Input.Keyboard != null)
{
context.Control.PointerPressed += (s, e) => OnPointerPressed(e.CurrentPoint);
context.Control.PointerWheelChanged += (s, e) => OnPointerWheelChanged(e.CurrentPoint);
context.Control.KeyDown += (s, e) => OnKeyDown(e.VirtualKey);
Input.Keyboard.KeyDown += (s, e) => OnKeyDown(e.Key);
}
else if (Game.Context is XamlGameContext xamlContext)
{
xamlContext.Control.PointerPressed += (s, e) => OnPointerPressed(e.GetCurrentPoint(xamlContext.Control));
xamlContext.Control.PointerWheelChanged += (s, e) => OnPointerWheelChanged(e.GetCurrentPoint(xamlContext.Control));
xamlContext.Control.KeyDown += (s, e) => OnKeyDown(e.Key);
}
#endif
#if NETCOREAPP
if (Game.Context is WinFormsGameContext winFormsContext)

if (Input.Pointer != null)
{
winFormsContext.Control.KeyDown += (s, e) => OnKeyDown((Windows.System.VirtualKey)e.KeyCode);
Input.Pointer.PointerPressed += (s, e) => OnPointerPressed(e.CurrentPoint);
Input.Pointer.PointerWheelChanged += (s, e) => OnPointerWheelChanged(e.CurrentPoint);
}
#endif
}

public override void Update()
Expand All @@ -51,7 +41,7 @@ private void MoveCamera(float value)
}
}

private void OnPointerPressed(Windows.UI.Input.PointerPoint pointerPoint)
private void OnPointerPressed(PointerPoint pointerPoint)
{
if (pointerPoint.Properties.IsLeftButtonPressed)
{
Expand All @@ -63,25 +53,25 @@ private void OnPointerPressed(Windows.UI.Input.PointerPoint pointerPoint)
}
}

private void OnPointerWheelChanged(Windows.UI.Input.PointerPoint pointerPoint)
private void OnPointerWheelChanged(PointerPoint pointerPoint)
{
MoveCamera(-pointerPoint.Properties.MouseWheelDelta * scrollSpeed);
}

private void OnKeyDown(Windows.System.VirtualKey key)
private void OnKeyDown(VirtualKey key)
{
switch (key)
{
case Windows.System.VirtualKey.Left:
case VirtualKey.Left:
if (Camera?.Entity != null) Camera.Entity.Transform.Rotation *= Quaternion.CreateFromAxisAngle(Vector3.UnitY, (float)(10 * Math.PI / 180.0f));
break;
case Windows.System.VirtualKey.Right:
case VirtualKey.Right:
if (Camera?.Entity != null) Camera.Entity.Transform.Rotation *= Quaternion.CreateFromAxisAngle(Vector3.UnitY, (float)(-10 * Math.PI / 180.0f));
break;
case Windows.System.VirtualKey.Up:
case VirtualKey.Up:
MoveCamera(-10.0f);
break;
case Windows.System.VirtualKey.Down:
case VirtualKey.Down:
MoveCamera(10.0f);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion DirectX12Game/DirectX12Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<DefineConstants>WINDOWS_UWP</DefineConstants>
</PropertyGroup>
Expand Down
88 changes: 28 additions & 60 deletions DirectX12Game/TestScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,93 +8,61 @@
using DirectX12GameEngine.Rendering;
using DirectX12GameEngine.Rendering.Materials;
using Microsoft.Extensions.DependencyInjection;
using DirectX12GameEngine.Input;

#nullable enable

namespace DirectX12Game
{
public class TestScript : StartupScript
{
public string NullProperty { get; set; }
public string? NullProperty { get; set; }

public ColorChannel MyColorChannel { get; set; } = ColorChannel.G;

public ColorChannel MyOtherColorChannel { get; set; } = ColorChannel.B;

public DateTimeOffset? MyDateTime { get; set; }

public List<string> MyStringList { get; } = new List<string> { "One", "Two", "Three", "Four" };

public List<Vector3> MyVectorList { get; } = new List<Vector3> { new Vector3(4, 3, 2), new Vector3(34, 2, 9) };

public override void Start()
{
#if WINDOWS_UWP
if (Game.Context is CoreWindowGameContext context)
{
context.Control.KeyDown += Control_KeyDown;
}
else if (Game.Context is XamlGameContext xamlContext)
{
xamlContext.Control.KeyDown += Control_KeyDown1;
}
#endif
#if NETCOREAPP
if (Game.Context is WinFormsGameContext winFormsContext)
if (Input.Keyboard != null)
{
winFormsContext.Control.KeyDown += Control_KeyDown2;
Input.Keyboard.KeyDown += Keyboard_KeyDown;
}
#endif
}

#if WINDOWS_UWP
private void Control_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
public override void Cancel()
{
OnKeyDown(args.VirtualKey);
if (Input.Keyboard != null)
{
Input.Keyboard.KeyDown -= Keyboard_KeyDown;
}
}

private void Control_KeyDown1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
private void Keyboard_KeyDown(object sender, KeyEventArgs e)
{
OnKeyDown(e.Key);
}
#endif
#if NETCOREAPP
private void Control_KeyDown2(object sender, System.Windows.Forms.KeyEventArgs e)
{
OnKeyDown((Windows.System.VirtualKey)e.KeyCode);
}
#endif

public override void Cancel()
{
#if WINDOWS_UWP
if (Game.Context is CoreWindowGameContext context)
{
context.Control.KeyDown -= Control_KeyDown;
}
else if (Game.Context is XamlGameContext xamlContext)
{
xamlContext.Control.KeyDown -= Control_KeyDown1;
}
#endif
#if NETCOREAPP
if (Game.Context is WinFormsGameContext winFormsContext)
{
winFormsContext.Control.KeyDown -= Control_KeyDown2;
}
#endif
}

private async void OnKeyDown(Windows.System.VirtualKey key)
private async void OnKeyDown(VirtualKey key)
{
Entity? cameraEntity = SceneSystem.CurrentCamera?.Entity;
Entity scene = SceneSystem.SceneInstance?.RootEntity ?? throw new InvalidOperationException();

switch (key)
{
case Windows.System.VirtualKey.Number0 when GraphicsDevice?.Presenter != null:
case VirtualKey.Number0 when GraphicsDevice?.Presenter != null:
GraphicsDevice.Presenter.PresentationParameters.SyncInterval = 0;
break;
case Windows.System.VirtualKey.Number1 when GraphicsDevice?.Presenter != null:
case VirtualKey.Number1 when GraphicsDevice?.Presenter != null:
GraphicsDevice.Presenter.PresentationParameters.SyncInterval = 1;
break;
case Windows.System.VirtualKey.D:
case VirtualKey.D:
Entity? customCliffhouse = Entity?.EntityManager?.FirstOrDefault(m => m.Name == "CustomCliffhouse");
if (customCliffhouse != null)
{
Expand All @@ -107,18 +75,18 @@ private async void OnKeyDown(Windows.System.VirtualKey key)
}
}
break;
case Windows.System.VirtualKey.R:
case VirtualKey.R:
Entity? cliffhouse = SceneSystem.SceneInstance?.RootEntity?.Children.FirstOrDefault(m => m.Name == "Cliffhouse");
if (cliffhouse != null)
{
SceneSystem.SceneInstance?.RootEntity?.Children.Remove(cliffhouse);
}
break;
case Windows.System.VirtualKey.T:
case VirtualKey.T:
Entity? cliffhouse1 = SceneSystem.SceneInstance?.RootEntity?.Children.FirstOrDefault(m => m.Name == "Cliffhouse");
cliffhouse1?.Remove<ModelComponent>();
break;
case Windows.System.VirtualKey.A:
case VirtualKey.A:
Entity newCliffhouse = new Entity("Cliffhouse")
{
new TransformComponent { Position = new Vector3(-200.0f, 120.0f, 500.0f) },
Expand All @@ -127,23 +95,23 @@ private async void OnKeyDown(Windows.System.VirtualKey key)

SceneSystem.SceneInstance?.RootEntity?.Children.Add(newCliffhouse);
break;
case Windows.System.VirtualKey.P:
case VirtualKey.P:
Entity? child1 = Entity?.EntityManager?.FirstOrDefault(m => m.Name == "Child1");
if (child1 != null && child1.Parent is null)
{
child1.Transform.Parent = null;
SceneSystem.SceneInstance?.RootEntity?.Children.Add(child1);
}
break;
case Windows.System.VirtualKey.Q:
case VirtualKey.Q:
Entity? child2 = Entity?.EntityManager?.FirstOrDefault(m => m.Name == "Child1");
if (child2 != null && child2.Parent != null)
{
child2.Parent = null;
child2.Transform.Parent = Entity?.EntityManager?.FirstOrDefault(m => m.Name == "Parent1").Transform;
}
break;
case Windows.System.VirtualKey.S:
case VirtualKey.S:
SceneInstance? sceneInstance = SceneSystem.SceneInstance;
Entity? previousRootScene = sceneInstance?.RootEntity;

Expand All @@ -156,14 +124,14 @@ private async void OnKeyDown(Windows.System.VirtualKey key)
sceneInstance.RootEntity.Children.Add(previousRootScene);
}
break;
case Windows.System.VirtualKey.O:
case VirtualKey.O:
Entity? entity = Entity?.EntityManager?.FirstOrDefault(m => m.Name == "CustomCliffhouse");
if (entity != null)
{
await Content.SaveAsync(@"Assets\CustomCliffhouse", entity);
}
break;
case Windows.System.VirtualKey.I:
case VirtualKey.I:
{
if (scene != null)
{
Expand All @@ -180,10 +148,10 @@ private async void OnKeyDown(Windows.System.VirtualKey key)
}
}
break;
case Windows.System.VirtualKey.G:
case VirtualKey.G:
GC.Collect();
break;
case Windows.System.VirtualKey.K:
case VirtualKey.K:
await Content.ReloadAsync(scene);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
34 changes: 11 additions & 23 deletions DirectX12GameEngine.Core.Assets/ContentManager.Deserializing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,26 @@ public Task<object> DeserializeAsync(XElement element, object? obj = null)
return DeserializeAsync(element, operation, obj);
}

internal async Task<object> DeserializeAsync(string initialPath, string newPath, Type type, object? obj)
internal async Task<object> DeserializeExistingObjectAsync(string initialPath, string newPath, object obj)
{
Reference? reference = null;
Type type = obj.GetType();

if (obj != null)
{
reference = FindDeserializedObject(initialPath, type);

if (reference is null || reference.Object != obj)
{
throw new InvalidOperationException();
}
}
Reference reference = FindDeserializedObject(initialPath, type);

HashSet<Reference>? references = null;

if (reference != null)
if (reference is null || reference.Object != obj)
{
references = reference.References;
reference.References = new HashSet<Reference>();

reference.IsDeserialized = false;
throw new InvalidOperationException();
}

HashSet<Reference>? references = reference.References;
reference.References = new HashSet<Reference>();
reference.IsDeserialized = false;

object asset = await DeserializeAsync(newPath, type, null, obj);

if (references != null)
foreach (Reference childReference in references)
{
foreach (Reference childReference in references)
{
DecrementReference(childReference, false);
}
DecrementReference(childReference, false);
}

return asset;
Expand Down
4 changes: 2 additions & 2 deletions DirectX12GameEngine.Core.Assets/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async Task<object> LoadAsync(Type type, string path)
{
using (await asyncLock.LockAsync())
{
return await DeserializeAsync(path, path, type, null);
return await DeserializeAsync(path, type, null, null);
}
}

Expand All @@ -113,7 +113,7 @@ public async Task<bool> ReloadAsync(object asset, string? newPath = null)

string path = newPath ?? reference.Path;

await DeserializeAsync(reference.Path, path, asset.GetType(), asset);
await DeserializeExistingObjectAsync(reference.Path, path, asset);

if (path != reference.Path)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<Nullable>enable</Nullable>
<DefineConstants>WINDOWS_UWP</DefineConstants>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<Compile Include="ViewModels\Properties\IPropertyViewModelFactory.cs" />
<Compile Include="ViewModels\Properties\PrimitivePropertyViewModels.cs" />
<Compile Include="ViewModels\Properties\PropertyViewModelFactory.cs" />
<Compile Include="ViewModels\Properties\VectorPropertyViewModels.cs" />
<Compile Include="ViewModels\PropertyGridViewModel.cs" />
<Compile Include="ViewModels\Properties\PropertyViewModel.cs" />
<Compile Include="ViewModels\SceneViewModel.cs" />
Expand Down
Loading

0 comments on commit 0873a62

Please sign in to comment.