Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pen input not being friendly with touch #6509

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ public void TestPenInputPassThrough()
});
});

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

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 @@ -307,7 +311,10 @@ 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("press pen", () => InputManager.PressPen());
AddStep("press pen", () => InputManager.Input(new MouseButtonInputFromPen(true)
{
DeviceType = TabletPenDeviceType.Unknown,
}));

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 @@ -318,7 +325,10 @@ 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.ReleasePen());
AddStep("release pen", () => InputManager.Input(new MouseButtonInputFromPen(false)
{
DeviceType = TabletPenDeviceType.Unknown,
}));

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
2 changes: 1 addition & 1 deletion osu.Framework.Tests/Visual/Input/TestSceneTouchInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace osu.Framework.Tests.Visual.Input
{
public partial class TestSceneTouchInput : ManualInputManagerTestScene
{
private static readonly TouchSource[] touch_sources = (TouchSource[])Enum.GetValues(typeof(TouchSource));
private static readonly TouchSource[] touch_sources = Enum.GetValues<TouchSource>().Take(10).ToArray();

private Container<InputReceptor> receptors;

Expand Down
7 changes: 0 additions & 7 deletions osu.Framework/Input/Handlers/Pen/PenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ public override bool Initialize(GameHost host)
if (enabled.NewValue)
{
window.PenMove += handlePenMove;
window.PenTouch += handlePenTouch;
window.PenButton += handlePenButton;
}
else
{
window.PenMove -= handlePenMove;
window.PenTouch -= handlePenTouch;
window.PenButton -= handlePenButton;
}
}, true);
Expand All @@ -58,11 +56,6 @@ private void handlePenMove(Vector2 position)
});
}

private void handlePenTouch(bool pressed)
{
enqueueInput(new MouseButtonInputFromPen(pressed) { DeviceType = device_type });
}

private void handlePenButton(TabletPenButton button, bool pressed)
{
enqueueInput(new TabletPenButtonInput(button, pressed));
Expand Down
11 changes: 8 additions & 3 deletions osu.Framework/Input/States/TouchState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ namespace osu.Framework.Input.States
public class TouchState
{
/// <summary>
/// The maximum amount of touches this can handle.
/// The maximum amount of touches this can handle (excluding <see cref="TouchSource.PenTouch"/>).
/// </summary>
public static readonly int MAX_TOUCH_COUNT = Enum.GetValues<TouchSource>().Length;
public static readonly int MAX_TOUCH_COUNT = 10;

/// <summary>
/// The maximum number of <see cref="TouchSource"/>s.
/// </summary>
public static readonly int MAX_SOURCES_COUNT = Enum.GetValues<TouchSource>().Length;

/// <summary>
/// The list of currently active touch sources.
Expand All @@ -25,7 +30,7 @@ public class TouchState
/// Using <see cref="GetTouchPosition"/> is recommended for retrieving
/// logically correct values, as this may contain already stale values.
/// </remarks>
public readonly Vector2[] TouchPositions = new Vector2[MAX_TOUCH_COUNT];
public readonly Vector2[] TouchPositions = new Vector2[MAX_SOURCES_COUNT];

/// <summary>
/// Retrieves the current touch position of a specified <paramref name="source"/>.
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Input/TouchSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ public enum TouchSource
/// The tenth and last available touch source.
/// </summary>
Touch10,

/// <summary>
/// A touch source that represents a pen/stylus.
/// </summary>
PenTouch,
Comment on lines +61 to +64
Copy link
Collaborator

@bdach bdach Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I can bring myself to particularly care about any of this, but this here looks immediately incorrect and a complete stretch just to make apple pencil specifically work. This has no semantic sense elsewhere. SDL generally classifies drawing tablet pens as pens, so treating it as a mobile stylus specifically is an overreach that can bite us in the rear later.

Maybe if you constrained this to only be a mobile thing I could accept it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's entirely weird how SDL pen input can originate from "indirect" / drawing tablet devices in the first place when we have local handling for those devices specifically via OTD handlers, but alas.

This logic makes sense for any kind of touchscreen with a stylus on it, be it Apple or Android or even Microsoft Surface with a Surface Pen I guess.

However, since there's no way to determine whether pen input originates from an on-screen pen or a drawing tablet pen, I can only resort to an is-mobile conditional.

}
}
24 changes: 17 additions & 7 deletions osu.Framework/Platform/SDL3/SDL3Window_Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,29 @@ private void handleKeyboardEvent(SDL_KeyboardEvent evtKey)

private void handleKeymapChangedEvent() => KeymapChanged?.Invoke();

// pen input events should ultimately have its own input flow with code from InputManager to fall back to mouse input,
// but as the current structure of InputManager completely disallows that, synthesize touch input on pen events
// so it still works correctly for our use case (consider OsuTouchInputMapper in osu!).

private void handlePenMotionEvent(SDL_PenMotionEvent evtPenMotion)
{
PenMove?.Invoke(new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale);
var pos = new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale;

if (evtPenMotion.pen_state.HasFlagFast(SDL_PenInputFlags.SDL_PEN_INPUT_DOWN))
TouchDown?.Invoke(new Touch(TouchSource.PenTouch, pos));
else
PenMove?.Invoke(pos);
}

private void handlePenTouchEvent(SDL_PenTouchEvent evtPenTouch)
{
PenTouch?.Invoke(evtPenTouch.down);
var pos = new Vector2(evtPenTouch.x, evtPenTouch.y) * Scale;
var touch = new Touch(TouchSource.PenTouch, pos);

if (evtPenTouch.down)
TouchDown?.Invoke(touch);
else
TouchUp?.Invoke(touch);
}

/// <summary>
Expand Down Expand Up @@ -741,11 +756,6 @@ private void updateConfineMode()
/// </summary>
public event Action<Vector2>? PenMove;

/// <summary>
/// Invoked when a pen touches (<c>true</c>) or lifts (<c>false</c>) from the tablet surface.
/// </summary>
public event Action<bool>? PenTouch;

/// <summary>
/// Invoked when a <see cref="TabletPenButton">pen button</see> is pressed (<c>true</c>) or released (<c>false</c>).
/// </summary>
Expand Down
9 changes: 0 additions & 9 deletions osu.Framework/Testing/Input/ManualInputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ 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 @@ -171,9 +165,6 @@ 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
Loading