Skip to content

Commit 5f4585f

Browse files
committed
Propagate tablet pen device type from SDL
The device type is currently: - direct on iOS - indirect or direct on Android - unknown on desktop
1 parent 0c8bac9 commit 5f4585f

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

osu.Framework/Input/Handlers/Pen/PenHandler.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,20 @@ public override bool Initialize(GameHost host)
4545
return true;
4646
}
4747

48-
// Pen input is not necessarily direct on mobile platforms (specifically Android, where external tablets are supported),
49-
// but until users experience issues with this, consider it "direct" for now.
50-
private static readonly TabletPenDeviceType device_type = RuntimeInfo.IsMobile ? TabletPenDeviceType.Direct : TabletPenDeviceType.Unknown;
51-
52-
private void handlePenMove(Vector2 position, bool pressed)
48+
private void handlePenMove(TabletPenDeviceType deviceType, Vector2 position, bool pressed)
5349
{
54-
if (pressed && device_type == TabletPenDeviceType.Direct)
50+
if (pressed && deviceType == TabletPenDeviceType.Direct)
5551
enqueueInput(new TouchInput(new Input.Touch(TouchSource.PenTouch, position), true));
5652
else
57-
enqueueInput(new MousePositionAbsoluteInputFromPen { DeviceType = device_type, Position = position });
53+
enqueueInput(new MousePositionAbsoluteInputFromPen { DeviceType = deviceType, Position = position });
5854
}
5955

60-
private void handlePenTouch(bool pressed, Vector2 position)
56+
private void handlePenTouch(TabletPenDeviceType deviceType, bool pressed, Vector2 position)
6157
{
62-
if (device_type == TabletPenDeviceType.Direct)
58+
if (deviceType == TabletPenDeviceType.Direct)
6359
enqueueInput(new TouchInput(new Input.Touch(TouchSource.PenTouch, position), pressed));
6460
else
65-
enqueueInput(new MouseButtonInputFromPen(pressed) { DeviceType = device_type });
61+
enqueueInput(new MouseButtonInputFromPen(pressed) { DeviceType = deviceType });
6662
}
6763

6864
private void handlePenButton(TabletPenButton button, bool pressed)

osu.Framework/Platform/SDL3/SDL3Extensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using osu.Framework.Graphics.Primitives;
77
using osu.Framework.Input;
88
using osu.Framework.Input.Bindings;
9+
using osu.Framework.Input.StateChanges;
910
using osuTK.Input;
1011
using SDL;
1112
using static SDL.SDL3;
@@ -1048,6 +1049,21 @@ public static unsafe DisplayMode ToDisplayMode(this SDL_DisplayMode mode, int di
10481049
return new DisplayMode(SDL_GetPixelFormatName(mode.format), new Size(mode.w, mode.h), bpp, mode.refresh_rate, displayIndex);
10491050
}
10501051

1052+
public static TabletPenDeviceType ToTabletPenDeviceType(this SDL_PenDeviceType type)
1053+
{
1054+
switch (type)
1055+
{
1056+
case SDL_PenDeviceType.SDL_PEN_DEVICE_TYPE_DIRECT:
1057+
return TabletPenDeviceType.Direct;
1058+
1059+
case SDL_PenDeviceType.SDL_PEN_DEVICE_TYPE_INDIRECT:
1060+
return TabletPenDeviceType.Indirect;
1061+
1062+
default:
1063+
return TabletPenDeviceType.Unknown;
1064+
}
1065+
}
1066+
10511067
public static string ReadableName(this SDL_LogCategory category)
10521068
{
10531069
switch (category)

osu.Framework/Platform/SDL3/SDL3Window_Input.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using osu.Framework.Extensions.EnumExtensions;
1212
using osu.Framework.Graphics.Primitives;
1313
using osu.Framework.Input;
14+
using osu.Framework.Input.StateChanges;
1415
using osu.Framework.Input.States;
1516
using osu.Framework.Logging;
1617
using osuTK;
@@ -530,14 +531,16 @@ private void handleKeyboardEvent(SDL_KeyboardEvent evtKey)
530531

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

534+
private static TabletPenDeviceType getPenType(SDL_PenID instanceID) => SDL_GetPenDeviceType(instanceID).ToTabletPenDeviceType();
535+
533536
private void handlePenMotionEvent(SDL_PenMotionEvent evtPenMotion)
534537
{
535-
PenMove?.Invoke(new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale, evtPenMotion.pen_state.HasFlagFast(SDL_PenInputFlags.SDL_PEN_INPUT_DOWN));
538+
PenMove?.Invoke(getPenType(evtPenMotion.which), new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale, evtPenMotion.pen_state.HasFlagFast(SDL_PenInputFlags.SDL_PEN_INPUT_DOWN));
536539
}
537540

538541
private void handlePenTouchEvent(SDL_PenTouchEvent evtPenTouch)
539542
{
540-
PenTouch?.Invoke(evtPenTouch.down, new Vector2(evtPenTouch.x, evtPenTouch.y) * Scale);
543+
PenTouch?.Invoke(getPenType(evtPenTouch.which), evtPenTouch.down, new Vector2(evtPenTouch.x, evtPenTouch.y) * Scale);
541544
}
542545

543546
/// <summary>
@@ -745,13 +748,13 @@ private void updateConfineMode()
745748
/// <summary>
746749
/// Invoked when a pen moves. Passes pen position and whether the pen is touching the tablet surface.
747750
/// </summary>
748-
public event Action<Vector2, bool>? PenMove;
751+
public event Action<TabletPenDeviceType, Vector2, bool>? PenMove;
749752

750753
/// <summary>
751754
/// Invoked when a pen touches (<c>true</c>) or lifts (<c>false</c>) from the tablet surface.
752755
/// Also passes the current position of the pen.
753756
/// </summary>
754-
public event Action<bool, Vector2>? PenTouch;
757+
public event Action<TabletPenDeviceType, bool, Vector2>? PenTouch;
755758

756759
/// <summary>
757760
/// Invoked when a <see cref="TabletPenButton">pen button</see> is pressed (<c>true</c>) or released (<c>false</c>).

0 commit comments

Comments
 (0)