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

395 lines
14 KiB
C#
Raw Normal View History

//Copyright (c) 2007-2016 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;
2016-10-24 13:47:05 +08:00
using System.Collections.Generic;
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;
using osu.Framework;
2016-11-11 06:40:42 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
2016-10-24 15:34:44 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
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;
2016-10-24 15:34:44 +08:00
using osu.Framework.Graphics.Transformations;
using osu.Framework.Input;
2016-11-07 22:12:58 +08:00
using osu.Framework.MathUtils;
using osu.Game.Beatmaps;
2016-11-11 10:35:58 +08:00
using osu.Game.Configuration;
2016-10-24 13:47:05 +08:00
using osu.Game.Database;
2016-10-24 13:00:06 +08:00
using osu.Game.Graphics;
namespace osu.Game.Overlays
{
public class MusicController : OverlayContainer
{
private static readonly Vector2 start_position = new Vector2(10, 60);
private MusicControllerBackground backgroundSprite;
2016-10-27 18:15:43 +08:00
private DragBar progress;
2016-10-24 17:19:48 +08:00
private TextAwesome playButton, listButton;
2016-10-24 13:00:06 +08:00
private SpriteText title, artist;
private Texture fallbackTexture;
2016-10-27 18:15:43 +08:00
2016-10-24 13:47:05 +08:00
private List<BeatmapSetInfo> playList;
2016-11-07 21:25:37 +08:00
private List<BeatmapInfo> playHistory = new List<BeatmapInfo>();
2016-11-07 20:57:33 +08:00
private int playListIndex;
2016-11-07 21:25:37 +08:00
private int playHistoryIndex = -1;
2016-11-07 20:57:33 +08:00
private TrackManager trackManager;
private Bindable<WorkingBeatmap> beatmapSource;
2016-11-11 10:35:58 +08:00
private Bindable<bool> preferUnicode;
private OsuConfigManager config;
2016-10-28 20:21:19 +08:00
private WorkingBeatmap current;
private BeatmapDatabase beatmaps;
public MusicController()
{
2016-10-28 21:21:47 +08:00
Width = 400;
Height = 130;
CornerRadius = 5;
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Colour = new Color4(0, 0, 0, 40),
Radius = 5,
};
2016-10-28 21:21:47 +08:00
Masking = true;
Anchor = Anchor.TopRight;//placeholder
Origin = Anchor.TopRight;
Position = start_position;
2016-10-24 10:50:49 +08:00
Children = new Drawable[]
{
2016-10-24 13:00:06 +08:00
title = new SpriteText
{
Origin = Anchor.BottomCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 40),
TextSize = 25,
2016-10-24 13:00:06 +08:00
Colour = Color4.White,
2016-11-06 01:50:38 +08:00
Text = @"Nothing to play",
Font = @"Exo2.0-MediumItalic"
2016-10-24 13:00:06 +08:00
},
artist = new SpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 45),
TextSize = 15,
2016-10-24 13:00:06 +08:00
Colour = Color4.White,
2016-11-06 01:50:38 +08:00
Text = @"Nothing to play",
Font = @"Exo2.0-BoldItalic"
2016-10-24 13:00:06 +08:00
},
2016-10-24 17:19:48 +08:00
new ClickableContainer
2016-10-24 13:00:06 +08:00
{
2016-10-24 17:19:48 +08:00
AutoSizeAxes = Axes.Both,
2016-10-24 13:00:06 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomCentre,
2016-10-24 15:34:44 +08:00
Position = new Vector2(0, 30),
Action = () =>
{
2016-10-28 20:21:19 +08:00
if (current?.Track == null) return;
if (current.Track.IsRunning)
current.Track.Stop();
2016-10-24 15:34:44 +08:00
else
2016-10-28 20:21:19 +08:00
current.Track.Start();
2016-10-24 17:19:48 +08:00
},
Children = new Drawable[]
{
playButton = new TextAwesome
{
TextSize = 30,
2016-11-07 19:00:20 +08:00
Icon = FontAwesome.fa_play_circle_o,
2016-10-24 17:19:48 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
2016-10-24 15:34:44 +08:00
}
2016-10-24 13:00:06 +08:00
},
2016-10-24 17:19:48 +08:00
new ClickableContainer
2016-10-24 13:00:06 +08:00
{
2016-10-24 17:19:48 +08:00
AutoSizeAxes = Axes.Both,
2016-10-24 13:00:06 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomCentre,
2016-10-24 15:34:44 +08:00
Position = new Vector2(-30, 30),
2016-10-24 17:19:48 +08:00
Action = prev,
Children = new Drawable[]
{
new TextAwesome
{
TextSize = 15,
2016-11-07 19:00:20 +08:00
Icon = FontAwesome.fa_step_backward,
2016-10-24 17:19:48 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
}
2016-10-24 13:00:06 +08:00
},
2016-10-24 17:19:48 +08:00
new ClickableContainer
2016-10-24 13:00:06 +08:00
{
2016-10-24 17:19:48 +08:00
AutoSizeAxes = Axes.Both,
2016-10-24 13:00:06 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomCentre,
2016-10-24 15:34:44 +08:00
Position = new Vector2(30, 30),
2016-10-24 17:19:48 +08:00
Action = next,
Children = new Drawable[]
{
new TextAwesome
{
TextSize = 15,
2016-11-07 19:00:20 +08:00
Icon = FontAwesome.fa_step_forward,
2016-10-24 17:19:48 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
}
2016-10-24 13:00:06 +08:00
},
2016-10-24 17:19:48 +08:00
new ClickableContainer
2016-10-24 13:00:06 +08:00
{
2016-10-24 17:19:48 +08:00
AutoSizeAxes = Axes.Both,
2016-10-24 13:00:06 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomRight,
2016-10-24 17:19:48 +08:00
Position = new Vector2(20, 30),
Children = new Drawable[]
{
listButton = new TextAwesome
{
TextSize = 15,
2016-11-07 19:00:20 +08:00
Icon = FontAwesome.fa_bars,
2016-10-24 17:19:48 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
}
2016-10-24 13:00:06 +08:00
},
2016-10-27 18:15:43 +08:00
progress = new DragBar
2016-10-24 10:50:49 +08:00
{
2016-10-27 18:15:43 +08:00
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
2016-10-24 10:50:49 +08:00
Height = 10,
2016-10-27 18:15:43 +08:00
Colour = Color4.Orange,
SeekRequested = seek
2016-10-24 10:50:49 +08:00
}
};
2016-10-24 15:34:44 +08:00
}
protected override bool OnDragStart(InputState state) => true;
protected override bool OnDrag(InputState state)
2016-11-06 00:24:03 +08:00
{
Vector2 change = (state.Mouse.Position - state.Mouse.PositionMouseDown.Value);
change.X = -change.X;
2016-11-06 00:24:03 +08:00
change *= (float)Math.Pow(change.Length, 0.7f) / change.Length;
2016-11-06 00:24:03 +08:00
MoveTo(start_position + change);
return base.OnDrag(state);
}
protected override bool OnDragEnd(InputState state)
{
MoveTo(start_position, 800, EasingTypes.OutElastic);
return base.OnDragEnd(state);
}
[BackgroundDependencyLoader]
private void load(OsuGameBase osuGame, BeatmapDatabase beatmaps, AudioManager audio, TextureStore textures)
2016-11-06 00:24:03 +08:00
{
this.beatmaps = beatmaps;
trackManager = osuGame.Audio.Track;
config = osuGame.Config;
preferUnicode = osuGame.Config.GetBindable<bool>(OsuConfig.ShowUnicode);
preferUnicode.ValueChanged += preferUnicode_changed;
2016-11-06 00:24:03 +08:00
beatmapSource = osuGame.Beatmap ?? new Bindable<WorkingBeatmap>();
playList = beatmaps.GetAllWithChildren<BeatmapSetInfo>();
2016-11-06 00:24:03 +08:00
backgroundSprite = new MusicControllerBackground(fallbackTexture = textures.Get(@"Backgrounds/bg4"));
2016-11-06 00:24:03 +08:00
AddInternal(backgroundSprite);
}
2016-11-06 00:46:09 +08:00
protected override void LoadComplete()
{
2016-11-06 16:42:40 +08:00
beatmapSource.ValueChanged += workingChanged;
2016-11-06 00:46:09 +08:00
workingChanged();
base.LoadComplete();
}
2016-10-24 17:23:30 +08:00
protected override void Update()
{
base.Update();
2016-10-28 20:21:19 +08:00
if (current?.Track == null) return;
2016-10-28 20:44:59 +08:00
2016-10-28 20:21:19 +08:00
progress.UpdatePosition((float)(current.Track.CurrentTime / current.Track.Length));
2016-11-07 19:00:20 +08:00
playButton.Icon = current.Track.IsRunning ? FontAwesome.fa_pause_circle_o : FontAwesome.fa_play_circle_o;
2016-10-28 20:44:59 +08:00
2016-11-06 16:42:40 +08:00
if (current.Track.HasCompleted && !current.Track.Looping) next();
2016-10-24 17:23:30 +08:00
}
2016-11-11 10:35:58 +08:00
void preferUnicode_changed(object sender, EventArgs e)
{
updateDisplay(current, TransformDirection.None);
2016-11-11 10:35:58 +08:00
}
2016-10-28 20:44:59 +08:00
private void workingChanged(object sender = null, EventArgs e = null)
{
if (beatmapSource.Value == current) return;
2016-11-07 21:58:30 +08:00
bool audioEquals = current?.BeatmapInfo.AudioEquals(beatmapSource.Value.BeatmapInfo) ?? false;
current = beatmapSource.Value;
updateDisplay(current, audioEquals ? TransformDirection.None : TransformDirection.Left);
2016-11-07 21:30:25 +08:00
appendToHistory(current.BeatmapInfo);
2016-10-28 20:44:59 +08:00
}
2016-11-07 21:25:37 +08:00
private void appendToHistory(BeatmapInfo beatmap)
2016-10-28 20:08:27 +08:00
{
2016-11-07 21:25:37 +08:00
if (playHistoryIndex >= 0)
{
2016-11-07 21:52:23 +08:00
if (beatmap.AudioEquals(playHistory[playHistoryIndex]))
2016-11-07 21:25:37 +08:00
return;
if (playHistoryIndex < playHistory.Count - 1)
playHistory.RemoveRange(playHistoryIndex + 1, playHistory.Count - playHistoryIndex - 1);
}
playHistory.Insert(++playHistoryIndex, beatmap);
2016-10-28 20:08:27 +08:00
}
2016-10-24 15:34:44 +08:00
private void prev()
{
2016-11-07 21:25:37 +08:00
if (playHistoryIndex > 0)
play(playHistory[--playHistoryIndex], false);
2016-10-24 15:34:44 +08:00
}
private void next()
{
2016-11-07 21:25:37 +08:00
if (playHistoryIndex < playHistory.Count - 1)
play(playHistory[++playHistoryIndex], true);
else
{
2016-11-07 23:06:14 +08:00
if (playList.Count == 0) return;
if (current != null && playList.Count == 1) return;
2016-11-07 22:12:58 +08:00
//shuffle
2016-11-07 23:06:14 +08:00
BeatmapInfo nextToPlay;
do
2016-11-07 22:12:58 +08:00
{
2016-11-07 23:06:14 +08:00
int j = RNG.Next(playListIndex, playList.Count);
if (j != playListIndex)
{
BeatmapSetInfo temp = playList[playListIndex];
playList[playListIndex] = playList[j];
playList[j] = temp;
}
nextToPlay = playList[playListIndex++].Beatmaps[0];
if (playListIndex == playList.Count) playListIndex = 0;
2016-11-07 22:12:58 +08:00
}
2016-11-07 23:12:49 +08:00
while (nextToPlay.AudioEquals(current?.BeatmapInfo));
2016-11-07 22:12:58 +08:00
2016-11-07 21:25:37 +08:00
play(nextToPlay, true);
appendToHistory(nextToPlay);
}
2016-10-28 20:08:27 +08:00
}
2016-11-07 21:30:25 +08:00
private void play(BeatmapInfo info, bool isNext)
2016-10-28 20:08:27 +08:00
{
current = beatmaps.GetWorkingBeatmap(info, current);
Task.Run(() =>
2016-11-06 00:46:09 +08:00
{
trackManager.SetExclusive(current.Track);
current.Track.Start();
beatmapSource.Value = current;
});
updateDisplay(current, isNext ? TransformDirection.Left : TransformDirection.Right);
2016-10-24 15:34:44 +08:00
}
private void updateDisplay(WorkingBeatmap beatmap, TransformDirection direction)
2016-10-24 15:34:44 +08:00
{
if (beatmap.Beatmap == null)
//todo: we may need to display some default text here (currently in the constructor).
return;
2016-11-11 10:35:58 +08:00
BeatmapMetadata metadata = beatmap.Beatmap.BeatmapInfo.Metadata;
title.Text = config.GetUnicodeString(metadata.Title, metadata.TitleUnicode);
artist.Text = config.GetUnicodeString(metadata.Artist, metadata.ArtistUnicode);
2016-10-27 17:04:41 +08:00
MusicControllerBackground newBackground = new MusicControllerBackground(beatmap.Background ?? fallbackTexture);
2016-10-27 17:04:41 +08:00
2016-10-24 15:34:44 +08:00
Add(newBackground);
2016-10-27 17:04:41 +08:00
if (direction == TransformDirection.Left)
2016-10-24 15:34:44 +08:00
{
newBackground.Position = new Vector2(400, 0);
newBackground.MoveToX(0, 500, EasingTypes.OutCubic);
backgroundSprite.MoveToX(-400, 500, EasingTypes.OutCubic);
2016-10-24 15:34:44 +08:00
}
else if (direction == TransformDirection.Right)
2016-10-24 15:34:44 +08:00
{
newBackground.Position = new Vector2(-400, 0);
newBackground.MoveToX(0, 500, EasingTypes.OutCubic);
backgroundSprite.MoveToX(400, 500, EasingTypes.OutCubic);
2016-10-24 15:34:44 +08:00
}
2016-11-06 16:42:40 +08:00
backgroundSprite.Expire();
2016-10-27 17:04:41 +08:00
backgroundSprite = newBackground;
}
2016-10-27 19:51:38 +08:00
private void seek(float position)
{
2016-10-28 20:21:19 +08:00
current?.Track?.Seek(current.Track.Length * position);
current?.Track?.Start();
2016-10-27 19:51:38 +08:00
}
2016-11-11 10:35:58 +08:00
protected override void Dispose(bool isDisposing)
{
if (preferUnicode != null)
preferUnicode.ValueChanged -= preferUnicode_changed;
base.Dispose(isDisposing);
}
protected override bool OnClick(InputState state) => true;
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) => true;
//placeholder for toggling
2016-11-06 05:35:35 +08:00
protected override void PopIn() => FadeIn(100);
2016-11-06 05:35:35 +08:00
protected override void PopOut() => FadeOut(100);
private enum TransformDirection { None, Left, Right }
private class MusicControllerBackground : BufferedContainer
{
private Sprite sprite;
public MusicControllerBackground(Texture backgroundTexture)
{
CacheDrawnFrameBuffer = true;
RelativeSizeAxes = Axes.Both;
Depth = float.MinValue;
Children = new Drawable[]
{
sprite = new Sprite
{
Texture = backgroundTexture,
Colour = new Color4(150, 150, 150, 255)
},
new Box
{
RelativeSizeAxes = Axes.X,
Height = 50,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
Colour = new Color4(0, 0, 0, 127)
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
sprite.Scale = new Vector2(Math.Max(DrawSize.X / sprite.DrawSize.X, DrawSize.Y / sprite.DrawSize.Y));
}
}
}
}