osu/osu.Game/Overlays/MusicController.cs

288 lines
10 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 05:00:06 +00:00
using System;
2016-10-24 05:47:05 +00:00
using System.Collections.Generic;
using System.Linq;
2016-10-24 05:00:06 +00:00
using OpenTK;
2016-10-24 02:50:49 +00:00
using OpenTK.Graphics;
using osu.Framework;
2016-10-24 07:34:44 +00:00
using osu.Framework.Audio.Track;
2016-10-24 02:50:49 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2016-10-24 02:50:49 +00:00
using osu.Framework.Graphics.Sprites;
2016-10-24 05:13:49 +00:00
using osu.Framework.Graphics.Textures;
2016-10-24 07:34:44 +00:00
using osu.Framework.Graphics.Transformations;
2016-10-24 05:00:06 +00:00
using osu.Framework.Input;
2016-10-24 07:34:44 +00:00
using osu.Game.Beatmaps.IO;
2016-10-24 05:47:05 +00:00
using osu.Game.Database;
2016-10-24 05:00:06 +00:00
using osu.Game.Graphics;
namespace osu.Game.Overlays
{
public class MusicController : OverlayContainer
{
2016-10-24 05:13:49 +00:00
private Sprite backgroundSprite;
2016-10-24 02:50:49 +00:00
private Box progress;
2016-10-24 09:19:48 +00:00
private TextAwesome playButton, listButton;
2016-10-24 05:00:06 +00:00
private SpriteText title, artist;
2016-10-24 07:34:44 +00:00
private OsuGameBase osuGame;
2016-10-24 05:47:05 +00:00
private List<BeatmapSetInfo> playList;
private BeatmapDatabase database;
2016-10-24 05:47:05 +00:00
private BeatmapSetInfo currentPlay;
2016-10-24 10:26:50 +00:00
public AudioTrack CurrentTrack { get; set; }//TODO:gets exterally
public MusicController(BeatmapDatabase db = null)
{
database = db;
}
public override void Load(BaseGame game)
{
base.Load(game);
2016-10-24 07:34:44 +00:00
osuGame = game as OsuGameBase;
if (database == null) database = osuGame.Beatmaps;
playList = database.Query<BeatmapSetInfo>().ToList();
2016-10-24 05:47:05 +00:00
currentPlay = playList.FirstOrDefault();
Width = 400;
Height = 130;
CornerRadius = 5;
Masking = true;
2016-10-24 07:34:44 +00:00
2016-10-24 02:50:49 +00:00
Children = new Drawable[]
{
2016-10-24 05:13:49 +00:00
backgroundSprite = getScaledSprite(game.Textures.Get(@"Backgrounds/bg4")),//placeholder
2016-10-24 02:50:49 +00:00
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = new Color4(0, 0, 0, 127)
},
2016-10-24 05:00:06 +00:00
title = new SpriteText
{
Origin = Anchor.BottomCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 40),
TextSize = 20,
Colour = Color4.White,
2016-10-24 07:34:44 +00:00
Text = @"Nothing to play"
2016-10-24 05:00:06 +00:00
},
artist = new SpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 45),
TextSize = 12,
Colour = Color4.White,
2016-10-24 07:34:44 +00:00
Text = @"Nothing to play"
2016-10-24 05:00:06 +00:00
},
2016-10-24 02:50:49 +00:00
new Box
{
RelativeSizeAxes = Axes.X,
Height = 50,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
Colour = new Color4(0, 0, 0, 127)
},
2016-10-24 09:19:48 +00:00
new ClickableContainer
2016-10-24 05:00:06 +00:00
{
2016-10-24 09:19:48 +00:00
AutoSizeAxes = Axes.Both,
2016-10-24 05:00:06 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomCentre,
2016-10-24 07:34:44 +00:00
Position = new Vector2(0, 30),
Action = () =>
{
2016-10-24 10:26:50 +00:00
if (CurrentTrack == null) return;
if (CurrentTrack.IsRunning)
2016-10-24 07:34:44 +00:00
{
2016-10-24 10:26:50 +00:00
CurrentTrack.Stop();
2016-10-24 07:34:44 +00:00
playButton.Icon = FontAwesome.play_circle_o;
}
else
{
2016-10-24 10:26:50 +00:00
CurrentTrack.Start();
2016-10-24 07:34:44 +00:00
playButton.Icon = FontAwesome.pause;
}
2016-10-24 09:19:48 +00:00
},
Children = new Drawable[]
{
playButton = new TextAwesome
{
TextSize = 30,
Icon = FontAwesome.play_circle_o,
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
2016-10-24 07:34:44 +00:00
}
2016-10-24 05:00:06 +00:00
},
2016-10-24 09:19:48 +00:00
new ClickableContainer
2016-10-24 05:00:06 +00:00
{
2016-10-24 09:19:48 +00:00
AutoSizeAxes = Axes.Both,
2016-10-24 05:00:06 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomCentre,
2016-10-24 07:34:44 +00:00
Position = new Vector2(-30, 30),
2016-10-24 09:19:48 +00:00
Action = prev,
Children = new Drawable[]
{
new TextAwesome
{
TextSize = 15,
Icon = FontAwesome.step_backward,
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
}
2016-10-24 05:00:06 +00:00
},
2016-10-24 09:19:48 +00:00
new ClickableContainer
2016-10-24 05:00:06 +00:00
{
2016-10-24 09:19:48 +00:00
AutoSizeAxes = Axes.Both,
2016-10-24 05:00:06 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomCentre,
2016-10-24 07:34:44 +00:00
Position = new Vector2(30, 30),
2016-10-24 09:19:48 +00:00
Action = next,
Children = new Drawable[]
{
new TextAwesome
{
TextSize = 15,
Icon = FontAwesome.step_forward,
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
}
2016-10-24 05:00:06 +00:00
},
2016-10-24 09:19:48 +00:00
new ClickableContainer
2016-10-24 05:00:06 +00:00
{
2016-10-24 09:19:48 +00:00
AutoSizeAxes = Axes.Both,
2016-10-24 05:00:06 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.BottomRight,
2016-10-24 09:19:48 +00:00
Position = new Vector2(20, 30),
Children = new Drawable[]
{
listButton = new TextAwesome
{
TextSize = 15,
Icon = FontAwesome.bars,
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
}
2016-10-24 05:00:06 +00:00
},
2016-10-24 02:50:49 +00:00
progress = new Box
{
RelativeSizeAxes = Axes.X,
Height = 10,
2016-10-24 09:23:30 +00:00
Width = 0,
2016-10-24 02:50:49 +00:00
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Colour = Color4.Orange
}
};
2016-10-24 09:19:48 +00:00
if (currentPlay != null)
{
playButton.Icon=FontAwesome.pause;
play(currentPlay, null);
}
2016-10-24 07:34:44 +00:00
}
2016-10-24 09:23:30 +00:00
protected override void Update()
{
base.Update();
2016-10-24 10:26:50 +00:00
if (CurrentTrack == null) return;
progress.Width = (float)(CurrentTrack.CurrentTime / CurrentTrack.Length);
if (CurrentTrack.HasCompleted) next();
2016-10-24 09:23:30 +00:00
}
2016-10-24 07:34:44 +00:00
private void prev()
{
int i = playList.IndexOf(currentPlay);
if (i == -1) return;
i = (i - 1 + playList.Count) % playList.Count;
currentPlay = playList[i];
play(currentPlay, false);
}
private void next()
{
int i = playList.IndexOf(currentPlay);
if (i == -1) return;
i = (i + 1) % playList.Count;
currentPlay = playList[i];
play(currentPlay, true);
}
private void play(BeatmapSetInfo beatmap, bool? isNext)
{
2016-10-24 09:19:48 +00:00
BeatmapMetadata metadata = osuGame.Beatmaps.Query<BeatmapMetadata>().Where(x => x.ID == beatmap.BeatmapMetadataID).First();
title.Text = metadata.TitleUnicode ?? metadata.Title;
artist.Text = metadata.ArtistUnicode ?? metadata.Artist;
2016-10-25 01:33:52 +00:00
Sprite newBackground;
using (ArchiveReader reader = osuGame.Beatmaps.GetReader(currentPlay))
{
CurrentTrack?.Stop();
CurrentTrack = new AudioTrackBass(reader.ReadFile(metadata.AudioFile));
CurrentTrack.Start();
newBackground = getScaledSprite(TextureLoader.FromStream(reader.ReadFile(metadata.BackgroundFile)));
}
2016-10-24 07:34:44 +00:00
Add(newBackground);
if (isNext == true)
{
newBackground.Position = new Vector2(400, 0);
newBackground.MoveToX(0, 500, EasingTypes.OutCubic);
backgroundSprite.MoveToX(-400, 500, EasingTypes.OutCubic);
2016-10-24 07:34:44 +00:00
backgroundSprite.Expire();
}
else if (isNext == false)
{
newBackground.Position = new Vector2(-400, 0);
newBackground.MoveToX(0, 500, EasingTypes.OutCubic);
backgroundSprite.MoveToX(400, 500, EasingTypes.OutCubic);
2016-10-24 07:34:44 +00:00
backgroundSprite.Expire();
}
else
{
Remove(backgroundSprite);
}
backgroundSprite = newBackground;
}
2016-10-24 05:13:49 +00:00
private Sprite getScaledSprite(Texture background)
{
Sprite scaledSprite = new Sprite
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Texture = background,
Depth = float.MinValue
};
scaledSprite.Scale = new Vector2(Math.Max(DrawSize.X / scaledSprite.DrawSize.X, DrawSize.Y / scaledSprite.DrawSize.Y));
return scaledSprite;
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
trySeek(state);
return base.OnMouseDown(state, args);
}
protected override bool OnMouseMove(InputState state)
{
trySeek(state);
return base.OnMouseMove(state);
}
private void trySeek(InputState state)
{
if (state.Mouse.LeftButton)
{
Vector2 pos = GetLocalPosition(state.Mouse.NativeState.Position);
if (pos.Y > 120)
CurrentTrack?.Seek(CurrentTrack.Length * pos.X / 400f);
}
}
//placeholder for toggling
protected override void PopIn() => FadeIn(500);
protected override void PopOut() => FadeOut(500);
}
}