1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 00:33:21 +08:00
osu-lazer/osu.Game/Overlays/Music/PlaylistItem.cs

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

139 lines
4.4 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
2022-06-17 15:37:17 +08:00
#nullable disable
2017-05-01 14:03:11 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2017-05-01 14:03:11 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
2017-07-26 12:22:46 +08:00
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2017-05-01 14:03:11 +08:00
namespace osu.Game.Overlays.Music
{
2022-01-26 14:12:01 +08:00
public partial class PlaylistItem : OsuRearrangeableListItem<Live<BeatmapSetInfo>>, IFilterable
{
2022-01-26 14:12:01 +08:00
public readonly Bindable<Live<BeatmapSetInfo>> SelectedSet = new Bindable<Live<BeatmapSetInfo>>();
2022-01-26 14:12:01 +08:00
public Action<Live<BeatmapSetInfo>> RequestSelection;
private TextFlowContainer text;
private ITextPart titlePart;
[Resolved]
private OsuColour colours { get; set; }
2022-01-26 14:12:01 +08:00
public PlaylistItem(Live<BeatmapSetInfo> item)
: base(item)
{
Padding = new MarginPadding { Left = 5 };
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
HandleColour = colours.Gray5;
}
protected override void LoadComplete()
{
base.LoadComplete();
Model.PerformRead(m =>
{
var metadata = m.Metadata;
var title = new RomanisableString(metadata.TitleUnicode, metadata.Title);
var artist = new RomanisableString(metadata.ArtistUnicode, metadata.Artist);
titlePart = text.AddText(title, sprite => sprite.Font = OsuFont.GetFont(weight: FontWeight.Regular));
2023-05-02 13:11:16 +08:00
titlePart.DrawablePartsRecreated += _ => updateSelectionState(SelectedSet.Value, applyImmediately: true);
text.AddText(@" "); // to separate the title from the artist.
text.AddText(artist, sprite =>
{
sprite.Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold);
sprite.Colour = colours.Gray9;
sprite.Padding = new MarginPadding { Top = 1 };
});
2023-05-02 13:11:16 +08:00
SelectedSet.BindValueChanged(set => updateSelectionState(set.NewValue));
updateSelectionState(SelectedSet.Value, applyImmediately: true);
});
}
private bool selected;
2023-05-02 13:11:16 +08:00
private void updateSelectionState(Live<BeatmapSetInfo> selectedSet, bool applyImmediately = false)
{
2023-05-02 13:11:16 +08:00
bool wasSelected = selected;
selected = selectedSet?.Equals(Model) == true;
2023-05-02 13:11:16 +08:00
// Immediate updates should forcibly set correct state regardless of previous state.
// This ensures that the initial state is correctly applied.
if (wasSelected == selected && !applyImmediately)
return;
foreach (Drawable s in titlePart.Drawables)
2023-05-02 13:11:16 +08:00
s.FadeColour(selected ? colours.Yellow : Color4.White, applyImmediately ? 0 : FADE_DURATION);
}
protected override Drawable CreateContent() => new DelayedLoadWrapper(text = new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
});
protected override bool OnClick(ClickEvent e)
{
RequestSelection?.Invoke(Model);
return true;
}
private bool inSelectedCollection = true;
public bool InSelectedCollection
{
get => inSelectedCollection;
set
{
if (inSelectedCollection == value)
return;
inSelectedCollection = value;
updateFilter();
}
}
public IEnumerable<LocalisableString> FilterTerms => Model.PerformRead(m => m.Metadata.GetSearchableTerms()).Select(s => (LocalisableString)s).ToArray();
2018-04-13 17:19:50 +08:00
private bool matchingFilter = true;
2018-04-13 17:19:50 +08:00
public bool MatchingFilter
{
get => matchingFilter && inSelectedCollection;
set
{
if (matchingFilter == value)
return;
2019-03-28 23:29:07 +08:00
matchingFilter = value;
updateFilter();
}
}
private void updateFilter() => this.FadeTo(MatchingFilter ? 1 : 0, 200);
public bool FilteringActive { get; set; }
2017-05-01 14:03:11 +08:00
}
}