1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 21:02:54 +08:00
osu-lazer/osu.Game/Tests/Visual/OsuManualInputManagerTestScene.cs

167 lines
6.5 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing;
2018-04-13 17:19:50 +08:00
using osu.Framework.Testing.Input;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterfaceV2;
2020-01-27 11:21:17 +08:00
using osu.Game.Input.Bindings;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Tests.Visual
{
2022-11-24 13:32:20 +08:00
public abstract partial class OsuManualInputManagerTestScene : OsuTestScene
2018-04-13 17:19:50 +08:00
{
protected override Container<Drawable> Content => content;
private readonly Container content;
2018-04-13 17:19:50 +08:00
protected readonly ManualInputManager InputManager;
private readonly RoundedButton buttonTest;
private readonly RoundedButton buttonLocal;
/// <summary>
/// Whether to create a nested container to handle <see cref="GlobalAction"/>s that result from local (manual) test input.
/// This should be disabled when instantiating an <see cref="OsuGame"/> instance else actions will be lost.
/// </summary>
protected virtual bool CreateNestedActionContainer => true;
/// <summary>
/// Whether a menu cursor controlled by the manual input manager should be displayed.
/// True by default, but is disabled for <see cref="OsuGameTestScene"/>s as they provide their own global cursor.
/// </summary>
protected virtual bool DisplayCursorForManualInput => true;
protected OsuManualInputManagerTestScene()
2018-04-13 17:19:50 +08:00
{
var mainContent = content = new Container { RelativeSizeAxes = Axes.Both };
if (DisplayCursorForManualInput)
{
var cursorDisplay = new GlobalCursorDisplay { RelativeSizeAxes = Axes.Both };
cursorDisplay.Add(content = new OsuTooltipContainer(cursorDisplay.MenuCursor)
{
RelativeSizeAxes = Axes.Both,
});
2023-01-17 15:24:47 +08:00
mainContent.Add(cursorDisplay);
}
if (CreateNestedActionContainer)
2023-01-17 15:24:47 +08:00
mainContent.Add(new GlobalActionContainer(null));
base.Content.AddRange(new Drawable[]
{
InputManager = new ManualInputManager
{
UseParentInput = true,
Child = mainContent
},
new Container
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(5),
CornerRadius = 5,
Masking = true,
Children = new Drawable[]
{
new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
Alpha = 0.5f,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Margin = new MarginPadding(5),
Spacing = new Vector2(5),
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "Input Priority"
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Margin = new MarginPadding(5),
Spacing = new Vector2(5),
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
buttonLocal = new RoundedButton
{
Text = "local",
Size = new Vector2(50, 30),
Action = returnUserInput
},
buttonTest = new RoundedButton
{
Text = "test",
Size = new Vector2(50, 30),
Action = returnTestInput
},
}
},
}
},
}
},
});
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Wait for a button to become enabled, then click it.
/// </summary>
/// <typeparam name="T"></typeparam>
protected void ClickButtonWhenEnabled<T>()
where T : Drawable
{
if (typeof(T) == typeof(Button))
2022-03-17 16:59:26 +08:00
AddUntilStep($"wait for {typeof(T).Name} enabled", () => (this.ChildrenOfType<T>().Single() as ClickableContainer)?.Enabled.Value == true);
else
2022-03-17 16:59:26 +08:00
AddUntilStep($"wait for {typeof(T).Name} enabled", () => this.ChildrenOfType<T>().Single().ChildrenOfType<ClickableContainer>().Single().Enabled.Value);
AddStep($"click {typeof(T).Name}", () =>
{
InputManager.MoveMouseTo(this.ChildrenOfType<T>().Single());
InputManager.Click(MouseButton.Left);
});
}
protected override void Update()
{
base.Update();
buttonTest.Enabled.Value = InputManager.UseParentInput;
buttonLocal.Enabled.Value = !InputManager.UseParentInput;
2018-04-13 17:19:50 +08:00
}
private void returnUserInput() =>
InputManager.UseParentInput = true;
private void returnTestInput() =>
InputManager.UseParentInput = false;
2018-04-13 17:19:50 +08:00
}
}