2019-06-16 01:02:26 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 16:43:03 +08:00
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2017-11-08 06:13:09 +08:00
|
|
|
|
using System;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using JetBrains.Annotations;
|
2017-02-07 12:52:19 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-07-13 16:15:06 +08:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2017-02-07 12:52:19 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-07-13 16:15:06 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-06-16 01:02:26 +08:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
2019-04-02 13:51:28 +08:00
|
|
|
|
using osu.Framework.Graphics.Effects;
|
2018-07-13 16:15:06 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2019-03-27 18:29:27 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2019-06-16 01:02:26 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2017-08-03 13:36:21 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2018-12-27 17:16:17 +08:00
|
|
|
|
using osu.Game.Rulesets;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2016-11-23 10:59:50 +08:00
|
|
|
|
namespace osu.Game.Beatmaps.Drawables
|
2016-10-27 10:55:55 +08:00
|
|
|
|
{
|
2021-08-29 00:09:37 +08:00
|
|
|
|
public class DifficultyIcon : CompositeDrawable, IHasCustomTooltip<DifficultyIconTooltipContent>
|
2017-02-07 12:52:19 +08:00
|
|
|
|
{
|
2019-08-24 04:11:36 +08:00
|
|
|
|
private readonly Container iconContainer;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Size of this difficulty icon.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public new Vector2 Size
|
2017-02-07 12:52:19 +08:00
|
|
|
|
{
|
2019-08-24 04:11:36 +08:00
|
|
|
|
get => iconContainer.Size;
|
|
|
|
|
set => iconContainer.Size = value;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-09-30 23:53:25 +08:00
|
|
|
|
[NotNull]
|
2021-10-29 16:03:50 +08:00
|
|
|
|
private readonly IBeatmapInfo beatmapInfo;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
|
|
|
|
[CanBeNull]
|
2021-10-29 16:03:50 +08:00
|
|
|
|
private readonly IRulesetInfo ruleset;
|
2019-08-17 14:16:24 +08:00
|
|
|
|
|
2020-09-30 23:53:25 +08:00
|
|
|
|
[CanBeNull]
|
|
|
|
|
private readonly IReadOnlyList<Mod> mods;
|
|
|
|
|
|
|
|
|
|
private readonly bool shouldShowTooltip;
|
2020-10-14 13:15:53 +08:00
|
|
|
|
|
|
|
|
|
private readonly bool performBackgroundDifficultyLookup;
|
|
|
|
|
|
|
|
|
|
private readonly Bindable<StarDifficulty> difficultyBindable = new Bindable<StarDifficulty>();
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
|
|
|
|
private Drawable background;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new <see cref="DifficultyIcon"/> with a given <see cref="RulesetInfo"/> and <see cref="Mod"/> combination.
|
|
|
|
|
/// </summary>
|
2021-10-02 23:55:29 +08:00
|
|
|
|
/// <param name="beatmapInfo">The beatmap to show the difficulty of.</param>
|
2020-09-30 23:53:25 +08:00
|
|
|
|
/// <param name="ruleset">The ruleset to show the difficulty with.</param>
|
|
|
|
|
/// <param name="mods">The mods to show the difficulty with.</param>
|
|
|
|
|
/// <param name="shouldShowTooltip">Whether to display a tooltip when hovered.</param>
|
2021-11-03 02:07:27 +08:00
|
|
|
|
/// <param name="performBackgroundDifficultyLookup">Whether to perform difficulty lookup (including calculation if necessary).</param>
|
|
|
|
|
public DifficultyIcon([NotNull] IBeatmapInfo beatmapInfo, [CanBeNull] IRulesetInfo ruleset, [CanBeNull] IReadOnlyList<Mod> mods, bool shouldShowTooltip = true, bool performBackgroundDifficultyLookup = true)
|
|
|
|
|
: this(beatmapInfo, shouldShowTooltip, performBackgroundDifficultyLookup)
|
2020-09-30 23:53:25 +08:00
|
|
|
|
{
|
2021-10-02 23:55:29 +08:00
|
|
|
|
this.ruleset = ruleset ?? beatmapInfo.Ruleset;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
this.mods = mods ?? Array.Empty<Mod>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new <see cref="DifficultyIcon"/> that follows the currently-selected ruleset and mods.
|
|
|
|
|
/// </summary>
|
2021-10-02 23:55:29 +08:00
|
|
|
|
/// <param name="beatmapInfo">The beatmap to show the difficulty of.</param>
|
2020-09-30 23:53:25 +08:00
|
|
|
|
/// <param name="shouldShowTooltip">Whether to display a tooltip when hovered.</param>
|
2020-10-14 13:15:53 +08:00
|
|
|
|
/// <param name="performBackgroundDifficultyLookup">Whether to perform difficulty lookup (including calculation if necessary).</param>
|
2021-10-29 16:03:50 +08:00
|
|
|
|
public DifficultyIcon([NotNull] IBeatmapInfo beatmapInfo, bool shouldShowTooltip = true, bool performBackgroundDifficultyLookup = true)
|
2020-09-30 23:53:25 +08:00
|
|
|
|
{
|
2021-10-02 23:55:29 +08:00
|
|
|
|
this.beatmapInfo = beatmapInfo ?? throw new ArgumentNullException(nameof(beatmapInfo));
|
2020-09-30 23:53:25 +08:00
|
|
|
|
this.shouldShowTooltip = shouldShowTooltip;
|
2020-10-14 13:15:53 +08:00
|
|
|
|
this.performBackgroundDifficultyLookup = performBackgroundDifficultyLookup;
|
2018-12-27 17:16:17 +08:00
|
|
|
|
|
2019-08-24 04:11:36 +08:00
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
|
|
InternalChild = iconContainer = new Container { Size = new Vector2(20f) };
|
2017-02-07 12:52:19 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-29 16:03:50 +08:00
|
|
|
|
[Resolved]
|
2021-12-03 17:14:44 +08:00
|
|
|
|
private IRulesetStore rulesets { get; set; }
|
2021-10-29 16:03:50 +08:00
|
|
|
|
|
2019-08-15 11:11:54 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2019-08-17 14:16:24 +08:00
|
|
|
|
private void load(OsuColour colours)
|
2019-08-15 11:11:54 +08:00
|
|
|
|
{
|
2019-08-24 04:11:36 +08:00
|
|
|
|
iconContainer.Children = new Drawable[]
|
2019-08-15 11:11:54 +08:00
|
|
|
|
{
|
|
|
|
|
new CircularContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Masking = true,
|
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
|
|
|
|
{
|
2021-08-06 17:00:12 +08:00
|
|
|
|
Colour = Color4.Black.Opacity(0.06f),
|
|
|
|
|
|
2019-08-15 11:11:54 +08:00
|
|
|
|
Type = EdgeEffectType.Shadow,
|
2021-08-06 17:00:12 +08:00
|
|
|
|
Radius = 3,
|
2019-08-15 11:11:54 +08:00
|
|
|
|
},
|
2020-09-30 23:53:25 +08:00
|
|
|
|
Child = background = new Box
|
2019-08-15 11:11:54 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2021-10-29 16:03:50 +08:00
|
|
|
|
Colour = colours.ForStarDifficulty(beatmapInfo.StarRating) // Default value that will be re-populated once difficulty calculation completes
|
2019-08-15 11:11:54 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
new ConstrainedIconContainer
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
// the null coalesce here is only present to make unit tests work (ruleset dlls aren't copied correctly for testing at the moment)
|
2021-10-29 17:16:53 +08:00
|
|
|
|
Icon = getRulesetIcon()
|
2020-09-30 23:53:25 +08:00
|
|
|
|
},
|
2019-08-15 11:11:54 +08:00
|
|
|
|
};
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
2020-10-14 13:15:53 +08:00
|
|
|
|
if (performBackgroundDifficultyLookup)
|
2021-10-02 23:55:29 +08:00
|
|
|
|
iconContainer.Add(new DelayedLoadUnloadWrapper(() => new DifficultyRetriever(beatmapInfo, ruleset, mods) { StarDifficulty = { BindTarget = difficultyBindable } }, 0));
|
2020-10-14 13:15:53 +08:00
|
|
|
|
else
|
2021-10-29 16:03:50 +08:00
|
|
|
|
difficultyBindable.Value = new StarDifficulty(beatmapInfo.StarRating, 0);
|
2020-10-14 13:15:53 +08:00
|
|
|
|
|
2021-08-03 19:17:02 +08:00
|
|
|
|
difficultyBindable.BindValueChanged(difficulty => background.Colour = colours.ForStarDifficulty(difficulty.NewValue.Stars));
|
2020-09-30 23:53:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 17:16:53 +08:00
|
|
|
|
private Drawable getRulesetIcon()
|
|
|
|
|
{
|
2021-10-29 17:22:23 +08:00
|
|
|
|
int? onlineID = (ruleset ?? beatmapInfo.Ruleset).OnlineID;
|
2021-10-29 17:16:53 +08:00
|
|
|
|
|
|
|
|
|
if (onlineID >= 0 && rulesets.GetRuleset(onlineID.Value)?.CreateInstance() is Ruleset rulesetInstance)
|
|
|
|
|
return rulesetInstance.CreateIcon();
|
|
|
|
|
|
|
|
|
|
return new SpriteIcon { Icon = FontAwesome.Regular.QuestionCircle };
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 02:16:02 +08:00
|
|
|
|
ITooltip<DifficultyIconTooltipContent> IHasCustomTooltip<DifficultyIconTooltipContent>.GetCustomTooltip() => new DifficultyIconTooltip();
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
2021-10-02 23:55:29 +08:00
|
|
|
|
DifficultyIconTooltipContent IHasCustomTooltip<DifficultyIconTooltipContent>.TooltipContent => shouldShowTooltip ? new DifficultyIconTooltipContent(beatmapInfo, difficultyBindable) : null;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
2020-10-01 20:39:40 +08:00
|
|
|
|
private class DifficultyRetriever : Component
|
2020-09-30 23:53:25 +08:00
|
|
|
|
{
|
|
|
|
|
public readonly Bindable<StarDifficulty> StarDifficulty = new Bindable<StarDifficulty>();
|
|
|
|
|
|
2021-10-29 16:03:50 +08:00
|
|
|
|
private readonly IBeatmapInfo beatmapInfo;
|
|
|
|
|
private readonly IRulesetInfo ruleset;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
private readonly IReadOnlyList<Mod> mods;
|
|
|
|
|
|
|
|
|
|
private CancellationTokenSource difficultyCancellation;
|
|
|
|
|
|
|
|
|
|
[Resolved]
|
2020-11-06 12:14:23 +08:00
|
|
|
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
2021-10-29 16:03:50 +08:00
|
|
|
|
public DifficultyRetriever(IBeatmapInfo beatmapInfo, IRulesetInfo ruleset, IReadOnlyList<Mod> mods)
|
2020-09-30 23:53:25 +08:00
|
|
|
|
{
|
2021-10-02 23:55:29 +08:00
|
|
|
|
this.beatmapInfo = beatmapInfo;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
this.ruleset = ruleset;
|
|
|
|
|
this.mods = mods;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 15:19:01 +08:00
|
|
|
|
private IBindable<StarDifficulty?> localStarDifficulty;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
difficultyCancellation = new CancellationTokenSource();
|
|
|
|
|
localStarDifficulty = ruleset != null
|
2021-10-02 23:55:29 +08:00
|
|
|
|
? difficultyCache.GetBindableDifficulty(beatmapInfo, ruleset, mods, difficultyCancellation.Token)
|
|
|
|
|
: difficultyCache.GetBindableDifficulty(beatmapInfo, difficultyCancellation.Token);
|
2021-02-25 15:19:01 +08:00
|
|
|
|
localStarDifficulty.BindValueChanged(d =>
|
|
|
|
|
{
|
|
|
|
|
if (d.NewValue is StarDifficulty diff)
|
|
|
|
|
StarDifficulty.Value = diff;
|
|
|
|
|
});
|
2020-09-30 23:53:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
difficultyCancellation?.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-27 10:55:55 +08:00
|
|
|
|
}
|
2017-04-24 19:11:36 +08:00
|
|
|
|
}
|