diff --git a/osu.Game.Rulesets.Mania.Tests/TestSceneManiaTouchInputArea.cs b/osu.Game.Rulesets.Mania.Tests/TestSceneManiaTouchInputArea.cs new file mode 100644 index 0000000000..30c0113bff --- /dev/null +++ b/osu.Game.Rulesets.Mania.Tests/TestSceneManiaTouchInputArea.cs @@ -0,0 +1,49 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Linq; +using NUnit.Framework; +using osu.Framework.Graphics.Containers; +using osu.Framework.Input; +using osu.Framework.Testing; +using osu.Game.Rulesets.Mania.UI; +using osu.Game.Tests.Visual; + +namespace osu.Game.Rulesets.Mania.Tests +{ + public partial class TestSceneManiaTouchInputArea : PlayerTestScene + { + protected override Ruleset CreatePlayerRuleset() => new ManiaRuleset(); + + [Test] + public void TestTouchAreaNotInitiallyVisible() + { + AddAssert("touch area not visible", () => getTouchOverlay()?.State.Value == Visibility.Hidden); + } + + [Test] + public void TestPressReceptors() + { + AddAssert("touch area not visible", () => getTouchOverlay()?.State.Value == Visibility.Hidden); + + for (int i = 0; i < 4; i++) + { + int index = i; + + AddStep($"touch receptor {index}", () => InputManager.BeginTouch(new Touch(TouchSource.Touch1, getReceptor(index).ScreenSpaceDrawQuad.Centre))); + + AddAssert("action sent", + () => this.ChildrenOfType().SelectMany(m => m.KeyBindingContainer.PressedActions), + () => Does.Contain(getReceptor(index).Action.Value)); + + AddStep($"release receptor {index}", () => InputManager.EndTouch(new Touch(TouchSource.Touch1, getReceptor(index).ScreenSpaceDrawQuad.Centre))); + + AddAssert("touch area visible", () => getTouchOverlay()?.State.Value == Visibility.Visible); + } + } + + private ManiaTouchInputArea? getTouchOverlay() => this.ChildrenOfType().SingleOrDefault(); + + private ManiaTouchInputArea.ColumnInputReceptor getReceptor(int index) => this.ChildrenOfType().ElementAt(index); + } +} diff --git a/osu.Game.Rulesets.Mania/Edit/ManiaHitObjectComposer.cs b/osu.Game.Rulesets.Mania/Edit/ManiaHitObjectComposer.cs index 967cdb0e54..c229039dc3 100644 --- a/osu.Game.Rulesets.Mania/Edit/ManiaHitObjectComposer.cs +++ b/osu.Game.Rulesets.Mania/Edit/ManiaHitObjectComposer.cs @@ -28,7 +28,7 @@ namespace osu.Game.Rulesets.Mania.Edit { } - public new ManiaPlayfield Playfield => ((ManiaPlayfield)drawableRuleset.Playfield); + public new ManiaPlayfield Playfield => drawableRuleset.Playfield; public IScrollingInfo ScrollingInfo => drawableRuleset.ScrollingInfo; diff --git a/osu.Game.Rulesets.Mania/UI/Column.cs b/osu.Game.Rulesets.Mania/UI/Column.cs index 6cd55bb099..c05a8f2a29 100644 --- a/osu.Game.Rulesets.Mania/UI/Column.cs +++ b/osu.Game.Rulesets.Mania/UI/Column.cs @@ -93,8 +93,7 @@ namespace osu.Game.Rulesets.Mania.UI // For input purposes, the background is added at the highest depth, but is then proxied back below all other elements externally // (see `Stage.columnBackgrounds`). BackgroundContainer, - TopLevelContainer, - new ColumnTouchInputArea(this) + TopLevelContainer }; var background = new SkinnableDrawable(new ManiaSkinComponentLookup(ManiaSkinComponents.ColumnBackground), _ => new DefaultColumnBackground()) @@ -181,38 +180,5 @@ namespace osu.Game.Rulesets.Mania.UI public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) // This probably shouldn't exist as is, but the columns in the stage are separated by a 1px border => DrawRectangle.Inflate(new Vector2(Stage.COLUMN_SPACING / 2, 0)).Contains(ToLocalSpace(screenSpacePos)); - - public partial class ColumnTouchInputArea : Drawable - { - private readonly Column column; - - [Resolved(canBeNull: true)] - private ManiaInputManager maniaInputManager { get; set; } - - private KeyBindingContainer keyBindingContainer; - - public ColumnTouchInputArea(Column column) - { - RelativeSizeAxes = Axes.Both; - - this.column = column; - } - - protected override void LoadComplete() - { - keyBindingContainer = maniaInputManager?.KeyBindingContainer; - } - - protected override bool OnTouchDown(TouchDownEvent e) - { - keyBindingContainer?.TriggerPressed(column.Action.Value); - return true; - } - - protected override void OnTouchUp(TouchUpEvent e) - { - keyBindingContainer?.TriggerReleased(column.Action.Value); - } - } } } diff --git a/osu.Game.Rulesets.Mania/UI/DrawableManiaRuleset.cs b/osu.Game.Rulesets.Mania/UI/DrawableManiaRuleset.cs index 275b1311de..ce53862c76 100644 --- a/osu.Game.Rulesets.Mania/UI/DrawableManiaRuleset.cs +++ b/osu.Game.Rulesets.Mania/UI/DrawableManiaRuleset.cs @@ -31,6 +31,7 @@ using osu.Game.Skinning; namespace osu.Game.Rulesets.Mania.UI { + [Cached] public partial class DrawableManiaRuleset : DrawableScrollingRuleset { /// @@ -43,7 +44,7 @@ namespace osu.Game.Rulesets.Mania.UI /// public const double MAX_TIME_RANGE = 11485; - protected new ManiaPlayfield Playfield => (ManiaPlayfield)base.Playfield; + public new ManiaPlayfield Playfield => (ManiaPlayfield)base.Playfield; public new ManiaBeatmap Beatmap => (ManiaBeatmap)base.Beatmap; @@ -103,6 +104,8 @@ namespace osu.Game.Rulesets.Mania.UI configScrollSpeed.BindValueChanged(speed => this.TransformTo(nameof(smoothTimeRange), ComputeScrollTime(speed.NewValue), 200, Easing.OutQuint)); TimeRange.Value = smoothTimeRange = ComputeScrollTime(configScrollSpeed.Value); + + KeyBindingInputManager.Add(new ManiaTouchInputArea()); } protected override void AdjustScrollSpeed(int amount) => configScrollSpeed.Value += amount; diff --git a/osu.Game.Rulesets.Mania/UI/ManiaTouchInputArea.cs b/osu.Game.Rulesets.Mania/UI/ManiaTouchInputArea.cs new file mode 100644 index 0000000000..32e4616a25 --- /dev/null +++ b/osu.Game.Rulesets.Mania/UI/ManiaTouchInputArea.cs @@ -0,0 +1,216 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using osu.Framework.Allocation; +using osu.Framework.Bindables; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Framework.Input.Events; +using osu.Game.Configuration; +using osuTK; + +namespace osu.Game.Rulesets.Mania.UI +{ + /// + /// An overlay that captures and displays osu!mania mouse and touch input. + /// + public partial class ManiaTouchInputArea : VisibilityContainer + { + // visibility state affects our child. we always want to handle input. + public override bool PropagatePositionalInputSubTree => true; + public override bool PropagateNonPositionalInputSubTree => true; + + [SettingSource("Spacing", "The spacing between receptors.")] + public BindableFloat Spacing { get; } = new BindableFloat(10) + { + Precision = 1, + MinValue = 0, + MaxValue = 100, + }; + + [SettingSource("Opacity", "The receptor opacity.")] + public BindableFloat Opacity { get; } = new BindableFloat(1) + { + Precision = 0.1f, + MinValue = 0, + MaxValue = 1 + }; + + [Resolved] + private DrawableManiaRuleset drawableRuleset { get; set; } = null!; + + private GridContainer gridContainer = null!; + + public ManiaTouchInputArea() + { + Anchor = Anchor.BottomCentre; + Origin = Anchor.BottomCentre; + + RelativeSizeAxes = Axes.Both; + Height = 0.5f; + } + + [BackgroundDependencyLoader] + private void load() + { + List receptorGridContent = new List(); + List receptorGridDimensions = new List(); + + bool first = true; + + foreach (var stage in drawableRuleset.Playfield.Stages) + { + foreach (var column in stage.Columns) + { + if (!first) + { + receptorGridContent.Add(new Gutter { Spacing = { BindTarget = Spacing } }); + receptorGridDimensions.Add(new Dimension(GridSizeMode.AutoSize)); + } + + receptorGridContent.Add(new ColumnInputReceptor { Action = { BindTarget = column.Action } }); + receptorGridDimensions.Add(new Dimension()); + + first = false; + } + } + + InternalChild = gridContainer = new GridContainer + { + RelativeSizeAxes = Axes.Both, + AlwaysPresent = true, + Content = new[] { receptorGridContent.ToArray() }, + ColumnDimensions = receptorGridDimensions.ToArray() + }; + } + + protected override void LoadComplete() + { + base.LoadComplete(); + Opacity.BindValueChanged(o => Alpha = o.NewValue, true); + } + + protected override bool OnKeyDown(KeyDownEvent e) + { + // Hide whenever the keyboard is used. + Hide(); + return false; + } + + protected override bool OnMouseDown(MouseDownEvent e) + { + Show(); + return true; + } + + protected override bool OnTouchDown(TouchDownEvent e) + { + Show(); + return true; + } + + protected override void PopIn() + { + gridContainer.FadeIn(500, Easing.OutQuint); + } + + protected override void PopOut() + { + gridContainer.FadeOut(300); + } + + public partial class ColumnInputReceptor : CompositeDrawable + { + public readonly IBindable Action = new Bindable(); + + private readonly Box highlightOverlay; + + [Resolved] + private ManiaInputManager? inputManager { get; set; } + + private bool isPressed; + + public ColumnInputReceptor() + { + RelativeSizeAxes = Axes.Both; + + InternalChildren = new Drawable[] + { + new Container + { + RelativeSizeAxes = Axes.Both, + Masking = true, + CornerRadius = 10, + Children = new Drawable[] + { + new Box + { + RelativeSizeAxes = Axes.Both, + Alpha = 0.15f, + }, + highlightOverlay = new Box + { + RelativeSizeAxes = Axes.Both, + Alpha = 0, + Blending = BlendingParameters.Additive, + } + } + } + }; + } + + protected override bool OnTouchDown(TouchDownEvent e) + { + updateButton(true); + return false; // handled by parent container to show overlay. + } + + protected override void OnTouchUp(TouchUpEvent e) + { + updateButton(false); + } + + protected override bool OnMouseDown(MouseDownEvent e) + { + updateButton(true); + return false; // handled by parent container to show overlay. + } + + protected override void OnMouseUp(MouseUpEvent e) + { + updateButton(false); + } + + private void updateButton(bool press) + { + if (press == isPressed) + return; + + isPressed = press; + + if (press) + { + inputManager?.KeyBindingContainer?.TriggerPressed(Action.Value); + highlightOverlay.FadeTo(0.1f, 80, Easing.OutQuint); + } + else + { + inputManager?.KeyBindingContainer?.TriggerReleased(Action.Value); + highlightOverlay.FadeTo(0, 400, Easing.OutQuint); + } + } + } + + private partial class Gutter : Drawable + { + public readonly IBindable Spacing = new Bindable(); + + public Gutter() + { + Spacing.BindValueChanged(s => Size = new Vector2(s.NewValue)); + } + } + } +}