From 8d7db52200ec08817b46cb70596e5d80f085fd74 Mon Sep 17 00:00:00 2001 From: EVAST9919 Date: Wed, 20 Sep 2017 19:45:38 +0300 Subject: [PATCH] Add remaining time counter --- osu.Game/Screens/Play/BreakOverlay.cs | 86 +++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/osu.Game/Screens/Play/BreakOverlay.cs b/osu.Game/Screens/Play/BreakOverlay.cs index 5162aa8007..fe7edf36ff 100644 --- a/osu.Game/Screens/Play/BreakOverlay.cs +++ b/osu.Game/Screens/Play/BreakOverlay.cs @@ -8,6 +8,9 @@ using OpenTK; using osu.Framework.Graphics.Shapes; using OpenTK.Graphics; +using osu.Game.Graphics.Sprites; +using System; +using osu.Framework.Timing; namespace osu.Game.Screens.Play { @@ -18,9 +21,19 @@ public class BreakOverlay : Container public List Breaks; + public override IFrameBasedClock Clock + { + set + { + base.Clock = remainingTimeCounter.Clock = value; + } + get { return base.Clock; } + } + private readonly bool letterboxing; private readonly LetterboxOverlay letterboxOverlay; - private readonly Container remainingTimeContainer; + private readonly Container remainingTimeBox; + private readonly RemainingTimeCounter remainingTimeCounter; public BreakOverlay(bool letterboxing) { @@ -30,7 +43,7 @@ public BreakOverlay(bool letterboxing) Children = new Drawable[] { letterboxOverlay = new LetterboxOverlay(), - remainingTimeContainer = new Container + remainingTimeBox = new Container { Anchor = Anchor.Centre, Origin = Anchor.Centre, @@ -42,6 +55,12 @@ public BreakOverlay(bool letterboxing) RelativeSizeAxes = Axes.Both, Colour = Color4.White, } + }, + remainingTimeCounter = new RemainingTimeCounter + { + Anchor = Anchor.Centre, + Origin = Anchor.BottomCentre, + Margin = new MarginPadding { Bottom = 25 }, } }; } @@ -77,12 +96,71 @@ private void onBreakIn(BreakPeriod b) if (letterboxing) letterboxOverlay.FadeIn(fade_duration); - remainingTimeContainer.ResizeWidthTo(remaining_time_container_max_size, fade_duration, Easing.OutQuint).Then().ResizeWidthTo(0, b.Duration); + remainingTimeBox + .ResizeWidthTo(remaining_time_container_max_size, fade_duration, Easing.OutQuint) + .Then() + .ResizeWidthTo(0, b.Duration); + + Scheduler.AddDelayed(() => remainingTimeCounter.StartCounting(b.EndTime), b.StartTime - Clock.CurrentTime); + remainingTimeCounter.FadeIn(fade_duration); } private void onBreakOut() { - if (letterboxing) letterboxOverlay.FadeOut(fade_duration); + if (letterboxing) + letterboxOverlay.FadeOut(fade_duration); + + remainingTimeCounter.FadeOut(fade_duration); + } + + private class RemainingTimeCounter : Container + { + private readonly OsuSpriteText counter; + + private int? previousSecond; + + private double remainingTime; + + private bool isCounting; + + public RemainingTimeCounter() + { + AutoSizeAxes = Axes.Both; + Alpha = 0; + Child = counter = new OsuSpriteText + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + TextSize = 35, + Font = "Venera", + }; + } + + public void StartCounting(double remainingTime) + { + this.remainingTime = remainingTime; + isCounting = true; + } + + protected override void Update() + { + base.Update(); + + if (isCounting) + { + var currentTime = Clock.CurrentTime; + if (currentTime < remainingTime) + { + int currentSecond = (int)Math.Floor((remainingTime - Clock.CurrentTime) / 1000.0) + 1; + if (currentSecond != previousSecond) + { + counter.Text = currentSecond.ToString(); + previousSecond = currentSecond; + } + } + else isCounting = false; + } + } } } }