1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 21:02:54 +08:00
osu-lazer/osu.Game/Graphics/DrawableDate.cs

68 lines
1.8 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using Humanizer;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics
{
public class DrawableDate : OsuSpriteText, IHasTooltip
{
protected readonly DateTimeOffset Date;
2018-05-25 20:05:50 +08:00
public DrawableDate(DateTimeOffset date)
2018-04-13 17:19:50 +08:00
{
AutoSizeAxes = Axes.Both;
Font = "Exo2.0-RegularItalic";
2018-06-28 18:23:56 +08:00
Date = date.ToLocalTime();
2018-04-13 17:19:50 +08:00
}
2018-05-23 18:07:28 +08:00
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
updateTime();
}
protected override void LoadComplete()
{
base.LoadComplete();
Scheduler.Add(updateTimeWithReschedule);
}
private void updateTimeWithReschedule()
{
updateTime();
var diffToNow = DateTimeOffset.Now.Subtract(Date);
2018-04-13 17:19:50 +08:00
double timeUntilNextUpdate = 1000;
if (diffToNow.TotalSeconds > 60)
{
timeUntilNextUpdate *= 60;
if (diffToNow.TotalMinutes > 60)
{
timeUntilNextUpdate *= 60;
if (diffToNow.TotalHours > 24)
timeUntilNextUpdate *= 24;
}
}
Scheduler.AddDelayed(updateTimeWithReschedule, timeUntilNextUpdate);
}
public override bool HandleMouseInput => true;
protected virtual string Format() => Date.Humanize();
2018-06-15 20:34:01 +08:00
private void updateTime() => Text = Format();
2018-04-13 17:19:50 +08:00
public virtual string TooltipText => string.Format($"{Date:MMMM d, yyyy h:mm tt \"UTC\"z}");
2018-04-13 17:19:50 +08:00
}
}