2023-01-16 18:35:55 +08: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 19:13:31 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
2023-01-16 18:35:55 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.Input.Events;
|
2023-01-16 19:11:59 +08:00
|
|
|
|
using osu.Framework.Input.StateChanges;
|
2023-01-16 19:13:31 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2023-01-16 18:35:55 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.UI
|
|
|
|
|
{
|
|
|
|
|
public partial class OsuTouchInputMapper : Drawable
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is our parent <see cref="osuInputManager"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly OsuInputManager osuInputManager;
|
|
|
|
|
|
|
|
|
|
/// <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-16 19:13:31 +08:00
|
|
|
|
private Bindable<bool> mouseDisabled = null!;
|
|
|
|
|
|
2023-01-16 18:35:55 +08:00
|
|
|
|
public OsuTouchInputMapper(OsuInputManager inputManager)
|
|
|
|
|
{
|
|
|
|
|
osuInputManager = inputManager;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 19:13:31 +08:00
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(OsuConfigManager config)
|
|
|
|
|
{
|
|
|
|
|
mouseDisabled = config.GetBindable<bool>(OsuSetting.MouseDisableButtons);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 19:11:59 +08:00
|
|
|
|
protected override void OnTouchMove(TouchMoveEvent e)
|
|
|
|
|
{
|
|
|
|
|
base.OnTouchMove(e);
|
|
|
|
|
handleTouchMovement(e);
|
|
|
|
|
}
|
2023-01-16 18:35:55 +08:00
|
|
|
|
|
|
|
|
|
protected override bool OnTouchDown(TouchDownEvent e)
|
|
|
|
|
{
|
2023-01-16 19:11:59 +08:00
|
|
|
|
OsuAction action = trackedTouches.Any(t => t.Action == OsuAction.LeftButton)
|
|
|
|
|
? OsuAction.RightButton
|
|
|
|
|
: OsuAction.LeftButton;
|
|
|
|
|
|
|
|
|
|
handleTouchMovement(e);
|
2023-01-16 18:35:55 +08:00
|
|
|
|
|
2023-01-16 19:13:31 +08:00
|
|
|
|
if (!mouseDisabled.Value && trackedTouches.All(t => t.Action != action))
|
2023-01-16 18:35:55 +08:00
|
|
|
|
{
|
|
|
|
|
trackedTouches.Add(new TrackedTouch(e.Touch, action));
|
|
|
|
|
osuInputManager.KeyBindingContainer.TriggerPressed(action);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Ignore any taps which trigger an action which is already handled. But track them for potential positional input in the future.
|
|
|
|
|
trackedTouches.Add(new TrackedTouch(e.Touch, null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 19:11:59 +08:00
|
|
|
|
private void handleTouchMovement(TouchEvent touchEvent)
|
|
|
|
|
{
|
|
|
|
|
new MousePositionAbsoluteInput { Position = touchEvent.ScreenSpaceTouch.Position }.Apply(osuInputManager.CurrentState, osuInputManager);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 18:35:55 +08:00
|
|
|
|
protected override void OnTouchUp(TouchUpEvent e)
|
|
|
|
|
{
|
|
|
|
|
var tracked = trackedTouches.First(t => t.Touch.Source == e.Touch.Source);
|
|
|
|
|
|
|
|
|
|
if (tracked.Action is OsuAction action)
|
|
|
|
|
osuInputManager.KeyBindingContainer.TriggerReleased(action);
|
|
|
|
|
|
|
|
|
|
trackedTouches.Remove(tracked);
|
|
|
|
|
|
|
|
|
|
base.OnTouchUp(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TrackedTouch
|
|
|
|
|
{
|
|
|
|
|
public readonly Touch Touch;
|
|
|
|
|
|
|
|
|
|
public readonly OsuAction? Action;
|
|
|
|
|
|
|
|
|
|
public TrackedTouch(Touch touch, OsuAction? action)
|
|
|
|
|
{
|
|
|
|
|
Touch = touch;
|
|
|
|
|
Action = action;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|