From 6cf227a7af53014b2b5a5a14112e71537af68b7b Mon Sep 17 00:00:00 2001 From: HoutarouOreki Date: Wed, 23 May 2018 03:24:18 +0200 Subject: [PATCH] Allow formatting for DrawableDate class --- osu.Game/Graphics/DrawableDate.cs | 60 +++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/osu.Game/Graphics/DrawableDate.cs b/osu.Game/Graphics/DrawableDate.cs index 763e57e397..c38deba007 100644 --- a/osu.Game/Graphics/DrawableDate.cs +++ b/osu.Game/Graphics/DrawableDate.cs @@ -13,6 +13,8 @@ namespace osu.Game.Graphics public class DrawableDate : OsuSpriteText, IHasTooltip { private readonly DateTimeOffset date; + private readonly String dateFormat; + private readonly String tooltipFormat; public DrawableDate(DateTimeOffset date) { @@ -20,6 +22,50 @@ public DrawableDate(DateTimeOffset date) Font = "Exo2.0-RegularItalic"; this.date = date.ToLocalTime(); + // if date's format is not specified, set it to an empty string, + // so that later we will know to humanize it + dateFormat = ""; + // if tooltip's format is not specified, set it to an empty string + // so later we will know to default to a default time format + tooltipFormat = ""; + } + + public DrawableDate(string dateFormat, DateTimeOffset date) + { + AutoSizeAxes = Axes.Both; + Font = "Exo2.0-RegularItalic"; + + this.date = date.ToLocalTime(); + // set a date format for later from an argument + this.dateFormat = dateFormat; + // if tooltip's format is not specified, set it to an empty string, + // so later we will know to default to a default time format + tooltipFormat = ""; + } + + public DrawableDate(DateTimeOffset date, string tooltipFormat) + { + AutoSizeAxes = Axes.Both; + Font = "Exo2.0-RegularItalic"; + + this.date = date.ToLocalTime(); + // if date's format is not specified, set it to an empty string, + // so that later we will know to humanize it + dateFormat = ""; + // set a tooltip format for later from an argument + this.tooltipFormat = tooltipFormat; + } + + public DrawableDate(string dateFormat, DateTimeOffset date, string tooltipFormat) + { + AutoSizeAxes = Axes.Both; + Font = "Exo2.0-RegularItalic"; + + this.date = date.ToLocalTime(); + // set a date format for text generator from an argument + this.dateFormat = dateFormat; + // set a tooltip format for tooltip generator from an argument + this.tooltipFormat = tooltipFormat; } [BackgroundDependencyLoader] @@ -58,8 +104,18 @@ private void updateTimeWithReschedule() public override bool HandleMouseInput => true; - private void updateTime() => Text = date.Humanize(); + // if date's format is specified + private void updateTime() => Text = dateFormat != "" ? + // format it as requested in a passed argument + String.Format(dateFormat, date) : + // otherwise, humanize it (for example: 2 hours ago) + date.Humanize(); - public string TooltipText => date.ToString(); + // if we know that the tooltip format exists + public string TooltipText => tooltipFormat != "" ? + // then we format the tooltip text using that format + String.Format(tooltipFormat, date) : + // but otherwise, simply convert the date to string + date.ToString(); } }