Skip to content

Commit aa95565

Browse files
committed
Map pen to touch input when touching the screen
1 parent 804df9b commit aa95565

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

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

-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ public override bool Initialize(GameHost host)
3131
if (enabled.NewValue)
3232
{
3333
window.PenMove += handlePenMove;
34-
window.PenTouch += handlePenTouch;
3534
window.PenButton += handlePenButton;
3635
}
3736
else
3837
{
3938
window.PenMove -= handlePenMove;
40-
window.PenTouch -= handlePenTouch;
4139
window.PenButton -= handlePenButton;
4240
}
4341
}, true);
@@ -58,11 +56,6 @@ private void handlePenMove(Vector2 position)
5856
});
5957
}
6058

61-
private void handlePenTouch(bool pressed)
62-
{
63-
enqueueInput(new MouseButtonInputFromPen(pressed) { DeviceType = device_type });
64-
}
65-
6659
private void handlePenButton(TabletPenButton button, bool pressed)
6760
{
6861
enqueueInput(new TabletPenButtonInput(button, pressed));

osu.Framework/Input/States/TouchState.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ namespace osu.Framework.Input.States
99
public class TouchState
1010
{
1111
/// <summary>
12-
/// The maximum amount of touches this can handle.
12+
/// The maximum amount of touches this can handle (excluding <see cref="TouchSource.PenTouch"/>).
1313
/// </summary>
14-
public static readonly int MAX_TOUCH_COUNT = Enum.GetValues<TouchSource>().Length;
14+
public static readonly int MAX_TOUCH_COUNT = 10;
15+
16+
/// <summary>
17+
/// The maximum number of <see cref="TouchSource"/>s.
18+
/// </summary>
19+
public static readonly int MAX_SOURCES_COUNT = Enum.GetValues<TouchSource>().Length;
1520

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

3035
/// <summary>
3136
/// Retrieves the current touch position of a specified <paramref name="source"/>.

osu.Framework/Input/TouchSource.cs

+5
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/SDL3/SDL3Window_Input.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -524,14 +524,29 @@ private void handleKeyboardEvent(SDL_KeyboardEvent evtKey)
524524

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

527+
// pen input events should ultimately have its own input flow with code from InputManager to fall back to mouse input,
528+
// but as the current structure of InputManager completely disallows that, synthesize touch input on pen events
529+
// so it still works correctly for our use case (consider OsuTouchInputMapper in osu!).
530+
527531
private void handlePenMotionEvent(SDL_PenMotionEvent evtPenMotion)
528532
{
529-
PenMove?.Invoke(new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale);
533+
var pos = new Vector2(evtPenMotion.x, evtPenMotion.y) * Scale;
534+
535+
if (evtPenMotion.pen_state.HasFlagFast(SDL_PenInputFlags.SDL_PEN_INPUT_DOWN))
536+
TouchDown?.Invoke(new Touch(TouchSource.PenTouch, pos));
537+
else
538+
PenMove?.Invoke(pos);
530539
}
531540

532541
private void handlePenTouchEvent(SDL_PenTouchEvent evtPenTouch)
533542
{
534-
PenTouch?.Invoke(evtPenTouch.down);
543+
var pos = new Vector2(evtPenTouch.x, evtPenTouch.y) * Scale;
544+
var touch = new Touch(TouchSource.PenTouch, pos);
545+
546+
if (evtPenTouch.down)
547+
TouchDown?.Invoke(touch);
548+
else
549+
TouchUp?.Invoke(touch);
535550
}
536551

537552
/// <summary>
@@ -741,11 +756,6 @@ private void updateConfineMode()
741756
/// </summary>
742757
public event Action<Vector2>? PenMove;
743758

744-
/// <summary>
745-
/// Invoked when a pen touches (<c>true</c>) or lifts (<c>false</c>) from the tablet surface.
746-
/// </summary>
747-
public event Action<bool>? PenTouch;
748-
749759
/// <summary>
750760
/// Invoked when a <see cref="TabletPenButton">pen button</see> is pressed (<c>true</c>) or released (<c>false</c>).
751761
/// </summary>

0 commit comments

Comments
 (0)