From 589f2863ca16b75a959a873d1f4c6a1517107ede Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sat, 28 Aug 2021 02:38:45 +0300 Subject: [PATCH] Move incompatibility tooltip logic to local player mod select overlays This one turned out to be a bit more involved, due to tooltips being shared and having the potential of being used somewhere where it shouldn't be, due to the same content type matching. That's the reason I've defined a protected `TargetContentType`, to be able to separate "local player mod tooltips" and regular mod tooltips apart. Definitely unsure about the solution, but that's as far as I can think of right now. --- .../Overlays/Mods/LocalPlayerModButton.cs | 51 ++++++++++++++++ osu.Game/Overlays/Mods/ModButton.cs | 2 +- osu.Game/Overlays/Mods/ModButtonTooltip.cs | 58 +++++-------------- 3 files changed, 68 insertions(+), 43 deletions(-) diff --git a/osu.Game/Overlays/Mods/LocalPlayerModButton.cs b/osu.Game/Overlays/Mods/LocalPlayerModButton.cs index 10c81da7a6..9427c3e63e 100644 --- a/osu.Game/Overlays/Mods/LocalPlayerModButton.cs +++ b/osu.Game/Overlays/Mods/LocalPlayerModButton.cs @@ -65,5 +65,56 @@ private void updateCompatibility() else incompatibleIcon.Hide(); } + + public override ITooltip GetCustomTooltip() => new LocalPlayerModButtonTooltip(); + + private class LocalPlayerModButtonTooltip : ModButtonTooltip + { + private readonly OsuSpriteText incompatibleText; + + private readonly Bindable> incompatibleMods = new Bindable>(); + + [Resolved] + private Bindable ruleset { get; set; } + + public LocalPlayerModButtonTooltip() + { + AddRange(new Drawable[] + { + incompatibleText = new OsuSpriteText + { + Margin = new MarginPadding { Top = 5 }, + Font = OsuFont.GetFont(weight: FontWeight.Regular), + Text = "Incompatible with:" + }, + new ModDisplay + { + Current = incompatibleMods, + ExpansionMode = ExpansionMode.AlwaysExpanded, + Scale = new Vector2(0.7f) + } + }); + } + + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + incompatibleText.Colour = colours.BlueLight; + } + + protected override Type TargetContentType => typeof(LocalPlayerModButton); + + protected override void UpdateDisplay(Mod mod) + { + base.UpdateDisplay(mod); + + var incompatibleTypes = mod.IncompatibleMods; + + var allMods = ruleset.Value.CreateInstance().GetAllMods(); + + incompatibleMods.Value = allMods.Where(m => m.GetType() != mod.GetType() && incompatibleTypes.Any(t => t.IsInstanceOfType(m))).ToList(); + incompatibleText.Text = incompatibleMods.Value.Any() ? "Incompatible with:" : "Compatible with all mods"; + } + } } } diff --git a/osu.Game/Overlays/Mods/ModButton.cs b/osu.Game/Overlays/Mods/ModButton.cs index 8f6fc734e8..cc8acb7513 100644 --- a/osu.Game/Overlays/Mods/ModButton.cs +++ b/osu.Game/Overlays/Mods/ModButton.cs @@ -314,6 +314,6 @@ public ModButton(Mod mod) public virtual ITooltip GetCustomTooltip() => new ModButtonTooltip(); - public object TooltipContent => SelectedMod ?? Mods.FirstOrDefault(); + public object TooltipContent => this; } } diff --git a/osu.Game/Overlays/Mods/ModButtonTooltip.cs b/osu.Game/Overlays/Mods/ModButtonTooltip.cs index 666ed07e28..125357ea44 100644 --- a/osu.Game/Overlays/Mods/ModButtonTooltip.cs +++ b/osu.Game/Overlays/Mods/ModButtonTooltip.cs @@ -1,19 +1,16 @@ // 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 System; using System.Linq; 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.Graphics.Shapes; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; -using osu.Game.Rulesets; using osu.Game.Rulesets.Mods; -using osu.Game.Screens.Play.HUD; using osuTK; namespace osu.Game.Overlays.Mods @@ -22,12 +19,8 @@ public class ModButtonTooltip : VisibilityContainer, ITooltip { private readonly OsuSpriteText descriptionText; private readonly Box background; - private readonly OsuSpriteText incompatibleText; - private readonly Bindable> incompatibleMods = new Bindable>(); - - [Resolved] - private Bindable ruleset { get; set; } + protected override Container Content { get; } public ModButtonTooltip() { @@ -35,13 +28,13 @@ public ModButtonTooltip() Masking = true; CornerRadius = 5; - Children = new Drawable[] + InternalChildren = new Drawable[] { background = new Box { RelativeSizeAxes = Axes.Both }, - new FillFlowContainer + Content = new FillFlowContainer { AutoSizeAxes = Axes.Both, Direction = FillDirection.Vertical, @@ -51,19 +44,7 @@ public ModButtonTooltip() descriptionText = new OsuSpriteText { Font = OsuFont.GetFont(weight: FontWeight.Regular), - Margin = new MarginPadding { Bottom = 5 } }, - incompatibleText = new OsuSpriteText - { - Font = OsuFont.GetFont(weight: FontWeight.Regular), - Text = "Incompatible with:" - }, - new ModDisplay - { - Current = incompatibleMods, - ExpansionMode = ExpansionMode.AlwaysExpanded, - Scale = new Vector2(0.7f) - } } }, }; @@ -74,7 +55,6 @@ private void load(OsuColour colours) { background.Colour = colours.Gray3; descriptionText.Colour = colours.BlueLighter; - incompatibleText.Colour = colours.BlueLight; } protected override void PopIn() => this.FadeIn(200, Easing.OutQuint); @@ -82,34 +62,28 @@ private void load(OsuColour colours) private Mod lastMod; - public bool SetContent(object content) + protected virtual Type TargetContentType => typeof(ModButton); + + public virtual bool SetContent(object content) { - if (!(content is Mod mod)) + if (!(content is ModButton button) || content.GetType() != TargetContentType) return false; + var mod = button.SelectedMod ?? button.Mods.First(); + if (mod.Equals(lastMod)) return true; lastMod = mod; - descriptionText.Text = mod.Description; - - var incompatibleTypes = mod.IncompatibleMods; - - var allMods = ruleset.Value.CreateInstance().GetAllMods(); - - incompatibleMods.Value = allMods.Where(m => m.GetType() != mod.GetType() && incompatibleTypes.Any(t => t.IsInstanceOfType(m))).ToList(); - - if (!incompatibleMods.Value.Any()) - { - incompatibleText.Text = "Compatible with all mods"; - return true; - } - - incompatibleText.Text = "Incompatible with:"; - + UpdateDisplay(mod); return true; } + protected virtual void UpdateDisplay(Mod mod) + { + descriptionText.Text = mod.Description; + } + public void Move(Vector2 pos) => Position = pos; } }