1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 16:57:25 +08:00
osu-lazer/osu.Game/Screens/Select/PlaySongSelect.cs

461 lines
16 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-09-29 19:13:58 +08:00
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;
2017-02-17 17:59:30 +08:00
using osu.Framework.Screens;
using osu.Framework.Graphics;
2016-11-14 16:23:33 +08:00
using osu.Framework.Graphics.Primitives;
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 osu.Game.Screens.Play;
2016-12-05 19:09:56 +08:00
using osu.Framework.Audio.Sample;
2017-02-25 20:12:39 +08:00
using osu.Framework.Graphics.Transforms;
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;
2017-01-18 08:18:15 +08:00
using System.Collections.Generic;
using osu.Framework.Threading;
using osu.Game.Overlays.Mods;
using osu.Game.Overlays;
using osu.Game.Screens.Select.Options;
2016-09-29 19:13:58 +08:00
namespace osu.Game.Screens.Select
2016-09-29 19:13:58 +08:00
{
2017-02-17 17:59:30 +08:00
public class PlaySongSelect : OsuScreen
2016-09-29 19:13:58 +08:00
{
private Bindable<PlayMode> playMode = new Bindable<PlayMode>();
private BeatmapDatabase database;
2017-02-17 17:59:30 +08:00
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap(Beatmap);
2016-11-23 12:28:49 +08:00
private CarouselContainer carousel;
2016-10-28 18:55:48 +08:00
private TrackManager trackManager;
private DialogOverlay dialogOverlay;
private static readonly Vector2 wedged_container_size = new Vector2(0.5f, 225);
2016-11-24 12:48:48 +08:00
private BeatmapInfoWedge beatmapInfoWedge;
private ModSelectOverlay modSelect;
2017-02-17 04:05:03 +08:00
2017-02-07 15:15:45 +08:00
private static readonly Vector2 background_blur = new Vector2(20);
private CancellationTokenSource initialAddSetsTask;
private SampleChannel sampleChangeDifficulty;
private SampleChannel sampleChangeBeatmap;
2017-02-08 19:30:05 +08:00
2017-01-18 08:18:15 +08:00
private List<BeatmapGroup> beatmapGroups;
2016-12-05 19:09:56 +08:00
2017-03-02 20:40:55 +08:00
private BeatmapOptionsOverlay beatmapOptions;
private Footer footer;
2017-02-22 20:43:29 +08:00
OsuScreen player;
2017-02-24 23:54:27 +08:00
private FilterControl filter;
public FilterControl Filter
{
2017-02-24 23:54:27 +08:00
get
{
2017-02-24 23:54:27 +08:00
return filter;
}
private set
{
2017-02-24 23:54:27 +08:00
if (filter != value)
{
2017-02-24 23:54:27 +08:00
filter = value;
filterChanged();
}
2017-02-24 23:54:27 +08:00
}
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapDatabase beatmaps, AudioManager audio, DialogOverlay dialog, Framework.Game game,
OsuGame osu, OsuColour colours)
{
2017-02-07 15:15:45 +08:00
const float carousel_width = 640;
2017-02-08 19:30:05 +08:00
const float filter_height = 100;
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
{
2017-02-08 19:30:05 +08:00
Padding = new MarginPadding { Top = filter_height },
ParallaxAmount = 0.005f,
2016-10-14 04:55:15 +08:00
RelativeSizeAxes = Axes.Both,
2017-02-08 19:30:05 +08:00
Children = new[]
2016-10-20 02:02:03 +08:00
{
new WedgeBackground
2016-10-20 02:02:03 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-02-08 19:30:05 +08:00
Padding = new MarginPadding
{
Right = carousel_width * 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,
2017-02-07 15:15:45 +08:00
Size = new Vector2(carousel_width, 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
{
RelativeSizeAxes = Axes.X,
2017-02-08 19:30:05 +08:00
Height = filter_height,
FilterChanged = () => filterChanged(),
Exit = Exit,
},
2016-11-24 12:48:48 +08:00
beatmapInfoWedge = new BeatmapInfoWedge
{
Alpha = 0,
Size = wedged_container_size,
RelativeSizeAxes = Axes.X,
2017-02-08 19:30:05 +08:00
Margin = new MarginPadding
{
Top = 20,
Right = 20,
},
},
2017-03-03 16:19:03 +08:00
beatmapOptions = new BeatmapOptionsOverlay
2017-02-17 04:05:03 +08:00
{
2017-03-03 16:19:03 +08:00
OnRemoveFromUnplayed = null,
OnClearLocalScores = null,
OnEdit = null,
OnDelete = promptDelete,
Margin = new MarginPadding
{
Bottom = 50,
},
2017-02-17 04:05:03 +08:00
},
2017-03-03 16:19:03 +08:00
modSelect = new ModSelectOverlay
2017-03-02 20:40:55 +08:00
{
2017-03-03 16:19:03 +08:00
RelativeSizeAxes = Axes.X,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
2017-03-02 20:40:55 +08:00
Margin = new MarginPadding
{
Bottom = 50,
},
},
2017-02-22 20:43:29 +08:00
footer = new Footer
2016-10-21 22:52:52 +08:00
{
OnBack = Exit,
2017-02-22 20:43:29 +08:00
OnStart = () =>
{
if (player != null || Beatmap == null)
return;
(player = new PlayerLoader(new Player
{
BeatmapInfo = carousel.SelectedGroup.SelectedPanel.Beatmap,
PreferredPlayMode = playMode.Value
})).LoadAsync(Game, l => Push(player));
2017-02-22 20:43:29 +08:00
}
2017-02-28 11:19:28 +08:00
},
};
2017-02-17 04:05:03 +08:00
footer.AddButton(@"mods", colours.Yellow, modSelect.ToggleVisibility);
footer.AddButton(@"random", colours.Green, carousel.SelectRandom);
2017-03-02 20:40:55 +08:00
footer.AddButton(@"options", colours.Blue, beatmapOptions.ToggleVisibility);
if (osu != null)
playMode.BindTo(osu.PlayMode);
playMode.ValueChanged += playMode_ValueChanged;
if (database == null)
2016-11-09 07:13:20 +08:00
database = beatmaps;
2017-02-24 16:21:14 +08:00
database.BeatmapSetAdded += onBeatmapSetAdded;
database.BeatmapSetRemoved += onBeatmapSetRemoved;
2016-11-09 07:13:20 +08:00
trackManager = audio.Track;
dialogOverlay = dialog;
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;
private void filterChanged(bool debounce = true, bool eagerSelection = true)
2017-01-18 08:18:15 +08:00
{
filterTask?.Cancel();
filterTask = Scheduler.AddDelayed(() =>
2017-01-18 08:18:15 +08:00
{
filterTask = null;
var search = filter.Search;
BeatmapGroup newSelection = null;
2017-02-18 00:41:53 +08:00
carousel.Sort(filter.Sort);
foreach (var beatmapGroup in carousel)
2017-01-18 08:18:15 +08:00
{
var set = beatmapGroup.BeatmapSet;
bool hasCurrentMode = set.Beatmaps.Any(bm => bm.Mode == playMode);
bool match = hasCurrentMode;
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)
{
if (newSelection == null || beatmapGroup.BeatmapSet.OnlineBeatmapSetID == Beatmap.BeatmapSetInfo.OnlineBeatmapSetID)
{
if (newSelection != null)
newSelection.State = BeatmapGroupState.Collapsed;
newSelection = beatmapGroup;
}
else
beatmapGroup.State = BeatmapGroupState.Collapsed;
}
else
{
beatmapGroup.State = BeatmapGroupState.Hidden;
}
2017-01-18 08:18:15 +08:00
}
if (newSelection != null)
{
if (newSelection.BeatmapPanels.Any(b => b.Beatmap.ID == Beatmap.BeatmapInfo.ID))
carousel.SelectBeatmap(Beatmap.BeatmapInfo, false);
else if (eagerSelection)
carousel.SelectBeatmap(newSelection.BeatmapSet.Beatmaps[0], false);
}
}, debounce ? 250 : 0);
2017-01-18 08:18:15 +08:00
}
2017-02-24 16:21:14 +08:00
private void onBeatmapSetAdded(BeatmapSetInfo s) => Schedule(() => addBeatmapSet(s, Game, true));
2016-10-08 18:12:31 +08:00
2017-02-24 16:21:14 +08:00
private void onBeatmapSetRemoved(BeatmapSetInfo s) => Schedule(() => removeBeatmapSet(s));
2016-10-08 18:12:31 +08:00
2017-02-17 17:59:30 +08:00
protected override void OnEntering(Screen last)
2016-10-28 18:55:48 +08:00
{
base.OnEntering(last);
ensurePlayingSelected();
changeBackground(Beatmap);
Content.FadeInFromZero(250);
2017-02-08 18:32:55 +08:00
beatmapInfoWedge.MoveToX(-50);
beatmapInfoWedge.MoveToX(0, 800, EasingTypes.OutQuint);
2017-02-08 19:30:05 +08:00
filter.Activate();
2016-10-28 18:55:48 +08:00
}
2017-02-17 17:59:30 +08:00
protected override void OnResuming(Screen last)
2016-10-28 18:55:48 +08:00
{
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);
filter.Activate();
}
2017-02-17 17:59:30 +08:00
protected override void OnSuspending(Screen next)
{
Content.ScaleTo(1.1f, 250, EasingTypes.InSine);
Content.FadeOut(250);
2017-02-08 19:30:05 +08:00
filter.Deactivate();
base.OnSuspending(next);
}
2017-02-17 17:59:30 +08:00
protected override bool OnExiting(Screen next)
{
2017-02-08 18:32:55 +08:00
beatmapInfoWedge.MoveToX(-100, 800, EasingTypes.InQuint);
beatmapInfoWedge.RotateTo(10, 800, EasingTypes.InQuint);
Content.FadeOut(100);
2017-02-08 19:30:05 +08:00
filter.Deactivate();
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);
2017-02-24 16:21:14 +08:00
database.BeatmapSetAdded -= onBeatmapSetAdded;
database.BeatmapSetRemoved -= onBeatmapSetRemoved;
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
{
filterChanged(false);
2016-10-06 22:33:28 +08:00
}
private void changeBackground(WorkingBeatmap beatmap)
2016-10-26 22:52:04 +08:00
{
2017-02-17 17:59:30 +08:00
var backgroundModeBeatmap = Background as BackgroundScreenBeatmap;
if (backgroundModeBeatmap != null)
{
backgroundModeBeatmap.Beatmap = beatmap;
2017-02-07 15:15:45 +08:00
backgroundModeBeatmap.BlurTo(background_blur, 1000);
2017-02-22 20:43:29 +08:00
backgroundModeBeatmap.FadeTo(1, 250);
}
2017-01-30 22:44:02 +08:00
if (beatmap != null)
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);
carousel.SelectBeatmap(beatmap?.BeatmapInfo);
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
{
Track 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-02-23 14:38:17 +08:00
private void addBeatmapSet(BeatmapSetInfo beatmapSet, Framework.Game 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;
});
var group = new BeatmapGroup(beatmapSet, database)
{
SelectionChanged = selectionChanged,
2017-02-22 20:43:29 +08:00
StartRequested = b => footer.StartButton.TriggerClick()
};
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.LoadAsync(game))).ContinueWith(task => Schedule(delegate
2016-10-26 22:52:04 +08:00
{
2017-01-18 08:18:15 +08:00
beatmapGroups.Add(group);
group.State = BeatmapGroupState.Collapsed;
2016-11-23 12:28:49 +08:00
carousel.AddGroup(group);
2016-11-01 22:24:14 +08:00
filterChanged(false, false);
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
2017-03-04 21:05:02 +08:00
carousel.SelectBeatmap(Beatmap.BeatmapInfo);
}));
2016-10-26 22:52:04 +08:00
}
private void removeBeatmapSet(BeatmapSetInfo beatmapSet)
{
var group = beatmapGroups.Find(b => b.BeatmapSet.ID == beatmapSet.ID);
if (group == null) return;
if (carousel.SelectedGroup == group)
carousel.SelectNext();
beatmapGroups.Remove(group);
carousel.RemoveGroup(group);
if (beatmapGroups.Count == 0)
Beatmap = null;
}
2017-02-23 14:38:17 +08:00
private void addBeatmapSets(Framework.Game game, CancellationToken token)
2016-10-26 22:52:04 +08:00
{
2017-02-24 16:08:13 +08:00
foreach (var beatmapSet in database.Query<BeatmapSetInfo>().Where(b => !b.DeletePending))
{
if (token.IsCancellationRequested) return;
addBeatmapSet(beatmapSet, game);
}
2016-10-26 22:52:04 +08:00
}
2016-12-15 19:55:37 +08:00
2017-03-02 20:40:55 +08:00
private void promptDelete()
{
if (Beatmap != null)
dialogOverlay?.Push(new BeatmapDeleteDialog(Beatmap));
}
2016-12-15 19:55:37 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
switch (args.Key)
{
case Key.Enter:
2017-02-22 20:43:29 +08:00
footer.StartButton.TriggerClick();
2016-12-15 19:55:37 +08:00
return true;
case Key.Delete:
2017-02-28 11:19:28 +08:00
if (state.Keyboard.ShiftPressed)
{
2017-03-02 20:40:55 +08:00
promptDelete();
2017-02-28 11:19:28 +08:00
return true;
}
break;
2016-12-15 19:55:37 +08:00
}
return base.OnKeyDown(state, args);
}
2016-09-29 19:13:58 +08:00
}
}