Skip to content

Commit 5e4e9b3

Browse files
authored
Merge pull request ppy#6523 from frenzibyte/revert-pen-work
Revert pen input flow and map pen to touch on mobile platforms
2 parents 64b6cdb + cb42b6d commit 5e4e9b3

File tree

8 files changed

+39
-25
lines changed

8 files changed

+39
-25
lines changed

osu.Framework.Tests/Visual/Input/TestSceneTouchInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ public void TestMouseInputAppliedFromLatestTouch()
203203

204204
AddStep("retrieve receptors", () =>
205205
{
206-
firstReceptor = receptors[(int)TouchSource.Touch1];
207-
lastReceptor = receptors[(int)TouchSource.Touch10];
206+
firstReceptor = receptors.First();
207+
lastReceptor = receptors.Last();
208208
});
209209

210210
AddStep("activate first", () =>

osu.Framework.Tests/Visual/Input/TestSceneTouchVisualiser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TestSceneTouchVisualiser()
3535

3636
public partial class TouchVisualiser : CompositeDrawable
3737
{
38-
private readonly Drawable[] drawableTouches = new Drawable[10];
38+
private readonly Drawable[] drawableTouches = new Drawable[TouchState.MAX_TOUCH_COUNT];
3939

4040
public TouchVisualiser()
4141
{

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

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

48-
// iPadOS doesn't support external tablets, so we are sure it's direct Apple Pencil input.
49-
// Other platforms support both direct and indirect tablet input, but SDL doesn't provide any information on the current device type.
50-
private static readonly TabletPenDeviceType device_type = RuntimeInfo.OS == RuntimeInfo.Platform.iOS ? TabletPenDeviceType.Direct : TabletPenDeviceType.Unknown;
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;
5151

52-
private void handlePenMove(Vector2 position)
52+
private void handlePenMove(Vector2 position, bool pressed)
5353
{
54-
enqueueInput(new MousePositionAbsoluteInputFromPen
55-
{
56-
Position = position,
57-
DeviceType = device_type
58-
});
54+
if (pressed && device_type == TabletPenDeviceType.Direct)
55+
enqueueInput(new TouchInput(new Input.Touch(TouchSource.PenTouch, position), true));
56+
else
57+
enqueueInput(new MousePositionAbsoluteInputFromPen { DeviceType = device_type, Position = position });
5958
}
6059

61-
private void handlePenTouch(bool pressed)
60+
private void handlePenTouch(bool pressed, Vector2 position)
6261
{
63-
enqueueInput(new MouseButtonInputFromPen(pressed) { DeviceType = device_type });
62+
if (device_type == TabletPenDeviceType.Direct)
63+
enqueueInput(new TouchInput(new Input.Touch(TouchSource.PenTouch, position), pressed));
64+
else
65+
enqueueInput(new MouseButtonInputFromPen(pressed) { DeviceType = device_type });
6466
}
6567

6668
private void handlePenButton(TabletPenButton button, bool pressed)

osu.Framework/Input/StateChanges/ISourcedFromPen.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ namespace osu.Framework.Input.StateChanges
66
/// <summary>
77
/// Denotes a simulated mouse input that was made by a tablet/pen device.
88
/// </summary>
9-
public interface ISourcedFromPen : IInput
9+
// todo: this is not ready to be used externally for distinguishing input, therefore it's internal for now.
10+
internal interface ISourcedFromPen : IInput
1011
{
1112
/// <summary>
1213
/// The type of the tablet or pen device that made this input.

osu.Framework/Input/States/TouchState.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ namespace osu.Framework.Input.States
99
public class TouchState
1010
{
1111
/// <summary>
12-
/// The maximum amount of touches this can handle.
12+
/// The maximum number of touches this can handle.
1313
/// </summary>
1414
public static readonly int MAX_TOUCH_COUNT = Enum.GetValues<TouchSource>().Length;
1515

16+
/// <summary>
17+
/// The maximum number of touches this can handle excluding the synthetic source <see cref="TouchSource.PenTouch"/>.
18+
/// </summary>
19+
internal static readonly int MAX_NATIVE_TOUCH_COUNT = 10;
20+
1621
/// <summary>
1722
/// The list of currently active touch sources.
1823
/// </summary>

osu.Framework/Input/TouchSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,10 @@ public enum TouchSource
5757
/// The tenth and last available touch source.
5858
/// </summary>
5959
Touch10,
60+
61+
/// <summary>
62+
/// A touch source that represents a pen/stylus.
63+
/// </summary>
64+
PenTouch,
6065
}
6166
}

osu.Framework/Platform/SDL2/SDL2Window_Input.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private void handleDropEvent(SDL_DropEvent evtDrop)
207207
}
208208
}
209209

210-
private readonly long?[] activeTouches = new long?[TouchState.MAX_TOUCH_COUNT];
210+
private readonly long?[] activeTouches = new long?[TouchState.MAX_NATIVE_TOUCH_COUNT];
211211

212212
private TouchSource? getTouchSource(long fingerId)
213213
{
@@ -230,7 +230,7 @@ private void handleDropEvent(SDL_DropEvent evtDrop)
230230
return (TouchSource)i;
231231
}
232232

233-
// we only handle up to TouchState.MAX_TOUCH_COUNT. Ignore any further touches for now.
233+
// we only handle up to TouchState.MAX_NATIVE_TOUCH_COUNT. Ignore any further touches for now.
234234
return null;
235235
}
236236

osu.Framework/Platform/SDL3/SDL3Window_Input.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void handleDropEvent(SDL_DropEvent evtDrop)
247247
}
248248
}
249249

250-
private readonly SDL_FingerID?[] activeTouches = new SDL_FingerID?[TouchState.MAX_TOUCH_COUNT];
250+
private readonly SDL_FingerID?[] activeTouches = new SDL_FingerID?[TouchState.MAX_NATIVE_TOUCH_COUNT];
251251

252252
private TouchSource? getTouchSource(SDL_FingerID fingerId)
253253
{
@@ -270,7 +270,7 @@ private void handleDropEvent(SDL_DropEvent evtDrop)
270270
return (TouchSource)i;
271271
}
272272

273-
// we only handle up to TouchState.MAX_TOUCH_COUNT. Ignore any further touches for now.
273+
// we only handle up to TouchState.MAX_NATIVE_TOUCH_COUNT. Ignore any further touches for now.
274274
return null;
275275
}
276276

@@ -532,12 +532,12 @@ private void handleKeyboardEvent(SDL_KeyboardEvent evtKey)
532532

533533
private void handlePenMotionEvent(SDL_PenMotionEvent evtPenMotion)
534534
{
535-
PenMove?.Invoke(new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale);
535+
PenMove?.Invoke(new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale, evtPenMotion.pen_state.HasFlagFast(SDL_PenInputFlags.SDL_PEN_INPUT_DOWN));
536536
}
537537

538538
private void handlePenTouchEvent(SDL_PenTouchEvent evtPenTouch)
539539
{
540-
PenTouch?.Invoke(evtPenTouch.down);
540+
PenTouch?.Invoke(evtPenTouch.down, new Vector2(evtPenTouch.x, evtPenTouch.y) * Scale);
541541
}
542542

543543
/// <summary>
@@ -743,14 +743,15 @@ private void updateConfineMode()
743743
public event Action<Touch>? TouchUp;
744744

745745
/// <summary>
746-
/// Invoked when a pen moves.
746+
/// Invoked when a pen moves. Passes pen position and whether the pen is touching the tablet surface.
747747
/// </summary>
748-
public event Action<Vector2>? PenMove;
748+
public event Action<Vector2, bool>? PenMove;
749749

750750
/// <summary>
751751
/// Invoked when a pen touches (<c>true</c>) or lifts (<c>false</c>) from the tablet surface.
752+
/// Also passes the current position of the pen.
752753
/// </summary>
753-
public event Action<bool>? PenTouch;
754+
public event Action<bool, Vector2>? PenTouch;
754755

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

0 commit comments

Comments
 (0)