Skip to content

Commit

Permalink
Merge pull request #19389 from peppy/cursor-with-touch
Browse files Browse the repository at this point in the history
Add ability to make cursor show even during touch input
  • Loading branch information
smoogipoo authored Jul 26, 2022
2 parents a7598c6 + ef10145 commit 5345e43
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 132 deletions.
2 changes: 1 addition & 1 deletion osu.Game.Tests/Visual/Gameplay/TestScenePause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TestScenePause : OsuPlayerTestScene

public TestScenePause()
{
base.Content.Add(content = new MenuCursorContainer { RelativeSizeAxes = Axes.Both });
base.Content.Add(content = new GlobalCursorDisplay { RelativeSizeAxes = Axes.Both });
}

[SetUpSteps]
Expand Down
62 changes: 31 additions & 31 deletions osu.Game.Tests/Visual/UserInterface/TestSceneCursors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace osu.Game.Tests.Visual.UserInterface
[TestFixture]
public class TestSceneCursors : OsuManualInputManagerTestScene
{
private readonly MenuCursorContainer menuCursorContainer;
private readonly GlobalCursorDisplay globalCursorDisplay;
private readonly CustomCursorBox[] cursorBoxes = new CustomCursorBox[6];

public TestSceneCursors()
{
Child = menuCursorContainer = new MenuCursorContainer
Child = globalCursorDisplay = new GlobalCursorDisplay
{
RelativeSizeAxes = Axes.Both,
Children = new[]
Expand Down Expand Up @@ -96,11 +96,11 @@ public TestSceneCursors()
private void testUserCursor()
{
AddStep("Move to green area", () => InputManager.MoveMouseTo(cursorBoxes[0]));
AddAssert("Check green cursor visible", () => checkVisible(cursorBoxes[0].Cursor));
AddAssert("Check green cursor at mouse", () => checkAtMouse(cursorBoxes[0].Cursor));
AddAssert("Check green cursor visible", () => checkVisible(cursorBoxes[0].MenuCursor));
AddAssert("Check green cursor at mouse", () => checkAtMouse(cursorBoxes[0].MenuCursor));
AddStep("Move out", moveOut);
AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].Cursor));
AddAssert("Check global cursor visible", () => checkVisible(menuCursorContainer.Cursor));
AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].MenuCursor));
AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
}

/// <summary>
Expand All @@ -111,13 +111,13 @@ private void testUserCursor()
private void testLocalCursor()
{
AddStep("Move to purple area", () => InputManager.MoveMouseTo(cursorBoxes[3]));
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].Cursor));
AddAssert("Check global cursor visible", () => checkVisible(menuCursorContainer.Cursor));
AddAssert("Check global cursor at mouse", () => checkAtMouse(menuCursorContainer.Cursor));
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].MenuCursor));
AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].MenuCursor));
AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
AddAssert("Check global cursor at mouse", () => checkAtMouse(globalCursorDisplay.MenuCursor));
AddStep("Move out", moveOut);
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
AddAssert("Check global cursor visible", () => checkVisible(menuCursorContainer.Cursor));
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].MenuCursor));
AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
}

/// <summary>
Expand All @@ -128,12 +128,12 @@ private void testLocalCursor()
private void testUserCursorOverride()
{
AddStep("Move to blue-green boundary", () => InputManager.MoveMouseTo(cursorBoxes[1].ScreenSpaceDrawQuad.BottomRight - new Vector2(10)));
AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].Cursor));
AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].Cursor));
AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].Cursor));
AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].MenuCursor));
AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].MenuCursor));
AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].MenuCursor));
AddStep("Move out", moveOut);
AddAssert("Check blue cursor not visible", () => !checkVisible(cursorBoxes[1].Cursor));
AddAssert("Check green cursor not visible", () => !checkVisible(cursorBoxes[0].Cursor));
AddAssert("Check blue cursor not visible", () => !checkVisible(cursorBoxes[1].MenuCursor));
AddAssert("Check green cursor not visible", () => !checkVisible(cursorBoxes[0].MenuCursor));
}

/// <summary>
Expand All @@ -143,13 +143,13 @@ private void testUserCursorOverride()
private void testMultipleLocalCursors()
{
AddStep("Move to yellow-purple boundary", () => InputManager.MoveMouseTo(cursorBoxes[5].ScreenSpaceDrawQuad.BottomRight - new Vector2(10)));
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].Cursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].Cursor));
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].MenuCursor));
AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].MenuCursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].MenuCursor));
AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].MenuCursor));
AddStep("Move out", moveOut);
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].MenuCursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].MenuCursor));
}

/// <summary>
Expand All @@ -159,13 +159,13 @@ private void testMultipleLocalCursors()
private void testUserOverrideWithLocal()
{
AddStep("Move to yellow-blue boundary", () => InputManager.MoveMouseTo(cursorBoxes[5].ScreenSpaceDrawQuad.TopRight - new Vector2(10)));
AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].Cursor));
AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].Cursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].Cursor));
AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].MenuCursor));
AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].MenuCursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].MenuCursor));
AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].MenuCursor));
AddStep("Move out", moveOut);
AddAssert("Check blue cursor invisible", () => !checkVisible(cursorBoxes[1].Cursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
AddAssert("Check blue cursor invisible", () => !checkVisible(cursorBoxes[1].MenuCursor));
AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].MenuCursor));
}

/// <summary>
Expand All @@ -191,7 +191,7 @@ private class CustomCursorBox : Container, IProvideCursor
{
public bool SmoothTransition;

public CursorContainer Cursor { get; }
public CursorContainer MenuCursor { get; }
public bool ProvidingUserCursor { get; }

public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => base.ReceivePositionalInputAt(screenSpacePos) || (SmoothTransition && !ProvidingUserCursor);
Expand All @@ -218,7 +218,7 @@ public CustomCursorBox(Color4 cursorColour, bool providesUserCursor = true)
Origin = Anchor.Centre,
Text = providesUserCursor ? "User cursor" : "Local cursor"
},
Cursor = new TestCursorContainer
MenuCursor = new TestCursorContainer
{
State = { Value = providesUserCursor ? Visibility.Hidden : Visibility.Visible },
}
Expand Down
4 changes: 2 additions & 2 deletions osu.Game.Tournament/TournamentGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ private void load(Storage baseStorage)

protected override void LoadComplete()
{
MenuCursorContainer.Cursor.AlwaysPresent = true; // required for tooltip display
GlobalCursorDisplay.MenuCursor.AlwaysPresent = true; // required for tooltip display

// we don't want to show the menu cursor as it would appear on stream output.
MenuCursorContainer.Cursor.Alpha = 0;
GlobalCursorDisplay.MenuCursor.Alpha = 0;

base.LoadComplete();

Expand Down
2 changes: 2 additions & 0 deletions osu.Game/Configuration/OsuConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected override void InitialiseDefaults()
// Input
SetDefault(OsuSetting.MenuCursorSize, 1.0f, 0.5f, 2f, 0.01f);
SetDefault(OsuSetting.GameplayCursorSize, 1.0f, 0.1f, 2f, 0.01f);
SetDefault(OsuSetting.GameplayCursorDuringTouch, false);
SetDefault(OsuSetting.AutoCursorSize, false);

SetDefault(OsuSetting.MouseDisableButtons, false);
Expand Down Expand Up @@ -292,6 +293,7 @@ public enum OsuSetting
MenuCursorSize,
GameplayCursorSize,
AutoCursorSize,
GameplayCursorDuringTouch,
DimLevel,
BlurLevel,
LightenDuringBreaks,
Expand Down
92 changes: 92 additions & 0 deletions osu.Game/Graphics/Cursor/GlobalCursorDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Input;
using osu.Framework.Input.StateChanges;
using osu.Game.Configuration;

namespace osu.Game.Graphics.Cursor
{
/// <summary>
/// A container which provides the main <see cref="Cursor.MenuCursor"/>.
/// Also handles cases where a more localised cursor is provided by another component (via <see cref="IProvideCursor"/>).
/// </summary>
public class GlobalCursorDisplay : Container, IProvideCursor
{
/// <summary>
/// Control whether any cursor should be displayed.
/// </summary>
internal bool ShowCursor = true;

public CursorContainer MenuCursor { get; }

public bool ProvidingUserCursor => true;

protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };

private Bindable<bool> showDuringTouch = null!;

private InputManager inputManager = null!;

private IProvideCursor? currentOverrideProvider;

[Resolved]
private OsuConfigManager config { get; set; } = null!;

public GlobalCursorDisplay()
{
AddRangeInternal(new Drawable[]
{
MenuCursor = new MenuCursor { State = { Value = Visibility.Hidden } },
Content = new Container { RelativeSizeAxes = Axes.Both }
});
}

protected override void LoadComplete()
{
base.LoadComplete();

inputManager = GetContainingInputManager();
showDuringTouch = config.GetBindable<bool>(OsuSetting.GameplayCursorDuringTouch);
}

protected override void Update()
{
base.Update();

var lastMouseSource = inputManager.CurrentState.Mouse.LastSource;
bool hasValidInput = lastMouseSource != null && (showDuringTouch.Value || lastMouseSource is not ISourcedFromTouch);

if (!hasValidInput || !ShowCursor)
{
currentOverrideProvider?.MenuCursor?.Hide();
currentOverrideProvider = null;
return;
}

IProvideCursor newOverrideProvider = this;

foreach (var d in inputManager.HoveredDrawables)
{
if (d is IProvideCursor p && p.ProvidingUserCursor)
{
newOverrideProvider = p;
break;
}
}

if (currentOverrideProvider == newOverrideProvider)
return;

currentOverrideProvider?.MenuCursor?.Hide();
newOverrideProvider.MenuCursor?.Show();

currentOverrideProvider = newOverrideProvider;
}
}
}
4 changes: 2 additions & 2 deletions osu.Game/Graphics/Cursor/IProvideCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public interface IProvideCursor : IDrawable
/// The cursor provided by this <see cref="IDrawable"/>.
/// May be null if no cursor should be visible.
/// </summary>
CursorContainer Cursor { get; }
CursorContainer MenuCursor { get; }

/// <summary>
/// Whether <see cref="Cursor"/> should be displayed as the singular user cursor. This will temporarily hide any other user cursor.
/// Whether <see cref="MenuCursor"/> should be displayed as the singular user cursor. This will temporarily hide any other user cursor.
/// This value is checked every frame and may be used to control whether multiple cursors are displayed (e.g. watching replays).
/// </summary>
bool ProvidingUserCursor { get; }
Expand Down
83 changes: 0 additions & 83 deletions osu.Game/Graphics/Cursor/MenuCursorContainer.cs

This file was deleted.

5 changes: 5 additions & 0 deletions osu.Game/Localisation/SkinSettingsStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static class SkinSettingsStrings
/// </summary>
public static LocalisableString AutoCursorSize => new TranslatableString(getKey(@"auto_cursor_size"), @"Adjust gameplay cursor size based on current beatmap");

/// <summary>
/// "Show gameplay cursor during touch input"
/// </summary>
public static LocalisableString GameplayCursorDuringTouch => new TranslatableString(getKey(@"gameplay_cursor_during_touch"), @"Show gameplay cursor during touch input");

/// <summary>
/// "Beatmap skins"
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions osu.Game/OsuGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ protected override void LoadComplete()
// The next time this is updated is in UpdateAfterChildren, which occurs too late and results
// in the cursor being shown for a few frames during the intro.
// This prevents the cursor from showing until we have a screen with CursorVisible = true
MenuCursorContainer.CanShowCursor = menuScreen?.CursorVisible ?? false;
GlobalCursorDisplay.ShowCursor = menuScreen?.CursorVisible ?? false;

// todo: all archive managers should be able to be looped here.
SkinManager.PostNotification = n => Notifications.Post(n);
Expand Down Expand Up @@ -1231,7 +1231,7 @@ protected override void UpdateAfterChildren()
ScreenOffsetContainer.X = horizontalOffset;
overlayContent.X = horizontalOffset * 1.2f;

MenuCursorContainer.CanShowCursor = (ScreenStack.CurrentScreen as IOsuScreen)?.CursorVisible ?? false;
GlobalCursorDisplay.ShowCursor = (ScreenStack.CurrentScreen as IOsuScreen)?.CursorVisible ?? false;
}

private void screenChanged(IScreen current, IScreen newScreen)
Expand Down
Loading

0 comments on commit 5345e43

Please sign in to comment.