1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 17:37:24 +08:00
osu-lazer/osu.Game/Screens/Edit/Timing/TapTimingControl.cs

344 lines
14 KiB
C#
Raw Normal View History

2022-05-20 13:34:33 +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.Collections.Generic;
2022-05-31 20:10:02 +08:00
using System.Linq;
using System.Runtime.CompilerServices;
2022-05-20 13:34:33 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2022-05-20 13:34:33 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2022-05-20 14:56:39 +08:00
using osu.Game.Beatmaps.ControlPoints;
2022-05-20 13:34:33 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
2022-05-20 13:34:33 +08:00
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
using osu.Game.Rulesets.Objects;
using osuTK;
2022-05-20 13:34:33 +08:00
namespace osu.Game.Screens.Edit.Timing
{
2022-11-24 13:32:20 +08:00
public partial class TapTimingControl : CompositeDrawable
2022-05-20 13:34:33 +08:00
{
[Resolved]
2022-06-02 12:10:50 +08:00
private EditorClock editorClock { get; set; } = null!;
2022-05-20 13:34:33 +08:00
2022-05-31 20:10:02 +08:00
[Resolved]
2022-06-02 12:10:50 +08:00
private EditorBeatmap beatmap { get; set; } = null!;
2022-05-31 20:10:02 +08:00
[Resolved]
2022-06-02 12:10:50 +08:00
private Bindable<ControlPointGroup> selectedGroup { get; set; } = null!;
private readonly BindableBool isHandlingTapping = new BindableBool();
2022-06-02 12:10:50 +08:00
private MetronomeDisplay metronome = null!;
private LabelledSwitchButton adjustPlacedNotes = null!;
2022-05-20 13:34:33 +08:00
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider, OsuColour colours)
2022-05-20 13:34:33 +08:00
{
const float padding = 10;
2022-05-20 13:34:33 +08:00
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2022-05-20 13:34:33 +08:00
CornerRadius = LabelledDrawable<Drawable>.CORNER_RADIUS;
Masking = true;
InternalChildren = new Drawable[]
{
new Box
{
Colour = colourProvider.Background4,
RelativeSizeAxes = Axes.Both,
},
new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2022-05-20 13:34:33 +08:00
RowDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 200),
new Dimension(GridSizeMode.Absolute, 50),
new Dimension(GridSizeMode.Absolute, 50),
new Dimension(GridSizeMode.Absolute, TapButton.SIZE + padding),
2022-05-20 13:34:33 +08:00
},
Content = new[]
{
new Drawable[]
{
new GridContainer
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(padding),
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension()
},
Content = new[]
{
new Drawable[]
{
metronome = new MetronomeDisplay
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
new WaveformComparisonDisplay()
}
},
}
2022-05-31 20:10:02 +08:00
},
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Bottom = padding, Horizontal = padding },
2022-05-31 20:10:02 +08:00
Children = new Drawable[]
{
2022-06-01 16:46:05 +08:00
new TimingAdjustButton(1)
2022-05-31 20:10:02 +08:00
{
Text = "Offset",
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.48f, 1),
2022-05-31 20:10:02 +08:00
Action = adjustOffset,
},
2022-06-01 16:46:05 +08:00
new TimingAdjustButton(0.1)
2022-05-31 20:10:02 +08:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Text = "BPM",
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.48f, 1),
2022-05-31 20:10:02 +08:00
Action = adjustBpm,
}
}
},
2022-05-20 13:34:33 +08:00
},
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Bottom = padding, Horizontal = padding },
Children = new Drawable[]
{
adjustPlacedNotes = new LabelledSwitchButton { Label = "Move already placed notes\nwhen changing the offset/BPM" },
}
},
},
new Drawable[]
2022-05-20 13:34:33 +08:00
{
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Bottom = padding, Horizontal = padding },
2022-05-20 13:34:33 +08:00
Children = new Drawable[]
{
new Container
2022-05-20 13:34:33 +08:00
{
RelativeSizeAxes = Axes.Y,
Anchor = Anchor.Centre,
Origin = Anchor.CentreRight,
Height = 0.98f,
Width = TapButton.SIZE / 1.3f,
Masking = true,
CornerRadius = 15,
Children = new Drawable[]
{
2022-06-02 12:10:50 +08:00
new InlineButton(FontAwesome.Solid.Stop, Anchor.TopLeft)
{
BackgroundColour = colourProvider.Background1,
RelativeSizeAxes = Axes.Both,
Height = 0.49f,
Action = reset,
},
2022-06-02 12:10:50 +08:00
new InlineButton(FontAwesome.Solid.Play, Anchor.BottomLeft)
{
BackgroundColour = colourProvider.Background1,
RelativeSizeAxes = Axes.Both,
Height = 0.49f,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Action = start,
},
},
2022-05-20 13:34:33 +08:00
},
new TapButton
2022-05-20 13:34:33 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
IsHandlingTapping = { BindTarget = isHandlingTapping }
2022-05-20 13:34:33 +08:00
}
}
},
2022-06-01 17:53:35 +08:00
},
2022-05-20 13:34:33 +08:00
}
},
};
isHandlingTapping.BindValueChanged(handling =>
{
metronome.EnableClicking = !handling.NewValue;
if (handling.NewValue)
start();
}, true);
2022-05-20 13:34:33 +08:00
}
2022-06-02 12:10:50 +08:00
private void start()
{
if (selectedGroup.Value == null)
return;
2022-06-02 12:10:50 +08:00
editorClock.Seek(selectedGroup.Value.Time);
editorClock.Start();
}
private void reset()
{
if (selectedGroup.Value == null)
return;
2022-06-02 12:10:50 +08:00
editorClock.Stop();
editorClock.Seek(selectedGroup.Value.Time);
}
private static List<HitObject> hitObjectsInTimingRange(EditorBeatmap beatmap, ControlPointGroup selectedGroup)
{
// If the first group, we grab all hitobjects prior to the next, if the last group, we grab all remaining hitobjects
double startTime = beatmap.ControlPointInfo.TimingPoints.Any(x => x.Time < selectedGroup.Time) ? selectedGroup.Time : double.MinValue;
double endTime = beatmap.ControlPointInfo.TimingPoints.FirstOrDefault(x => x.Time > selectedGroup.Time)?.Time ?? double.MaxValue;
return beatmap.HitObjects.Where(x => Precision.AlmostBigger(x.StartTime, startTime) && Precision.DefinitelyBigger(endTime, x.StartTime)).ToList();
}
2022-06-02 12:10:50 +08:00
private void adjustOffset(double adjust)
{
if (selectedGroup.Value == null)
return;
bool wasAtStart = editorClock.CurrentTimeAccurate == selectedGroup.Value.Time;
List<HitObject> hitObjectsInRange = hitObjectsInTimingRange(beatmap, selectedGroup.Value);
2022-06-02 12:10:50 +08:00
// VERY TEMPORARY
var currentGroupItems = selectedGroup.Value.ControlPoints.ToArray();
beatmap.ControlPointInfo.RemoveGroup(selectedGroup.Value);
double newOffset = selectedGroup.Value.Time + adjust;
foreach (var cp in currentGroupItems)
beatmap.ControlPointInfo.Add(newOffset, cp);
// the control point might not necessarily exist yet, if currentGroupItems was empty.
selectedGroup.Value = beatmap.ControlPointInfo.GroupAt(newOffset, true);
if (adjustPlacedNotes.Current.Value)
{
foreach (HitObject hitObject in hitObjectsInRange)
{
hitObject.StartTime += adjust;
}
}
if (!editorClock.IsRunning && wasAtStart)
2022-06-02 12:10:50 +08:00
editorClock.Seek(newOffset);
}
private void adjustBpm(double adjust)
{
var timing = selectedGroup.Value?.ControlPoints.OfType<TimingControlPoint>().FirstOrDefault();
2022-06-02 12:10:50 +08:00
if (timing == null)
return;
double newBeatLength = 60000 / (timing.BPM + adjust);
List<HitObject> hitObjectsInRange = hitObjectsInTimingRange(beatmap, selectedGroup.Value!);
if (adjustPlacedNotes.Current.Value)
{
foreach (HitObject hitObject in hitObjectsInRange)
{
double beat = (hitObject.StartTime - selectedGroup.Value!.Time) / timing.BeatLength;
2024-06-02 22:59:16 +08:00
hitObject.StartTime = (beat * newBeatLength) + selectedGroup.Value.Time;
}
}
timing.BeatLength = newBeatLength;
2022-06-02 12:10:50 +08:00
}
2022-11-24 13:32:20 +08:00
private partial class InlineButton : OsuButton
{
private readonly IconUsage icon;
private readonly Anchor anchor;
2022-06-02 12:10:50 +08:00
private SpriteIcon spriteIcon = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
2022-06-02 12:10:50 +08:00
public InlineButton(IconUsage icon, Anchor anchor)
{
this.icon = icon;
this.anchor = anchor;
}
protected override void LoadComplete()
{
base.LoadComplete();
Content.CornerRadius = 0;
Content.Masking = false;
BackgroundColour = colourProvider.Background2;
Content.Add(new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(15),
Children = new Drawable[]
{
spriteIcon = new SpriteIcon
{
Icon = icon,
Size = new Vector2(22),
Anchor = anchor,
Origin = anchor,
Colour = colourProvider.Background1,
},
}
});
}
protected override bool OnMouseDown(MouseDownEvent e)
{
// scale looks bad so don't call base.
return false;
}
protected override bool OnHover(HoverEvent e)
{
spriteIcon.FadeColour(colourProvider.Content2, 200, Easing.OutQuint);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
spriteIcon.FadeColour(colourProvider.Background1, 200, Easing.OutQuint);
base.OnHoverLost(e);
}
}
2022-05-20 13:34:33 +08:00
}
}