1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 13:13:22 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/DrawableCarouselBeatmapSet.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

282 lines
9.6 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
2017-01-30 12:35:40 +08:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-08-30 19:41:41 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Utils;
2017-12-12 16:48:38 +08:00
using osu.Game.Beatmaps;
2020-09-02 20:08:31 +08:00
using osu.Game.Collections;
2017-08-30 19:41:41 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
namespace osu.Game.Screens.Select.Carousel
2016-10-27 10:55:55 +08:00
{
2017-12-12 16:48:38 +08:00
public class DrawableCarouselBeatmapSet : DrawableCarouselItem, IHasContextMenu
{
public const float HEIGHT = MAX_HEIGHT;
2017-12-18 10:13:16 +08:00
private Action<BeatmapSetInfo> restoreHiddenRequested;
private Action<int> viewDetails;
2018-04-13 17:19:50 +08:00
2020-02-14 21:59:51 +08:00
[Resolved(CanBeNull = true)]
private IDialogOverlay dialogOverlay { get; set; }
2020-02-14 21:30:27 +08:00
2020-09-09 14:39:15 +08:00
[Resolved(CanBeNull = true)]
private CollectionManager collectionManager { get; set; }
2020-09-02 20:08:31 +08:00
2020-09-05 02:52:07 +08:00
[Resolved(CanBeNull = true)]
2020-09-07 20:08:48 +08:00
private ManageCollectionsDialog manageCollectionsDialog { get; set; }
2020-09-05 02:52:07 +08:00
public IEnumerable<DrawableCarouselItem> DrawableBeatmaps => beatmapContainer?.IsLoaded != true ? Enumerable.Empty<DrawableCarouselItem>() : beatmapContainer.AliveChildren;
[CanBeNull]
private Container<DrawableCarouselItem> beatmapContainer;
private BeatmapSetInfo beatmapSet;
[CanBeNull]
private Task beatmapsLoadTask;
2020-10-12 14:36:03 +08:00
[Resolved]
private BeatmapManager manager { get; set; }
2018-04-13 17:19:50 +08:00
protected override void FreeAfterUse()
{
base.FreeAfterUse();
Item = null;
ClearTransforms();
}
[BackgroundDependencyLoader(true)]
2020-10-12 14:36:03 +08:00
private void load(BeatmapSetOverlay beatmapOverlay)
{
restoreHiddenRequested = s =>
{
foreach (var b in s.Beatmaps)
manager.Restore(b);
};
if (beatmapOverlay != null)
viewDetails = beatmapOverlay.FetchAndShowBeatmapSet;
2020-10-12 14:36:03 +08:00
}
protected override void Update()
{
base.Update();
// position updates should not occur if the item is filtered away.
// this avoids panels flying across the screen only to be eventually off-screen or faded out.
if (!Item.Visible)
return;
float targetY = Item.CarouselYPosition;
if (Precision.AlmostEquals(targetY, Y))
Y = targetY;
else
// algorithm for this is taken from ScrollContainer.
// while it doesn't necessarily need to match 1:1, as we are emulating scroll in some cases this feels most correct.
Y = (float)Interpolation.Lerp(targetY, Y, Math.Exp(-0.01 * Time.Elapsed));
}
2020-10-12 14:36:03 +08:00
protected override void UpdateItem()
{
base.UpdateItem();
Content.Clear();
beatmapContainer = null;
beatmapsLoadTask = null;
if (Item == null)
return;
beatmapSet = ((CarouselBeatmapSet)Item).BeatmapSet;
DelayedLoadWrapper background;
DelayedLoadWrapper mainFlow;
Header.Children = new Drawable[]
{
background = new DelayedLoadWrapper(() => new SetPanelBackground(manager.GetWorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault()))
{
RelativeSizeAxes = Axes.Both,
}, 300)
{
RelativeSizeAxes = Axes.Both
},
mainFlow = new DelayedLoadWrapper(() => new SetPanelContent((CarouselBeatmapSet)Item), 100)
{
RelativeSizeAxes = Axes.Both
},
};
background.DelayedLoadComplete += fadeContentIn;
mainFlow.DelayedLoadComplete += fadeContentIn;
2022-01-30 13:07:29 +08:00
}
private void fadeContentIn(Drawable d) => d.FadeInFromZero(750, Easing.OutQuint);
protected override void Deselected()
{
base.Deselected();
2020-10-12 17:19:10 +08:00
MovementContainer.MoveToX(0, 500, Easing.OutExpo);
updateBeatmapYPositions();
}
protected override void Selected()
{
base.Selected();
2020-10-12 14:36:03 +08:00
MovementContainer.MoveToX(-100, 500, Easing.OutExpo);
2020-10-12 14:36:03 +08:00
updateBeatmapDifficulties();
}
private void updateBeatmapDifficulties()
{
var carouselBeatmapSet = (CarouselBeatmapSet)Item;
var visibleBeatmaps = carouselBeatmapSet.Children.Where(c => c.Visible).ToArray();
// if we are already displaying all the correct beatmaps, only run animation updates.
// note that the displayed beatmaps may change due to the applied filter.
// a future optimisation could add/remove only changed difficulties rather than reinitialise.
if (beatmapContainer != null && visibleBeatmaps.Length == beatmapContainer.Count && visibleBeatmaps.All(b => beatmapContainer.Any(c => c.Item == b)))
{
updateBeatmapYPositions();
}
else
{
// on selection we show our child beatmaps.
// for now this is a simple drawable construction each selection.
// can be improved in the future.
beatmapContainer = new Container<DrawableCarouselItem>
{
X = 100,
RelativeSizeAxes = Axes.Both,
ChildrenEnumerable = visibleBeatmaps.Select(c => c.CreateDrawableRepresentation())
};
2020-10-12 14:36:03 +08:00
beatmapsLoadTask = LoadComponentAsync(beatmapContainer, loaded =>
{
// make sure the pooled target hasn't changed.
if (beatmapContainer != loaded)
return;
Content.Child = loaded;
updateBeatmapYPositions();
});
}
}
private void updateBeatmapYPositions()
{
if (beatmapContainer == null)
return;
if (beatmapsLoadTask == null || !beatmapsLoadTask.IsCompleted)
return;
float yPos = DrawableCarouselBeatmap.CAROUSEL_BEATMAP_SPACING;
bool isSelected = Item.State.Value == CarouselItemState.Selected;
foreach (var panel in beatmapContainer.Children)
{
if (isSelected)
{
panel.MoveToY(yPos, 800, Easing.OutQuint);
yPos += panel.Item.TotalHeight;
}
else
panel.MoveToY(0, 800, Easing.OutQuint);
}
}
2018-04-13 17:19:50 +08:00
2017-12-13 11:46:02 +08:00
public MenuItem[] ContextMenuItems
{
get
{
Debug.Assert(beatmapSet != null);
2017-12-13 11:46:02 +08:00
List<MenuItem> items = new List<MenuItem>();
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
if (Item.State.Value == CarouselItemState.NotSelected)
2017-12-13 11:46:02 +08:00
items.Add(new OsuMenuItem("Expand", MenuItemType.Highlighted, () => Item.State.Value = CarouselItemState.Selected));
2018-04-13 17:19:50 +08:00
if (beatmapSet.OnlineID > 0 && viewDetails != null)
items.Add(new OsuMenuItem("Details...", MenuItemType.Standard, () => viewDetails(beatmapSet.OnlineID)));
2018-04-13 17:19:50 +08:00
2020-09-09 14:39:15 +08:00
if (collectionManager != null)
{
var collectionItems = collectionManager.Collections.Select(createCollectionMenuItem).ToList();
if (manageCollectionsDialog != null)
collectionItems.Add(new OsuMenuItem("Manage...", MenuItemType.Standard, manageCollectionsDialog.Show));
2020-09-05 02:52:07 +08:00
2020-09-09 14:39:15 +08:00
items.Add(new OsuMenuItem("Collections") { Items = collectionItems });
}
2020-09-02 20:08:31 +08:00
2020-09-08 11:04:35 +08:00
if (beatmapSet.Beatmaps.Any(b => b.Hidden))
items.Add(new OsuMenuItem("Restore all hidden", MenuItemType.Standard, () => restoreHiddenRequested(beatmapSet)));
if (dialogOverlay != null)
items.Add(new OsuMenuItem("Delete...", MenuItemType.Destructive, () => dialogOverlay.Push(new BeatmapDeleteDialog(beatmapSet))));
2017-12-13 11:46:02 +08:00
return items.ToArray();
}
}
2018-04-13 17:19:50 +08:00
2020-09-02 20:08:31 +08:00
private MenuItem createCollectionMenuItem(BeatmapCollection collection)
{
Debug.Assert(beatmapSet != null);
2020-09-02 20:08:31 +08:00
TernaryState state;
int countExisting = beatmapSet.Beatmaps.Count(b => collection.Beatmaps.Contains(b));
2020-09-02 20:08:31 +08:00
if (countExisting == beatmapSet.Beatmaps.Count)
state = TernaryState.True;
else if (countExisting > 0)
state = TernaryState.Indeterminate;
else
state = TernaryState.False;
return new TernaryStateToggleMenuItem(collection.Name.Value, MenuItemType.Standard, s =>
2020-09-02 20:08:31 +08:00
{
foreach (var b in beatmapSet.Beatmaps)
{
switch (s)
{
case TernaryState.True:
if (collection.Beatmaps.Contains(b))
continue;
collection.Beatmaps.Add(b);
break;
case TernaryState.False:
collection.Beatmaps.Remove(b);
break;
}
}
})
{
State = { Value = state }
};
}
2016-10-27 10:55:55 +08:00
}
}