1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 22:52:57 +08:00
osu-lazer/osu.Game/Screens/Edit/Timing/TapButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

420 lines
15 KiB
C#
Raw Normal View History

2022-06-01 17:53: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.
2022-06-01 19:37:02 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2022-06-01 17:53:35 +08:00
using osu.Framework.Allocation;
2022-06-01 19:44:05 +08:00
using osu.Framework.Bindables;
2022-06-01 17:53:35 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
2022-06-01 19:37:02 +08:00
using osu.Framework.Graphics.Colour;
2022-06-01 17:53:35 +08:00
using osu.Framework.Graphics.Containers;
2022-06-01 19:04:44 +08:00
using osu.Framework.Graphics.Effects;
2022-06-01 17:53:35 +08:00
using osu.Framework.Graphics.Shapes;
2022-06-01 18:45:11 +08:00
using osu.Framework.Graphics.UserInterface;
2022-06-02 11:27:11 +08:00
using osu.Framework.Input.Bindings;
2022-06-01 17:53:35 +08:00
using osu.Framework.Input.Events;
2022-06-01 19:37:02 +08:00
using osu.Framework.Threading;
using osu.Game.Beatmaps;
2022-06-01 19:44:05 +08:00
using osu.Game.Beatmaps.ControlPoints;
2022-06-01 18:45:11 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2022-06-02 11:27:11 +08:00
using osu.Game.Input.Bindings;
2022-06-01 17:53:35 +08:00
using osu.Game.Overlays;
using osuTK;
using osuTK.Graphics;
2022-06-02 11:27:11 +08:00
using osuTK.Input;
2022-06-01 17:53:35 +08:00
namespace osu.Game.Screens.Edit.Timing
{
2022-06-02 11:27:11 +08:00
internal partial class TapButton : CircularContainer, IKeyBindingHandler<GlobalAction>
2022-06-01 17:53:35 +08:00
{
public const float SIZE = 140;
2022-06-01 17:53:35 +08:00
public readonly BindableBool IsHandlingTapping = new BindableBool();
2022-06-01 17:53:35 +08:00
[Resolved]
2022-06-01 19:44:05 +08:00
private OverlayColourProvider colourProvider { get; set; } = null!;
2023-01-14 07:23:21 +08:00
[Resolved]
2022-06-01 19:44:05 +08:00
private Bindable<ControlPointGroup>? selectedGroup { get; set; }
2022-06-01 17:53:35 +08:00
2023-01-14 07:23:21 +08:00
[Resolved]
private IBeatSyncProvider? beatSyncSource { get; set; }
2022-06-01 19:44:05 +08:00
private Circle hoverLayer = null!;
2022-06-01 18:45:11 +08:00
2022-06-01 19:44:05 +08:00
private CircularContainer innerCircle = null!;
private Box innerCircleHighlight = null!;
2022-06-01 18:45:11 +08:00
private int currentLight;
2022-06-01 17:53:35 +08:00
2022-06-01 19:44:05 +08:00
private Container scaleContainer = null!;
private Container lights = null!;
private Container lightsGlow = null!;
private OsuSpriteText bpmText = null!;
private Container textContainer = null!;
2022-06-01 19:37:02 +08:00
private bool grabbedMouseDown;
2022-06-01 19:44:05 +08:00
private ScheduledDelegate? resetDelegate;
2022-06-01 18:45:11 +08:00
private const int light_count = 8;
2022-06-01 17:53:35 +08:00
private const int initial_taps_to_ignore = 4;
private const int max_taps_to_consider = 128;
2022-06-02 12:16:31 +08:00
private const double transition_length = 500;
private const float angular_light_gap = 0.007f;
2022-06-02 11:27:11 +08:00
private readonly List<double> tapTimings = new List<double>();
2022-06-01 17:53:35 +08:00
[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(SIZE);
const float ring_width = 22;
2022-06-01 17:53:35 +08:00
const float light_padding = 3;
InternalChild = scaleContainer = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
2022-06-01 18:45:11 +08:00
new Circle
2022-06-01 17:53:35 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4
2022-06-01 17:53:35 +08:00
},
2022-06-01 18:45:11 +08:00
lights = new Container
{
RelativeSizeAxes = Axes.Both,
},
2022-06-01 17:53:35 +08:00
new CircularContainer
{
RelativeSizeAxes = Axes.Both,
2022-06-01 19:37:02 +08:00
Name = @"outer masking",
2022-06-01 17:53:35 +08:00
Masking = true,
BorderThickness = light_padding,
BorderColour = colourProvider.Background4,
2022-06-01 17:53:35 +08:00
Children = new Drawable[]
{
new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true,
},
}
},
2022-06-01 18:45:11 +08:00
new Circle
{
2022-06-01 19:37:02 +08:00
Name = @"inner masking",
2022-06-01 18:45:11 +08:00
Size = new Vector2(SIZE - ring_width * 2 + light_padding * 2),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Colour = colourProvider.Background4,
2022-06-01 18:45:11 +08:00
},
2022-06-01 19:04:44 +08:00
lightsGlow = new Container
{
RelativeSizeAxes = Axes.Both,
},
2022-06-01 17:53:35 +08:00
innerCircle = new CircularContainer
{
2022-06-01 18:45:11 +08:00
Size = new Vector2(SIZE - ring_width * 2),
2022-06-01 17:53:35 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true,
Children = new Drawable[]
{
new Box
{
Colour = colourProvider.Background2,
RelativeSizeAxes = Axes.Both,
2022-06-01 18:45:11 +08:00
},
innerCircleHighlight = new Box
{
Colour = colourProvider.Colour3,
Blending = BlendingParameters.Additive,
RelativeSizeAxes = Axes.Both,
Alpha = 0,
},
2022-06-01 19:37:02 +08:00
textContainer = new Container
2022-06-01 18:45:11 +08:00
{
2022-06-01 19:37:02 +08:00
RelativeSizeAxes = Axes.Both,
2022-06-01 18:45:11 +08:00
Colour = colourProvider.Background1,
2022-06-01 19:37:02 +08:00
Children = new Drawable[]
{
new OsuSpriteText
{
Font = OsuFont.Torus.With(size: 34, weight: FontWeight.SemiBold),
2022-06-01 19:37:02 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.BottomCentre,
Y = 5,
Text = "Tap",
2022-06-01 19:37:02 +08:00
},
bpmText = new OsuSpriteText
{
Font = OsuFont.Torus.With(size: 23, weight: FontWeight.Regular),
2022-06-01 19:37:02 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
Y = -1,
2022-06-01 19:37:02 +08:00
},
}
2022-06-01 18:45:11 +08:00
},
hoverLayer = new Circle
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background1.Opacity(0.3f),
Blending = BlendingParameters.Additive,
Alpha = 0,
2022-06-01 17:53:35 +08:00
},
}
},
}
};
2022-06-01 18:45:11 +08:00
for (int i = 0; i < light_count; i++)
{
2022-06-01 19:04:44 +08:00
var light = new Light
2022-06-01 18:45:11 +08:00
{
Rotation = (i + 1) * (360f / light_count) + 360 * angular_light_gap / 2,
2022-06-01 19:04:44 +08:00
};
lights.Add(light);
lightsGlow.Add(light.Glow.CreateProxy());
2022-06-01 18:45:11 +08:00
}
2022-06-01 19:37:02 +08:00
reset();
2022-06-01 17:53:35 +08:00
}
2022-06-01 18:45:11 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
hoverLayer.ReceivePositionalInputAt(screenSpacePos);
2022-06-01 19:37:02 +08:00
private ColourInfo textColour
{
get
{
if (grabbedMouseDown)
return colourProvider.Background4;
if (IsHovered)
return colourProvider.Content2;
return colourProvider.Background1;
}
}
2022-06-01 17:53:35 +08:00
protected override bool OnHover(HoverEvent e)
{
2022-06-02 12:16:31 +08:00
hoverLayer.FadeIn(transition_length, Easing.OutQuint);
textContainer.FadeColour(textColour, transition_length, Easing.OutQuint);
return true;
2022-06-01 17:53:35 +08:00
}
protected override void OnHoverLost(HoverLostEvent e)
{
2022-06-02 12:16:31 +08:00
hoverLayer.FadeOut(transition_length, Easing.OutQuint);
textContainer.FadeColour(textColour, transition_length, Easing.OutQuint);
2022-06-01 17:53:35 +08:00
base.OnHoverLost(e);
}
protected override bool OnMouseDown(MouseDownEvent e)
{
2022-06-01 18:45:11 +08:00
const double in_duration = 100;
2022-06-01 19:37:02 +08:00
grabbedMouseDown = true;
IsHandlingTapping.Value = true;
2022-06-01 19:37:02 +08:00
resetDelegate?.Cancel();
handleTap();
2022-06-01 19:37:02 +08:00
textContainer.FadeColour(textColour, in_duration, Easing.OutQuint);
2022-06-01 19:04:44 +08:00
2022-06-01 18:45:11 +08:00
scaleContainer.ScaleTo(0.99f, in_duration, Easing.OutQuint);
innerCircle.ScaleTo(0.96f, in_duration, Easing.OutQuint);
innerCircleHighlight
.FadeIn(50, Easing.OutQuint)
.FlashColour(Color4.White, 1000, Easing.OutQuint);
lights[currentLight % light_count].Hide();
lights[(currentLight + light_count / 2) % light_count].Hide();
currentLight++;
lights[currentLight % light_count].Show();
lights[(currentLight + light_count / 2) % light_count].Show();
2022-06-01 19:37:02 +08:00
return true;
2022-06-01 17:53:35 +08:00
}
protected override void OnMouseUp(MouseUpEvent e)
{
2022-06-01 18:45:11 +08:00
const double out_duration = 800;
2022-06-01 19:37:02 +08:00
grabbedMouseDown = false;
textContainer.FadeColour(textColour, out_duration, Easing.OutQuint);
2022-06-01 19:04:44 +08:00
2022-06-01 18:45:11 +08:00
scaleContainer.ScaleTo(1, out_duration, Easing.OutQuint);
innerCircle.ScaleTo(1, out_duration, Easing.OutQuint);
2022-06-02 12:16:31 +08:00
2022-06-01 18:45:11 +08:00
innerCircleHighlight.FadeOut(out_duration, Easing.OutQuint);
resetDelegate = Scheduler.AddDelayed(reset, 1000);
2022-06-01 17:53:35 +08:00
base.OnMouseUp(e);
}
2022-06-01 18:45:11 +08:00
2022-06-02 11:27:11 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Action == GlobalAction.EditorTapForBPM && !e.Repeat)
{
// Direct through mouse handling to achieve animation
OnMouseDown(new MouseDownEvent(e.CurrentState, MouseButton.Left));
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
if (e.Action == GlobalAction.EditorTapForBPM)
OnMouseUp(new MouseUpEvent(e.CurrentState, MouseButton.Left));
}
2022-06-01 19:37:02 +08:00
private void handleTap()
{
if (selectedGroup?.Value == null)
return;
2022-06-01 19:37:02 +08:00
tapTimings.Add(Clock.CurrentTime);
if (tapTimings.Count > initial_taps_to_ignore + max_taps_to_consider)
2022-06-01 19:37:02 +08:00
tapTimings.RemoveAt(0);
if (tapTimings.Count < initial_taps_to_ignore * 2)
2022-06-01 19:37:02 +08:00
{
bpmText.Text = new string('.', tapTimings.Count);
return;
}
double averageBeatLength = (tapTimings.Last() - tapTimings.Skip(initial_taps_to_ignore).First()) / (tapTimings.Count - initial_taps_to_ignore - 1);
double clockRate = beatSyncSource?.Clock?.Rate ?? 1;
double bpm = Math.Round(60000 / averageBeatLength / clockRate);
2022-06-01 19:37:02 +08:00
bpmText.Text = $"{bpm} BPM";
2022-06-01 19:44:05 +08:00
var timingPoint = selectedGroup?.Value.ControlPoints.OfType<TimingControlPoint>().FirstOrDefault();
if (timingPoint != null)
{
// Intentionally use the rounded BPM here.
timingPoint.BeatLength = 60000 / bpm;
}
2022-06-01 19:37:02 +08:00
}
private void reset()
{
2022-06-02 12:16:31 +08:00
bpmText.FadeOut(transition_length, Easing.OutQuint);
2022-06-02 12:16:31 +08:00
using (BeginDelayedSequence(tapTimings.Count > 0 ? transition_length : 0))
{
Schedule(() => bpmText.Text = "the beat!");
bpmText.FadeIn(800, Easing.OutQuint);
}
foreach (var light in lights)
light.Hide();
tapTimings.Clear();
currentLight = 0;
IsHandlingTapping.Value = false;
}
2022-06-01 18:45:11 +08:00
private partial class Light : CompositeDrawable
{
2022-06-01 19:44:05 +08:00
public Drawable Glow { get; private set; } = null!;
2022-06-01 19:04:44 +08:00
2022-06-01 19:44:05 +08:00
private Container fillContent = null!;
2022-06-01 18:45:11 +08:00
[Resolved]
2022-06-01 19:44:05 +08:00
private OverlayColourProvider colourProvider { get; set; } = null!;
2022-06-01 18:45:11 +08:00
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.Both;
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
2022-06-01 19:04:44 +08:00
Size = new Vector2(0.98f); // Avoid bleed into masking edge.
2022-06-01 18:45:11 +08:00
InternalChildren = new Drawable[]
{
new CircularProgress
{
RelativeSizeAxes = Axes.Both,
Current = { Value = 1f / light_count - angular_light_gap },
2022-06-01 18:45:11 +08:00
Colour = colourProvider.Background2,
},
2022-06-01 19:04:44 +08:00
fillContent = new Container
2022-06-01 18:45:11 +08:00
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
Colour = colourProvider.Colour1,
2022-06-01 19:04:44 +08:00
Children = new[]
{
new CircularProgress
{
RelativeSizeAxes = Axes.Both,
Current = { Value = 1f / light_count - angular_light_gap },
2022-06-01 19:04:44 +08:00
Blending = BlendingParameters.Additive
},
// Please do not try and make sense of this.
// Getting the visual effect I was going for relies on what I can only imagine is broken implementation
// of `PadExtent`. If that's ever fixed in the future this will likely need to be adjusted.
2022-06-01 19:04:44 +08:00
Glow = new CircularProgress
{
RelativeSizeAxes = Axes.Both,
Current = { Value = 1f / light_count - 0.01f },
Blending = BlendingParameters.Additive
}.WithEffect(new GlowEffect
{
Colour = colourProvider.Colour1.Opacity(0.4f),
BlurSigma = new Vector2(9f),
Strength = 10,
PadExtent = true
}),
}
2022-06-01 18:45:11 +08:00
},
};
}
public override void Show()
{
2022-06-01 19:04:44 +08:00
fillContent
2022-06-01 18:45:11 +08:00
.FadeIn(50, Easing.OutQuint)
.FlashColour(Color4.White, 1000, Easing.OutQuint);
}
public override void Hide()
{
2022-06-01 19:04:44 +08:00
fillContent
2022-06-01 18:45:11 +08:00
.FadeOut(300, Easing.OutQuint);
}
}
2022-06-01 17:53:35 +08:00
}
}