2023-01-16 10:35:55 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-01-16 11:13:31 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
2023-01-16 10:35:55 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.Input.Events;
|
2023-01-16 11:11:59 +00:00
|
|
|
|
using osu.Framework.Input.StateChanges;
|
2023-01-16 11:13:31 +00:00
|
|
|
|
using osu.Game.Configuration;
|
2023-01-16 10:35:55 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.UI
|
|
|
|
|
{
|
|
|
|
|
public partial class OsuTouchInputMapper : Drawable
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// All the active <see cref="TouchSource"/>s and the <see cref="OsuAction"/> that it triggered (if any).
|
|
|
|
|
/// Ordered from oldest to newest touch chronologically.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly List<TrackedTouch> trackedTouches = new List<TrackedTouch>();
|
|
|
|
|
|
2023-01-24 03:30:11 +00:00
|
|
|
|
private TrackedTouch? positionTrackingTouch;
|
|
|
|
|
|
2023-01-17 05:53:24 +00:00
|
|
|
|
private readonly OsuInputManager osuInputManager;
|
|
|
|
|
|
2023-01-16 11:13:31 +00:00
|
|
|
|
private Bindable<bool> mouseDisabled = null!;
|
|
|
|
|
|
2023-01-16 10:35:55 +00:00
|
|
|
|
public OsuTouchInputMapper(OsuInputManager inputManager)
|
|
|
|
|
{
|
|
|
|
|
osuInputManager = inputManager;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 05:52:15 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
2023-01-16 11:13:31 +00:00
|
|
|
|
private void load(OsuConfigManager config)
|
|
|
|
|
{
|
2023-01-17 05:54:52 +00:00
|
|
|
|
// The mouse button disable setting affects touch. It's a bit weird.
|
|
|
|
|
// This is mostly just doing the same as what is done in RulesetInputManager to match behaviour.
|
2023-01-16 11:13:31 +00:00
|
|
|
|
mouseDisabled = config.GetBindable<bool>(OsuSetting.MouseDisableButtons);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 11:11:59 +00:00
|
|
|
|
protected override void OnTouchMove(TouchMoveEvent e)
|
|
|
|
|
{
|
|
|
|
|
base.OnTouchMove(e);
|
|
|
|
|
handleTouchMovement(e);
|
|
|
|
|
}
|
2023-01-16 10:35:55 +00:00
|
|
|
|
|
|
|
|
|
protected override bool OnTouchDown(TouchDownEvent e)
|
|
|
|
|
{
|
2023-01-16 11:11:59 +00:00
|
|
|
|
OsuAction action = trackedTouches.Any(t => t.Action == OsuAction.LeftButton)
|
|
|
|
|
? OsuAction.RightButton
|
|
|
|
|
: OsuAction.LeftButton;
|
|
|
|
|
|
2023-01-16 12:07:28 +00:00
|
|
|
|
// Ignore any taps which trigger an action which is already handled. But track them for potential positional input in the future.
|
2023-01-17 06:01:01 +00:00
|
|
|
|
bool shouldResultInAction = osuInputManager.AllowGameplayInputs && !mouseDisabled.Value && trackedTouches.All(t => t.Action != action);
|
2023-01-16 12:07:28 +00:00
|
|
|
|
|
2023-01-23 08:07:27 +00:00
|
|
|
|
// If we can actually accept as an action, check whether this tap was on a circle's receptor.
|
|
|
|
|
// This case gets special handling to allow for empty-space stream tapping.
|
|
|
|
|
bool isDirectCircleTouch = osuInputManager.CheckScreenSpaceActionPressJudgeable(e.ScreenSpaceTouchDownPosition);
|
|
|
|
|
|
|
|
|
|
var trackedTouch = new TrackedTouch(e.Touch.Source, shouldResultInAction ? action : null, isDirectCircleTouch);
|
|
|
|
|
|
|
|
|
|
if (isDirectCircleTouch)
|
|
|
|
|
positionTrackingTouch = trackedTouch;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If no direct touch is registered, we always use the new (latest) touch for positional tracking.
|
|
|
|
|
if (positionTrackingTouch?.DirectTouch != true)
|
|
|
|
|
positionTrackingTouch = trackedTouch;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If not a direct circle touch, consider whether to release the an original direct touch.
|
|
|
|
|
if (positionTrackingTouch.DirectTouch && positionTrackingTouch.Action is OsuAction directTouchAction)
|
|
|
|
|
{
|
|
|
|
|
osuInputManager.KeyBindingContainer.TriggerReleased(directTouchAction);
|
|
|
|
|
positionTrackingTouch.Action = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trackedTouches.Add(trackedTouch);
|
2023-01-16 12:07:28 +00:00
|
|
|
|
|
|
|
|
|
// Important to update position before triggering the pressed action.
|
2023-01-16 11:11:59 +00:00
|
|
|
|
handleTouchMovement(e);
|
2023-01-16 10:35:55 +00:00
|
|
|
|
|
2023-01-16 12:07:28 +00:00
|
|
|
|
if (shouldResultInAction)
|
2023-01-16 10:35:55 +00:00
|
|
|
|
osuInputManager.KeyBindingContainer.TriggerPressed(action);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 11:11:59 +00:00
|
|
|
|
private void handleTouchMovement(TouchEvent touchEvent)
|
|
|
|
|
{
|
2023-01-16 12:07:28 +00:00
|
|
|
|
// Movement should only be tracked for the most recent touch.
|
2023-01-23 08:07:27 +00:00
|
|
|
|
if (touchEvent.Touch.Source != positionTrackingTouch?.Source)
|
2023-01-16 12:07:28 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2023-01-17 06:01:01 +00:00
|
|
|
|
if (!osuInputManager.AllowUserCursorMovement)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-01-16 11:11:59 +00:00
|
|
|
|
new MousePositionAbsoluteInput { Position = touchEvent.ScreenSpaceTouch.Position }.Apply(osuInputManager.CurrentState, osuInputManager);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 10:35:55 +00:00
|
|
|
|
protected override void OnTouchUp(TouchUpEvent e)
|
|
|
|
|
{
|
2023-01-17 05:52:45 +00:00
|
|
|
|
var tracked = trackedTouches.Single(t => t.Source == e.Touch.Source);
|
2023-01-16 10:35:55 +00:00
|
|
|
|
|
|
|
|
|
if (tracked.Action is OsuAction action)
|
|
|
|
|
osuInputManager.KeyBindingContainer.TriggerReleased(action);
|
|
|
|
|
|
2023-01-23 08:07:27 +00:00
|
|
|
|
if (positionTrackingTouch == tracked)
|
|
|
|
|
positionTrackingTouch = null;
|
|
|
|
|
|
2023-01-16 10:35:55 +00:00
|
|
|
|
trackedTouches.Remove(tracked);
|
|
|
|
|
|
|
|
|
|
base.OnTouchUp(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TrackedTouch
|
|
|
|
|
{
|
2023-01-17 05:51:45 +00:00
|
|
|
|
public readonly TouchSource Source;
|
2023-01-16 10:35:55 +00:00
|
|
|
|
|
2023-01-23 08:07:27 +00:00
|
|
|
|
public OsuAction? Action;
|
|
|
|
|
|
|
|
|
|
public readonly bool DirectTouch;
|
2023-01-16 10:35:55 +00:00
|
|
|
|
|
2023-01-23 08:07:27 +00:00
|
|
|
|
public TrackedTouch(TouchSource source, OsuAction? action, bool directTouch)
|
2023-01-16 10:35:55 +00:00
|
|
|
|
{
|
2023-01-17 05:51:45 +00:00
|
|
|
|
Source = source;
|
2023-01-16 10:35:55 +00:00
|
|
|
|
Action = action;
|
2023-01-23 08:07:27 +00:00
|
|
|
|
DirectTouch = directTouch;
|
2023-01-16 10:35:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|