From 75fbbb35ad2f88524670a19312dcb43569db8190 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Mon, 6 Nov 2023 18:28:51 +0300 Subject: [PATCH] Move cursor scale application within `OsuCursor` Doing so takes down two birds with one stone. 1. `ResumeOverlay` having to manually apply cursor scale to its "resume cursor". 2. Resume cursor input handling scaling up with the gameplay setting. Now, only the sprite itself gets scaled. --- osu.Game.Rulesets.Osu/UI/Cursor/OsuCursor.cs | 67 ++++++++++++++++--- .../UI/Cursor/OsuCursorContainer.cs | 61 ++--------------- osu.Game.Rulesets.Osu/UI/OsuResumeOverlay.cs | 12 +--- 3 files changed, 68 insertions(+), 72 deletions(-) diff --git a/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursor.cs b/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursor.cs index 66c86ee09d..ab1bb0cf5a 100644 --- a/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursor.cs +++ b/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursor.cs @@ -4,12 +4,16 @@ #nullable disable using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Effects; using osu.Framework.Graphics.Shapes; +using osu.Game.Beatmaps; +using osu.Game.Configuration; using osu.Game.Rulesets.Osu.Skinning; +using osu.Game.Screens.Play; using osu.Game.Skinning; using osuTK; using osuTK.Graphics; @@ -20,12 +24,29 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor { private const float size = 28; + private const float pressed_scale = 1.2f; + private const float released_scale = 1f; + private bool cursorExpand; private SkinnableDrawable cursorSprite; + private Container cursorScaleContainer = null!; private Drawable expandTarget => (cursorSprite.Drawable as OsuCursorSprite)?.ExpandTarget ?? cursorSprite; + public IBindable CursorScale => cursorScale; + + private readonly Bindable cursorScale = new BindableFloat(1); + + private Bindable userCursorScale = null!; + private Bindable autoCursorScale = null!; + + [Resolved(canBeNull: true)] + private GameplayState state { get; set; } + + [Resolved] + private OsuConfigManager config { get; set; } + public OsuCursor() { Origin = Anchor.Centre; @@ -33,15 +54,10 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor Size = new Vector2(size); } - protected override void SkinChanged(ISkinSource skin) - { - cursorExpand = skin.GetConfig(OsuSkinConfiguration.CursorExpand)?.Value ?? true; - } - [BackgroundDependencyLoader] private void load() { - InternalChild = new Container + InternalChild = cursorScaleContainer = new Container { RelativeSizeAxes = Axes.Both, Origin = Anchor.Centre, @@ -52,10 +68,45 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor Anchor = Anchor.Centre, } }; + + userCursorScale = config.GetBindable(OsuSetting.GameplayCursorSize); + userCursorScale.ValueChanged += _ => calculateCursorScale(); + + autoCursorScale = config.GetBindable(OsuSetting.AutoCursorSize); + autoCursorScale.ValueChanged += _ => calculateCursorScale(); + + cursorScale.BindValueChanged(e => cursorScaleContainer.Scale = new Vector2(e.NewValue), true); } - private const float pressed_scale = 1.2f; - private const float released_scale = 1f; + protected override void LoadComplete() + { + base.LoadComplete(); + calculateCursorScale(); + } + + /// + /// Get the scale applicable to the ActiveCursor based on a beatmap's circle size. + /// + public static float GetScaleForCircleSize(float circleSize) => + 1f - 0.7f * (1f + circleSize - BeatmapDifficulty.DEFAULT_DIFFICULTY) / BeatmapDifficulty.DEFAULT_DIFFICULTY; + + private void calculateCursorScale() + { + float scale = userCursorScale.Value; + + if (autoCursorScale.Value && state != null) + { + // if we have a beatmap available, let's get its circle size to figure out an automatic cursor scale modifier. + scale *= GetScaleForCircleSize(state.Beatmap.Difficulty.CircleSize); + } + + cursorScale.Value = scale; + } + + protected override void SkinChanged(ISkinSource skin) + { + cursorExpand = skin.GetConfig(OsuSkinConfiguration.CursorExpand)?.Value ?? true; + } public void Expand() { diff --git a/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursorContainer.cs b/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursorContainer.cs index bf1ff872dd..ba8a634ff7 100644 --- a/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursorContainer.cs +++ b/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursorContainer.cs @@ -11,11 +11,8 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Textures; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; -using osu.Game.Beatmaps; -using osu.Game.Configuration; using osu.Game.Rulesets.Osu.Configuration; using osu.Game.Rulesets.UI; -using osu.Game.Screens.Play; using osu.Game.Skinning; using osuTK; @@ -23,6 +20,8 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor { public partial class OsuCursorContainer : GameplayCursorContainer, IKeyBindingHandler { + public new OsuCursor ActiveCursor => (OsuCursor)base.ActiveCursor; + protected override Drawable CreateCursor() => new OsuCursor(); protected override Container Content => fadeContainer; @@ -33,13 +32,6 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor private readonly Drawable cursorTrail; - public IBindable CursorScale => cursorScale; - - private readonly Bindable cursorScale = new BindableFloat(1); - - private Bindable userCursorScale; - private Bindable autoCursorScale; - private readonly CursorRippleVisualiser rippleVisualiser; public OsuCursorContainer() @@ -56,12 +48,6 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor }; } - [Resolved(canBeNull: true)] - private GameplayState state { get; set; } - - [Resolved] - private OsuConfigManager config { get; set; } - [BackgroundDependencyLoader(true)] private void load(OsuRulesetConfigManager rulesetConfig) { @@ -74,46 +60,13 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor showTrail.BindValueChanged(v => cursorTrail.FadeTo(v.NewValue ? 1 : 0, 200), true); - userCursorScale = config.GetBindable(OsuSetting.GameplayCursorSize); - userCursorScale.ValueChanged += _ => calculateScale(); - - autoCursorScale = config.GetBindable(OsuSetting.AutoCursorSize); - autoCursorScale.ValueChanged += _ => calculateScale(); - - CursorScale.BindValueChanged(e => + ActiveCursor.CursorScale.BindValueChanged(e => { var newScale = new Vector2(e.NewValue); - ActiveCursor.Scale = newScale; rippleVisualiser.CursorScale = newScale; cursorTrail.Scale = newScale; }, true); - - calculateScale(); - } - - /// - /// Get the scale applicable to the ActiveCursor based on a beatmap's circle size. - /// - public static float GetScaleForCircleSize(float circleSize) => - 1f - 0.7f * (1f + circleSize - BeatmapDifficulty.DEFAULT_DIFFICULTY) / BeatmapDifficulty.DEFAULT_DIFFICULTY; - - private void calculateScale() - { - float scale = userCursorScale.Value; - - if (autoCursorScale.Value && state != null) - { - // if we have a beatmap available, let's get its circle size to figure out an automatic cursor scale modifier. - scale *= GetScaleForCircleSize(state.Beatmap.Difficulty.CircleSize); - } - - cursorScale.Value = scale; - - var newScale = new Vector2(scale); - - ActiveCursor.ScaleTo(newScale, 400, Easing.OutQuint); - cursorTrail.Scale = newScale; } private int downCount; @@ -121,9 +74,9 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor private void updateExpandedState() { if (downCount > 0) - (ActiveCursor as OsuCursor)?.Expand(); + ActiveCursor.Expand(); else - (ActiveCursor as OsuCursor)?.Contract(); + ActiveCursor.Contract(); } public bool OnPressed(KeyBindingPressEvent e) @@ -160,13 +113,13 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor protected override void PopIn() { fadeContainer.FadeTo(1, 300, Easing.OutQuint); - ActiveCursor.ScaleTo(CursorScale.Value, 400, Easing.OutQuint); + ActiveCursor.ScaleTo(1f, 400, Easing.OutQuint); } protected override void PopOut() { fadeContainer.FadeTo(0.05f, 450, Easing.OutQuint); - ActiveCursor.ScaleTo(CursorScale.Value * 0.8f, 450, Easing.OutQuint); + ActiveCursor.ScaleTo(0.8f, 450, Easing.OutQuint); } private partial class DefaultCursorTrail : CursorTrail diff --git a/osu.Game.Rulesets.Osu/UI/OsuResumeOverlay.cs b/osu.Game.Rulesets.Osu/UI/OsuResumeOverlay.cs index 555610a3b6..ea49836772 100644 --- a/osu.Game.Rulesets.Osu/UI/OsuResumeOverlay.cs +++ b/osu.Game.Rulesets.Osu/UI/OsuResumeOverlay.cs @@ -5,7 +5,6 @@ using System; using osu.Framework.Allocation; -using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Cursor; @@ -14,7 +13,6 @@ using osu.Framework.Input.Events; using osu.Framework.Localisation; using osu.Game.Rulesets.Osu.UI.Cursor; using osu.Game.Screens.Play; -using osuTK; using osuTK.Graphics; namespace osu.Game.Rulesets.Osu.UI @@ -25,7 +23,6 @@ namespace osu.Game.Rulesets.Osu.UI private OsuClickToResumeCursor clickToResumeCursor; private OsuCursorContainer localCursorContainer; - private IBindable localCursorScale; public override CursorContainer LocalCursor => State.Value == Visibility.Visible ? localCursorContainer : null; @@ -49,13 +46,7 @@ namespace osu.Game.Rulesets.Osu.UI clickToResumeCursor.Appear(); if (localCursorContainer == null) - { Add(localCursorContainer = new OsuCursorContainer()); - - localCursorScale = new BindableFloat(); - localCursorScale.BindTo(localCursorContainer.CursorScale); - localCursorScale.BindValueChanged(scale => cursorScaleContainer.Scale = new Vector2(scale.NewValue), true); - } } protected override void PopOut() @@ -98,7 +89,8 @@ namespace osu.Game.Rulesets.Osu.UI { case OsuAction.LeftButton: case OsuAction.RightButton: - if (!IsHovered) return false; + if (!IsHovered) + return false; this.ScaleTo(2, TRANSITION_TIME, Easing.OutQuint);