1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 03:22:54 +08:00

General refactorings

This commit is contained in:
smoogipoo 2020-09-07 21:08:48 +09:00
parent c1d255a04c
commit 98e9c4dc25
10 changed files with 95 additions and 40 deletions

View File

@ -18,7 +18,7 @@ namespace osu.Game.Tests.Visual.Collections
{ {
Children = new Drawable[] Children = new Drawable[]
{ {
new ManageCollectionDialog { State = { Value = Visibility.Visible } }, new ManageCollectionsDialog { State = { Value = Visibility.Visible } },
dialogOverlay = new DialogOverlay() dialogOverlay = new DialogOverlay()
}; };
} }

View File

@ -0,0 +1,47 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
namespace osu.Game.Collections
{
/// <summary>
/// A collection of beatmaps grouped by a name.
/// </summary>
public class BeatmapCollection
{
/// <summary>
/// Invoked whenever any change occurs on this <see cref="BeatmapCollection"/>.
/// </summary>
public event Action Changed;
/// <summary>
/// The collection's name.
/// </summary>
public readonly Bindable<string> Name = new Bindable<string>();
/// <summary>
/// The beatmaps contained by the collection.
/// </summary>
public readonly BindableList<BeatmapInfo> Beatmaps = new BindableList<BeatmapInfo>();
/// <summary>
/// The date when this collection was last modified.
/// </summary>
public DateTimeOffset LastModifyDate { get; private set; } = DateTimeOffset.UtcNow;
public BeatmapCollection()
{
Beatmaps.CollectionChanged += (_, __) => onChange();
Name.ValueChanged += _ => onChange();
}
private void onChange()
{
LastModifyDate = DateTimeOffset.Now;
Changed?.Invoke();
}
}
}

View File

@ -21,7 +21,7 @@ namespace osu.Game.Collections
public class BeatmapCollectionManager : CompositeDrawable public class BeatmapCollectionManager : CompositeDrawable
{ {
/// <summary> /// <summary>
/// Database version in YYYYMMDD format (matching stable). /// Database version in stable-compatible YYYYMMDD format.
/// </summary> /// </summary>
private const int database_version = 30000000; private const int database_version = 30000000;
@ -213,29 +213,4 @@ namespace osu.Game.Collections
save(); save();
} }
} }
public class BeatmapCollection
{
/// <summary>
/// Invoked whenever any change occurs on this <see cref="BeatmapCollection"/>.
/// </summary>
public event Action Changed;
public readonly Bindable<string> Name = new Bindable<string>();
public readonly BindableList<BeatmapInfo> Beatmaps = new BindableList<BeatmapInfo>();
public DateTimeOffset LastModifyTime { get; private set; }
public BeatmapCollection()
{
LastModifyTime = DateTimeOffset.UtcNow;
Beatmaps.CollectionChanged += (_, __) =>
{
LastModifyTime = DateTimeOffset.Now;
Changed?.Invoke();
};
}
}
} }

View File

@ -13,7 +13,7 @@ using osuTK;
namespace osu.Game.Collections namespace osu.Game.Collections
{ {
public class ManageCollectionDialog : OsuFocusedOverlayContainer public class ManageCollectionsDialog : OsuFocusedOverlayContainer
{ {
private const double enter_duration = 500; private const double enter_duration = 500;
private const double exit_duration = 200; private const double exit_duration = 200;
@ -21,7 +21,7 @@ namespace osu.Game.Collections
[Resolved] [Resolved]
private BeatmapCollectionManager collectionManager { get; set; } private BeatmapCollectionManager collectionManager { get; set; }
public ManageCollectionDialog() public ManageCollectionsDialog()
{ {
Anchor = Anchor.Centre; Anchor = Anchor.Centre;
Origin = Anchor.Centre; Origin = Anchor.Centre;

View File

@ -618,7 +618,7 @@ namespace osu.Game
loadComponentSingleFile(CreateUpdateManager(), Add, true); loadComponentSingleFile(CreateUpdateManager(), Add, true);
// overlay elements // overlay elements
loadComponentSingleFile(new ManageCollectionDialog(), overlayContent.Add, true); loadComponentSingleFile(new ManageCollectionsDialog(), overlayContent.Add, true);
loadComponentSingleFile(beatmapListing = new BeatmapListingOverlay(), overlayContent.Add, true); loadComponentSingleFile(beatmapListing = new BeatmapListingOverlay(), overlayContent.Add, true);
loadComponentSingleFile(dashboard = new DashboardOverlay(), overlayContent.Add, true); loadComponentSingleFile(dashboard = new DashboardOverlay(), overlayContent.Add, true);
loadComponentSingleFile(news = new NewsOverlay(), overlayContent.Add, true); loadComponentSingleFile(news = new NewsOverlay(), overlayContent.Add, true);

View File

@ -52,7 +52,7 @@ namespace osu.Game.Screens.Select.Carousel
private BeatmapCollectionManager collectionManager { get; set; } private BeatmapCollectionManager collectionManager { get; set; }
[Resolved(CanBeNull = true)] [Resolved(CanBeNull = true)]
private ManageCollectionDialog manageCollectionDialog { get; set; } private ManageCollectionsDialog manageCollectionsDialog { get; set; }
private IBindable<StarDifficulty> starDifficultyBindable; private IBindable<StarDifficulty> starDifficultyBindable;
private CancellationTokenSource starDifficultyCancellationSource; private CancellationTokenSource starDifficultyCancellationSource;
@ -227,9 +227,9 @@ namespace osu.Game.Screens.Select.Carousel
if (beatmap.OnlineBeatmapID.HasValue && beatmapOverlay != null) if (beatmap.OnlineBeatmapID.HasValue && beatmapOverlay != null)
items.Add(new OsuMenuItem("Details", MenuItemType.Standard, () => beatmapOverlay.FetchAndShowBeatmap(beatmap.OnlineBeatmapID.Value))); items.Add(new OsuMenuItem("Details", MenuItemType.Standard, () => beatmapOverlay.FetchAndShowBeatmap(beatmap.OnlineBeatmapID.Value)));
var collectionItems = collectionManager.Collections.OrderByDescending(c => c.LastModifyTime).Take(3).Select(createCollectionMenuItem).ToList(); var collectionItems = collectionManager.Collections.OrderByDescending(c => c.LastModifyDate).Take(3).Select(createCollectionMenuItem).ToList();
if (manageCollectionDialog != null) if (manageCollectionsDialog != null)
collectionItems.Add(new OsuMenuItem("More...", MenuItemType.Standard, manageCollectionDialog.Show)); collectionItems.Add(new OsuMenuItem("More...", MenuItemType.Standard, manageCollectionsDialog.Show));
items.Add(new OsuMenuItem("Add to...") { Items = collectionItems }); items.Add(new OsuMenuItem("Add to...") { Items = collectionItems });

View File

@ -39,7 +39,7 @@ namespace osu.Game.Screens.Select.Carousel
private BeatmapCollectionManager collectionManager { get; set; } private BeatmapCollectionManager collectionManager { get; set; }
[Resolved(CanBeNull = true)] [Resolved(CanBeNull = true)]
private ManageCollectionDialog manageCollectionDialog { get; set; } private ManageCollectionsDialog manageCollectionsDialog { get; set; }
private readonly BeatmapSetInfo beatmapSet; private readonly BeatmapSetInfo beatmapSet;
@ -148,9 +148,9 @@ namespace osu.Game.Screens.Select.Carousel
if (dialogOverlay != null) if (dialogOverlay != null)
items.Add(new OsuMenuItem("Delete", MenuItemType.Destructive, () => dialogOverlay.Push(new BeatmapDeleteDialog(beatmapSet)))); items.Add(new OsuMenuItem("Delete", MenuItemType.Destructive, () => dialogOverlay.Push(new BeatmapDeleteDialog(beatmapSet))));
var collectionItems = collectionManager.Collections.OrderByDescending(c => c.LastModifyTime).Take(3).Select(createCollectionMenuItem).ToList(); var collectionItems = collectionManager.Collections.OrderByDescending(c => c.LastModifyDate).Take(3).Select(createCollectionMenuItem).ToList();
if (manageCollectionDialog != null) if (manageCollectionsDialog != null)
collectionItems.Add(new OsuMenuItem("More...", MenuItemType.Standard, manageCollectionDialog.Show)); collectionItems.Add(new OsuMenuItem("More...", MenuItemType.Standard, manageCollectionsDialog.Show));
items.Add(new OsuMenuItem("Add all to...") { Items = collectionItems }); items.Add(new OsuMenuItem("Add all to...") { Items = collectionItems });

View File

@ -3,21 +3,45 @@
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Bindables;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Collections; using osu.Game.Collections;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
/// <summary>
/// A <see cref="BeatmapCollection"/> filter.
/// </summary>
public class CollectionFilter public class CollectionFilter
{ {
/// <summary>
/// The collection to filter beatmaps from.
/// May be null to not filter by collection (include all beatmaps).
/// </summary>
[CanBeNull] [CanBeNull]
public readonly BeatmapCollection Collection; public readonly BeatmapCollection Collection;
/// <summary>
/// The name of the collection.
/// </summary>
[NotNull]
public readonly Bindable<string> CollectionName;
/// <summary>
/// Creates a new <see cref="CollectionFilter"/>.
/// </summary>
/// <param name="collection">The collection to filter beatmaps from.</param>
public CollectionFilter([CanBeNull] BeatmapCollection collection) public CollectionFilter([CanBeNull] BeatmapCollection collection)
{ {
Collection = collection; Collection = collection;
CollectionName = Collection?.Name.GetBoundCopy() ?? new Bindable<string>("All beatmaps");
} }
/// <summary>
/// Whether the collection contains a given beatmap.
/// </summary>
/// <param name="beatmap">The beatmap to check.</param>
/// <returns>Whether <see cref="Collection"/> contains <paramref name="beatmap"/>.</returns>
public virtual bool ContainsBeatmap(BeatmapInfo beatmap) public virtual bool ContainsBeatmap(BeatmapInfo beatmap)
=> Collection?.Beatmaps.Any(b => b.Equals(beatmap)) ?? true; => Collection?.Beatmaps.Any(b => b.Equals(beatmap)) ?? true;
} }

View File

@ -19,6 +19,9 @@ using osuTK;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
/// <summary>
/// A dropdown to select the <see cref="CollectionFilter"/> to filter beatmaps using.
/// </summary>
public class CollectionFilterDropdown : OsuDropdown<CollectionFilter> public class CollectionFilterDropdown : OsuDropdown<CollectionFilter>
{ {
private readonly IBindableList<BeatmapCollection> collections = new BindableList<BeatmapCollection>(); private readonly IBindableList<BeatmapCollection> collections = new BindableList<BeatmapCollection>();
@ -64,6 +67,7 @@ namespace osu.Game.Screens.Select
/// </summary> /// </summary>
private void filterChanged(ValueChangedEvent<CollectionFilter> filter) private void filterChanged(ValueChangedEvent<CollectionFilter> filter)
{ {
// Binding the beatmaps will trigger a collection change event, which results in an infinite-loop. This is rebound later, when it's safe to do so.
beatmaps.CollectionChanged -= filterBeatmapsChanged; beatmaps.CollectionChanged -= filterBeatmapsChanged;
if (filter.OldValue?.Collection != null) if (filter.OldValue?.Collection != null)
@ -122,7 +126,7 @@ namespace osu.Game.Screens.Select
private void updateBindable() private void updateBindable()
{ {
collectionName.UnbindAll(); collectionName.UnbindAll();
collectionName.BindTo(SelectedItem.Value.Collection?.Name ?? new Bindable<string>("All beatmaps")); collectionName.BindTo(SelectedItem.Value.CollectionName);
collectionName.BindValueChanged(_ => updateText(), true); collectionName.BindValueChanged(_ => updateText(), true);
} }
@ -164,7 +168,7 @@ namespace osu.Game.Screens.Select
: base(item) : base(item)
{ {
collectionBeatmaps = Item.Collection?.Beatmaps.GetBoundCopy(); collectionBeatmaps = Item.Collection?.Beatmaps.GetBoundCopy();
collectionName = Item.Collection?.Name.GetBoundCopy() ?? new Bindable<string>("All beatmaps"); collectionName = Item.CollectionName.GetBoundCopy();
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Screens.Select.Filter; using osu.Game.Screens.Select.Filter;
@ -51,6 +52,10 @@ namespace osu.Game.Screens.Select
} }
} }
/// <summary>
/// The collection to filter beatmaps from.
/// </summary>
[CanBeNull]
public CollectionFilter Collection; public CollectionFilter Collection;
public struct OptionalRange<T> : IEquatable<OptionalRange<T>> public struct OptionalRange<T> : IEquatable<OptionalRange<T>>