Skip to content

Commit

Permalink
Merge pull request stride3d#751 from LittleFinix/master
Browse files Browse the repository at this point in the history
[Input] Fix Issue stride3d#750, VirtualButton.RegisterFromType
  • Loading branch information
xen2 authored Jun 7, 2020
2 parents 73b7365 + 468d166 commit 859f1d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
49 changes: 36 additions & 13 deletions sources/engine/Stride.Input.Tests/TestInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public TestInput()
{
EnableSimulatedInputSource();
}

/// <summary>
/// Checks keyboard press/release
/// </summary>
Expand All @@ -25,7 +25,7 @@ void TestPressRelease()

keyboard.SimulateDown(Keys.A);
Input.Update(DrawTime);

// Test press
Assert.Equal(1, events.Count);
Assert.True(events[0] is KeyEvent);
Expand Down Expand Up @@ -72,7 +72,7 @@ void TestRepeat()
Input.Update(DrawTime);
keyboard.SimulateDown(Keys.A);
Input.Update(DrawTime);

// Test press with release
Assert.Equal(1, events.Count);
Assert.True(events[0] is KeyEvent);
Expand Down Expand Up @@ -114,7 +114,7 @@ void TestMouse()
mouse.SetPosition(targetPosition = new Vector2(0.6f, 0.5f));
mouse.SimulateMouseDown(MouseButton.Left);
Input.Update(DrawTime);

// Check for pointer events (2, 1 move, 1 down)
Assert.Equal(2, Input.PointerEvents.Count);
Assert.Equal(PointerEventType.Moved, Input.PointerEvents[0].EventType);
Expand All @@ -132,7 +132,7 @@ void TestMouse()
Assert.Equal(1, mouse.PressedPointers.Count);
Assert.Equal(0, mouse.ReleasedPointers.Count);
Assert.Equal(1, mouse.DownPointers.Count);

// Check delta
Assert.Equal(new Vector2(0.1f, 0.0f), Input.PointerEvents[0].DeltaPosition);
Assert.Equal(new Vector2(0.0f, 0.0f), Input.PointerEvents[1].DeltaPosition);
Expand Down Expand Up @@ -175,7 +175,7 @@ void TestSingleFrameStates()
Assert.True(Input.IsKeyPressed(Keys.Space));
Assert.True(Input.IsKeyReleased(Keys.Space));
Assert.False(Input.IsKeyDown(Keys.Space));

var mouse = MouseSimulated;

mouse.SimulateMouseDown(MouseButton.Extended2);
Expand All @@ -185,7 +185,7 @@ void TestSingleFrameStates()
Assert.True(Input.IsMouseButtonPressed(MouseButton.Extended2));
Assert.True(Input.IsMouseButtonReleased(MouseButton.Extended2));
Assert.False(Input.IsMouseButtonDown(MouseButton.Extended2));

mouse.SimulateMouseDown(MouseButton.Left);
mouse.SimulateMouseUp(MouseButton.Left);
mouse.SimulateMouseDown(MouseButton.Left);
Expand All @@ -198,7 +198,7 @@ void TestSingleFrameStates()
mouse.SimulateMouseUp(MouseButton.Left);
Input.Update(DrawTime);
}

/// <summary>
/// Checks adding/removal of keyboard and mouse
/// </summary>
Expand Down Expand Up @@ -261,7 +261,7 @@ void TestLockedMousePosition()
var mouse = MouseSimulated;
mouse.LockPosition(true);
Input.Update(DrawTime);

Assert.Equal(new Vector2(0.5f), mouse.Position);
Input.Update(DrawTime);

Expand All @@ -276,7 +276,7 @@ void TestLockedMousePosition()
Assert.Equal(new Vector2(0.5f, 0.5f), mouse.Position);

mouse.UnlockPosition();

// Validate mouse delta with unlocked position
mouse.SetPosition(new Vector2(0.6f, 0.5f));
Input.Update(DrawTime);
Expand All @@ -292,9 +292,9 @@ void TestGamePad()
Assert.Equal(0, InputSourceSimulated.GamePads.Count);
Assert.False(Input.HasGamePad);
Assert.Null(Input.DefaultGamePad);

var gamePad0 = InputSourceSimulated.AddGamePad();

Assert.Equal(1, Input.GamePadCount);
Assert.True(Input.HasGamePad);
Assert.NotNull(Input.DefaultGamePad);
Expand Down Expand Up @@ -334,13 +334,35 @@ void TestGamePad()
Assert.False(gamePad0.IsButtonPressed(GamePadButton.A));
Assert.True(gamePad0.IsButtonReleased(GamePadButton.A));
Assert.False(gamePad0.IsButtonDown(GamePadButton.A));

InputSourceSimulated.RemoveGamePad(gamePad0);
InputSourceSimulated.RemoveGamePad(gamePad1);

Assert.Equal(0, Input.GamePadCount);
}

/// <summary>
/// Checks whether VirtualButton.Find works for Keyboard, Mouse and GamePad.
/// </summary>
void TestVirtualButtonFind()
{
// Test Keyboard Keys
Assert.Equal(VirtualButton.Keyboard.A, VirtualButton.Find("Keyboard.a"));
Assert.Equal(VirtualButton.Keyboard.Z, VirtualButton.Find("Keyboard.z"));
Assert.Equal(VirtualButton.Keyboard.LeftCtrl, VirtualButton.Find("Keyboard.leftctrl"));
Assert.Equal(VirtualButton.Keyboard.LeftShift, VirtualButton.Find("Keyboard.leftshift"));

// Test Mouse Buttons
Assert.Equal(VirtualButton.Mouse.Left, VirtualButton.Find("Mouse.Left"));
Assert.Equal(VirtualButton.Mouse.PositionX, VirtualButton.Find("Mouse.PositionX"));
Assert.Equal(VirtualButton.Mouse.DeltaY, VirtualButton.Find("Mouse.DeltaY"));

// Test GamePad Buttons
Assert.Equal(VirtualButton.GamePad.LeftThumbAxisX, VirtualButton.Find("GamePad.LeftThumbAxisX"));
Assert.Equal(VirtualButton.GamePad.RightThumb, VirtualButton.Find("GamePad.RightThumb"));
Assert.Equal(VirtualButton.GamePad.A, VirtualButton.Find("GamePad.A"));
}

protected override void RegisterTests()
{
base.RegisterTests();
Expand All @@ -352,6 +374,7 @@ protected override void RegisterTests()
FrameGameSystem.Update(TestLockedMousePosition);
FrameGameSystem.Update(TestSingleFrameStates);
FrameGameSystem.Update(TestGamePad);
FrameGameSystem.Update(TestVirtualButtonFind);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Input/VirtualButton/VirtualButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private static void RegisterFromType(Type type)
{
foreach (var fieldInfo in type.GetTypeInfo().DeclaredFields)
{
if (fieldInfo.IsStatic && fieldInfo.FieldType == typeof(VirtualButton))
if (fieldInfo.IsStatic && typeof(VirtualButton).IsAssignableFrom(fieldInfo.FieldType))
{
Register((VirtualButton)fieldInfo.GetValue(null));
}
Expand Down

0 comments on commit 859f1d5

Please sign in to comment.