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 System;
|
|
|
|
|
using Humanizer;
|
|
|
|
|
using osu.Framework.Allocation;
|
2018-12-19 11:44:51 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics
|
|
|
|
|
{
|
|
|
|
|
public class DrawableDate : OsuSpriteText, IHasTooltip
|
|
|
|
|
{
|
2018-12-19 11:44:51 +08:00
|
|
|
|
private DateTimeOffset date;
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset Date
|
|
|
|
|
{
|
|
|
|
|
get => date;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (date == value)
|
|
|
|
|
return;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-12-19 11:44:51 +08:00
|
|
|
|
date = value.ToLocalTime();
|
|
|
|
|
|
|
|
|
|
if (LoadState >= LoadState.Ready)
|
|
|
|
|
updateTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-25 20:05:50 +08:00
|
|
|
|
|
2018-05-25 19:30:29 +08:00
|
|
|
|
public DrawableDate(DateTimeOffset date)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-02-20 15:52:36 +08:00
|
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Regular, italics: true);
|
2018-12-19 11:44:51 +08:00
|
|
|
|
Date = date;
|
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();
|
|
|
|
|
|
2018-06-28 18:19:00 +08:00
|
|
|
|
var diffToNow = DateTimeOffset.Now.Subtract(Date);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
double timeUntilNextUpdate = 1000;
|
2018-12-19 12:07:43 +08:00
|
|
|
|
if (Math.Abs(diffToNow.TotalSeconds) > 120)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
timeUntilNextUpdate *= 60;
|
2018-12-19 12:07:43 +08:00
|
|
|
|
if (Math.Abs(diffToNow.TotalMinutes) > 120)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
timeUntilNextUpdate *= 60;
|
|
|
|
|
|
2018-12-19 12:07:43 +08:00
|
|
|
|
if (Math.Abs(diffToNow.TotalHours) > 48)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
timeUntilNextUpdate *= 24;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scheduler.AddDelayed(updateTimeWithReschedule, timeUntilNextUpdate);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 18:19:00 +08:00
|
|
|
|
protected virtual string Format() => Date.Humanize();
|
2018-05-25 20:00:32 +08:00
|
|
|
|
|
2018-06-15 20:34:01 +08:00
|
|
|
|
private void updateTime() => Text = Format();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-06-28 18:19:00 +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
|
|
|
|
}
|
|
|
|
|
}
|