1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 07:52:56 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/DigitalClockDisplay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.3 KiB
C#
Raw Normal View History

2022-03-26 19:34:17 +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.
2022-06-17 15:37:17 +08:00
#nullable disable
2022-03-26 19:34:17 +08:00
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Overlays.Toolbar
{
public class DigitalClockDisplay : ClockDisplay
{
private OsuSpriteText realTime;
private OsuSpriteText gameTime;
private bool showRuntime = true;
public bool ShowRuntime
{
get => showRuntime;
set
{
if (showRuntime == value)
return;
showRuntime = value;
updateMetrics();
}
}
2022-04-07 17:11:30 +08:00
private bool use24HourDisplay;
2022-04-07 17:11:30 +08:00
public bool Use24HourDisplay
{
2022-04-07 17:11:30 +08:00
get => use24HourDisplay;
set
{
2022-04-07 17:11:30 +08:00
if (use24HourDisplay == value)
return;
2022-04-07 17:11:30 +08:00
use24HourDisplay = value;
updateMetrics();
UpdateDisplay(DateTimeOffset.Now); //Update realTime.Text immediately instead of waiting until next second
}
}
2022-03-26 19:34:17 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
{
realTime = new OsuSpriteText(),
gameTime = new OsuSpriteText
{
Y = 14,
Colour = colours.PinkLight,
Font = OsuFont.Default.With(size: 10, weight: FontWeight.SemiBold),
2022-03-26 19:34:17 +08:00
}
};
updateMetrics();
}
protected override void UpdateDisplay(DateTimeOffset now)
{
realTime.Text = use24HourDisplay ? $"{now:HH:mm:ss}" : $"{now:h:mm:ss tt}";
2022-03-26 19:34:17 +08:00
gameTime.Text = $"running {new TimeSpan(TimeSpan.TicksPerSecond * (int)(Clock.CurrentTime / 1000)):c}";
}
private void updateMetrics()
{
Width = showRuntime || !use24HourDisplay ? 66 : 45; // Allows for space for game time up to 99 days (in the padding area since this is quite rare).
2022-03-26 19:34:17 +08:00
gameTime.FadeTo(showRuntime ? 1 : 0);
}
}
}