1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 03:43:22 +08:00
osu-lazer/osu.Game/Screens/Select/SongSelect.cs

511 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-09-29 19:13:58 +08:00
2017-07-19 12:32:16 +08:00
using System;
using System.Threading;
using OpenTK;
using OpenTK.Input;
2016-11-14 16:23:33 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2016-11-14 16:23:33 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Screens;
using osu.Framework.Threading;
2016-11-14 16:23:33 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
2016-11-14 16:23:33 +08:00
using osu.Game.Screens.Backgrounds;
2017-10-06 23:59:14 +08:00
using osu.Game.Screens.Edit;
using osu.Game.Screens.Menu;
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
{
public abstract class SongSelect : OsuScreen
2016-09-29 19:13:58 +08:00
{
private static readonly Vector2 wedged_container_size = new Vector2(0.5f, 245);
2017-02-07 15:15:45 +08:00
private static readonly Vector2 background_blur = new Vector2(20);
2017-12-15 11:48:15 +08:00
private const float left_area_padding = 20;
2017-12-15 11:48:15 +08:00
public readonly FilterControl FilterControl;
2017-02-08 19:30:05 +08:00
2017-03-15 10:10:59 +08:00
protected virtual bool ShowFooter => true;
2017-03-14 21:51:26 +08:00
/// <summary>
2017-03-16 13:08:37 +08:00
/// Can be null if <see cref="ShowFooter"/> is false.
2017-03-14 21:51:26 +08:00
/// </summary>
2017-03-15 10:10:59 +08:00
protected readonly BeatmapOptionsOverlay BeatmapOptions;
2017-03-14 21:51:26 +08:00
/// <summary>
2017-03-16 13:08:37 +08:00
/// Can be null if <see cref="ShowFooter"/> is false.
2017-03-14 21:51:26 +08:00
/// </summary>
2017-03-15 10:10:59 +08:00
protected readonly Footer Footer;
/// <summary>
/// Contains any panel which is triggered by a footer button.
/// Helps keep them located beneath the footer itself.
/// </summary>
protected readonly Container FooterPanels;
2017-12-15 11:48:15 +08:00
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap();
2017-12-15 11:48:15 +08:00
protected Container LeftContent;
protected readonly BeatmapCarousel Carousel;
2017-12-15 11:48:15 +08:00
private readonly BeatmapInfoWedge beatmapInfoWedge;
private DialogOverlay dialogOverlay;
private BeatmapManager beatmaps;
2017-12-12 16:48:38 +08:00
2017-12-15 11:48:15 +08:00
private SampleChannel sampleChangeDifficulty;
private SampleChannel sampleChangeBeatmap;
private CancellationTokenSource initialAddSetsTask;
private DependencyContainer dependencies;
2017-12-12 16:48:38 +08:00
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) => dependencies = new DependencyContainer(parent);
2017-03-15 10:10:59 +08:00
protected SongSelect()
{
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-12-13 11:46:02 +08:00
AddRange(new Drawable[]
{
2017-12-13 11:46:02 +08:00
new ParallaxContainer
2016-10-14 04:55:15 +08:00
{
2017-12-13 11:46:02 +08:00
Padding = new MarginPadding { Top = filter_height },
ParallaxAmount = 0.005f,
RelativeSizeAxes = Axes.Both,
Children = new[]
2016-10-20 02:02:03 +08:00
{
2017-12-13 11:46:02 +08:00
new WedgeBackground
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = carousel_width * 0.76f },
}
2016-10-20 02:02:03 +08:00
}
2017-12-13 11:46:02 +08:00
},
LeftContent = new Container
{
2017-12-13 11:46:02 +08:00
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Size = new Vector2(wedged_container_size.X, 1),
Padding = new MarginPadding
{
Bottom = 50,
Top = wedged_container_size.Y + left_area_padding,
Left = left_area_padding,
Right = left_area_padding * 2,
}
},
new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Width = 2, //avoid horizontal masking so the panels don't clip when screen stack is pushed.
Child = new Container
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Width = 0.5f,
Children = new Drawable[]
{
Carousel = new BeatmapCarousel
{
Masking = false,
RelativeSizeAxes = Axes.Y,
Size = new Vector2(carousel_width, 1),
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
SelectionChanged = carouselSelectionChanged,
BeatmapSetsChanged = carouselBeatmapsLoaded,
},
FilterControl = new FilterControl
{
RelativeSizeAxes = Axes.X,
Height = filter_height,
FilterChanged = c => Carousel.Filter(c),
Background = { Width = 2 },
Exit = Exit,
},
}
},
2017-12-13 11:46:02 +08:00
},
beatmapInfoWedge = new BeatmapInfoWedge
{
Size = wedged_container_size,
RelativeSizeAxes = Axes.X,
Margin = new MarginPadding
{
Top = left_area_padding,
Right = left_area_padding,
},
},
new ResetScrollContainer(() => Carousel.ScrollToSelected())
2017-12-13 11:46:02 +08:00
{
RelativeSizeAxes = Axes.Y,
Width = 250,
}
2017-07-29 22:33:20 +08:00
});
2017-03-15 10:10:59 +08:00
if (ShowFooter)
2017-03-14 21:51:26 +08:00
{
Add(FooterPanels = new Container
2017-02-17 04:05:03 +08:00
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding
{
Bottom = Footer.HEIGHT,
},
2017-03-14 21:51:26 +08:00
});
Add(Footer = new Footer
2016-10-21 22:52:52 +08:00
{
OnBack = Exit,
2017-03-14 21:51:26 +08:00
});
FooterPanels.Add(BeatmapOptions = new BeatmapOptionsOverlay());
2017-03-14 21:51:26 +08:00
}
}
2017-03-14 21:51:26 +08:00
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapManager beatmaps, AudioManager audio, DialogOverlay dialog, OsuGame osu, OsuColour colours)
2017-03-14 21:51:26 +08:00
{
2017-12-12 16:48:38 +08:00
dependencies.Cache(this);
2017-03-14 21:51:26 +08:00
if (Footer != null)
{
Footer.AddButton(@"random", colours.Green, triggerRandom, Key.F2);
Footer.AddButton(@"options", colours.Blue, BeatmapOptions, Key.F3);
2017-03-14 20:18:14 +08:00
2017-12-15 11:48:15 +08:00
BeatmapOptions.AddButton(@"Delete", @"Beatmap", FontAwesome.fa_trash, colours.Pink, () => delete(Beatmap.Value.BeatmapSetInfo), Key.Number4, float.MaxValue);
2017-03-14 21:51:26 +08:00
}
2017-03-14 20:18:14 +08:00
2017-10-17 10:10:55 +08:00
if (this.beatmaps == null)
this.beatmaps = beatmaps;
2017-04-15 04:22:41 +08:00
if (osu != null)
Ruleset.BindTo(osu.Ruleset);
2017-10-17 10:10:55 +08:00
this.beatmaps.BeatmapSetAdded += onBeatmapSetAdded;
this.beatmaps.BeatmapSetRemoved += onBeatmapSetRemoved;
this.beatmaps.BeatmapHidden += onBeatmapHidden;
this.beatmaps.BeatmapRestored += onBeatmapRestored;
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();
Carousel.BeatmapSets = this.beatmaps.GetAllUsableBeatmapSets();
2017-07-19 12:32:16 +08:00
Beatmap.DisabledChanged += disabled => Carousel.AllowSelection = !disabled;
2017-12-15 11:48:15 +08:00
Beatmap.TriggerChange();
Beatmap.ValueChanged += b =>
{
if (IsCurrentScreen)
Carousel.SelectBeatmap(b?.BeatmapInfo);
2017-12-15 11:48:15 +08:00
};
}
2017-12-12 16:48:38 +08:00
public void Edit(BeatmapInfo beatmap)
2017-10-06 23:59:14 +08:00
{
2017-10-17 10:10:55 +08:00
Beatmap.Value = beatmaps.GetWorkingBeatmap(beatmap, Beatmap);
2017-10-06 23:59:14 +08:00
Push(new Editor());
}
/// <summary>
/// Call to make a selection and perform the default action for this SongSelect.
/// </summary>
/// <param name="beatmap">An optional beatmap to override the current carousel selection.</param>
public void FinaliseSelection(BeatmapInfo beatmap = null)
2017-03-14 21:51:26 +08:00
{
2017-07-21 16:20:52 +08:00
// if we have a pending filter operation, we want to run it now.
// it could change selection (ie. if the ruleset has been changed).
Carousel.FlushPendingFilterOperations();
if (beatmap != null)
Carousel.SelectBeatmap(beatmap);
if (selectionChangedDebounce?.Completed == false)
{
selectionChangedDebounce.RunTask();
selectionChangedDebounce.Cancel(); // cancel the already scheduled task.
selectionChangedDebounce = null;
}
OnSelectionFinalised();
2017-03-14 21:51:26 +08:00
}
2017-12-15 11:48:15 +08:00
/// <summary>
/// Called when a selection is made.
/// </summary>
/// <returns>If a resultant action occurred that takes the user away from SongSelect.</returns>
protected abstract bool OnSelectionFinalised();
2017-12-15 11:48:15 +08:00
2017-07-19 12:32:16 +08:00
private ScheduledDelegate selectionChangedDebounce;
// We need to keep track of the last selected beatmap ignoring debounce to play the correct selection sounds.
private BeatmapInfo beatmapNoDebounce;
/// <summary>
/// selection has been changed as the result of interaction with the carousel.
/// </summary>
private void carouselSelectionChanged(BeatmapInfo beatmap)
{
Action performLoad = delegate
{
// We may be arriving here due to another component changing the bindable Beatmap.
// In these cases, the other component has already loaded the beatmap, so we don't need to do so again.
2017-07-20 14:16:07 +08:00
if (beatmap?.Equals(Beatmap.Value.BeatmapInfo) != true)
{
2017-12-12 16:48:38 +08:00
bool preview = beatmap?.BeatmapSetInfoID != Beatmap.Value?.BeatmapInfo.BeatmapSetInfoID;
2017-07-19 16:56:43 +08:00
2017-10-17 10:10:55 +08:00
Beatmap.Value = beatmaps.GetWorkingBeatmap(beatmap, Beatmap);
ensurePlayingSelected(preview);
}
2017-07-19 16:56:43 +08:00
UpdateBeatmap(Beatmap.Value);
2017-07-19 12:32:16 +08:00
};
if (beatmap?.Equals(beatmapNoDebounce) == true)
return;
selectionChangedDebounce?.Cancel();
beatmapNoDebounce = beatmap;
2017-07-19 16:56:43 +08:00
if (beatmap == null)
2017-11-30 04:09:08 +08:00
performLoad();
2017-07-19 12:32:16 +08:00
else
2017-07-19 16:56:43 +08:00
{
2017-10-14 13:28:25 +08:00
if (beatmap.BeatmapSetInfoID == beatmapNoDebounce?.BeatmapSetInfoID)
2017-07-19 16:56:43 +08:00
sampleChangeDifficulty.Play();
else
sampleChangeBeatmap.Play();
if (beatmap == Beatmap.Value.BeatmapInfo)
performLoad();
else
2017-12-21 23:50:32 +08:00
selectionChangedDebounce = Scheduler.AddDelayed(performLoad, 200);
2017-07-19 16:56:43 +08:00
}
2017-07-19 12:32:16 +08:00
}
private void triggerRandom()
2017-06-05 17:24:28 +08:00
{
if (GetContainingInputManager().CurrentState.Keyboard.ShiftPressed)
Carousel.SelectPreviousRandom();
2017-06-05 17:24:28 +08:00
else
Carousel.SelectNextRandom();
2017-06-05 17:24:28 +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);
Content.FadeInFromZero(250);
FilterControl.Activate();
2016-10-28 18:55:48 +08:00
}
private const double logo_transition = 250;
2017-11-09 16:38:20 +08:00
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
2017-11-09 16:38:20 +08:00
base.LogoArriving(logo, resuming);
logo.RelativePositionAxes = Axes.Both;
Vector2 position = new Vector2(0.95f, 0.96f);
if (logo.Alpha > 0.8f)
{
logo.MoveTo(position, 500, Easing.OutQuint);
}
else
{
logo.Hide();
logo.ScaleTo(0.2f);
logo.MoveTo(position);
}
logo.FadeIn(logo_transition, Easing.OutQuint);
logo.ScaleTo(0.4f, logo_transition, Easing.OutQuint);
logo.Action = () =>
{
FinaliseSelection();
return false;
};
}
2017-11-09 16:38:20 +08:00
protected override void LogoExiting(OsuLogo logo)
{
2017-11-09 16:38:20 +08:00
base.LogoExiting(logo);
logo.ScaleTo(0.2f, logo_transition / 2, Easing.Out);
logo.FadeOut(logo_transition / 2, Easing.Out);
}
2017-02-17 17:59:30 +08:00
protected override void OnResuming(Screen last)
2016-10-28 18:55:48 +08:00
{
2017-07-19 12:32:16 +08:00
if (Beatmap != null && !Beatmap.Value.BeatmapSetInfo.DeletePending)
2017-07-19 09:59:13 +08:00
{
UpdateBeatmap(Beatmap);
2017-07-19 09:59:13 +08:00
ensurePlayingSelected();
}
2016-10-28 18:55:48 +08:00
base.OnResuming(last);
Content.FadeIn(250);
2017-07-23 02:50:25 +08:00
Content.ScaleTo(1, 250, Easing.OutSine);
FilterControl.Activate();
}
2017-02-17 17:59:30 +08:00
protected override void OnSuspending(Screen next)
{
2017-07-23 02:50:25 +08:00
Content.ScaleTo(1.1f, 250, Easing.InSine);
Content.FadeOut(250);
2017-02-08 19:30:05 +08:00
FilterControl.Deactivate();
base.OnSuspending(next);
}
2017-02-17 17:59:30 +08:00
protected override bool OnExiting(Screen next)
{
beatmapInfoWedge.State = Visibility.Hidden;
Content.FadeOut(100);
FilterControl.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-10-17 10:10:55 +08:00
if (beatmaps != null)
{
2017-10-17 10:10:55 +08:00
beatmaps.BeatmapSetAdded -= onBeatmapSetAdded;
beatmaps.BeatmapSetRemoved -= onBeatmapSetRemoved;
beatmaps.BeatmapHidden -= onBeatmapHidden;
beatmaps.BeatmapRestored -= onBeatmapRestored;
}
initialAddSetsTask?.Cancel();
2016-10-06 22:33:28 +08:00
}
/// <summary>
/// Allow components in SongSelect to update their loaded beatmap details.
/// This is a debounced call (unlike directly binding to WorkingBeatmap.ValueChanged).
/// </summary>
/// <param name="beatmap">The working beatmap.</param>
protected virtual void UpdateBeatmap(WorkingBeatmap beatmap)
2016-10-26 22:52:04 +08:00
{
2017-12-15 11:48:15 +08:00
if (Background is BackgroundScreenBeatmap backgroundModeBeatmap)
{
backgroundModeBeatmap.Beatmap = beatmap;
2017-11-26 04:08:20 +08:00
backgroundModeBeatmap.BlurTo(background_blur, 750, Easing.OutQuint);
2017-02-22 20:43:29 +08:00
backgroundModeBeatmap.FadeTo(1, 250);
}
2017-03-14 04:59:44 +08:00
beatmapInfoWedge.UpdateBeatmap(beatmap);
}
private void ensurePlayingSelected(bool preview = false)
2016-10-28 18:55:48 +08:00
{
Track track = Beatmap.Value.Track;
2016-10-28 18:55:48 +08:00
if (!track.IsRunning)
2016-10-28 18:55:48 +08:00
{
// Ensure the track is added to the TrackManager, since it is removed after the player finishes the map.
// Using AddItemToList rather than AddItem so that it doesn't attempt to register adjustment dependencies more than once.
Game.Audio.Track.AddItemToList(track);
if (preview) track.Seek(Beatmap.Value.Metadata.PreviewTime);
track.Start();
}
2016-10-28 18:55:48 +08:00
}
private void onBeatmapSetAdded(BeatmapSetInfo s) => Carousel.UpdateBeatmapSet(s);
private void onBeatmapSetRemoved(BeatmapSetInfo s) => Carousel.RemoveBeatmapSet(s);
private void onBeatmapRestored(BeatmapInfo b) => Carousel.UpdateBeatmapSet(beatmaps.QueryBeatmapSet(s => s.ID == b.BeatmapSetInfoID));
private void onBeatmapHidden(BeatmapInfo b) => Carousel.UpdateBeatmapSet(beatmaps.QueryBeatmapSet(s => s.ID == b.BeatmapSetInfoID));
2017-08-30 19:41:41 +08:00
2017-12-15 11:48:15 +08:00
private void carouselBeatmapsLoaded()
{
if (!Beatmap.IsDefault && Beatmap.Value.BeatmapSetInfo?.DeletePending == false)
{
Carousel.SelectBeatmap(Beatmap.Value.BeatmapInfo);
}
else if (Carousel.SelectedBeatmapSet == null)
{
if (!Carousel.SelectNextRandom())
// in the case random selection failed, we want to trigger selectionChanged
// to show the dummy beatmap (we have nothing else to display).
carouselSelectionChanged(null);
}
2017-12-15 11:48:15 +08:00
}
private void delete(BeatmapSetInfo beatmap)
{
if (beatmap == null) return;
2017-08-30 19:41:41 +08:00
dialogOverlay?.Push(new BeatmapDeleteDialog(beatmap));
2017-03-02 20:40:55 +08:00
}
2016-12-15 19:55:37 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
2017-03-14 21:51:26 +08:00
if (args.Repeat) return false;
switch (args.Key)
2016-12-15 19:55:37 +08:00
{
case Key.KeypadEnter:
2017-03-14 21:51:26 +08:00
case Key.Enter:
FinaliseSelection();
2017-03-14 21:51:26 +08:00
return true;
case Key.Delete:
if (state.Keyboard.ShiftPressed)
{
2017-08-30 19:41:41 +08:00
if (!Beatmap.IsDefault)
2017-12-15 11:48:15 +08:00
delete(Beatmap.Value.BeatmapSetInfo);
2017-03-14 21:51:26 +08:00
return true;
}
2017-12-13 11:46:02 +08:00
2017-03-14 21:51:26 +08:00
break;
2016-12-15 19:55:37 +08:00
}
return base.OnKeyDown(state, args);
}
2017-07-29 22:33:20 +08:00
2017-07-31 19:20:12 +08:00
private class ResetScrollContainer : Container
2017-07-29 22:33:20 +08:00
{
2017-07-31 19:20:12 +08:00
private readonly Action onHoverAction;
public ResetScrollContainer(Action onHoverAction)
{
this.onHoverAction = onHoverAction;
}
2017-07-29 22:33:20 +08:00
protected override bool OnHover(InputState state)
{
2017-07-31 19:20:12 +08:00
onHoverAction?.Invoke();
2017-07-29 22:33:20 +08:00
return base.OnHover(state);
}
}
2016-09-29 19:13:58 +08:00
}
}