1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 18:52:56 +08:00
osu-lazer/osu.Game/Overlays/MusicController.cs

469 lines
18 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-10-24 13:00:06 +08:00
using System;
using System.Diagnostics;
using System.Linq;
2016-11-06 00:46:09 +08:00
using System.Threading.Tasks;
2016-10-24 13:00:06 +08:00
using OpenTK;
2016-10-24 10:50:49 +08:00
using OpenTK.Graphics;
2016-11-11 06:40:42 +08:00
using osu.Framework.Allocation;
2017-06-22 23:32:50 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
2017-04-26 20:04:32 +08:00
using osu.Framework.Extensions.Color4Extensions;
2016-10-24 10:50:49 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2016-10-24 10:50:49 +08:00
using osu.Framework.Graphics.Sprites;
2016-10-24 13:13:49 +08:00
using osu.Framework.Graphics.Textures;
2017-06-22 23:32:50 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
2017-04-26 20:04:32 +08:00
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
2016-10-24 13:47:05 +08:00
using osu.Game.Database;
2016-10-24 13:00:06 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2017-05-01 12:41:22 +08:00
using osu.Framework.Threading;
2017-05-01 12:01:08 +08:00
using osu.Game.Overlays.Music;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Graphics.Shapes;
namespace osu.Game.Overlays
{
public class MusicController : FocusedOverlayContainer
{
2017-04-09 15:26:21 +08:00
private const float player_height = 130;
2017-05-01 12:01:08 +08:00
2017-05-01 12:25:32 +08:00
private const float transition_length = 800;
private const float progress_height = 10;
private const float bottom_black_area_height = 55;
private Drawable currentBackground;
2017-06-22 23:32:50 +08:00
private ProgressBar progressBar;
2017-05-01 12:41:22 +08:00
2017-05-30 17:23:53 +08:00
private IconButton playButton;
private IconButton playlistButton;
2017-05-01 12:41:22 +08:00
2016-10-24 13:00:06 +08:00
private SpriteText title, artist;
2017-05-01 12:41:22 +08:00
2017-05-01 14:03:11 +08:00
private PlaylistOverlay playlist;
2016-10-27 18:15:43 +08:00
private LocalisationEngine localisation;
2017-05-01 12:41:22 +08:00
private readonly Bindable<WorkingBeatmap> beatmapBacking = new Bindable<WorkingBeatmap>();
private Container dragContainer;
2017-04-09 15:26:21 +08:00
private Container playerContainer;
public MusicController()
{
2016-10-28 21:21:47 +08:00
Width = 400;
Margin = new MarginPadding(10);
}
protected override bool OnDragStart(InputState state) => true;
protected override bool OnDrag(InputState state)
{
Trace.Assert(state.Mouse.PositionMouseDown != null, "state.Mouse.PositionMouseDown != null");
2017-03-07 09:59:19 +08:00
Vector2 change = state.Mouse.Position - state.Mouse.PositionMouseDown.Value;
// Diminish the drag distance as we go further to simulate "rubber band" feeling.
change *= change.Length <= 0 ? 0 : (float)Math.Pow(change.Length, 0.7f) / change.Length;
dragContainer.MoveTo(change);
return base.OnDrag(state);
}
protected override bool OnDragEnd(InputState state)
{
dragContainer.MoveTo(Vector2.Zero, 800, EasingTypes.OutElastic);
return base.OnDragEnd(state);
}
[BackgroundDependencyLoader]
2017-05-01 14:03:11 +08:00
private void load(OsuGameBase game, OsuColour colours, LocalisationEngine localisation)
{
2017-05-01 12:41:22 +08:00
this.localisation = localisation;
2017-04-09 15:26:21 +08:00
2016-10-24 10:50:49 +08:00
Children = new Drawable[]
{
dragContainer = new Container
2016-10-24 13:00:06 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2017-05-01 12:01:08 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2016-10-24 17:19:48 +08:00
Children = new Drawable[]
{
2017-05-01 14:03:11 +08:00
playlist = new PlaylistOverlay
2016-10-24 17:19:48 +08:00
{
2017-05-01 12:01:08 +08:00
RelativeSizeAxes = Axes.X,
Y = player_height + 10,
},
2017-04-09 15:26:21 +08:00
playerContainer = new Container
{
2017-03-24 16:59:26 +08:00
RelativeSizeAxes = Axes.X,
2017-04-09 15:26:21 +08:00
Height = player_height,
Masking = true,
CornerRadius = 5,
2017-06-12 11:48:47 +08:00
EdgeEffect = new EdgeEffectParameters
2017-04-09 15:26:21 +08:00
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
},
2017-05-01 12:41:22 +08:00
Children = new[]
{
2017-05-01 12:41:22 +08:00
currentBackground = new Background(),
2017-04-09 15:26:21 +08:00
title = new OsuSpriteText
{
Origin = Anchor.BottomCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 40),
TextSize = 25,
Colour = Color4.White,
Text = @"Nothing to play",
Font = @"Exo2.0-MediumItalic"
},
artist = new OsuSpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 45),
TextSize = 15,
Colour = Color4.White,
Text = @"Nothing to play",
Font = @"Exo2.0-BoldItalic"
},
new Container
{
2017-04-09 15:26:21 +08:00
Padding = new MarginPadding { Bottom = progress_height },
Height = bottom_black_area_height,
RelativeSizeAxes = Axes.X,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
Children = new Drawable[]
2017-03-24 16:59:26 +08:00
{
2017-05-30 17:23:53 +08:00
new FillFlowContainer<IconButton>
2017-03-24 16:59:26 +08:00
{
2017-04-09 15:26:21 +08:00
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new[]
2017-04-07 14:28:18 +08:00
{
2017-05-30 17:23:53 +08:00
new IconButton
2017-04-09 15:26:21 +08:00
{
Action = prev,
Icon = FontAwesome.fa_step_backward,
},
2017-05-30 17:23:53 +08:00
playButton = new IconButton
2017-04-09 15:26:21 +08:00
{
Scale = new Vector2(1.4f),
IconScale = new Vector2(1.4f),
2017-05-01 18:32:31 +08:00
Action = play,
2017-04-09 15:26:21 +08:00
Icon = FontAwesome.fa_play_circle_o,
},
2017-05-30 17:23:53 +08:00
new IconButton
2017-04-09 15:26:21 +08:00
{
Action = next,
Icon = FontAwesome.fa_step_forward,
},
}
2017-04-07 14:28:18 +08:00
},
2017-05-30 17:23:53 +08:00
playlistButton = new IconButton
2017-04-07 14:28:18 +08:00
{
2017-04-09 15:26:21 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.CentreRight,
Position = new Vector2(-bottom_black_area_height / 2, 0),
Icon = FontAwesome.fa_bars,
Action = () => playlist.ToggleVisibility(),
2017-04-07 14:28:18 +08:00
},
2017-03-24 16:59:26 +08:00
}
},
2017-06-22 23:32:50 +08:00
progressBar = new ProgressBar
2017-03-24 16:59:26 +08:00
{
2017-04-09 15:26:21 +08:00
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
Height = progress_height,
2017-06-22 23:32:50 +08:00
FillColour = colours.Yellow,
OnSeek = progress => current?.Track.Seek(progress)
2017-04-09 15:26:21 +08:00
}
},
},
2016-10-24 17:19:48 +08:00
}
2016-10-24 10:50:49 +08:00
}
};
2017-03-14 04:59:44 +08:00
2017-05-01 12:25:32 +08:00
beatmapBacking.BindTo(game.Beatmap);
2016-11-06 00:24:03 +08:00
2017-05-01 12:41:22 +08:00
playlist.StateChanged += (c, s) => playlistButton.FadeColour(s == Visibility.Visible ? colours.Yellow : Color4.White, 200, EasingTypes.OutQuint);
2016-11-06 00:24:03 +08:00
}
2016-11-06 00:46:09 +08:00
protected override void LoadComplete()
{
2017-05-01 12:25:32 +08:00
beatmapBacking.ValueChanged += beatmapChanged;
beatmapBacking.TriggerChange();
2016-11-06 00:46:09 +08:00
base.LoadComplete();
}
2017-05-01 12:01:08 +08:00
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
Height = dragContainer.Height;
}
2016-10-24 17:23:30 +08:00
protected override void Update()
{
base.Update();
2016-10-28 20:44:59 +08:00
2016-11-22 19:47:28 +08:00
if (current?.TrackLoaded ?? false)
{
2017-05-01 12:25:32 +08:00
var track = current.Track;
2016-10-28 20:44:59 +08:00
2017-06-23 00:42:29 +08:00
progressBar.CurrentTime = track.CurrentTime;
2017-05-01 12:25:32 +08:00
playButton.Icon = track.IsRunning ? FontAwesome.fa_pause_circle_o : FontAwesome.fa_play_circle_o;
if (track.HasCompleted && !track.Looping) next();
2016-11-22 19:47:28 +08:00
}
2017-03-14 04:59:44 +08:00
else
playButton.Icon = FontAwesome.fa_play_circle_o;
2016-10-24 17:23:30 +08:00
}
2017-05-01 12:41:22 +08:00
private void play()
2016-11-11 10:35:58 +08:00
{
2017-05-01 12:41:22 +08:00
var track = current?.Track;
2017-05-01 12:25:32 +08:00
2017-05-01 12:41:22 +08:00
if (track == null)
{
2017-05-01 14:03:11 +08:00
playlist.PlayNext();
return;
2017-05-01 12:41:22 +08:00
}
if (track.IsRunning)
track.Stop();
else
track.Start();
2016-10-28 20:44:59 +08:00
}
2017-04-09 17:11:53 +08:00
private void prev()
2016-10-28 20:08:27 +08:00
{
2017-05-01 14:03:11 +08:00
queuedDirection = TransformDirection.Prev;
playlist.PlayPrevious();
2016-10-24 15:34:44 +08:00
}
private void next()
{
2017-05-01 14:03:11 +08:00
queuedDirection = TransformDirection.Next;
playlist.PlayNext();
2016-10-24 15:34:44 +08:00
}
2017-05-01 12:41:22 +08:00
private WorkingBeatmap current;
private TransformDirection? queuedDirection;
2017-05-01 12:41:22 +08:00
private void beatmapChanged(WorkingBeatmap beatmap)
{
2017-05-25 23:49:47 +08:00
TransformDirection direction = TransformDirection.None;
if (current != null)
{
bool audioEquals = beatmapBacking.Value?.BeatmapInfo?.AudioEquals(current.BeatmapInfo) ?? false;
if (audioEquals)
direction = TransformDirection.None;
else if (queuedDirection.HasValue)
{
direction = queuedDirection.Value;
queuedDirection = null;
}
else
{
//figure out the best direction based on order in playlist.
var last = playlist.BeatmapSets.TakeWhile(b => b.ID != current.BeatmapSetInfo.ID).Count();
var next = beatmapBacking.Value == null ? -1 : playlist.BeatmapSets.TakeWhile(b => b.ID != beatmapBacking.Value.BeatmapSetInfo.ID).Count();
direction = last > next ? TransformDirection.Prev : TransformDirection.Next;
}
}
2017-05-01 12:41:22 +08:00
current = beatmapBacking.Value;
2017-06-22 23:32:50 +08:00
updateProgressBar(current?.Track);
updateDisplay(beatmapBacking, direction);
queuedDirection = null;
2017-05-01 12:41:22 +08:00
}
2017-06-22 23:32:50 +08:00
private void updateProgressBar(Track t)
{
if (t?.IsLoaded ?? false)
progressBar.EndTime = t.Length;
else if (t != null)
t.OnLoaded += loadedTrack => progressBar.EndTime = loadedTrack.Length;
}
2017-05-01 12:41:22 +08:00
private ScheduledDelegate pendingBeatmapSwitch;
2016-11-22 19:47:28 +08:00
private void updateDisplay(WorkingBeatmap beatmap, TransformDirection direction)
{
//we might be off-screen when this update comes in.
//rather than Scheduling, manually handle this to avoid possible memory contention.
2017-05-01 12:41:22 +08:00
pendingBeatmapSwitch?.Cancel();
pendingBeatmapSwitch = Schedule(delegate
2016-11-22 19:47:28 +08:00
{
Task.Run(() =>
{
if (beatmap?.Beatmap == null) //this is not needed if a placeholder exists
2017-03-14 04:59:44 +08:00
{
title.Current = null;
2017-03-14 04:59:44 +08:00
title.Text = @"Nothing to play";
2017-04-26 20:04:32 +08:00
artist.Current = null;
2017-03-14 04:59:44 +08:00
artist.Text = @"Nothing to play";
}
else
{
2017-05-06 14:03:08 +08:00
BeatmapMetadata metadata = beatmap.Metadata;
title.Current = localisation.GetUnicodePreference(metadata.TitleUnicode, metadata.Title);
artist.Current = localisation.GetUnicodePreference(metadata.ArtistUnicode, metadata.Artist);
2017-03-14 04:59:44 +08:00
}
});
2016-10-27 17:04:41 +08:00
2017-05-01 12:41:22 +08:00
playerContainer.Add(new AsyncLoadWrapper(new Background(beatmap)
{
2017-04-02 14:56:12 +08:00
OnLoadComplete = d =>
{
switch (direction)
{
case TransformDirection.Next:
d.Position = new Vector2(400, 0);
d.MoveToX(0, 500, EasingTypes.OutCubic);
currentBackground.MoveToX(-400, 500, EasingTypes.OutCubic);
break;
case TransformDirection.Prev:
d.Position = new Vector2(-400, 0);
d.MoveToX(0, 500, EasingTypes.OutCubic);
currentBackground.MoveToX(400, 500, EasingTypes.OutCubic);
break;
}
currentBackground.Expire();
currentBackground = d;
}
2017-04-02 14:56:12 +08:00
})
{
Depth = float.MaxValue,
});
2017-05-01 12:41:22 +08:00
});
}
protected override void PopIn()
{
base.PopIn();
FadeIn(transition_length, EasingTypes.OutQuint);
dragContainer.ScaleTo(1, transition_length, EasingTypes.OutElastic);
}
protected override void PopOut()
{
base.PopOut();
FadeOut(transition_length, EasingTypes.OutQuint);
dragContainer.ScaleTo(0.9f, transition_length, EasingTypes.OutQuint);
}
2017-05-01 12:41:22 +08:00
private enum TransformDirection
{
None,
Next,
Prev
}
2017-05-01 12:41:22 +08:00
private class Background : BufferedContainer
{
private readonly Sprite sprite;
private readonly WorkingBeatmap beatmap;
2017-05-01 12:41:22 +08:00
public Background(WorkingBeatmap beatmap = null)
{
2016-11-22 19:47:28 +08:00
this.beatmap = beatmap;
CacheDrawnFrameBuffer = true;
2016-11-30 03:50:12 +08:00
Depth = float.MaxValue;
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
sprite = new Sprite
{
Colour = OsuColour.Gray(150),
2016-11-23 12:18:22 +08:00
FillMode = FillMode.Fill,
},
new Box
{
RelativeSizeAxes = Axes.X,
Height = bottom_black_area_height,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
2017-01-11 02:44:40 +08:00
Colour = Color4.Black.Opacity(0.5f)
}
};
}
2016-11-22 19:47:28 +08:00
[BackgroundDependencyLoader]
private void load(TextureStore textures)
{
sprite.Texture = beatmap?.Background ?? textures.Get(@"Backgrounds/bg4");
}
}
2017-06-22 23:32:50 +08:00
private class ProgressBar : SliderBar<double>
{
public Action<double> OnSeek;
private Box fill;
public Color4 FillColour
{
set { fill.Colour = value; }
}
public double EndTime
{
set { CurrentNumber.MaxValue = value; }
}
2017-06-23 00:42:29 +08:00
public double CurrentTime
2017-06-22 23:32:50 +08:00
{
set { CurrentNumber.Value = value; }
}
public ProgressBar()
{
CurrentNumber.MinValue = 0;
2017-06-23 00:42:29 +08:00
CurrentNumber.MinValue = 1;
2017-06-22 23:32:50 +08:00
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
fill = new Box
{
RelativeSizeAxes = Axes.Y
}
};
}
protected override void UpdateValue(float value)
{
fill.Width = value * UsableWidth;
}
protected override void OnUserChange() => OnSeek?.Invoke(Current);
}
}
}