1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-29 00:32:57 +08:00

Added bindable stardifficulty to StarRatingDisplay

This commit is contained in:
Denrage 2021-04-20 10:25:12 +02:00
parent 505a117862
commit 6e72ee5f76
2 changed files with 95 additions and 48 deletions

View File

@ -1,8 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Globalization; using System.Globalization;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Colour;
@ -22,7 +24,11 @@ namespace osu.Game.Screens.Ranking.Expanded
/// </summary> /// </summary>
public class StarRatingDisplay : CompositeDrawable public class StarRatingDisplay : CompositeDrawable
{ {
private readonly StarDifficulty difficulty; private CircularContainer colorContainer;
private OsuTextFlowContainer textContainer;
private readonly StarDifficulty starDifficulty;
private readonly IBindable<StarDifficulty?> bindableStarDifficulty;
/// <summary> /// <summary>
/// Creates a new <see cref="StarRatingDisplay"/> using an already computed <see cref="StarDifficulty"/>. /// Creates a new <see cref="StarRatingDisplay"/> using an already computed <see cref="StarDifficulty"/>.
@ -30,14 +36,21 @@ namespace osu.Game.Screens.Ranking.Expanded
/// <param name="starDifficulty">The already computed <see cref="StarDifficulty"/> to display the star difficulty of.</param> /// <param name="starDifficulty">The already computed <see cref="StarDifficulty"/> to display the star difficulty of.</param>
public StarRatingDisplay(StarDifficulty starDifficulty) public StarRatingDisplay(StarDifficulty starDifficulty)
{ {
difficulty = starDifficulty; this.starDifficulty = starDifficulty;
} }
[BackgroundDependencyLoader] /// <summary>
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache) /// Creates a new <see cref="StarRatingDisplay"/> using a binded nullable <see cref="StarDifficulty"/>.
/// </summary>
/// <param name="starDifficulty">The binded nullable <see cref="StarDifficulty"/> to display the star difficulty of. If <c>null</c>, a new instance of <see cref="StarDifficulty"/> will be created </param>
public StarRatingDisplay(IBindable<StarDifficulty?> starDifficulty)
{ {
AutoSizeAxes = Axes.Both; bindableStarDifficulty = starDifficulty;
}
private void setDifficulty(OsuColour colours)
{
var difficulty = bindableStarDifficulty == null ? starDifficulty : bindableStarDifficulty.Value ?? new StarDifficulty();
var starRatingParts = difficulty.Stars.ToString("0.00", CultureInfo.InvariantCulture).Split('.'); var starRatingParts = difficulty.Stars.ToString("0.00", CultureInfo.InvariantCulture).Split('.');
string wholePart = starRatingParts[0]; string wholePart = starRatingParts[0];
string fractionPart = starRatingParts[1]; string fractionPart = starRatingParts[1];
@ -47,9 +60,36 @@ namespace osu.Game.Screens.Ranking.Expanded
? ColourInfo.GradientVertical(Color4Extensions.FromHex("#C1C1C1"), Color4Extensions.FromHex("#595959")) ? ColourInfo.GradientVertical(Color4Extensions.FromHex("#C1C1C1"), Color4Extensions.FromHex("#595959"))
: (ColourInfo)colours.ForDifficultyRating(difficulty.DifficultyRating); : (ColourInfo)colours.ForDifficultyRating(difficulty.DifficultyRating);
colorContainer.Colour = backgroundColour;
textContainer.Text = string.Empty;
textContainer.With(t =>
{
t.AddText($"{wholePart}", s =>
{
s.Colour = Color4.Black;
s.Font = s.Font.With(size: 14);
s.UseFullGlyphHeight = false;
});
t.AddText($"{separator}{fractionPart}", s =>
{
s.Colour = Color4.Black;
s.Font = s.Font.With(size: 7);
s.UseFullGlyphHeight = false;
});
});
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
{
AutoSizeAxes = Axes.Both;
InternalChildren = new Drawable[] InternalChildren = new Drawable[]
{ {
new CircularContainer colorContainer = new CircularContainer
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Masking = true, Masking = true,
@ -58,7 +98,6 @@ namespace osu.Game.Screens.Ranking.Expanded
new Box new Box
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Colour = backgroundColour
}, },
} }
}, },
@ -78,32 +117,24 @@ namespace osu.Game.Screens.Ranking.Expanded
Icon = FontAwesome.Solid.Star, Icon = FontAwesome.Solid.Star,
Colour = Color4.Black Colour = Color4.Black
}, },
new OsuTextFlowContainer(s => s.Font = OsuFont.Numeric.With(weight: FontWeight.Black)) textContainer = new OsuTextFlowContainer(s => s.Font = OsuFont.Numeric.With(weight: FontWeight.Black))
{ {
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft, Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
TextAnchor = Anchor.BottomLeft, TextAnchor = Anchor.BottomLeft,
}.With(t => },
{
t.AddText($"{wholePart}", s =>
{
s.Colour = Color4.Black;
s.Font = s.Font.With(size: 14);
s.UseFullGlyphHeight = false;
});
t.AddText($"{separator}{fractionPart}", s =>
{
s.Colour = Color4.Black;
s.Font = s.Font.With(size: 7);
s.UseFullGlyphHeight = false;
});
})
} }
} }
}; };
if (bindableStarDifficulty != null)
{
bindableStarDifficulty.BindValueChanged(_ => setDifficulty(colours));
}
setDifficulty(colours);
} }
} }
} }

View File

@ -105,7 +105,6 @@ namespace osu.Game.Screens.Select
beatmapDifficulty?.UnbindAll(); beatmapDifficulty?.UnbindAll();
beatmapDifficulty = difficultyCache.GetBindableDifficulty(beatmap.BeatmapInfo, cancellationSource.Token); beatmapDifficulty = difficultyCache.GetBindableDifficulty(beatmap.BeatmapInfo, cancellationSource.Token);
beatmapDifficulty.BindValueChanged(_ => updateDisplay());
updateDisplay(); updateDisplay();
} }
@ -151,7 +150,7 @@ namespace osu.Game.Screens.Select
removeOldInfo(); removeOldInfo();
Add(Background = loaded); Add(Background = loaded);
Add(Info = new WedgeInfoText(beatmap, ruleset.Value, mods.Value, beatmapDifficulty.Value ?? new StarDifficulty()) Add(Info = new WedgeInfoText(beatmap, ruleset.Value, mods.Value, beatmapDifficulty)
{ {
Shear = -Shear Shear = -Shear
}); });
@ -176,15 +175,16 @@ namespace osu.Game.Screens.Select
private ILocalisedBindableString titleBinding; private ILocalisedBindableString titleBinding;
private ILocalisedBindableString artistBinding; private ILocalisedBindableString artistBinding;
private FillFlowContainer infoLabelContainer; private FillFlowContainer infoLabelContainer;
private Drawable starRatingDisplay;
private Container bpmLabelContainer; private Container bpmLabelContainer;
private ModSettingChangeTracker settingChangeTracker; private ModSettingChangeTracker settingChangeTracker;
private readonly WorkingBeatmap beatmap; private readonly WorkingBeatmap beatmap;
private readonly RulesetInfo ruleset; private readonly RulesetInfo ruleset;
private readonly IReadOnlyList<Mod> mods; private readonly IReadOnlyList<Mod> mods;
private readonly StarDifficulty starDifficulty; private readonly IBindable<StarDifficulty?> starDifficulty;
public WedgeInfoText(WorkingBeatmap beatmap, RulesetInfo userRuleset, IReadOnlyList<Mod> mods, StarDifficulty difficulty) public WedgeInfoText(WorkingBeatmap beatmap, RulesetInfo userRuleset, IReadOnlyList<Mod> mods, IBindable<StarDifficulty?> difficulty)
{ {
this.beatmap = beatmap; this.beatmap = beatmap;
ruleset = userRuleset ?? beatmap.BeatmapInfo.Ruleset; ruleset = userRuleset ?? beatmap.BeatmapInfo.Ruleset;
@ -241,12 +241,13 @@ namespace osu.Game.Screens.Select
Shear = wedged_container_shear, Shear = wedged_container_shear,
Children = new[] Children = new[]
{ {
createStarRatingDisplay(starDifficulty).With(display => starRatingDisplay = new StarRatingDisplay(starDifficulty)
{ {
display.Anchor = Anchor.TopRight; Anchor = Anchor.TopRight,
display.Origin = Anchor.TopRight; Origin = Anchor.TopRight,
display.Shear = -wedged_container_shear; Shear = -wedged_container_shear,
}), Margin = new MarginPadding { Bottom = 5 }
},
StatusPill = new BeatmapSetOnlineStatusPill StatusPill = new BeatmapSetOnlineStatusPill
{ {
Anchor = Anchor.TopRight, Anchor = Anchor.TopRight,
@ -309,6 +310,18 @@ namespace osu.Game.Screens.Select
addInfoLabels(); addInfoLabels();
} }
private void setStarRatingDisplayVisibility()
{
if (starDifficulty.Value.HasValue && starDifficulty.Value.Value.Stars > 0)
{
starRatingDisplay.Show();
}
else
{
starRatingDisplay.Hide();
}
}
private InfoLabel[] getRulesetInfoLabels() private InfoLabel[] getRulesetInfoLabels()
{ {
try try
@ -420,18 +433,14 @@ namespace osu.Game.Screens.Select
}; };
} }
private static Drawable createStarRatingDisplay(StarDifficulty difficulty) => difficulty.Stars > 0
? new StarRatingDisplay(difficulty)
{
Margin = new MarginPadding { Bottom = 5 }
}
: Empty();
private class DifficultyColourBar : Container private class DifficultyColourBar : Container
{ {
private readonly StarDifficulty difficulty; private Box solidDifficultyBox;
private Box transparentDifficultyBox;
public DifficultyColourBar(StarDifficulty difficulty) private readonly IBindable<StarDifficulty?> difficulty;
public DifficultyColourBar(IBindable<StarDifficulty?> difficulty)
{ {
this.difficulty = difficulty; this.difficulty = difficulty;
} }
@ -441,26 +450,33 @@ namespace osu.Game.Screens.Select
{ {
const float full_opacity_ratio = 0.7f; const float full_opacity_ratio = 0.7f;
var difficultyColour = colours.ForDifficultyRating(difficulty.DifficultyRating);
Children = new Drawable[] Children = new Drawable[]
{ {
new Box solidDifficultyBox = new Box
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Colour = difficultyColour,
Width = full_opacity_ratio, Width = full_opacity_ratio,
}, },
new Box transparentDifficultyBox = new Box
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both, RelativePositionAxes = Axes.Both,
Colour = difficultyColour,
Alpha = 0.5f, Alpha = 0.5f,
X = full_opacity_ratio, X = full_opacity_ratio,
Width = 1 - full_opacity_ratio, Width = 1 - full_opacity_ratio,
} }
}; };
difficulty.BindValueChanged(_ => setColour(colours));
setColour(colours);
}
private void setColour(OsuColour colours)
{
var difficultyColour = colours.ForDifficultyRating(difficulty.Value?.DifficultyRating ?? (new StarDifficulty()).DifficultyRating);
solidDifficultyBox.Colour = difficultyColour;
transparentDifficultyBox.Colour = difficultyColour;
} }
} }