2023-01-16 18:34:59 +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.
|
|
|
|
|
|
2023-01-16 19:45:04 +08:00
|
|
|
|
using System.Diagnostics;
|
2023-01-16 18:34:59 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
2023-01-16 19:58:24 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2023-01-16 18:34:59 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2023-01-16 19:45:04 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2023-01-16 18:34:59 +08:00
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
|
using osu.Framework.Input.Events;
|
2023-01-16 19:45:04 +08:00
|
|
|
|
using osu.Framework.Input.States;
|
2023-01-16 18:34:59 +08:00
|
|
|
|
using osu.Framework.Testing;
|
2023-01-16 19:58:24 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2023-01-16 18:34:59 +08:00
|
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
|
using osu.Game.Tests.Visual;
|
|
|
|
|
using osuTK;
|
2023-01-16 19:45:04 +08:00
|
|
|
|
using osuTK.Graphics;
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.Tests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public partial class TestSceneTouchInput : OsuManualInputManagerTestScene
|
|
|
|
|
{
|
2023-01-16 19:58:24 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OsuConfigManager config { get; set; } = null!;
|
|
|
|
|
|
2023-01-16 18:34:59 +08:00
|
|
|
|
private TestActionKeyCounter leftKeyCounter = null!;
|
|
|
|
|
|
|
|
|
|
private TestActionKeyCounter rightKeyCounter = null!;
|
|
|
|
|
|
|
|
|
|
private OsuInputManager osuInputManager = null!;
|
|
|
|
|
|
|
|
|
|
[SetUpSteps]
|
|
|
|
|
public void SetUpSteps()
|
|
|
|
|
{
|
|
|
|
|
releaseAllTouches();
|
|
|
|
|
|
|
|
|
|
AddStep("Create tests", () =>
|
|
|
|
|
{
|
2023-01-16 19:45:04 +08:00
|
|
|
|
Children = new Drawable[]
|
2023-01-16 18:34:59 +08:00
|
|
|
|
{
|
2023-01-16 19:45:04 +08:00
|
|
|
|
osuInputManager = new OsuInputManager(new OsuRuleset().RulesetInfo)
|
2023-01-16 18:34:59 +08:00
|
|
|
|
{
|
2023-01-16 19:45:04 +08:00
|
|
|
|
Child = new Container
|
2023-01-16 18:34:59 +08:00
|
|
|
|
{
|
2023-01-16 19:45:04 +08:00
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
leftKeyCounter = new TestActionKeyCounter(OsuAction.LeftButton)
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
X = -100,
|
|
|
|
|
},
|
|
|
|
|
rightKeyCounter = new TestActionKeyCounter(OsuAction.RightButton)
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
|
X = 100,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new TouchVisualiser(),
|
2023-01-16 18:34:59 +08:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestSimpleInput()
|
|
|
|
|
{
|
|
|
|
|
beginTouch(TouchSource.Touch1);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 0);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
beginTouch(TouchSource.Touch2);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
// Subsequent touches should be ignored.
|
|
|
|
|
beginTouch(TouchSource.Touch3);
|
|
|
|
|
beginTouch(TouchSource.Touch4);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 19:58:24 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestSimpleInputButtonsDisabled()
|
|
|
|
|
{
|
|
|
|
|
AddStep("Disable mouse buttons", () => config.SetValue(OsuSetting.MouseDisableButtons, true));
|
|
|
|
|
|
|
|
|
|
beginTouch(TouchSource.Touch1);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(0, 0);
|
|
|
|
|
checkNotPressed(OsuAction.LeftButton);
|
|
|
|
|
|
|
|
|
|
beginTouch(TouchSource.Touch2);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(0, 0);
|
|
|
|
|
checkNotPressed(OsuAction.LeftButton);
|
|
|
|
|
checkNotPressed(OsuAction.RightButton);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 18:34:59 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestAlternatingInput()
|
|
|
|
|
{
|
|
|
|
|
beginTouch(TouchSource.Touch1);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 0);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
beginTouch(TouchSource.Touch2);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
{
|
|
|
|
|
endTouch(TouchSource.Touch1);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.RightButton);
|
|
|
|
|
checkNotPressed(OsuAction.LeftButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
beginTouch(TouchSource.Touch1);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
endTouch(TouchSource.Touch2);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkNotPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
beginTouch(TouchSource.Touch2);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
|
|
|
|
}
|
2023-01-16 18:34:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPressReleaseOrder()
|
|
|
|
|
{
|
|
|
|
|
beginTouch(TouchSource.Touch1);
|
|
|
|
|
beginTouch(TouchSource.Touch2);
|
|
|
|
|
beginTouch(TouchSource.Touch3);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
// Touch 3 was ignored, but let's ensure that if 1 or 2 are released, 3 will be handled a second attempt.
|
|
|
|
|
endTouch(TouchSource.Touch1);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
endTouch(TouchSource.Touch3);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
beginTouch(TouchSource.Touch3);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(2, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestWithDisallowedUserCursor()
|
|
|
|
|
{
|
|
|
|
|
beginTouch(TouchSource.Touch1);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 0);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
beginTouch(TouchSource.Touch2);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
// Subsequent touches should be ignored.
|
|
|
|
|
beginTouch(TouchSource.Touch3);
|
|
|
|
|
beginTouch(TouchSource.Touch4);
|
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
checkPressed(OsuAction.LeftButton);
|
|
|
|
|
checkPressed(OsuAction.RightButton);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
assertKeyCounter(1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void beginTouch(TouchSource source, Vector2? screenSpacePosition = null) =>
|
2023-01-16 19:45:04 +08:00
|
|
|
|
AddStep($"Begin touch for {source}", () => InputManager.BeginTouch(new Touch(source, screenSpacePosition ??= getSanePositionForSource(source))));
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
2023-01-16 19:45:04 +08:00
|
|
|
|
private void endTouch(TouchSource source, Vector2? screenSpacePosition = null) =>
|
|
|
|
|
AddStep($"Release touch for {source}", () => InputManager.EndTouch(new Touch(source, screenSpacePosition ??= getSanePositionForSource(source))));
|
|
|
|
|
|
|
|
|
|
private Vector2 getSanePositionForSource(TouchSource source)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2(
|
|
|
|
|
osuInputManager.ScreenSpaceDrawQuad.Centre.X + osuInputManager.ScreenSpaceDrawQuad.Width * (-1 + (int)source) / 8,
|
|
|
|
|
osuInputManager.ScreenSpaceDrawQuad.Centre.Y - 100
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
private void assertKeyCounter(int left, int right)
|
|
|
|
|
{
|
|
|
|
|
AddAssert($"The left key was pressed {left} times", () => leftKeyCounter.CountPresses, () => Is.EqualTo(left));
|
|
|
|
|
AddAssert($"The right key was pressed {right} times", () => rightKeyCounter.CountPresses, () => Is.EqualTo(right));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void releaseAllTouches()
|
|
|
|
|
{
|
|
|
|
|
AddStep("Release all touches", () =>
|
|
|
|
|
{
|
2023-01-16 19:58:24 +08:00
|
|
|
|
config.SetValue(OsuSetting.MouseDisableButtons, false);
|
2023-01-16 18:34:59 +08:00
|
|
|
|
foreach (TouchSource source in InputManager.CurrentState.Touch.ActiveSources)
|
|
|
|
|
InputManager.EndTouch(new Touch(source, osuInputManager.ScreenSpaceDrawQuad.Centre));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 19:55:53 +08:00
|
|
|
|
private void checkNotPressed(OsuAction action) => AddAssert($"Not pressing {action}", () => !osuInputManager.PressedActions.Contains(action));
|
|
|
|
|
private void checkPressed(OsuAction action) => AddAssert($"Is pressing {action}", () => osuInputManager.PressedActions.Contains(action));
|
2023-01-16 18:34:59 +08:00
|
|
|
|
|
|
|
|
|
public partial class TestActionKeyCounter : KeyCounter, IKeyBindingHandler<OsuAction>
|
|
|
|
|
{
|
|
|
|
|
public OsuAction Action { get; }
|
|
|
|
|
|
|
|
|
|
public TestActionKeyCounter(OsuAction action)
|
|
|
|
|
: base(action.ToString())
|
|
|
|
|
{
|
|
|
|
|
Action = action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OnPressed(KeyBindingPressEvent<OsuAction> e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Action == Action)
|
|
|
|
|
{
|
|
|
|
|
IsLit = true;
|
|
|
|
|
Increment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<OsuAction> e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Action == Action) IsLit = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-16 19:45:04 +08:00
|
|
|
|
|
|
|
|
|
public partial class TouchVisualiser : CompositeDrawable
|
|
|
|
|
{
|
|
|
|
|
private readonly Drawable?[] drawableTouches = new Drawable?[10];
|
|
|
|
|
|
|
|
|
|
public TouchVisualiser()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
|
|
|
|
|
|
|
|
|
protected override bool OnTouchDown(TouchDownEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (IsDisposed)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var circle = new Circle
|
|
|
|
|
{
|
|
|
|
|
Alpha = 0.5f,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Size = new Vector2(20),
|
|
|
|
|
Position = e.Touch.Position,
|
|
|
|
|
Colour = colourFor(e.Touch.Source),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddInternal(circle);
|
|
|
|
|
drawableTouches[(int)e.Touch.Source] = circle;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnTouchMove(TouchMoveEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (IsDisposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var circle = drawableTouches[(int)e.Touch.Source];
|
|
|
|
|
|
|
|
|
|
Debug.Assert(circle != null);
|
|
|
|
|
|
|
|
|
|
AddInternal(new FadingCircle(circle));
|
|
|
|
|
circle.Position = e.Touch.Position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnTouchUp(TouchUpEvent e)
|
|
|
|
|
{
|
|
|
|
|
var circle = drawableTouches[(int)e.Touch.Source];
|
|
|
|
|
circle.FadeOut(200, Easing.OutQuint).Expire();
|
|
|
|
|
drawableTouches[(int)e.Touch.Source] = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Color4 colourFor(TouchSource source)
|
|
|
|
|
{
|
|
|
|
|
return Color4.FromHsv(new Vector4((float)source / TouchState.MAX_TOUCH_COUNT, 1f, 1f, 1f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private partial class FadingCircle : Circle
|
|
|
|
|
{
|
|
|
|
|
public FadingCircle(Drawable source)
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.Centre;
|
|
|
|
|
Size = source.Size;
|
|
|
|
|
Position = source.Position;
|
|
|
|
|
Colour = source.Colour;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
this.FadeOut(200).Expire();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-16 18:34:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|