1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 05:42:54 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/DigitalClockDisplay.cs

85 lines
2.4 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.Extensions.LocalisationExtensions;
2022-03-26 19:34:17 +08:00
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Overlays.Toolbar
{
2022-11-24 13:32:20 +08:00
public partial class DigitalClockDisplay : ClockDisplay
2022-03-26 19:34:17 +08:00
{
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 = now.ToLocalisableString(use24HourDisplay ? @"HH:mm:ss" : @"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);
}
}
}