1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 13:07:24 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/DailyChallenge/DailyChallengeTotalsDisplay.cs
Bartłomiej Dach 174dc91f4b
Implement component for displaying running totals in daily challenge
Total pass count and cumulative total score, to be more precise.
2024-07-26 09:51:39 +02:00

127 lines
4.7 KiB
C#

// 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;
using osu.Framework.Allocation;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.OnlinePlay.DailyChallenge.Events;
using osuTK;
namespace osu.Game.Screens.OnlinePlay.DailyChallenge
{
public partial class DailyChallengeTotalsDisplay : CompositeDrawable
{
private Container passCountContainer = null!;
private TotalRollingCounter passCounter = null!;
private Container totalScoreContainer = null!;
private TotalRollingCounter totalScoreCounter = null!;
private long totalPassCountInstantaneous;
private long cumulativeTotalScoreInstantaneous;
[BackgroundDependencyLoader]
private void load()
{
InternalChild = new GridContainer
{
RelativeSizeAxes = Axes.Both,
RowDimensions =
[
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
],
Content = new[]
{
new Drawable[]
{
new SectionHeader("Total pass count")
},
new Drawable[]
{
passCountContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = passCounter = new TotalRollingCounter
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
}
},
new Drawable[]
{
new SectionHeader("Cumulative total score")
},
new Drawable[]
{
totalScoreContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = totalScoreCounter = new TotalRollingCounter
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
}
},
}
};
}
public void SetInitialCounts(long totalPassCount, long cumulativeTotalScore)
{
totalPassCountInstantaneous = totalPassCount;
cumulativeTotalScoreInstantaneous = cumulativeTotalScore;
}
public void AddNewScore(NewScoreEvent ev)
{
totalPassCountInstantaneous += 1;
cumulativeTotalScoreInstantaneous += ev.TotalScore;
}
protected override void Update()
{
base.Update();
passCounter.Current.Value = totalPassCountInstantaneous;
totalScoreCounter.Current.Value = cumulativeTotalScoreInstantaneous;
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
var totalPassCountProportionOfParent = Vector2.Divide(passCountContainer.DrawSize, passCounter.DrawSize);
passCounter.Scale = new Vector2(Math.Min(Math.Min(totalPassCountProportionOfParent.X, totalPassCountProportionOfParent.Y) * 0.8f, 1));
var totalScoreTextProportionOfParent = Vector2.Divide(totalScoreContainer.DrawSize, totalScoreCounter.DrawSize);
totalScoreCounter.Scale = new Vector2(Math.Min(Math.Min(totalScoreTextProportionOfParent.X, totalScoreTextProportionOfParent.Y) * 0.8f, 1));
}
private partial class TotalRollingCounter : RollingCounter<long>
{
protected override double RollingDuration => 400;
protected override OsuSpriteText CreateSpriteText() => new OsuSpriteText
{
Font = OsuFont.Default.With(size: 80f, fixedWidth: true),
};
protected override LocalisableString FormatCount(long count) => count.ToLocalisableString(@"N0");
}
}
}