Add bar chart to tooltip

This commit is contained in:
Henry Lin 2022-01-17 21:26:55 +08:00
parent b81fc675e8
commit c49cd60487

View File

@ -2,22 +2,27 @@
// 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;
using System.Globalization; using System.Globalization;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Difficulty; using osu.Game.Rulesets.Difficulty;
using osuTK; using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Ranking.Expanded.Statistics namespace osu.Game.Screens.Ranking.Expanded.Statistics
{ {
public class PerformanceStatisticTooltip : VisibilityContainer, ITooltip<PerformanceAttributes> public class PerformanceStatisticTooltip : VisibilityContainer, ITooltip<PerformanceAttributes>
{ {
private readonly Box background; private readonly Box background;
private Colour4 textColor; private Colour4 totalColour;
private Colour4 textColour;
protected override Container<Drawable> Content { get; } protected override Container<Drawable> Content { get; }
@ -46,10 +51,16 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
private void load(OsuColour colours) private void load(OsuColour colours)
{ {
background.Colour = colours.Gray3; background.Colour = colours.Gray3;
textColor = colours.BlueLighter; totalColour = colours.Blue;
textColour = colours.BlueLighter;
}
protected override void PopIn()
{
if (lastAttributes.GetAttributesForDisplay().Count() > 1)
this.FadeIn(200, Easing.OutQuint);
} }
protected override void PopIn() => this.FadeIn(200, Easing.OutQuint);
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint); protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
private PerformanceAttributes lastAttributes; private PerformanceAttributes lastAttributes;
@ -64,17 +75,15 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
UpdateDisplay(attributes); UpdateDisplay(attributes);
} }
protected virtual void UpdateDisplay(PerformanceAttributes attributes) private Drawable createAttributeItem(PerformanceDisplayAttribute attribute, double attributeSum)
{ {
Content.Clear(); bool isTotal = attribute.PropertyName == nameof(PerformanceAttributes.Total);
return new GridContainer
foreach (PerformanceDisplayAttribute attr in attributes.GetAttributesForDisplay())
{
Content.Add(new GridContainer
{ {
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
ColumnDimensions = new[] ColumnDimensions = new[]
{ {
new Dimension(GridSizeMode.Absolute, 110),
new Dimension(GridSizeMode.Absolute, 140), new Dimension(GridSizeMode.Absolute, 140),
new Dimension(GridSizeMode.AutoSize) new Dimension(GridSizeMode.AutoSize)
}, },
@ -88,18 +97,49 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
{ {
new OsuSpriteText new OsuSpriteText
{ {
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Font = OsuFont.GetFont(weight: FontWeight.Regular), Font = OsuFont.GetFont(weight: FontWeight.Regular),
Text = attr.DisplayName, Text = attribute.DisplayName,
Colour = textColor Colour = isTotal ? totalColour : textColour
},
new Bar
{
Alpha = isTotal ? 0 : 1,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Width = 130,
Height = 5,
BackgroundColour = Color4.White.Opacity(0.5f),
Length = (float)(attribute.Value / attributeSum),
Margin = new MarginPadding { Left = 5, Right = 5 }
}, },
new OsuSpriteText new OsuSpriteText
{ {
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Font = OsuFont.GetFont(weight: FontWeight.SemiBold), Font = OsuFont.GetFont(weight: FontWeight.SemiBold),
Text = ((int)Math.Round(attr.Value, MidpointRounding.AwayFromZero)).ToString(CultureInfo.CurrentCulture) Text = ((int)Math.Round(attribute.Value, MidpointRounding.AwayFromZero)).ToString(CultureInfo.CurrentCulture),
Colour = isTotal ? totalColour : textColour
} }
} }
} }
}); };
}
protected virtual void UpdateDisplay(PerformanceAttributes attributes)
{
Content.Clear();
var displayAttributes = attributes.GetAttributesForDisplay();
double attributeSum = displayAttributes
.Where(attr => attr.PropertyName != nameof(PerformanceAttributes.Total))
.Sum(attr => attr.Value);
foreach (PerformanceDisplayAttribute attr in displayAttributes)
{
Content.Add(createAttributeItem(attr, attributeSum));
} }
} }