1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 20:07:24 +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.

375 lines
13 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:44:05 +08:00
#nullable enable
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-01 17:53:35 +08:00
using osu.Framework.Input.Events;
2022-06-01 19:37:02 +08:00
using osu.Framework.Threading;
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-01 17:53:35 +08:00
using osu.Game.Overlays;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Edit.Timing
{
internal class TapButton : CircularContainer
{
public const float SIZE = 100;
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!;
[Resolved(canBeNull: true)]
private Bindable<ControlPointGroup>? selectedGroup { get; set; }
2022-06-01 17:53:35 +08:00
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 = 6;
2022-06-01 17:53:35 +08:00
[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(SIZE);
2022-06-01 18:45:11 +08:00
const float ring_width = 18;
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.Background3
},
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.Background3,
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.Background3,
},
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: 20),
Anchor = Anchor.Centre,
Origin = Anchor.BottomCentre,
Y = 3,
Text = "Tap"
},
bpmText = new OsuSpriteText
{
Font = OsuFont.Torus.With(size: 14),
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
},
}
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 * (360f / light_count)
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)
{
hoverLayer.FadeIn(500, Easing.OutQuint);
2022-06-01 19:37:02 +08:00
textContainer.FadeColour(textColour, 500, Easing.OutQuint);
2022-06-01 17:53:35 +08:00
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
hoverLayer.FadeOut(500, Easing.OutQuint);
2022-06-01 19:37:02 +08:00
textContainer.FadeColour(textColour, 500, 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
handleTap();
resetDelegate?.Cancel();
resetDelegate = Scheduler.AddDelayed(reset, 1000);
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);
innerCircleHighlight.FadeOut(out_duration, Easing.OutQuint);
2022-06-01 17:53:35 +08:00
base.OnMouseUp(e);
}
2022-06-01 18:45:11 +08:00
2022-06-01 19:37:02 +08:00
private readonly List<double> tapTimings = new List<double>();
private void reset()
{
bpmText.FadeOut(500, Easing.OutQuint);
using (BeginDelayedSequence(tapTimings.Count > 0 ? 500 : 0))
{
Schedule(() => bpmText.Text = "the beat!");
bpmText.FadeIn(800, Easing.OutQuint);
}
foreach (var light in lights)
light.Hide();
tapTimings.Clear();
IsHandlingTapping.Value = false;
2022-06-01 19:37:02 +08:00
}
private void handleTap()
{
tapTimings.Add(Clock.CurrentTime);
if (tapTimings.Count > 8)
tapTimings.RemoveAt(0);
if (tapTimings.Count < 5)
{
bpmText.Text = new string('.', tapTimings.Count);
return;
}
double bpm = Math.Round(60000 / ((tapTimings.Last() - tapTimings.First()) / (tapTimings.Count - 1)));
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
}
2022-06-01 18:45:11 +08:00
private 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 - 0.01f },
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 - 0.01f },
Blending = BlendingParameters.Additive
},
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
}
}