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
|
|
|
|
|
|
|
|
|
using System;
|
2020-09-30 23:53:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using JetBrains.Annotations;
|
2018-04-13 17:19:50 +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;
|
2018-04-13 17:19:50 +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;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2019-06-16 01:02:26 +08:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
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
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Drawables
|
|
|
|
|
{
|
2019-08-24 04:11:36 +08:00
|
|
|
|
public class DifficultyIcon : CompositeDrawable, IHasCustomTooltip
|
2018-04-13 17:19:50 +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
|
2018-04-13 17:19:50 +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]
|
|
|
|
|
private readonly BeatmapInfo beatmap;
|
|
|
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
|
private readonly RulesetInfo 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>
|
|
|
|
|
/// <param name="beatmap">The beatmap to show the difficulty of.</param>
|
|
|
|
|
/// <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>
|
|
|
|
|
public DifficultyIcon([NotNull] BeatmapInfo beatmap, [CanBeNull] RulesetInfo ruleset, [CanBeNull] IReadOnlyList<Mod> mods, bool shouldShowTooltip = true)
|
|
|
|
|
: this(beatmap, shouldShowTooltip)
|
|
|
|
|
{
|
2018-12-27 17:16:17 +08:00
|
|
|
|
this.ruleset = ruleset ?? beatmap.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>
|
|
|
|
|
/// <param name="beatmap">The beatmap to show the difficulty of.</param>
|
|
|
|
|
/// <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>
|
|
|
|
|
public DifficultyIcon([NotNull] BeatmapInfo beatmap, bool shouldShowTooltip = true, bool performBackgroundDifficultyLookup = true)
|
2020-09-30 23:53:25 +08:00
|
|
|
|
{
|
|
|
|
|
this.beatmap = beatmap ?? throw new ArgumentNullException(nameof(beatmap));
|
|
|
|
|
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) };
|
2018-04-13 17:19: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,
|
|
|
|
|
Scale = new Vector2(0.84f),
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Masking = true,
|
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
|
|
|
|
{
|
|
|
|
|
Colour = Color4.Black.Opacity(0.08f),
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Radius = 5,
|
|
|
|
|
},
|
2020-09-30 23:53:25 +08:00
|
|
|
|
Child = background = new Box
|
2019-08-15 11:11:54 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-09-30 23:53:25 +08:00
|
|
|
|
Colour = colours.ForDifficultyRating(beatmap.DifficultyRating) // 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)
|
2020-09-30 23:59:41 +08:00
|
|
|
|
Icon = (ruleset ?? beatmap.Ruleset)?.CreateInstance()?.CreateIcon() ?? new SpriteIcon { Icon = FontAwesome.Regular.QuestionCircle }
|
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)
|
|
|
|
|
iconContainer.Add(new DelayedLoadUnloadWrapper(() => new DifficultyRetriever(beatmap, ruleset, mods) { StarDifficulty = { BindTarget = difficultyBindable } }, 0));
|
|
|
|
|
else
|
|
|
|
|
difficultyBindable.Value = new StarDifficulty(beatmap.StarDifficulty, 0);
|
|
|
|
|
|
2020-09-30 23:53:25 +08:00
|
|
|
|
difficultyBindable.BindValueChanged(difficulty => background.Colour = colours.ForDifficultyRating(difficulty.NewValue.DifficultyRating));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ITooltip GetCustomTooltip() => new DifficultyIconTooltip();
|
|
|
|
|
|
|
|
|
|
public object TooltipContent => shouldShowTooltip ? new DifficultyIconTooltipContent(beatmap, difficultyBindable) : null;
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
|
|
private readonly BeatmapInfo beatmap;
|
|
|
|
|
private readonly RulesetInfo ruleset;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
public DifficultyRetriever(BeatmapInfo beatmap, RulesetInfo ruleset, IReadOnlyList<Mod> mods)
|
|
|
|
|
{
|
|
|
|
|
this.beatmap = beatmap;
|
|
|
|
|
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
|
2020-11-06 12:14:23 +08:00
|
|
|
|
? difficultyCache.GetBindableDifficulty(beatmap, ruleset, mods, difficultyCancellation.Token)
|
|
|
|
|
: difficultyCache.GetBindableDifficulty(beatmap, 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DifficultyIconTooltipContent
|
|
|
|
|
{
|
|
|
|
|
public readonly BeatmapInfo Beatmap;
|
|
|
|
|
public readonly IBindable<StarDifficulty> Difficulty;
|
|
|
|
|
|
|
|
|
|
public DifficultyIconTooltipContent(BeatmapInfo beatmap, IBindable<StarDifficulty> difficulty)
|
|
|
|
|
{
|
|
|
|
|
Beatmap = beatmap;
|
|
|
|
|
Difficulty = difficulty;
|
|
|
|
|
}
|
2019-08-15 11:11:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DifficultyIconTooltip : VisibilityContainer, ITooltip
|
2019-06-16 01:02:26 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly OsuSpriteText difficultyName, starRating;
|
|
|
|
|
private readonly Box background;
|
2019-08-17 14:20:27 +08:00
|
|
|
|
private readonly FillFlowContainer difficultyFlow;
|
2019-06-16 01:02:26 +08:00
|
|
|
|
|
2019-08-17 14:20:27 +08:00
|
|
|
|
public DifficultyIconTooltip()
|
2019-06-16 01:02:26 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
Masking = true;
|
|
|
|
|
CornerRadius = 5;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
background = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
},
|
|
|
|
|
new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
2019-08-17 14:29:39 +08:00
|
|
|
|
AutoSizeDuration = 200,
|
|
|
|
|
AutoSizeEasing = Easing.OutQuint,
|
2019-06-16 01:02:26 +08:00
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
Padding = new MarginPadding(10),
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
difficultyName = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Font = OsuFont.GetFont(size: 16, weight: FontWeight.Bold),
|
|
|
|
|
},
|
2019-08-17 14:20:27 +08:00
|
|
|
|
difficultyFlow = new FillFlowContainer
|
2019-06-16 01:02:26 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
starRating = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Font = OsuFont.GetFont(size: 16, weight: FontWeight.Regular),
|
|
|
|
|
},
|
|
|
|
|
new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Margin = new MarginPadding { Left = 4 },
|
|
|
|
|
Icon = FontAwesome.Solid.Star,
|
|
|
|
|
Size = new Vector2(12),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 21:27:21 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OsuColour colours { get; set; }
|
2019-08-17 14:20:27 +08:00
|
|
|
|
|
2019-06-16 01:02:26 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2020-02-14 21:27:21 +08:00
|
|
|
|
private void load()
|
2019-06-16 01:02:26 +08:00
|
|
|
|
{
|
2019-08-19 11:32:01 +08:00
|
|
|
|
background.Colour = colours.Gray3;
|
2019-06-16 01:02:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 23:53:25 +08:00
|
|
|
|
private readonly IBindable<StarDifficulty> starDifficulty = new Bindable<StarDifficulty>();
|
|
|
|
|
|
2019-08-17 14:20:27 +08:00
|
|
|
|
public bool SetContent(object content)
|
|
|
|
|
{
|
2020-09-30 23:53:25 +08:00
|
|
|
|
if (!(content is DifficultyIconTooltipContent iconContent))
|
2019-08-17 14:20:27 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2020-09-30 23:53:25 +08:00
|
|
|
|
difficultyName.Text = iconContent.Beatmap.Version;
|
|
|
|
|
|
|
|
|
|
starDifficulty.UnbindAll();
|
|
|
|
|
starDifficulty.BindTo(iconContent.Difficulty);
|
|
|
|
|
starDifficulty.BindValueChanged(difficulty =>
|
|
|
|
|
{
|
|
|
|
|
starRating.Text = $"{difficulty.NewValue.Stars:0.##}";
|
|
|
|
|
difficultyFlow.Colour = colours.ForDifficultyRating(difficulty.NewValue.DifficultyRating, true);
|
|
|
|
|
}, true);
|
2019-08-17 14:20:27 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 23:44:28 +08:00
|
|
|
|
public void Move(Vector2 pos) => Position = pos;
|
2019-06-16 01:02:26 +08:00
|
|
|
|
|
|
|
|
|
protected override void PopIn() => this.FadeIn(200, Easing.OutQuint);
|
|
|
|
|
|
|
|
|
|
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|