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.
This commit is contained in:
Salman Ahmed 2021-08-28 02:38:45 +03:00
parent e527bfd4bf
commit 589f2863ca
3 changed files with 68 additions and 43 deletions

View File

@ -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<IReadOnlyList<Mod>> incompatibleMods = new Bindable<IReadOnlyList<Mod>>();
[Resolved]
private Bindable<RulesetInfo> 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";
}
}
}
}

View File

@ -314,6 +314,6 @@ public ModButton(Mod mod)
public virtual ITooltip GetCustomTooltip() => new ModButtonTooltip();
public object TooltipContent => SelectedMod ?? Mods.FirstOrDefault();
public object TooltipContent => this;
}
}

View File

@ -1,19 +1,16 @@
// 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 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<IReadOnlyList<Mod>> incompatibleMods = new Bindable<IReadOnlyList<Mod>>();
[Resolved]
private Bindable<RulesetInfo> ruleset { get; set; }
protected override Container<Drawable> 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;
}
}