1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 12:42:56 +08:00
osu-lazer/osu.Game/Screens/Edit/Timing/TimingAdjustButton.cs

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

216 lines
6.4 KiB
C#
Raw Normal View History

2022-05-31 20:10:02 +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 System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
namespace osu.Game.Screens.Edit.Timing
{
2022-06-01 16:46:05 +08:00
/// <summary>
/// A button with variable constant output based on hold position and length.
/// </summary>
public partial class TimingAdjustButton : CompositeDrawable
2022-05-31 20:10:02 +08:00
{
2023-01-14 07:23:21 +08:00
public Action<double>? Action;
2022-05-31 20:10:02 +08:00
private readonly double adjustAmount;
2022-06-01 17:02:17 +08:00
private const int max_multiplier = 10;
2022-05-31 20:10:02 +08:00
private const int adjust_levels = 4;
public Container Content { get; set; }
private readonly Box background;
private readonly OsuSpriteText text;
public LocalisableString Text
{
get => text.Text;
set => text.Text = value;
2022-05-31 20:10:02 +08:00
}
private readonly RepeatingButtonBehaviour repeatBehaviour;
2022-05-31 20:10:02 +08:00
[Resolved]
2023-01-14 07:23:21 +08:00
private OverlayColourProvider colourProvider { get; set; } = null!;
2022-05-31 20:10:02 +08:00
[Resolved]
2023-01-14 07:23:21 +08:00
private EditorBeatmap editorBeatmap { get; set; } = null!;
2022-06-01 16:46:05 +08:00
public TimingAdjustButton(double adjustAmount)
2022-05-31 20:10:02 +08:00
{
this.adjustAmount = adjustAmount;
CornerRadius = 5;
Masking = true;
AddInternal(Content = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue
},
text = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.Default.With(weight: FontWeight.SemiBold),
Padding = new MarginPadding(5),
Depth = float.MinValue
}
}
});
AddInternal(repeatBehaviour = new RepeatingButtonBehaviour(this)
{
RepeatBegan = () => editorBeatmap.BeginChange(),
RepeatEnded = () => editorBeatmap.EndChange()
});
2022-05-31 20:10:02 +08:00
}
[BackgroundDependencyLoader]
private void load()
2022-05-31 20:10:02 +08:00
{
background.Colour = colourProvider.Background3;
for (int i = 1; i <= adjust_levels; i++)
{
Content.Add(new IncrementBox(i, adjustAmount));
Content.Add(new IncrementBox(-i, adjustAmount));
}
}
protected override bool OnHover(HoverEvent e) => true;
2022-05-31 20:10:02 +08:00
protected override bool OnClick(ClickEvent e)
2022-05-31 20:10:02 +08:00
{
var hoveredBox = Content.OfType<IncrementBox>().FirstOrDefault(d => d.IsHovered);
if (hoveredBox == null)
return false;
2022-05-31 20:10:02 +08:00
2023-01-14 07:23:21 +08:00
Action?.Invoke(adjustAmount * hoveredBox.Multiplier);
2022-05-31 20:10:02 +08:00
hoveredBox.Flash();
2022-05-31 20:10:02 +08:00
repeatBehaviour.SampleFrequencyModifier = (hoveredBox.Multiplier / max_multiplier) * 0.2;
return true;
2022-05-31 20:10:02 +08:00
}
private partial class IncrementBox : CompositeDrawable
{
public readonly float Multiplier;
private readonly Box box;
private readonly OsuSpriteText text;
2023-01-14 07:23:21 +08:00
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
2022-05-31 20:10:02 +08:00
public IncrementBox(int index, double amount)
{
Multiplier = Math.Sign(index) * convertMultiplier(index);
float ratio = (float)index / adjust_levels;
RelativeSizeAxes = Axes.Both;
Width = 0.5f * Math.Abs(ratio);
Anchor direction = index < 0 ? Anchor.x2 : Anchor.x0;
Origin |= direction;
Depth = Math.Abs(index);
Anchor = Anchor.TopCentre;
InternalChildren = new Drawable[]
{
box = new Box
{
RelativeSizeAxes = Axes.Both,
Blending = BlendingParameters.Additive
},
text = new OsuSpriteText
{
Anchor = direction,
Origin = direction,
Font = OsuFont.Default.With(size: 10, weight: FontWeight.Bold),
Text = $"{(index > 0 ? "+" : "-")}{Math.Abs(Multiplier * amount)}",
Padding = new MarginPadding(2),
2022-05-31 20:10:02 +08:00
Alpha = 0,
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
box.Colour = colourProvider.Background1;
box.Alpha = 0.1f;
}
private float convertMultiplier(int m)
{
switch (Math.Abs(m))
{
default: return 1;
case 2: return 2;
case 3: return 5;
2022-06-01 17:02:17 +08:00
case 4:
return max_multiplier;
2022-05-31 20:10:02 +08:00
}
}
protected override bool OnHover(HoverEvent e)
{
box.Colour = colourProvider.Colour0;
box.FadeTo(0.2f, 100, Easing.OutQuint);
text.FadeIn(100, Easing.OutQuint);
return true;
}
protected override void OnHoverLost(HoverLostEvent e)
{
box.Colour = colourProvider.Background1;
box.FadeTo(0.1f, 500, Easing.OutQuint);
text.FadeOut(100, Easing.OutQuint);
base.OnHoverLost(e);
}
public void Flash()
{
box
.FadeTo(0.4f, 20, Easing.OutQuint)
.Then()
.FadeTo(0.2f, 400, Easing.OutQuint);
text
.MoveToY(-5, 20, Easing.OutQuint)
.Then()
.MoveToY(0, 400, Easing.OutQuint);
}
}
}
}