2022-05-20 16:12:35 +08: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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using osu.Framework.Allocation;
|
2022-06-01 13:36:25 +08:00
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Audio.Sample;
|
2022-05-20 16:12:35 +08:00
|
|
|
using osu.Framework.Audio.Track;
|
|
|
|
using osu.Framework.Bindables;
|
2022-05-21 20:48:51 +08:00
|
|
|
using osu.Framework.Extensions.LocalisationExtensions;
|
2022-05-20 16:12:35 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-05-20 16:21:56 +08:00
|
|
|
using osu.Framework.Graphics.Colour;
|
2022-05-20 16:12:35 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2022-06-21 13:52:57 +08:00
|
|
|
using osu.Framework.Threading;
|
2022-05-28 09:05:24 +08:00
|
|
|
using osu.Framework.Timing;
|
2022-05-20 16:12:35 +08:00
|
|
|
using osu.Framework.Utils;
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Timing
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class MetronomeDisplay : BeatSyncedContainer
|
2022-05-20 16:12:35 +08:00
|
|
|
{
|
2023-01-14 07:23:21 +08:00
|
|
|
private Container swing = null!;
|
2022-05-20 16:12:35 +08:00
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private OsuSpriteText bpmText = null!;
|
2022-05-20 16:12:35 +08:00
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private Drawable weight = null!;
|
|
|
|
private Drawable stick = null!;
|
2022-05-20 16:12:35 +08:00
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private IAdjustableClock metronomeClock = null!;
|
2022-05-28 09:05:24 +08:00
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private Sample? sampleTick;
|
|
|
|
private Sample? sampleTickDownbeat;
|
|
|
|
private Sample? sampleLatch;
|
2022-06-01 13:36:25 +08:00
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private ScheduledDelegate? tickPlaybackDelegate;
|
2022-06-18 09:07:21 +08:00
|
|
|
|
2022-05-20 16:12:35 +08:00
|
|
|
[Resolved]
|
2023-01-14 07:23:21 +08:00
|
|
|
private OverlayColourProvider overlayColourProvider { get; set; } = null!;
|
2022-05-20 16:12:35 +08:00
|
|
|
|
2022-06-01 19:48:06 +08:00
|
|
|
public bool EnableClicking { get; set; } = true;
|
|
|
|
|
2022-06-18 09:39:53 +08:00
|
|
|
public MetronomeDisplay()
|
|
|
|
{
|
|
|
|
AllowMistimedEventFiring = false;
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:12:35 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2022-06-01 13:36:25 +08:00
|
|
|
private void load(AudioManager audio)
|
2022-05-20 16:12:35 +08:00
|
|
|
{
|
2022-06-21 14:05:17 +08:00
|
|
|
sampleTick = audio.Samples.Get(@"UI/metronome-tick");
|
|
|
|
sampleTickDownbeat = audio.Samples.Get(@"UI/metronome-tick-downbeat");
|
|
|
|
sampleLatch = audio.Samples.Get(@"UI/metronome-latch");
|
2022-06-01 13:36:25 +08:00
|
|
|
|
2022-05-20 16:12:35 +08:00
|
|
|
const float taper = 25;
|
|
|
|
const float swing_vertical_offset = -23;
|
|
|
|
const float lower_cover_height = 32;
|
|
|
|
|
|
|
|
var triangleSize = new Vector2(90, 120 + taper);
|
|
|
|
|
|
|
|
Margin = new MarginPadding(10);
|
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
Name = @"Taper adjust",
|
|
|
|
Masking = true,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Size = new Vector2(triangleSize.X, triangleSize.Y - taper),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Triangle
|
|
|
|
{
|
|
|
|
Name = @"Main body",
|
|
|
|
EdgeSmoothness = new Vector2(1),
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Size = triangleSize,
|
|
|
|
Colour = overlayColourProvider.Background3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
new Circle
|
|
|
|
{
|
|
|
|
Name = "Centre marker",
|
|
|
|
Colour = overlayColourProvider.Background5,
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Width = 2,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Y = -(lower_cover_height + 3),
|
|
|
|
Height = 0.65f,
|
|
|
|
},
|
|
|
|
swing = new Container
|
|
|
|
{
|
|
|
|
Name = @"Swing",
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Y = swing_vertical_offset,
|
|
|
|
Height = 0.80f,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Children = new[]
|
|
|
|
{
|
|
|
|
stick = new Circle
|
|
|
|
{
|
|
|
|
Name = @"Stick",
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Colour = overlayColourProvider.Colour2,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Width = 4,
|
|
|
|
},
|
|
|
|
weight = new Container
|
|
|
|
{
|
|
|
|
Name = @"Weight",
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Size = new Vector2(10),
|
|
|
|
Rotation = 180,
|
|
|
|
RelativePositionAxes = Axes.Y,
|
|
|
|
Y = 0.4f,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Shear = new Vector2(0.2f, 0),
|
2022-05-20 16:21:56 +08:00
|
|
|
Colour = overlayColourProvider.Colour1,
|
2022-05-20 16:12:35 +08:00
|
|
|
EdgeSmoothness = new Vector2(1),
|
|
|
|
},
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Shear = new Vector2(-0.2f, 0),
|
2022-05-20 16:21:56 +08:00
|
|
|
Colour = overlayColourProvider.Colour1,
|
2022-05-20 16:12:35 +08:00
|
|
|
EdgeSmoothness = new Vector2(1),
|
|
|
|
},
|
2022-05-20 16:21:56 +08:00
|
|
|
new Circle
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Colour = ColourInfo.GradientVertical(overlayColourProvider.Colour1, overlayColourProvider.Colour0),
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Width = 1,
|
|
|
|
Height = 0.9f
|
|
|
|
},
|
2022-05-20 16:12:35 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
Name = @"Taper adjust",
|
|
|
|
Masking = true,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Size = new Vector2(triangleSize.X, triangleSize.Y - taper),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Circle
|
|
|
|
{
|
|
|
|
Name = @"Locking wedge",
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Colour = overlayColourProvider.Background1,
|
|
|
|
Size = new Vector2(8),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
new Circle
|
|
|
|
{
|
|
|
|
Name = @"Swing connection point",
|
|
|
|
Y = swing_vertical_offset,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Colour = overlayColourProvider.Colour0,
|
|
|
|
Size = new Vector2(8)
|
|
|
|
},
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
Name = @"Lower cover",
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Masking = true,
|
|
|
|
Height = lower_cover_height,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Triangle
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Size = triangleSize,
|
|
|
|
Colour = overlayColourProvider.Background2,
|
|
|
|
EdgeSmoothness = new Vector2(1),
|
|
|
|
Alpha = 0.8f
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
bpmText = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Name = @"BPM display",
|
|
|
|
Colour = overlayColourProvider.Content1,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Y = -3,
|
|
|
|
},
|
|
|
|
};
|
2022-05-28 09:05:24 +08:00
|
|
|
|
|
|
|
Clock = new FramedClock(metronomeClock = new StopwatchClock(true));
|
2022-05-20 16:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private double beatLength;
|
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private TimingControlPoint timingPoint = null!;
|
2022-05-20 16:12:35 +08:00
|
|
|
|
|
|
|
private bool isSwinging;
|
|
|
|
|
|
|
|
private readonly BindableInt interpolatedBpm = new BindableInt();
|
|
|
|
|
2023-01-14 07:23:21 +08:00
|
|
|
private ScheduledDelegate? latchDelegate;
|
2022-06-21 13:52:57 +08:00
|
|
|
|
2022-05-20 16:12:35 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-05-21 20:48:51 +08:00
|
|
|
interpolatedBpm.BindValueChanged(bpm => bpmText.Text = bpm.NewValue.ToLocalisableString());
|
2022-05-20 16:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2022-05-22 21:15:53 +08:00
|
|
|
if (BeatSyncSource.ControlPoints == null || BeatSyncSource.Clock == null)
|
|
|
|
return;
|
|
|
|
|
2022-05-28 09:05:24 +08:00
|
|
|
metronomeClock.Rate = IsBeatSyncedWithTrack ? BeatSyncSource.Clock.Rate : 1;
|
|
|
|
|
2022-05-22 21:15:53 +08:00
|
|
|
timingPoint = BeatSyncSource.ControlPoints.TimingPointAt(BeatSyncSource.Clock.CurrentTime);
|
2022-05-20 16:12:35 +08:00
|
|
|
|
|
|
|
if (beatLength != timingPoint.BeatLength)
|
|
|
|
{
|
|
|
|
beatLength = timingPoint.BeatLength;
|
|
|
|
|
|
|
|
EarlyActivationMilliseconds = timingPoint.BeatLength / 2;
|
|
|
|
|
|
|
|
float bpmRatio = (float)Interpolation.ApplyEasing(Easing.OutQuad, Math.Clamp((timingPoint.BPM - 30) / 480, 0, 1));
|
|
|
|
|
|
|
|
weight.MoveToY((float)Interpolation.Lerp(0.1f, 0.83f, bpmRatio), 600, Easing.OutQuint);
|
2022-05-20 22:24:21 +08:00
|
|
|
this.TransformBindableTo(interpolatedBpm, (int)Math.Round(timingPoint.BPM), 600, Easing.OutQuint);
|
2022-05-20 16:12:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-22 21:15:53 +08:00
|
|
|
if (BeatSyncSource.Clock?.IsRunning != true && isSwinging)
|
2022-05-20 16:12:35 +08:00
|
|
|
{
|
|
|
|
swing.ClearTransforms(true);
|
|
|
|
|
2022-06-17 15:56:09 +08:00
|
|
|
isSwinging = false;
|
|
|
|
|
2022-06-21 14:05:17 +08:00
|
|
|
tickPlaybackDelegate?.Cancel();
|
|
|
|
tickPlaybackDelegate = null;
|
2022-06-21 13:58:58 +08:00
|
|
|
|
2022-06-17 15:56:09 +08:00
|
|
|
// instantly latch if pendulum arm is close enough to center (to prevent awkward delayed playback of latch sound)
|
|
|
|
if (Precision.AlmostEquals(swing.Rotation, 0, 1))
|
|
|
|
{
|
2022-06-21 14:07:35 +08:00
|
|
|
swing.RotateTo(0, 60, Easing.OutQuint);
|
2022-06-22 14:33:42 +08:00
|
|
|
stick.FadeColour(overlayColourProvider.Colour2, 1000, Easing.OutQuint);
|
2022-06-21 14:05:17 +08:00
|
|
|
sampleLatch?.Play();
|
2022-06-17 15:56:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-05-20 16:12:35 +08:00
|
|
|
|
2022-06-21 14:02:53 +08:00
|
|
|
using (BeginDelayedSequence(350))
|
2022-05-20 16:12:35 +08:00
|
|
|
{
|
|
|
|
swing.RotateTo(0, 1000, Easing.OutQuint);
|
2022-06-21 14:02:43 +08:00
|
|
|
stick.FadeColour(overlayColourProvider.Colour2, 1000, Easing.OutQuint);
|
2022-06-21 13:52:57 +08:00
|
|
|
|
2022-06-21 14:02:53 +08:00
|
|
|
using (BeginDelayedSequence(380))
|
2022-06-21 14:05:17 +08:00
|
|
|
latchDelegate = Schedule(() => sampleLatch?.Play());
|
2022-05-20 16:12:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
|
|
|
|
{
|
|
|
|
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes);
|
|
|
|
|
|
|
|
const float angle = 27.5f;
|
|
|
|
|
|
|
|
if (!IsBeatSyncedWithTrack)
|
|
|
|
return;
|
|
|
|
|
|
|
|
isSwinging = true;
|
|
|
|
|
2022-06-21 13:52:57 +08:00
|
|
|
latchDelegate?.Cancel();
|
|
|
|
latchDelegate = null;
|
|
|
|
|
2022-05-20 16:12:35 +08:00
|
|
|
float currentAngle = swing.Rotation;
|
|
|
|
float targetAngle = currentAngle > 0 ? -angle : angle;
|
|
|
|
|
2022-05-22 23:03:42 +08:00
|
|
|
swing.RotateTo(targetAngle, beatLength, Easing.InOutQuad);
|
2022-05-20 16:12:35 +08:00
|
|
|
|
|
|
|
if (currentAngle != 0 && Math.Abs(currentAngle - targetAngle) > angle * 1.8f && isSwinging)
|
|
|
|
{
|
2022-06-01 13:36:25 +08:00
|
|
|
using (BeginDelayedSequence(beatLength / 2))
|
|
|
|
{
|
2022-05-20 16:12:35 +08:00
|
|
|
stick.FlashColour(overlayColourProvider.Content1, beatLength, Easing.OutQuint);
|
2022-06-01 17:03:03 +08:00
|
|
|
|
2022-06-21 14:05:17 +08:00
|
|
|
tickPlaybackDelegate = Schedule(() =>
|
2022-06-01 13:36:25 +08:00
|
|
|
{
|
2022-06-01 19:48:06 +08:00
|
|
|
if (!EnableClicking)
|
|
|
|
return;
|
|
|
|
|
2022-06-21 14:05:17 +08:00
|
|
|
var channel = beatIndex % timingPoint.TimeSignature.Numerator == 0 ? sampleTickDownbeat?.GetChannel() : sampleTick?.GetChannel();
|
2022-06-01 17:04:13 +08:00
|
|
|
|
2022-06-17 15:56:09 +08:00
|
|
|
if (channel == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
channel.Frequency.Value = RNG.NextDouble(0.98f, 1.02f);
|
|
|
|
channel.Play();
|
2022-06-01 17:04:13 +08:00
|
|
|
});
|
2022-06-01 13:36:25 +08:00
|
|
|
}
|
2022-05-20 16:12:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|