2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
|
{
|
|
|
|
|
public class SongProgressInfo : Container
|
|
|
|
|
{
|
|
|
|
|
private OsuSpriteText timeCurrent;
|
|
|
|
|
private OsuSpriteText timeLeft;
|
|
|
|
|
private OsuSpriteText progress;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2019-02-28 13:32:57 +08:00
|
|
|
|
set => startTime = value;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double EndTime
|
|
|
|
|
{
|
2019-02-28 13:32:57 +08:00
|
|
|
|
set => endTime = value;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-03-05 12:26:54 +08:00
|
|
|
|
private GameplayClock gameplayClock;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(OsuColour colours, GameplayClock clock)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-03-05 12:26:54 +08:00
|
|
|
|
if (clock != null)
|
|
|
|
|
gameplayClock = clock;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
timeCurrent = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Colour = colours.BlueLighter,
|
2019-02-22 18:42:09 +08:00
|
|
|
|
Font = OsuFont.Numeric,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Left = margin,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progress = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
|
Colour = colours.BlueLighter,
|
2019-02-22 18:42:09 +08:00
|
|
|
|
Font = OsuFont.Numeric,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
},
|
|
|
|
|
timeLeft = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomRight,
|
|
|
|
|
Anchor = Anchor.BottomRight,
|
|
|
|
|
Colour = colours.BlueLighter,
|
2019-02-22 18:42:09 +08:00
|
|
|
|
Font = OsuFont.Numeric,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Right = margin,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2019-03-05 12:26:54 +08:00
|
|
|
|
var 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)
|
|
|
|
|
{
|
2018-05-11 21:09:53 +08:00
|
|
|
|
timeCurrent.Text = formatTime(TimeSpan.FromSeconds(currentSecond));
|
2019-03-05 12:26:54 +08:00
|
|
|
|
timeLeft.Text = formatTime(TimeSpan.FromMilliseconds(endTime - time));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
previousSecond = currentSecond;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-11 21:09:53 +08:00
|
|
|
|
|
2018-06-02 17:25:49 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|