1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 13:22:55 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/SongProgressInfo.cs

147 lines
5.1 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
2022-08-05 19:56:08 +08:00
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
using System;
namespace osu.Game.Screens.Play.HUD
2018-04-13 17:19:50 +08:00
{
public class SongProgressInfo : Container
{
2022-09-01 05:05:06 +08:00
private SizePreservingSpriteText timeCurrent;
private SizePreservingSpriteText timeLeft;
private SizePreservingSpriteText progress;
2018-04-13 17:19:50 +08:00
private double startTime;
private double endTime;
private int? previousPercent;
private int? previousSecond;
private double songLength => endTime - startTime;
private const int margin = 10;
2019-02-28 12:31:40 +08:00
public double StartTime
{
set => startTime = value;
2019-02-28 12:31:40 +08:00
}
public double EndTime
{
set => endTime = value;
2019-02-28 12:31:40 +08:00
}
2018-04-13 17:19:50 +08:00
private IGameplayClock gameplayClock;
[BackgroundDependencyLoader(true)]
private void load(OsuColour colours, IGameplayClock clock)
2018-04-13 17:19:50 +08:00
{
if (clock != null)
gameplayClock = clock;
AutoSizeAxes = Axes.Y;
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new Container
2018-04-13 17:19:50 +08:00
{
2022-08-05 19:56:08 +08:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
2022-08-26 14:56:47 +08:00
Child = new UprightAspectMaintainingContainer
2018-04-13 17:19:50 +08:00
{
2022-08-05 19:56:08 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Scaling = ScaleMode.Vertical,
ScalingFactor = 0.5f,
2022-09-01 05:05:06 +08:00
Child = timeCurrent = new SizePreservingSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2022-08-22 21:24:52 +08:00
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
}
2018-04-13 17:19:50 +08:00
},
new Container
2018-04-13 17:19:50 +08:00
{
2022-08-05 19:56:08 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
2022-08-26 14:56:47 +08:00
Child = new UprightAspectMaintainingContainer
{
2022-08-05 19:56:08 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Scaling = ScaleMode.Vertical,
ScalingFactor = 0.5f,
2022-09-01 05:05:06 +08:00
Child = progress = new SizePreservingSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2022-08-22 21:24:52 +08:00
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
}
2018-04-13 17:19:50 +08:00
},
new Container
2018-04-13 17:19:50 +08:00
{
2022-08-05 19:56:08 +08:00
Origin = Anchor.CentreRight,
Anchor = Anchor.CentreRight,
AutoSizeAxes = Axes.Both,
2022-08-26 14:56:47 +08:00
Child = new UprightAspectMaintainingContainer
2018-04-13 17:19:50 +08:00
{
2022-08-05 19:56:08 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Scaling = ScaleMode.Vertical,
ScalingFactor = 0.5f,
2022-09-01 05:05:06 +08:00
Child = timeLeft = new SizePreservingSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2022-08-22 21:24:52 +08:00
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
}
2018-04-13 17:19:50 +08:00
}
};
}
protected override void Update()
{
base.Update();
double time = gameplayClock?.CurrentTime ?? Time.Current;
double songCurrentTime = time - startTime;
2018-04-13 17:19:50 +08:00
int currentPercent = Math.Max(0, Math.Min(100, (int)(songCurrentTime / songLength * 100)));
int currentSecond = (int)Math.Floor(songCurrentTime / 1000.0);
if (currentPercent != previousPercent)
{
progress.Text = currentPercent.ToString() + @"%";
previousPercent = currentPercent;
}
if (currentSecond != previousSecond && songCurrentTime < songLength)
{
timeCurrent.Text = formatTime(TimeSpan.FromSeconds(currentSecond));
timeLeft.Text = formatTime(TimeSpan.FromMilliseconds(endTime - time));
2018-04-13 17:19:50 +08:00
previousSecond = currentSecond;
}
}
private string formatTime(TimeSpan timeSpan) => $"{(timeSpan < TimeSpan.Zero ? "-" : "")}{Math.Floor(timeSpan.Duration().TotalMinutes)}:{timeSpan.Duration().Seconds:D2}";
2018-04-13 17:19:50 +08:00
}
}