1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 01:47:24 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/DrawableCarouselBeatmap.cs

281 lines
11 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;
using System.Collections.Generic;
2020-09-02 20:08:31 +08:00
using System.Linq;
using System.Threading;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
2020-09-02 20:08:31 +08:00
using osu.Game.Collections;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select.Carousel
{
public class DrawableCarouselBeatmap : DrawableCarouselItem, IHasContextMenu
{
2020-10-12 19:26:20 +08:00
public const float CAROUSEL_BEATMAP_SPACING = 5;
/// <summary>
/// The height of a carousel beatmap, including vertical spacing.
/// </summary>
public const float HEIGHT = height + CAROUSEL_BEATMAP_SPACING;
private const float height = MAX_HEIGHT * 0.6f;
2021-10-02 23:55:29 +08:00
private readonly BeatmapInfo beatmapInfo;
2018-04-13 17:19:50 +08:00
private Sprite background;
private Action<BeatmapInfo> startRequested;
private Action<BeatmapInfo> editRequested;
private Action<BeatmapInfo> hideRequested;
private Triangles triangles;
private StarCounter starCounter;
2020-02-14 21:59:51 +08:00
[Resolved(CanBeNull = true)]
2020-02-14 21:14:00 +08:00
private BeatmapSetOverlay beatmapOverlay { get; set; }
[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; }
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
private IBindable<StarDifficulty?> starDifficultyBindable;
2020-07-21 22:13:04 +08:00
private CancellationTokenSource starDifficultyCancellationSource;
2019-02-28 12:31:40 +08:00
public DrawableCarouselBeatmap(CarouselBeatmap panel)
2018-04-13 17:19:50 +08:00
{
2021-10-02 23:55:29 +08:00
beatmapInfo = panel.BeatmapInfo;
2020-10-12 14:36:03 +08:00
Item = panel;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader(true)]
private void load(BeatmapManager manager, SongSelect songSelect)
2018-04-13 17:19:50 +08:00
{
Header.Height = height;
2018-04-13 17:19:50 +08:00
if (songSelect != null)
{
startRequested = b => songSelect.FinaliseSelection(b);
if (songSelect.AllowEditing)
editRequested = songSelect.Edit;
2018-04-13 17:19:50 +08:00
}
if (manager != null)
hideRequested = manager.Hide;
Header.Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
triangles = new Triangles
{
TriangleScale = 2,
RelativeSizeAxes = Axes.Both,
ColourLight = Color4Extensions.FromHex(@"3a7285"),
ColourDark = Color4Extensions.FromHex(@"123744")
2018-04-13 17:19:50 +08:00
},
new FillFlowContainer
{
Padding = new MarginPadding(5),
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Children = new Drawable[]
{
2021-10-02 23:55:29 +08:00
new DifficultyIcon(beatmapInfo, shouldShowTooltip: false)
2018-04-13 17:19:50 +08:00
{
Scale = new Vector2(1.8f),
},
new FillFlowContainer
{
Padding = new MarginPadding { Left = 5 },
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
new FillFlowContainer
{
Direction = FillDirection.Horizontal,
Spacing = new Vector2(4, 0),
AutoSizeAxes = Axes.Both,
Children = new[]
{
new OsuSpriteText
{
2021-10-02 23:55:29 +08:00
Text = beatmapInfo.Version,
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 20),
2018-04-13 17:19:50 +08:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft
},
new OsuSpriteText
{
Text = "mapped by",
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft
},
new OsuSpriteText
{
Text = $"{(beatmapInfo.Metadata ?? beatmapInfo.BeatmapSet.Metadata).Author?.Username ?? string.Empty}",
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(italics: true),
2018-04-13 17:19:50 +08:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft
},
}
},
new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
Direction = FillDirection.Horizontal,
Spacing = new Vector2(4, 0),
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
2021-10-02 23:55:29 +08:00
new TopLocalRank(beatmapInfo)
{
Scale = new Vector2(0.8f),
Size = new Vector2(40, 20)
},
starCounter = new StarCounter
{
Scale = new Vector2(0.8f),
}
}
2018-04-13 17:19:50 +08:00
}
}
}
}
}
};
}
protected override void Selected()
{
base.Selected();
MovementContainer.MoveToX(-50, 500, Easing.OutExpo);
2018-04-13 17:19:50 +08:00
background.Colour = ColourInfo.GradientVertical(
new Color4(20, 43, 51, 255),
new Color4(40, 86, 102, 255));
triangles.Colour = Color4.White;
}
protected override void Deselected()
{
base.Deselected();
MovementContainer.MoveToX(0, 500, Easing.OutExpo);
2018-04-13 17:19:50 +08:00
background.Colour = new Color4(20, 43, 51, 255);
triangles.Colour = OsuColour.Gray(0.5f);
}
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
2019-02-21 17:56:34 +08:00
if (Item.State.Value == CarouselItemState.Selected)
2021-10-02 23:55:29 +08:00
startRequested?.Invoke(beatmapInfo);
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
return base.OnClick(e);
2018-04-13 17:19:50 +08:00
}
protected override void ApplyState()
{
if (Item.State.Value != CarouselItemState.Collapsed && Alpha == 0)
starCounter.ReplayAnimation();
2020-07-23 22:18:43 +08:00
starDifficultyCancellationSource?.Cancel();
2020-07-21 22:13:04 +08:00
2020-07-23 22:18:43 +08:00
// Only compute difficulty when the item is visible.
if (Item.State.Value != CarouselItemState.Collapsed)
{
2020-07-21 22:13:04 +08:00
// We've potentially cancelled the computation above so a new bindable is required.
2021-10-02 23:55:29 +08:00
starDifficultyBindable = difficultyCache.GetBindableDifficulty(beatmapInfo, (starDifficultyCancellationSource = new CancellationTokenSource()).Token);
starDifficultyBindable.BindValueChanged(d =>
{
starCounter.Current = (float)(d.NewValue?.Stars ?? 0);
}, true);
2020-07-21 22:13:04 +08:00
}
2018-04-13 17:19:50 +08:00
base.ApplyState();
}
public MenuItem[] ContextMenuItems
2018-04-13 17:19:50 +08:00
{
get
{
List<MenuItem> items = new List<MenuItem>();
2019-04-03 22:40:20 +08:00
if (startRequested != null)
2021-10-02 23:55:29 +08:00
items.Add(new OsuMenuItem("Play", MenuItemType.Highlighted, () => startRequested(beatmapInfo)));
if (editRequested != null)
2021-10-02 23:55:29 +08:00
items.Add(new OsuMenuItem("Edit", MenuItemType.Standard, () => editRequested(beatmapInfo)));
2021-10-02 23:55:29 +08:00
if (beatmapInfo.OnlineBeatmapID.HasValue && beatmapOverlay != null)
items.Add(new OsuMenuItem("Details...", MenuItemType.Standard, () => beatmapOverlay.FetchAndShowBeatmap(beatmapInfo.OnlineBeatmapID.Value)));
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 (hideRequested != null)
2021-10-02 23:55:29 +08:00
items.Add(new OsuMenuItem("Hide", MenuItemType.Destructive, () => hideRequested(beatmapInfo)));
2020-09-08 11:04:35 +08:00
return items.ToArray();
}
}
2020-07-21 22:13:04 +08:00
2020-09-02 20:08:31 +08:00
private MenuItem createCollectionMenuItem(BeatmapCollection collection)
{
2020-09-05 03:43:51 +08:00
return new ToggleMenuItem(collection.Name.Value, MenuItemType.Standard, s =>
2020-09-02 20:08:31 +08:00
{
if (s)
2021-10-02 23:55:29 +08:00
collection.Beatmaps.Add(beatmapInfo);
2020-09-02 20:08:31 +08:00
else
2021-10-02 23:55:29 +08:00
collection.Beatmaps.Remove(beatmapInfo);
2020-09-02 20:08:31 +08:00
})
{
2021-10-02 23:55:29 +08:00
State = { Value = collection.Beatmaps.Contains(beatmapInfo) }
2020-09-02 20:08:31 +08:00
};
}
2020-07-21 22:13:04 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
starDifficultyCancellationSource?.Cancel();
}
2018-04-13 17:19:50 +08:00
}
}