1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 18:07:24 +08:00
osu-lazer/osu.Game/Screens/Select/PlaySongSelect.cs

423 lines
15 KiB
C#
Raw Normal View History

2016-09-29 19:13:58 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2016-11-14 16:23:33 +08:00
using System.Linq;
using System.Threading;
2016-11-14 16:23:33 +08:00
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
2016-10-08 18:12:31 +08:00
using osu.Framework.Configuration;
2016-11-14 16:23:33 +08:00
using osu.Framework.GameModes;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2016-11-14 16:23:33 +08:00
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
2016-11-14 16:23:33 +08:00
using osu.Game.Beatmaps;
using osu.Game.Database;
2016-11-14 17:03:20 +08:00
using osu.Game.Modes;
2016-11-14 16:23:33 +08:00
using osu.Game.Screens.Backgrounds;
2016-10-14 04:55:15 +08:00
using OpenTK;
using OpenTK.Graphics;
using osu.Game.Screens.Play;
2016-11-21 03:43:43 +08:00
using osu.Framework;
2016-12-05 19:09:56 +08:00
using osu.Framework.Audio.Sample;
using osu.Framework.Graphics.Transformations;
2016-11-23 10:59:50 +08:00
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics;
2016-12-15 19:55:37 +08:00
using osu.Framework.Input;
using OpenTK.Input;
using osu.Game.Graphics;
2017-01-18 08:18:15 +08:00
using System.Collections.Generic;
using osu.Framework.Threading;
2016-09-29 19:13:58 +08:00
namespace osu.Game.Screens.Select
2016-09-29 19:13:58 +08:00
{
2016-10-14 03:10:00 +08:00
public class PlaySongSelect : OsuGameMode
2016-09-29 19:13:58 +08:00
{
2016-10-08 18:12:31 +08:00
private Bindable<PlayMode> playMode;
private BeatmapDatabase database;
protected override BackgroundMode CreateBackground() => new BackgroundModeBeatmap(Beatmap);
2016-11-23 12:28:49 +08:00
private CarouselContainer carousel;
2016-10-28 18:55:48 +08:00
private TrackManager trackManager;
private static readonly Vector2 wedged_container_size = new Vector2(0.5f, 225);
private static readonly Vector2 wedged_container_start_position = new Vector2(0, 50);
2016-11-24 12:48:48 +08:00
private BeatmapInfoWedge beatmapInfoWedge;
private static readonly Vector2 BACKGROUND_BLUR = new Vector2(20);
private CancellationTokenSource initialAddSetsTask;
2016-12-05 19:09:56 +08:00
private AudioSample sampleChangeDifficulty;
private AudioSample sampleChangeBeatmap;
2017-01-18 08:18:15 +08:00
private List<BeatmapGroup> beatmapGroups;
2016-12-05 19:09:56 +08:00
private Footer footer;
class WedgeBackground : Container
{
public WedgeBackground()
{
Children = new[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(1, 0.5f),
2017-01-11 02:44:40 +08:00
Colour = Color4.Black.Opacity(0.5f),
Shear = new Vector2(0.15f, 0),
EdgeSmoothness = new Vector2(2, 0),
},
new Box
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Y,
Size = new Vector2(1, -0.5f),
Position = new Vector2(0, 1),
2017-01-11 02:44:40 +08:00
Colour = Color4.Black.Opacity(0.5f),
Shear = new Vector2(-0.15f, 0),
EdgeSmoothness = new Vector2(2, 0),
},
};
}
}
Player player;
2017-01-18 08:18:15 +08:00
FilterControl filter;
private void start()
{
if (player != null)
return;
//in the future we may want to move this logic to a PlayerLoader gamemode or similar, so we can rely on the SongSelect transition
//and provide a better loading experience (at the moment song select is still accepting input during preload).
player = new Player
{
BeatmapInfo = carousel.SelectedGroup.SelectedPanel.Beatmap,
PreferredPlayMode = playMode.Value
};
player.Preload(Game, delegate
{
if (!Push(player))
{
player = null;
//error occured?
}
});
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapDatabase beatmaps, AudioManager audio, BaseGame game,
OsuGame osuGame, OsuColour colours)
{
const float carouselWidth = 640;
2016-10-24 23:08:48 +08:00
const float bottomToolHeight = 50;
2017-01-18 08:18:15 +08:00
beatmapGroups = new List<BeatmapGroup>();
2016-10-14 04:55:15 +08:00
Children = new Drawable[]
{
new ParallaxContainer
2016-10-14 04:55:15 +08:00
{
ParallaxAmount = 0.005f,
2016-10-14 04:55:15 +08:00
RelativeSizeAxes = Axes.Both,
Children = new []
2016-10-20 02:02:03 +08:00
{
new WedgeBackground
2016-10-20 02:02:03 +08:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = carouselWidth * 0.76f },
2016-10-20 02:02:03 +08:00
},
}
2016-10-14 04:55:15 +08:00
},
2016-11-23 12:28:49 +08:00
carousel = new CarouselContainer
{
2016-10-20 02:02:03 +08:00
RelativeSizeAxes = Axes.Y,
Size = new Vector2(carouselWidth, 1),
2016-10-20 02:02:03 +08:00
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
2016-10-21 22:52:52 +08:00
},
2017-01-18 08:18:15 +08:00
filter = new FilterControl
{
Position = wedged_container_start_position,
RelativeSizeAxes = Axes.X,
2017-01-18 08:18:15 +08:00
FilterChanged = filterChanged,
},
2016-11-24 12:48:48 +08:00
beatmapInfoWedge = new BeatmapInfoWedge
{
Alpha = 0,
Position = wedged_container_start_position,
Size = wedged_container_size,
RelativeSizeAxes = Axes.X,
Margin = new MarginPadding { Top = 20, Right = 20, },
},
footer = new Footer()
2016-10-21 22:52:52 +08:00
{
OnBack = Exit,
OnStart = start,
}
};
footer.AddButton(@"mods", colours.Yellow, null);
footer.AddButton(@"random", colours.Green, carousel.SelectRandom);
footer.AddButton(@"options", colours.Blue, null);
2016-11-21 03:43:43 +08:00
if (osuGame != null)
2016-10-14 03:10:00 +08:00
{
2016-11-21 03:43:43 +08:00
playMode = osuGame.PlayMode;
2016-10-28 18:55:48 +08:00
playMode.ValueChanged += playMode_ValueChanged;
2016-10-14 03:10:00 +08:00
}
if (database == null)
2016-11-09 07:13:20 +08:00
database = beatmaps;
database.BeatmapSetAdded += onDatabaseOnBeatmapSetAdded;
2016-11-09 07:13:20 +08:00
trackManager = audio.Track;
2016-10-28 18:55:48 +08:00
2016-12-05 19:09:56 +08:00
sampleChangeDifficulty = audio.Sample.Get(@"SongSelect/select-difficulty");
sampleChangeBeatmap = audio.Sample.Get(@"SongSelect/select-expand");
initialAddSetsTask = new CancellationTokenSource();
Task.Factory.StartNew(() => addBeatmapSets(game, initialAddSetsTask.Token), initialAddSetsTask.Token);
}
private ScheduledDelegate filterTask;
2017-01-18 08:18:15 +08:00
private void filterChanged()
{
if (filterTask != null)
filterTask.Cancel();
filterTask = Scheduler.AddDelayed(() =>
2017-01-18 08:18:15 +08:00
{
filterTask = null;
var search = filter.Search;
BeatmapGroup newSelection = null;
bool changed = false;
foreach (var beatmapGroup in carousel)
2017-01-18 08:18:15 +08:00
{
var set = beatmapGroup.BeatmapSet;
if (set == null)
continue;
bool match = string.IsNullOrEmpty(search)
|| (set.Metadata.Artist ?? "").IndexOf(search, StringComparison.InvariantCultureIgnoreCase) != -1
|| (set.Metadata.ArtistUnicode ?? "").IndexOf(search, StringComparison.InvariantCultureIgnoreCase) != -1
|| (set.Metadata.Title ?? "").IndexOf(search, StringComparison.InvariantCultureIgnoreCase) != -1
|| (set.Metadata.TitleUnicode ?? "").IndexOf(search, StringComparison.InvariantCultureIgnoreCase) != -1;
if (match)
{
changed = changed && !beatmapGroup.Hidden;
beatmapGroup.Hidden = false;
beatmapGroup.Header.Alpha = 1;
if (newSelection == null || beatmapGroup.BeatmapSet.OnlineBeatmapSetID == Beatmap.BeatmapSetInfo.OnlineBeatmapSetID)
newSelection = beatmapGroup;
}
else
{
changed = changed && beatmapGroup.Hidden;
beatmapGroup.Hidden = true;
beatmapGroup.Header.Alpha = 0;
beatmapGroup.Header.Masking = false;
beatmapGroup.State = BeatmapGroupState.Collapsed;
}
2017-01-18 08:18:15 +08:00
}
if (newSelection != null)
selectBeatmap(newSelection.BeatmapSet.Beatmaps[0]);
if (changed || true)
carousel.InvalidateVisible();
}, 250);
2017-01-18 08:18:15 +08:00
}
private void onDatabaseOnBeatmapSetAdded(BeatmapSetInfo s)
{
2017-01-18 06:05:06 +08:00
Schedule(() => addBeatmapSet(s, Game, true));
2016-10-08 18:12:31 +08:00
}
2016-10-28 18:55:48 +08:00
protected override void OnEntering(GameMode last)
{
base.OnEntering(last);
ensurePlayingSelected();
changeBackground(Beatmap);
Content.FadeInFromZero(250);
beatmapInfoWedge.MoveToX(wedged_container_start_position.X - 50);
beatmapInfoWedge.MoveToX(wedged_container_start_position.X, 800, EasingTypes.OutQuint);
2016-10-28 18:55:48 +08:00
}
protected override void OnResuming(GameMode last)
{
2016-12-15 20:18:30 +08:00
player = null;
changeBackground(Beatmap);
2016-10-28 18:55:48 +08:00
ensurePlayingSelected();
base.OnResuming(last);
Content.FadeIn(250);
Content.ScaleTo(1, 250, EasingTypes.OutSine);
}
protected override void OnSuspending(GameMode next)
{
Content.ScaleTo(1.1f, 250, EasingTypes.InSine);
Content.FadeOut(250);
base.OnSuspending(next);
}
protected override bool OnExiting(GameMode next)
{
beatmapInfoWedge.MoveTo(wedged_container_start_position + new Vector2(-100, 50), 800, EasingTypes.InQuint);
beatmapInfoWedge.RotateTo(10, 800, EasingTypes.InQuint);
Content.FadeOut(100);
return base.OnExiting(next);
2016-10-28 18:55:48 +08:00
}
2016-10-08 18:12:31 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (playMode != null)
2016-10-28 18:55:48 +08:00
playMode.ValueChanged -= playMode_ValueChanged;
database.BeatmapSetAdded -= onDatabaseOnBeatmapSetAdded;
initialAddSetsTask.Cancel();
2016-10-06 22:33:28 +08:00
}
2016-10-28 18:55:48 +08:00
private void playMode_ValueChanged(object sender, EventArgs e)
2016-10-06 22:33:28 +08:00
{
}
private void changeBackground(WorkingBeatmap beatmap)
2016-10-26 22:52:04 +08:00
{
if (beatmap == null)
return;
var backgroundModeBeatmap = Background as BackgroundModeBeatmap;
if (backgroundModeBeatmap != null)
{
backgroundModeBeatmap.Beatmap = beatmap;
// TODO: Remove this once we have non-nullable Beatmap
(Background as BackgroundModeBeatmap)?.BlurTo(BACKGROUND_BLUR, 1000);
}
2016-11-24 12:48:48 +08:00
beatmapInfoWedge.UpdateBeatmap(beatmap);
}
/// <summary>
/// The global Beatmap was changed.
/// </summary>
protected override void OnBeatmapChanged(WorkingBeatmap beatmap)
{
base.OnBeatmapChanged(beatmap);
//todo: change background in selectionChanged instead; support per-difficulty backgrounds.
changeBackground(beatmap);
2016-10-28 20:08:32 +08:00
selectBeatmap(beatmap.BeatmapInfo);
2016-10-28 18:55:48 +08:00
}
private void selectBeatmap(BeatmapInfo beatmap) => carousel.SelectBeatmap(beatmap);
2016-10-28 18:55:48 +08:00
/// <summary>
/// selection has been changed as the result of interaction with the carousel.
/// </summary>
private void selectionChanged(BeatmapGroup group, BeatmapInfo beatmap)
{
2016-12-16 07:11:48 +08:00
bool beatmapSetChange = false;
2016-10-28 20:08:32 +08:00
if (!beatmap.Equals(Beatmap?.BeatmapInfo))
2016-12-05 19:09:56 +08:00
{
2016-12-21 14:47:56 +08:00
if (beatmap.BeatmapSetInfoID == Beatmap?.BeatmapInfo.BeatmapSetInfoID)
2016-12-05 19:09:56 +08:00
sampleChangeDifficulty.Play();
else
2016-12-16 07:11:48 +08:00
{
2016-12-05 19:09:56 +08:00
sampleChangeBeatmap.Play();
2016-12-16 07:11:48 +08:00
beatmapSetChange = true;
}
Beatmap = database.GetWorkingBeatmap(beatmap, Beatmap);
2016-12-05 19:09:56 +08:00
}
2016-12-16 07:11:48 +08:00
ensurePlayingSelected(beatmapSetChange);
2016-10-26 22:52:04 +08:00
}
private void ensurePlayingSelected(bool preview = false)
2016-10-28 18:55:48 +08:00
{
AudioTrack track = Beatmap?.Track;
2016-10-28 18:55:48 +08:00
if (track != null)
2016-10-28 18:55:48 +08:00
{
trackManager.SetExclusive(track);
if (preview)
track.Seek(Beatmap.Beatmap.Metadata.PreviewTime);
track.Start();
}
2016-10-28 18:55:48 +08:00
}
2017-01-18 06:05:06 +08:00
private void addBeatmapSet(BeatmapSetInfo beatmapSet, BaseGame game, bool select = false)
2016-10-26 22:52:04 +08:00
{
2016-12-20 23:56:49 +08:00
beatmapSet = database.GetWithChildren<BeatmapSetInfo>(beatmapSet.ID);
beatmapSet.Beatmaps.ForEach(b =>
{
database.GetChildren(b);
if (b.Metadata == null) b.Metadata = beatmapSet.Metadata;
});
beatmapSet.Beatmaps = beatmapSet.Beatmaps.OrderBy(b => b.StarDifficulty).ToList();
2016-11-05 19:00:14 +08:00
var beatmap = new WorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault(), beatmapSet, database);
2016-11-05 19:00:14 +08:00
2017-01-18 08:18:15 +08:00
var group = new BeatmapGroup(beatmap, beatmapSet)
{
SelectionChanged = selectionChanged,
StartRequested = b => start()
};
2016-11-01 22:24:14 +08:00
//for the time being, let's completely load the difficulty panels in the background.
//this likely won't scale so well, but allows us to completely async the loading flow.
Task.WhenAll(group.BeatmapPanels.Select(panel => panel.Preload(game))).ContinueWith(task => Schedule(delegate
2016-10-26 22:52:04 +08:00
{
2017-01-18 08:18:15 +08:00
beatmapGroups.Add(group);
2016-11-23 12:28:49 +08:00
carousel.AddGroup(group);
2016-11-01 22:24:14 +08:00
2017-01-18 06:05:06 +08:00
if (Beatmap == null || select)
2016-11-23 12:28:49 +08:00
carousel.SelectBeatmap(beatmapSet.Beatmaps.First());
else
{
var panel = group.BeatmapPanels.FirstOrDefault(p => p.Beatmap.Equals(Beatmap.BeatmapInfo));
if (panel != null)
2016-11-23 12:28:49 +08:00
carousel.SelectGroup(group, panel);
}
}));
2016-10-26 22:52:04 +08:00
}
private void addBeatmapSets(BaseGame game, CancellationToken token)
2016-10-26 22:52:04 +08:00
{
foreach (var beatmapSet in database.Query<BeatmapSetInfo>())
{
if (token.IsCancellationRequested) return;
addBeatmapSet(beatmapSet, game);
}
2016-10-26 22:52:04 +08:00
}
2016-12-15 19:55:37 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
switch (args.Key)
{
case Key.Enter:
start();
return true;
}
return base.OnKeyDown(state, args);
}
2016-09-29 19:13:58 +08:00
}
}