1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 00:47:24 +08:00

Add basic implementation of clock

This commit is contained in:
Dean Herbert 2022-03-26 16:35:53 +09:00
parent a30d6256f4
commit 0d8a7246dd
3 changed files with 230 additions and 0 deletions

View File

@ -0,0 +1,64 @@
// 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.
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Overlays.Toolbar;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.Menus
{
[TestFixture]
public class TestSceneToolbarClock : OsuManualInputManagerTestScene
{
public TestSceneToolbarClock()
{
Children = new Drawable[]
{
new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
Height = Toolbar.HEIGHT,
Scale = new Vector2(4),
Children = new Drawable[]
{
new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
},
new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
new Box
{
Colour = Color4.DarkRed,
RelativeSizeAxes = Axes.Y,
Width = 2,
},
new ToolbarClock(),
new Box
{
Colour = Color4.DarkRed,
RelativeSizeAxes = Axes.Y,
Width = 2,
},
}
},
}
},
};
}
}
}

View File

@ -104,6 +104,7 @@ namespace osu.Game.Overlays.Toolbar
// Icon = FontAwesome.Solid.search
//},
userButton = new ToolbarUserButton(),
new ToolbarClock(),
new ToolbarNotificationButton(),
}
}

View File

@ -0,0 +1,165 @@
// 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.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays.Toolbar
{
public class ToolbarClock : CompositeDrawable
{
public ToolbarClock()
{
RelativeSizeAxes = Axes.Y;
Width = 110;
Padding = new MarginPadding(10);
}
[BackgroundDependencyLoader]
private void load()
{
InternalChildren = new Drawable[]
{
new AnalogDisplay
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
new DigitalDisplay
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
}
};
}
private class DigitalDisplay : ClockDisplay
{
private OsuSpriteText text;
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Y;
Width = 50;
InternalChildren = new Drawable[]
{
text = new OsuSpriteText
{
Text = "00:00:00",
}
};
}
protected override void UpdateDisplay(DateTimeOffset now)
{
text.Text = $"{now:HH:mm:ss}";
}
}
private class AnalogDisplay : ClockDisplay
{
private Drawable hour;
private Drawable minute;
private Drawable second;
[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(30);
InternalChildren = new[]
{
new Circle
{
RelativeSizeAxes = Axes.Both,
},
hour = new Hand
{
Colour = Color4.Orange,
Size = new Vector2(0.3f, 1.2f),
},
minute = new Hand
{
Colour = Color4.Green,
Size = new Vector2(0.45f, 1),
},
second = new Hand
{
Colour = Color4.Blue,
Size = new Vector2(0.48f, 0.5f),
},
new Circle
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(1.5f),
Colour = Color4.Black,
},
};
}
private class Hand : Circle
{
public Hand()
{
Anchor = Anchor.Centre;
Origin = Anchor.CentreLeft;
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);
}
}
}