2019-01-24 17:43:03 +09: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 18:19:50 +09:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
2018-12-19 12:44:51 +09:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-03-08 12:57:12 +09:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2019-07-30 13:58:08 +09:00
|
|
|
|
using osu.Game.Utils;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
namespace osu.Game.Graphics
|
|
|
|
|
{
|
2021-08-28 19:09:37 +03:00
|
|
|
|
public partial class DrawableDate : OsuSpriteText, IHasCustomTooltip<DateTimeOffset>
|
2018-03-08 12:57:12 +09:00
|
|
|
|
{
|
2018-12-19 12:44:51 +09:00
|
|
|
|
private DateTimeOffset date;
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset Date
|
|
|
|
|
{
|
|
|
|
|
get => date;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (date == value)
|
|
|
|
|
return;
|
2019-02-28 13:31:40 +09:00
|
|
|
|
|
2018-12-19 12:44:51 +09:00
|
|
|
|
date = value.ToLocalTime();
|
|
|
|
|
|
|
|
|
|
if (LoadState >= LoadState.Ready)
|
|
|
|
|
updateTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-25 14:05:50 +02:00
|
|
|
|
|
2020-02-02 17:10:11 +01:00
|
|
|
|
public DrawableDate(DateTimeOffset date, float textSize = OsuFont.DEFAULT_FONT_SIZE, bool italic = true)
|
2018-03-08 12:57:12 +09:00
|
|
|
|
{
|
2020-02-02 17:10:11 +01:00
|
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Regular, size: textSize, italics: italic);
|
2018-12-19 12:44:51 +09:00
|
|
|
|
Date = date;
|
2018-03-08 12:57:12 +09:00
|
|
|
|
}
|
2018-05-23 12:07:28 +02:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
updateTime();
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
Scheduler.Add(updateTimeWithReschedule);
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
private void updateTimeWithReschedule()
|
|
|
|
|
{
|
|
|
|
|
updateTime();
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-06-28 19:19:00 +09:00
|
|
|
|
var diffToNow = DateTimeOffset.Now.Subtract(Date);
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
double timeUntilNextUpdate = 1000;
|
2019-04-01 12:16:05 +09:00
|
|
|
|
|
2018-12-19 13:07:43 +09:00
|
|
|
|
if (Math.Abs(diffToNow.TotalSeconds) > 120)
|
2018-03-08 12:57:12 +09:00
|
|
|
|
{
|
|
|
|
|
timeUntilNextUpdate *= 60;
|
2019-04-01 12:16:05 +09:00
|
|
|
|
|
2018-12-19 13:07:43 +09:00
|
|
|
|
if (Math.Abs(diffToNow.TotalMinutes) > 120)
|
2018-03-08 12:57:12 +09:00
|
|
|
|
{
|
|
|
|
|
timeUntilNextUpdate *= 60;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-12-19 13:07:43 +09:00
|
|
|
|
if (Math.Abs(diffToNow.TotalHours) > 48)
|
2018-03-08 12:57:12 +09:00
|
|
|
|
timeUntilNextUpdate *= 24;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-03-08 12:57:12 +09:00
|
|
|
|
Scheduler.AddDelayed(updateTimeWithReschedule, timeUntilNextUpdate);
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-07-30 13:58:08 +09:00
|
|
|
|
protected virtual string Format() => HumanizerUtils.Humanize(Date);
|
2018-05-25 14:00:32 +02:00
|
|
|
|
|
2018-06-15 14:34:01 +02:00
|
|
|
|
private void updateTime() => Text = Format();
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2021-08-28 19:09:37 +03:00
|
|
|
|
public ITooltip<DateTimeOffset> GetCustomTooltip() => new DateTooltip();
|
2020-02-11 14:21:12 +01:00
|
|
|
|
|
2021-08-28 19:09:37 +03:00
|
|
|
|
public DateTimeOffset TooltipContent => Date;
|
2018-03-08 12:57:12 +09:00
|
|
|
|
}
|
|
|
|
|
}
|