From ab8db3b7dc1c4b68cfbc2885ba03e1840096357e Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 26 Mar 2022 20:34:17 +0900 Subject: [PATCH] Move nested classes to own files --- .../Overlays/Toolbar/AnalogClockDisplay.cs | 159 ++++++++++++ osu.Game/Overlays/Toolbar/ClockDisplay.cs | 28 +++ .../Overlays/Toolbar/DigitalClockDisplay.cs | 64 +++++ osu.Game/Overlays/Toolbar/ToolbarClock.cs | 230 +----------------- 4 files changed, 255 insertions(+), 226 deletions(-) create mode 100644 osu.Game/Overlays/Toolbar/AnalogClockDisplay.cs create mode 100644 osu.Game/Overlays/Toolbar/ClockDisplay.cs create mode 100644 osu.Game/Overlays/Toolbar/DigitalClockDisplay.cs diff --git a/osu.Game/Overlays/Toolbar/AnalogClockDisplay.cs b/osu.Game/Overlays/Toolbar/AnalogClockDisplay.cs new file mode 100644 index 0000000000..e100ee0c06 --- /dev/null +++ b/osu.Game/Overlays/Toolbar/AnalogClockDisplay.cs @@ -0,0 +1,159 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Game.Graphics; +using osuTK; +using osuTK.Graphics; + +namespace osu.Game.Overlays.Toolbar +{ + public class AnalogClockDisplay : ClockDisplay + { + private const float hand_thickness = 2.4f; + + private Drawable hour; + private Drawable minute; + private Drawable second; + + [BackgroundDependencyLoader] + private void load() + { + Size = new Vector2(22); + + InternalChildren = new[] + { + new CircularContainer + { + RelativeSizeAxes = Axes.Both, + Masking = true, + BorderThickness = 2, + BorderColour = Color4.White, + Child = new Box + { + AlwaysPresent = true, + Alpha = 0, + RelativeSizeAxes = Axes.Both + }, + }, + hour = new LargeHand(0.34f), + minute = new LargeHand(0.48f), + second = new SecondHand(), + new CentreCircle + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + } + }; + } + + protected override void UpdateDisplay(DateTimeOffset now) + { + float secondFractional = now.Second / 60f; + float minuteFractional = (now.Minute + secondFractional) / 60f; + float hourFractional = ((minuteFractional + now.Hour) % 12) / 12f; + + updateRotation(hour, hourFractional); + updateRotation(minute, minuteFractional); + updateRotation(second, secondFractional); + } + + private void updateRotation(Drawable hand, float fraction) + { + const float duration = 320; + + float rotation = fraction * 360 - 90; + + if (Math.Abs(hand.Rotation - rotation) > 180) + hand.RotateTo(rotation); + else + hand.RotateTo(rotation, duration, Easing.OutElastic); + } + + private class CentreCircle : CompositeDrawable + { + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + InternalChildren = new Drawable[] + { + new Circle + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(hand_thickness), + Colour = Color4.White, + }, + new Circle + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(hand_thickness * 0.7f), + Colour = colours.PinkLight, + }, + }; + } + } + + private class SecondHand : CompositeDrawable + { + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + RelativeSizeAxes = Axes.X; + Width = 0.66f; + + Height = hand_thickness * 0.7f; + Anchor = Anchor.Centre; + Origin = Anchor.Custom; + + OriginPosition = new Vector2(Height * 2, Height / 2); + + InternalChildren = new Drawable[] + { + new Circle + { + Colour = colours.PinkLight, + RelativeSizeAxes = Axes.Both, + }, + }; + } + } + + private class LargeHand : CompositeDrawable + { + public LargeHand(float length) + { + Width = length; + } + + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + Anchor = Anchor.Centre; + Origin = Anchor.CentreLeft; + + Origin = Anchor.Custom; + OriginPosition = new Vector2(hand_thickness / 2); // offset x also, to ensure the centre of the line is centered on the face. + Height = hand_thickness; + + InternalChildren = new Drawable[] + { + new Circle + { + Colour = Color4.White, + RelativeSizeAxes = Axes.Both, + BorderThickness = 0.7f, + BorderColour = colours.Gray2, + }, + }; + + RelativeSizeAxes = Axes.X; + } + } + } +} diff --git a/osu.Game/Overlays/Toolbar/ClockDisplay.cs b/osu.Game/Overlays/Toolbar/ClockDisplay.cs new file mode 100644 index 0000000000..c1befbb198 --- /dev/null +++ b/osu.Game/Overlays/Toolbar/ClockDisplay.cs @@ -0,0 +1,28 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using osu.Framework.Graphics.Containers; + +namespace osu.Game.Overlays.Toolbar +{ + public abstract class ClockDisplay : CompositeDrawable + { + private int? lastSecond; + + protected override void Update() + { + base.Update(); + + var now = DateTimeOffset.Now; + + if (now.Second != lastSecond) + { + lastSecond = now.Second; + UpdateDisplay(now); + } + } + + protected abstract void UpdateDisplay(DateTimeOffset now); + } +} diff --git a/osu.Game/Overlays/Toolbar/DigitalClockDisplay.cs b/osu.Game/Overlays/Toolbar/DigitalClockDisplay.cs new file mode 100644 index 0000000000..090f8c4a0f --- /dev/null +++ b/osu.Game/Overlays/Toolbar/DigitalClockDisplay.cs @@ -0,0 +1,64 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Game.Graphics; +using osu.Game.Graphics.Sprites; +using osuTK; + +namespace osu.Game.Overlays.Toolbar +{ + public class DigitalClockDisplay : ClockDisplay + { + private OsuSpriteText realTime; + private OsuSpriteText gameTime; + + private bool showRuntime = true; + + public bool ShowRuntime + { + get => showRuntime; + set + { + if (showRuntime == value) + return; + + showRuntime = value; + updateMetrics(); + } + } + + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + AutoSizeAxes = Axes.Y; + + InternalChildren = new Drawable[] + { + realTime = new OsuSpriteText(), + gameTime = new OsuSpriteText + { + Y = 14, + Colour = colours.PinkLight, + Scale = new Vector2(0.6f) + } + }; + + updateMetrics(); + } + + protected override void UpdateDisplay(DateTimeOffset now) + { + realTime.Text = $"{now:HH:mm:ss}"; + gameTime.Text = $"running {new TimeSpan(TimeSpan.TicksPerSecond * (int)(Clock.CurrentTime / 1000)):c}"; + } + + private void updateMetrics() + { + Width = showRuntime ? 66 : 45; // Allows for space for game time up to 99 days (in the padding area since this is quite rare). + gameTime.FadeTo(showRuntime ? 1 : 0); + } + } +} diff --git a/osu.Game/Overlays/Toolbar/ToolbarClock.cs b/osu.Game/Overlays/Toolbar/ToolbarClock.cs index 68772d8fc3..332bfdf638 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarClock.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarClock.cs @@ -1,18 +1,13 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Shapes; using osu.Framework.Input.Events; using osu.Game.Configuration; -using osu.Game.Graphics; -using osu.Game.Graphics.Sprites; using osuTK; -using osuTK.Graphics; namespace osu.Game.Overlays.Toolbar { @@ -20,10 +15,8 @@ namespace osu.Game.Overlays.Toolbar { private Bindable clockDisplayMode; - private DigitalDisplay digital; - private AnalogDisplay analog; - - private const float hand_thickness = 2.4f; + private DigitalClockDisplay digital; + private AnalogClockDisplay analog; public ToolbarClock() { @@ -46,12 +39,12 @@ namespace osu.Game.Overlays.Toolbar Spacing = new Vector2(5), Children = new Drawable[] { - analog = new AnalogDisplay + analog = new AnalogClockDisplay { Anchor = Anchor.CentreLeft, Origin = Anchor.CentreLeft, }, - digital = new DigitalDisplay + digital = new DigitalClockDisplay { Anchor = Anchor.CentreLeft, Origin = Anchor.CentreLeft, @@ -104,220 +97,5 @@ namespace osu.Game.Overlays.Toolbar break; } } - - private class DigitalDisplay : ClockDisplay - { - private OsuSpriteText realTime; - private OsuSpriteText gameTime; - - private bool showRuntime = true; - - public bool ShowRuntime - { - get => showRuntime; - set - { - if (showRuntime == value) - return; - - showRuntime = value; - updateMetrics(); - } - } - - [BackgroundDependencyLoader] - private void load(OsuColour colours) - { - AutoSizeAxes = Axes.Y; - - InternalChildren = new Drawable[] - { - realTime = new OsuSpriteText(), - gameTime = new OsuSpriteText - { - Y = 14, - Colour = colours.PinkLight, - Scale = new Vector2(0.6f) - } - }; - - updateMetrics(); - } - - protected override void UpdateDisplay(DateTimeOffset now) - { - realTime.Text = $"{now:HH:mm:ss}"; - gameTime.Text = $"running {new TimeSpan(TimeSpan.TicksPerSecond * (int)(Clock.CurrentTime / 1000)):c}"; - } - - private void updateMetrics() - { - Width = showRuntime ? 66 : 45; // Allows for space for game time up to 99 days (in the padding area since this is quite rare). - gameTime.FadeTo(showRuntime ? 1 : 0); - } - } - - private class AnalogDisplay : ClockDisplay - { - private Drawable hour; - private Drawable minute; - private Drawable second; - - [BackgroundDependencyLoader] - private void load() - { - Size = new Vector2(22); - - InternalChildren = new[] - { - new CircularContainer - { - RelativeSizeAxes = Axes.Both, - Masking = true, - BorderThickness = 2, - BorderColour = Color4.White, - Child = new Box - { - AlwaysPresent = true, - Alpha = 0, - RelativeSizeAxes = Axes.Both - }, - }, - hour = new LargeHand(0.34f), - minute = new LargeHand(0.48f), - second = new SecondHand(), - new CentreCircle - { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - } - }; - } - - private class CentreCircle : CompositeDrawable - { - [BackgroundDependencyLoader] - private void load(OsuColour colours) - { - InternalChildren = new Drawable[] - { - new Circle - { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - Size = new Vector2(hand_thickness), - Colour = Color4.White, - }, - new Circle - { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - Size = new Vector2(hand_thickness * 0.7f), - Colour = colours.PinkLight, - }, - }; - } - } - - private class SecondHand : CompositeDrawable - { - [BackgroundDependencyLoader] - private void load(OsuColour colours) - { - RelativeSizeAxes = Axes.X; - Width = 0.66f; - - Height = hand_thickness * 0.7f; - Anchor = Anchor.Centre; - Origin = Anchor.Custom; - - OriginPosition = new Vector2(Height * 2, Height / 2); - - InternalChildren = new Drawable[] - { - new Circle - { - Colour = colours.PinkLight, - RelativeSizeAxes = Axes.Both, - }, - }; - } - } - - private class LargeHand : CompositeDrawable - { - public LargeHand(float length) - { - Width = length; - } - - [BackgroundDependencyLoader] - private void load(OsuColour colours) - { - Anchor = Anchor.Centre; - Origin = Anchor.CentreLeft; - - Origin = Anchor.Custom; - OriginPosition = new Vector2(hand_thickness / 2); // offset x also, to ensure the centre of the line is centered on the face. - Height = hand_thickness; - - InternalChildren = new Drawable[] - { - new Circle - { - Colour = Color4.White, - RelativeSizeAxes = Axes.Both, - BorderThickness = 0.7f, - BorderColour = colours.Gray2, - }, - }; - - RelativeSizeAxes = Axes.X; - } - } - - protected override void UpdateDisplay(DateTimeOffset now) - { - float secondFractional = now.Second / 60f; - float minuteFractional = (now.Minute + secondFractional) / 60f; - float hourFractional = ((minuteFractional + now.Hour) % 12) / 12f; - - updateRotation(hour, hourFractional); - updateRotation(minute, minuteFractional); - updateRotation(second, secondFractional); - } - - private void updateRotation(Drawable hand, float fraction) - { - const float duration = 320; - - float rotation = fraction * 360 - 90; - - if (Math.Abs(hand.Rotation - rotation) > 180) - hand.RotateTo(rotation); - else - hand.RotateTo(rotation, duration, Easing.OutElastic); - } - } - - private abstract class ClockDisplay : CompositeDrawable - { - private int? lastSecond; - - protected override void Update() - { - base.Update(); - - var now = DateTimeOffset.Now; - - if (now.Second != lastSecond) - { - lastSecond = now.Second; - UpdateDisplay(now); - } - } - - protected abstract void UpdateDisplay(DateTimeOffset now); - } } }