2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
2018-12-19 03:44:51 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2020-02-11 13:21:12 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
2020-02-11 13:21:12 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2019-07-30 04:58:08 +00:00
|
|
|
|
using osu.Game.Utils;
|
2020-02-11 13:21:12 +00:00
|
|
|
|
using osuTK;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics
|
|
|
|
|
{
|
2020-02-11 13:21:12 +00:00
|
|
|
|
public class DrawableDate : OsuSpriteText, IHasCustomTooltip
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2018-12-19 03:44:51 +00:00
|
|
|
|
private DateTimeOffset date;
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset Date
|
|
|
|
|
{
|
|
|
|
|
get => date;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (date == value)
|
|
|
|
|
return;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2018-12-19 03:44:51 +00:00
|
|
|
|
date = value.ToLocalTime();
|
|
|
|
|
|
|
|
|
|
if (LoadState >= LoadState.Ready)
|
|
|
|
|
updateTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-25 12:05:50 +00:00
|
|
|
|
|
2020-02-02 16:10:11 +00:00
|
|
|
|
public DrawableDate(DateTimeOffset date, float textSize = OsuFont.DEFAULT_FONT_SIZE, bool italic = true)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-02-02 16:10:11 +00:00
|
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Regular, size: textSize, italics: italic);
|
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();
|
|
|
|
|
|
2018-06-28 10:19:00 +00:00
|
|
|
|
var diffToNow = DateTimeOffset.Now.Subtract(Date);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
double timeUntilNextUpdate = 1000;
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2018-12-19 04:07:43 +00:00
|
|
|
|
if (Math.Abs(diffToNow.TotalSeconds) > 120)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
timeUntilNextUpdate *= 60;
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2018-12-19 04:07:43 +00:00
|
|
|
|
if (Math.Abs(diffToNow.TotalMinutes) > 120)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
timeUntilNextUpdate *= 60;
|
|
|
|
|
|
2018-12-19 04:07:43 +00:00
|
|
|
|
if (Math.Abs(diffToNow.TotalHours) > 48)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
timeUntilNextUpdate *= 24;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scheduler.AddDelayed(updateTimeWithReschedule, timeUntilNextUpdate);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 04:58:08 +00:00
|
|
|
|
protected virtual string Format() => HumanizerUtils.Humanize(Date);
|
2018-05-25 12:00:32 +00:00
|
|
|
|
|
2018-06-15 12:34:01 +00:00
|
|
|
|
private void updateTime() => Text = Format();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-02-11 13:21:12 +00:00
|
|
|
|
public ITooltip GetCustomTooltip() => new DateTooltip();
|
|
|
|
|
|
|
|
|
|
public object TooltipContent => Date;
|
|
|
|
|
|
|
|
|
|
private class DateTooltip : VisibilityContainer, ITooltip
|
|
|
|
|
{
|
|
|
|
|
private readonly OsuSpriteText dateText, timeText;
|
|
|
|
|
private readonly Box background;
|
|
|
|
|
|
|
|
|
|
public DateTooltip()
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
Masking = true;
|
|
|
|
|
CornerRadius = 5;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
background = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
},
|
|
|
|
|
new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
Padding = new MarginPadding(10),
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
dateText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
},
|
|
|
|
|
timeText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular),
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
2020-02-13 13:07:14 +00:00
|
|
|
|
background.Colour = colours.GreySeafoamDarker;
|
|
|
|
|
timeText.Colour = colours.BlueLighter;
|
2020-02-11 13:21:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopIn() => this.FadeIn(200, Easing.OutQuint);
|
|
|
|
|
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
|
|
|
|
|
|
|
|
|
|
public bool SetContent(object content)
|
|
|
|
|
{
|
|
|
|
|
if (!(content is DateTimeOffset date))
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-02-13 13:07:14 +00:00
|
|
|
|
dateText.Text = $"{date:d MMMM yyyy} ";
|
2020-05-07 05:29:37 +00:00
|
|
|
|
timeText.Text = $"{date:HH:mm:ss \"UTC\"z}";
|
2020-02-11 13:21:12 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Move(Vector2 pos) => Position = pos;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|