2023-10-30 13:08:56 +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.
|
|
|
|
|
2023-11-02 18:48:34 +00:00
|
|
|
using System.Diagnostics;
|
2023-10-30 13:08:56 +00:00
|
|
|
using osu.Framework.Allocation;
|
2023-11-06 06:48:09 +00:00
|
|
|
using osu.Framework.Bindables;
|
2023-11-02 18:48:34 +00:00
|
|
|
using osu.Framework.Extensions.TypeExtensions;
|
2023-10-30 13:08:56 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
using osu.Framework.Input.StateChanges;
|
2023-11-02 18:48:34 +00:00
|
|
|
using osu.Framework.Logging;
|
2023-10-30 13:08:56 +00:00
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osuTK;
|
2023-11-02 18:48:34 +00:00
|
|
|
using osuTK.Input;
|
2023-10-30 13:08:56 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Input
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Intercepts all positional input events and sets the appropriate <see cref="Static.TouchInputActive"/> value
|
|
|
|
/// for consumption by particular game screens.
|
|
|
|
/// </summary>
|
|
|
|
public partial class TouchInputInterceptor : Component
|
|
|
|
{
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
|
|
|
|
2023-11-06 06:48:09 +00:00
|
|
|
private readonly BindableBool touchInputActive = new BindableBool();
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(SessionStatics statics)
|
|
|
|
{
|
|
|
|
statics.BindWith(Static.TouchInputActive, touchInputActive);
|
|
|
|
}
|
2023-10-30 13:08:56 +00:00
|
|
|
|
|
|
|
protected override bool Handle(UIEvent e)
|
|
|
|
{
|
2023-11-06 06:48:09 +00:00
|
|
|
bool touchInputWasActive = touchInputActive.Value;
|
2023-11-02 18:48:34 +00:00
|
|
|
|
2023-10-30 13:08:56 +00:00
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case MouseEvent:
|
|
|
|
if (e.CurrentState.Mouse.LastSource is not ISourcedFromTouch)
|
2023-11-02 18:48:34 +00:00
|
|
|
{
|
|
|
|
if (touchInputWasActive)
|
|
|
|
Logger.Log($@"Touch input deactivated due to received {e.GetType().ReadableName()}", LoggingTarget.Input);
|
2023-11-06 06:48:09 +00:00
|
|
|
touchInputActive.Value = false;
|
2023-11-02 18:48:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:08:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TouchEvent:
|
2023-11-02 18:48:34 +00:00
|
|
|
if (!touchInputWasActive)
|
|
|
|
Logger.Log($@"Touch input activated due to received {e.GetType().ReadableName()}", LoggingTarget.Input);
|
2023-11-06 06:48:09 +00:00
|
|
|
touchInputActive.Value = true;
|
2023-10-30 13:08:56 +00:00
|
|
|
break;
|
2023-11-02 18:48:34 +00:00
|
|
|
|
|
|
|
case KeyDownEvent keyDown:
|
|
|
|
if (keyDown.Key == Key.T && keyDown.ControlPressed && keyDown.ShiftPressed)
|
|
|
|
debugToggleTouchInputActive();
|
|
|
|
break;
|
2023-10-30 13:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-02 18:48:34 +00:00
|
|
|
|
|
|
|
[Conditional("TOUCH_INPUT_DEBUG")]
|
|
|
|
private void debugToggleTouchInputActive()
|
|
|
|
{
|
2023-11-06 10:50:04 +00:00
|
|
|
Logger.Log($@"Debug-toggling touch input to {(touchInputActive.Value ? @"inactive" : @"active")}", LoggingTarget.Information, LogLevel.Important);
|
2023-11-06 06:48:09 +00:00
|
|
|
touchInputActive.Toggle();
|
2023-11-02 18:48:34 +00:00
|
|
|
}
|
2023-10-30 13:08:56 +00:00
|
|
|
}
|
|
|
|
}
|