2019-01-24 16:43:03 +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.
|
2018-05-18 12:05:58 +08:00
|
|
|
|
2018-05-23 11:00:11 +08:00
|
|
|
using osu.Framework.Allocation;
|
2018-06-28 13:08:15 +08:00
|
|
|
using osu.Framework.Audio.Track;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2018-06-18 03:31:47 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2018-05-18 12:05:58 +08:00
|
|
|
using osu.Framework.Graphics;
|
2018-06-11 19:08:17 +08:00
|
|
|
using osu.Framework.Graphics.Audio;
|
2018-10-02 11:02:47 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2018-05-23 11:00:11 +08:00
|
|
|
using osu.Framework.Timing;
|
2018-05-18 12:05:58 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
2018-11-06 17:28:22 +08:00
|
|
|
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
|
2018-05-18 12:05:58 +08:00
|
|
|
{
|
|
|
|
public class Timeline : ZoomableScrollContainer
|
|
|
|
{
|
|
|
|
public readonly Bindable<bool> WaveformVisible = new Bindable<bool>();
|
2018-06-11 19:08:17 +08:00
|
|
|
public readonly IBindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
|
2018-05-18 12:05:58 +08:00
|
|
|
|
2018-05-23 11:00:11 +08:00
|
|
|
private IAdjustableClock adjustableClock;
|
|
|
|
|
2018-05-18 12:05:58 +08:00
|
|
|
public Timeline()
|
|
|
|
{
|
2018-05-18 16:53:09 +08:00
|
|
|
ZoomDuration = 200;
|
|
|
|
ZoomEasing = Easing.OutQuint;
|
|
|
|
Zoom = 10;
|
2018-06-12 14:51:48 +08:00
|
|
|
ScrollbarVisible = false;
|
2018-06-11 19:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private WaveformGraph waveform;
|
2018-05-18 16:53:09 +08:00
|
|
|
|
2018-06-11 19:08:17 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2019-02-01 14:42:15 +08:00
|
|
|
private void load(IBindable<WorkingBeatmap> beatmap, IAdjustableClock adjustableClock, OsuColour colours)
|
2018-06-11 19:08:17 +08:00
|
|
|
{
|
2018-06-12 13:19:21 +08:00
|
|
|
this.adjustableClock = adjustableClock;
|
2018-05-18 16:53:09 +08:00
|
|
|
|
2018-06-11 19:08:17 +08:00
|
|
|
Child = waveform = new WaveformGraph
|
2018-05-18 12:05:58 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2018-06-18 03:31:47 +08:00
|
|
|
Colour = colours.Blue.Opacity(0.2f),
|
|
|
|
LowColour = colours.BlueLighter,
|
|
|
|
MidColour = colours.BlueDark,
|
|
|
|
HighColour = colours.BlueDarker,
|
2018-05-18 12:05:58 +08:00
|
|
|
Depth = float.MaxValue
|
|
|
|
};
|
|
|
|
|
2018-05-24 14:23:59 +08:00
|
|
|
// We don't want the centre marker to scroll
|
|
|
|
AddInternal(new CentreMarker());
|
|
|
|
|
2019-02-22 16:51:39 +08:00
|
|
|
WaveformVisible.ValueChanged += visible => waveform.FadeTo(visible.NewValue ? 1 : 0, 200, Easing.OutQuint);
|
2018-06-11 19:08:17 +08:00
|
|
|
|
|
|
|
Beatmap.BindTo(beatmap);
|
2019-02-21 17:56:34 +08:00
|
|
|
Beatmap.BindValueChanged(e =>
|
2018-06-28 13:08:15 +08:00
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
waveform.Waveform = e.NewValue.Waveform;
|
|
|
|
track = e.NewValue.Track;
|
2018-06-28 13:08:15 +08:00
|
|
|
}, true);
|
2018-05-23 11:00:11 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 13:14:32 +08:00
|
|
|
/// <summary>
|
2018-06-18 17:56:54 +08:00
|
|
|
/// The timeline's scroll position in the last frame.
|
2018-05-23 13:14:32 +08:00
|
|
|
/// </summary>
|
2018-06-18 17:56:54 +08:00
|
|
|
private float lastScrollPosition;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The track time in the last frame.
|
|
|
|
/// </summary>
|
|
|
|
private double lastTrackTime;
|
2018-05-23 13:14:32 +08:00
|
|
|
|
|
|
|
/// <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>
|
2018-05-23 11:00:11 +08:00
|
|
|
private bool trackWasPlaying;
|
|
|
|
|
2018-06-28 13:08:15 +08:00
|
|
|
private Track track;
|
|
|
|
|
2018-05-18 12:05:58 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2018-05-23 13:14:32 +08:00
|
|
|
// 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
|
|
|
|
2018-06-25 19:31:06 +08:00
|
|
|
// This needs to happen after transforms are updated, but before the scroll position is updated in base.UpdateAfterChildren
|
|
|
|
if (adjustableClock.IsRunning)
|
|
|
|
scrollToTrackTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
{
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
2018-05-23 13:14:32 +08:00
|
|
|
if (handlingDragInput)
|
2018-05-23 13:32:00 +08:00
|
|
|
seekTrackToCurrent();
|
2018-06-25 19:31:06 +08:00
|
|
|
else if (!adjustableClock.IsRunning)
|
2018-05-23 13:14:32 +08:00
|
|
|
{
|
2018-06-25 19:31:06 +08:00
|
|
|
// The track isn't running. There are two cases we have to be wary of:
|
|
|
|
// 1) The user flick-drags on this timeline: We want the track to follow us
|
2018-05-23 13:14:32 +08:00
|
|
|
// 2) The user changes the track time through some other means (scrolling in the editor or overview timeline): We want to follow the track time
|
|
|
|
|
2018-06-18 17:56:54 +08:00
|
|
|
// The simplest way to cover both cases is by checking whether the scroll position has changed and the audio hasn't been changed externally
|
|
|
|
if (Current != lastScrollPosition && adjustableClock.CurrentTime == lastTrackTime)
|
2018-05-23 13:32:00 +08:00
|
|
|
seekTrackToCurrent();
|
2018-05-23 13:14:32 +08:00
|
|
|
else
|
2018-05-23 13:32:00 +08:00
|
|
|
scrollToTrackTime();
|
2018-05-23 13:14:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-18 17:02:26 +08:00
|
|
|
lastScrollPosition = Current;
|
2018-06-18 17:56:54 +08:00
|
|
|
lastTrackTime = adjustableClock.CurrentTime;
|
2018-06-25 19:31:06 +08:00
|
|
|
}
|
2018-05-23 13:32:00 +08:00
|
|
|
|
2018-06-25 19:31:06 +08:00
|
|
|
private void seekTrackToCurrent()
|
|
|
|
{
|
2018-06-28 13:08:15 +08:00
|
|
|
if (!track.IsLoaded)
|
2018-06-25 19:31:06 +08:00
|
|
|
return;
|
2018-06-18 18:27:08 +08:00
|
|
|
|
2018-06-28 13:08:15 +08:00
|
|
|
adjustableClock.Seek(Current / Content.DrawWidth * track.Length);
|
2018-06-25 19:31:06 +08:00
|
|
|
}
|
2018-05-24 13:26:53 +08:00
|
|
|
|
2018-06-25 19:31:06 +08:00
|
|
|
private void scrollToTrackTime()
|
|
|
|
{
|
2018-06-28 13:08:15 +08:00
|
|
|
if (!track.IsLoaded)
|
2018-06-25 19:31:06 +08:00
|
|
|
return;
|
2018-06-18 18:27:08 +08:00
|
|
|
|
2018-06-28 13:08:15 +08:00
|
|
|
ScrollTo((float)(adjustableClock.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;
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
protected override bool OnMouseUp(MouseUpEvent e)
|
2018-05-23 11:00:11 +08:00
|
|
|
{
|
2018-05-24 13:36:48 +08:00
|
|
|
endUserDrag();
|
2018-10-02 11:02:47 +08:00
|
|
|
return 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
|
|
|
{
|
2018-05-23 13:14:32 +08:00
|
|
|
handlingDragInput = true;
|
2018-05-23 11:00:11 +08:00
|
|
|
trackWasPlaying = adjustableClock.IsRunning;
|
|
|
|
adjustableClock.Stop();
|
|
|
|
}
|
|
|
|
|
2018-05-24 13:36:48 +08:00
|
|
|
private void endUserDrag()
|
2018-05-23 11:00:11 +08:00
|
|
|
{
|
2018-05-23 13:14:32 +08:00
|
|
|
handlingDragInput = false;
|
2018-05-23 11:00:11 +08:00
|
|
|
if (trackWasPlaying)
|
|
|
|
adjustableClock.Start();
|
|
|
|
}
|
2018-05-18 12:05:58 +08:00
|
|
|
}
|
|
|
|
}
|