1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 17:33:03 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/Timeline/Timeline.cs

258 lines
9.6 KiB
C#
Raw Normal View History

// 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.
2018-05-18 12:05:58 +08:00
using System;
2018-05-23 11:00:11 +08:00
using osu.Framework.Allocation;
2020-08-05 20:10:38 +08:00
using osu.Framework.Audio.Track;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
2018-05-18 12:05:58 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-05-18 12:05:58 +08:00
using osu.Game.Beatmaps;
using osu.Game.Configuration;
2018-05-18 12:05:58 +08:00
using osu.Game.Graphics;
using osu.Game.Rulesets.Edit;
using osuTK;
2018-05-18 12:05:58 +08:00
2018-11-06 17:28:22 +08:00
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
2018-05-18 12:05:58 +08:00
{
[Cached(typeof(IPositionSnapProvider))]
[Cached]
public class Timeline : ZoomableScrollContainer, IPositionSnapProvider
2018-05-18 12:05:58 +08:00
{
public readonly Bindable<bool> WaveformVisible = new Bindable<bool>();
public readonly Bindable<bool> ControlPointsVisible = new Bindable<bool>();
2020-10-01 17:14:10 +08:00
public readonly Bindable<bool> TicksVisible = new Bindable<bool>();
public readonly IBindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
2018-05-18 12:05:58 +08:00
2020-02-14 21:14:00 +08:00
[Resolved]
private EditorClock editorClock { get; set; }
2018-05-23 11:00:11 +08:00
/// <summary>
/// The timeline's scroll position in the last frame.
/// </summary>
private float lastScrollPosition;
/// <summary>
/// The track time in the last frame.
/// </summary>
private double lastTrackTime;
/// <summary>
/// Whether the user is currently dragging the timeline.
/// </summary>
private bool handlingDragInput;
/// <summary>
/// Whether the track was playing before a user drag event.
/// </summary>
private bool trackWasPlaying;
private Track track;
2018-05-18 12:05:58 +08:00
public Timeline()
{
2018-05-18 16:53:09 +08:00
ZoomDuration = 200;
ZoomEasing = Easing.OutQuint;
2018-06-12 14:51:48 +08:00
ScrollbarVisible = false;
}
private WaveformGraph waveform;
2020-10-01 17:14:10 +08:00
private TimelineTickDisplay ticks;
2018-05-18 16:53:09 +08:00
private TimelineControlPointDisplay controlPoints;
private Bindable<float> waveformOpacity;
[BackgroundDependencyLoader]
private void load(IBindable<WorkingBeatmap> beatmap, OsuColour colours, OsuConfigManager config)
{
AddRange(new Drawable[]
2018-05-18 12:05:58 +08:00
{
new Container
{
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue,
Children = new Drawable[]
{
waveform = new WaveformGraph
{
RelativeSizeAxes = Axes.Both,
2020-10-19 15:57:08 +08:00
BaseColour = colours.Blue.Opacity(0.2f),
LowColour = colours.BlueLighter,
MidColour = colours.BlueDark,
HighColour = colours.BlueDarker,
},
ticks = new TimelineTickDisplay(),
controlPoints = new TimelineControlPointDisplay(),
}
},
2019-12-06 10:26:50 +08:00
});
2018-05-18 12:05:58 +08:00
2018-05-24 14:23:59 +08:00
// We don't want the centre marker to scroll
AddInternal(new CentreMarker { Depth = float.MaxValue });
2018-05-24 14:23:59 +08:00
waveformOpacity = config.GetBindable<float>(OsuSetting.EditorWaveformOpacity);
waveformOpacity.BindValueChanged(_ => updateWaveformOpacity(), true);
WaveformVisible.ValueChanged += _ => updateWaveformOpacity();
ControlPointsVisible.ValueChanged += visible => controlPoints.FadeTo(visible.NewValue ? 1 : 0, 200, Easing.OutQuint);
2020-10-01 17:14:10 +08:00
TicksVisible.ValueChanged += visible => ticks.FadeTo(visible.NewValue ? 1 : 0, 200, Easing.OutQuint);
Beatmap.BindTo(beatmap);
Beatmap.BindValueChanged(b =>
2018-06-28 13:08:15 +08:00
{
waveform.Waveform = b.NewValue.Waveform;
track = b.NewValue.Track;
// todo: i don't think this is safe, the track may not be loaded yet.
2020-08-22 18:47:05 +08:00
if (track.Length > 0)
{
2020-08-05 20:10:38 +08:00
MaxZoom = getZoomLevelForVisibleMilliseconds(500);
MinZoom = getZoomLevelForVisibleMilliseconds(10000);
Zoom = getZoomLevelForVisibleMilliseconds(2000);
}
2018-06-28 13:08:15 +08:00
}, true);
2018-05-23 11:00:11 +08:00
}
private void updateWaveformOpacity() =>
waveform.FadeTo(WaveformVisible.Value ? waveformOpacity.Value : 0, 200, Easing.OutQuint);
private float getZoomLevelForVisibleMilliseconds(double milliseconds) => Math.Max(1, (float)(track.Length / milliseconds));
2018-05-18 12:05:58 +08:00
protected override void Update()
{
base.Update();
// The extrema of track time should be positioned at the centre of the container when scrolled to the start or end
2018-05-18 12:05:58 +08:00
Content.Margin = new MarginPadding { Horizontal = DrawWidth / 2 };
2018-05-23 11:00:11 +08:00
// This needs to happen after transforms are updated, but before the scroll position is updated in base.UpdateAfterChildren
if (editorClock.IsRunning)
scrollToTrackTime();
}
protected override bool OnScroll(ScrollEvent e)
{
// if this is not a precision scroll event, let the editor handle the seek itself (for snapping support)
if (!e.AltPressed && !e.IsPrecise)
return false;
return base.OnScroll(e);
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
if (handlingDragInput)
seekTrackToCurrent();
else if (!editorClock.IsRunning)
{
// The track isn't running. There are three cases we have to be wary of:
// 1) The user flick-drags on this timeline and we are applying an interpolated seek on the clock, until interrupted by 2 or 3.
// 2) The user changes the track time through some other means (scrolling in the editor or overview timeline; clicking a hitobject etc.). We want the timeline to track the clock's time.
// 3) An ongoing seek transform is running from an external seek. We want the timeline to track the clock's time.
// The simplest way to cover the first two cases is by checking whether the scroll position has changed and the audio hasn't been changed externally
// Checking IsSeeking covers the third case, where the transform may not have been applied yet.
if (Current != lastScrollPosition && editorClock.CurrentTime == lastTrackTime && !editorClock.IsSeeking)
seekTrackToCurrent();
else
scrollToTrackTime();
}
lastScrollPosition = Current;
lastTrackTime = editorClock.CurrentTime;
}
private void seekTrackToCurrent()
{
if (!track.IsLoaded)
return;
2020-08-05 20:10:38 +08:00
editorClock.Seek(Current / Content.DrawWidth * track.Length);
}
private void scrollToTrackTime()
{
if (!track.IsLoaded || track.Length == 0)
return;
// covers the case where the user starts playback after a drag is in progress.
// we want to ensure the clock is always stopped during drags to avoid weird audio playback.
if (handlingDragInput)
editorClock.Stop();
2020-08-05 20:10:38 +08:00
ScrollTo((float)(editorClock.CurrentTime / track.Length) * Content.DrawWidth, false);
2018-05-23 11:00:11 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-05-23 11:00:11 +08:00
{
2018-10-02 11:02:47 +08:00
if (base.OnMouseDown(e))
2018-05-23 11:00:11 +08:00
{
2018-05-24 13:36:48 +08:00
beginUserDrag();
2018-05-23 11:00:11 +08:00
return true;
}
return false;
}
protected override void OnMouseUp(MouseUpEvent e)
2018-05-23 11:00:11 +08:00
{
2018-05-24 13:36:48 +08:00
endUserDrag();
base.OnMouseUp(e);
2018-05-23 11:00:11 +08:00
}
2018-05-24 13:36:48 +08:00
private void beginUserDrag()
2018-05-23 11:00:11 +08:00
{
handlingDragInput = true;
trackWasPlaying = editorClock.IsRunning;
editorClock.Stop();
2018-05-23 11:00:11 +08:00
}
2018-05-24 13:36:48 +08:00
private void endUserDrag()
2018-05-23 11:00:11 +08:00
{
handlingDragInput = false;
2018-05-23 11:00:11 +08:00
if (trackWasPlaying)
editorClock.Start();
2018-05-23 11:00:11 +08:00
}
[Resolved]
private EditorBeatmap beatmap { get; set; }
[Resolved]
private IBeatSnapProvider beatSnapProvider { get; set; }
2020-11-13 16:10:29 +08:00
/// <summary>
/// The total amount of time visible on the timeline.
/// </summary>
public double VisibleRange => track.Length / Zoom;
public SnapResult SnapScreenSpacePositionToValidPosition(Vector2 screenSpacePosition) =>
new SnapResult(screenSpacePosition, null);
2020-05-22 18:23:07 +08:00
public SnapResult SnapScreenSpacePositionToValidTime(Vector2 screenSpacePosition) =>
new SnapResult(screenSpacePosition, beatSnapProvider.SnapTime(getTimeFromPosition(Content.ToLocalSpace(screenSpacePosition))));
private double getTimeFromPosition(Vector2 localPosition) =>
2020-08-05 20:10:38 +08:00
(localPosition.X / Content.DrawWidth) * track.Length;
public float GetBeatSnapDistanceAt(double referenceTime) => throw new NotImplementedException();
public float DurationToDistance(double referenceTime, double duration) => throw new NotImplementedException();
public double DistanceToDuration(double referenceTime, float distance) => throw new NotImplementedException();
public double GetSnappedDurationFromDistance(double referenceTime, float distance) => throw new NotImplementedException();
public float GetSnappedDistanceFromDistance(double referenceTime, float distance) => throw new NotImplementedException();
2018-05-18 12:05:58 +08:00
}
}