Skip to content

Commit

Permalink
Add general input methods for pen input in ManualInputManager
Browse files Browse the repository at this point in the history
Useful for wherever pen input is required in tests.
  • Loading branch information
frenzibyte committed Jan 21, 2025
1 parent d016c2d commit 66cdd7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ public void TestPenInputPassThrough()
});
});

AddStep("move pen to box",
() => InputManager.Input(new MousePositionAbsoluteInputFromPen { Position = testInputManager.ScreenSpaceDrawQuad.Centre, DeviceType = TabletPenDeviceType.Unknown }));
AddStep("move pen to box", () => InputManager.MovePenTo(testInputManager));

AddAssert("ensure parent manager produced mouse", () => InputManager.CurrentState.Mouse.Position == testInputManager.ScreenSpaceDrawQuad.Centre);
AddAssert("ensure pass-through produced mouse", () => testInputManager.CurrentState.Mouse.Position == testInputManager.ScreenSpaceDrawQuad.Centre);
Expand All @@ -308,7 +307,7 @@ public void TestPenInputPassThrough()
AddAssert("inner box received 1 pen event", () => inner.PenEvents, () => Is.EqualTo(1));
AddAssert("inner box received no mouse events", () => inner.MouseEvents, () => Is.EqualTo(0));

AddStep("click pen", () => InputManager.Input(new MouseButtonInputFromPen(true) { DeviceType = TabletPenDeviceType.Unknown }));
AddStep("press pen", () => InputManager.PressPen());

AddAssert("ensure parent manager produced mouse", () => InputManager.CurrentState.Mouse.Buttons.Single() == MouseButton.Left);
AddAssert("ensure pass-through produced mouse", () => testInputManager.CurrentState.Mouse.Buttons.Single() == MouseButton.Left);
Expand All @@ -319,7 +318,7 @@ public void TestPenInputPassThrough()
AddAssert("inner box received 2 pen events", () => inner.PenEvents, () => Is.EqualTo(2));
AddAssert("inner box received no mouse events", () => inner.MouseEvents, () => Is.EqualTo(0));

AddStep("release pen", () => InputManager.Input(new MouseButtonInputFromPen(false) { DeviceType = TabletPenDeviceType.Unknown }));
AddStep("release pen", () => InputManager.ReleasePen());

AddAssert("ensure parent manager produced mouse", () => InputManager.CurrentState.Mouse.Buttons.HasAnyButtonPressed, () => Is.False);
AddAssert("ensure pass-through produced mouse", () => testInputManager.CurrentState.Mouse.Buttons.HasAnyButtonPressed, () => Is.False);
Expand Down
9 changes: 9 additions & 0 deletions osu.Framework/Testing/Input/ManualInputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ public void Keys(PlatformAction action)

public void MoveTouchTo(Touch touch) => Input(new TouchInput(touch, CurrentState.Touch.IsActive(touch.Source)));

public void MovePenTo(Drawable drawable, Vector2? offset = null, TabletPenDeviceType deviceType = TabletPenDeviceType.Unknown)
=> MovePenTo(drawable.ToScreenSpace(drawable.LayoutRectangle.Centre) + (offset ?? Vector2.Zero), deviceType);

public void MovePenTo(Vector2 position, TabletPenDeviceType deviceType = TabletPenDeviceType.Unknown)
=> Input(new MousePositionAbsoluteInputFromPen { Position = position, DeviceType = deviceType });

public new bool TriggerClick() =>
throw new InvalidOperationException($"To trigger a click via a {nameof(ManualInputManager)} use {nameof(Click)} instead.");

Expand Down Expand Up @@ -165,6 +171,9 @@ public void Click(MouseButton button)
public void PressMidiKey(MidiKey key, byte velocity) => Input(new MidiKeyInput(key, velocity, true));
public void ReleaseMidiKey(MidiKey key, byte velocity) => Input(new MidiKeyInput(key, velocity, false));

public void PressPen(TabletPenDeviceType deviceType = TabletPenDeviceType.Unknown) => Input(new MouseButtonInputFromPen(true) { DeviceType = deviceType });
public void ReleasePen(TabletPenDeviceType deviceType = TabletPenDeviceType.Unknown) => Input(new MouseButtonInputFromPen(false) { DeviceType = deviceType });

public void PressTabletPenButton(TabletPenButton penButton) => Input(new TabletPenButtonInput(penButton, true));
public void ReleaseTabletPenButton(TabletPenButton penButton) => Input(new TabletPenButtonInput(penButton, false));

Expand Down

0 comments on commit 66cdd7a

Please sign in to comment.