osu/osu.Game/Graphics/DrawableDate.cs

79 lines
2.0 KiB
C#
Raw Normal View History

2018-04-13 09:19:50 +00: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;
2018-12-19 03:44:51 +00:00
using osu.Framework.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics
{
public class DrawableDate : OsuSpriteText, IHasTooltip
{
2018-12-19 03:44:51 +00:00
private DateTimeOffset date;
public DateTimeOffset Date
{
get => date;
set
{
if (date == value)
return;
date = value.ToLocalTime();
if (LoadState >= LoadState.Ready)
updateTime();
}
}
2018-05-25 12:05:50 +00:00
public DrawableDate(DateTimeOffset date)
2018-04-13 09:19:50 +00:00
{
Font = "Exo2.0-RegularItalic";
2018-12-19 03:44:51 +00:00
Date = date;
2018-04-13 09:19:50 +00:00
}
2018-05-23 10:07:28 +00:00
2018-04-13 09:19:50 +00: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 09:19:50 +00:00
double timeUntilNextUpdate = 1000;
if (Math.Abs(diffToNow.TotalSeconds) > 120)
2018-04-13 09:19:50 +00:00
{
timeUntilNextUpdate *= 60;
if (Math.Abs(diffToNow.TotalMinutes) > 120)
2018-04-13 09:19:50 +00:00
{
timeUntilNextUpdate *= 60;
if (Math.Abs(diffToNow.TotalHours) > 48)
2018-04-13 09:19:50 +00:00
timeUntilNextUpdate *= 24;
}
}
Scheduler.AddDelayed(updateTimeWithReschedule, timeUntilNextUpdate);
}
protected virtual string Format() => Date.Humanize();
2018-06-15 12:34:01 +00:00
private void updateTime() => Text = Format();
2018-04-13 09:19:50 +00:00
public virtual string TooltipText => string.Format($"{Date:MMMM d, yyyy h:mm tt \"UTC\"z}");
2018-04-13 09:19:50 +00:00
}
}