1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 02:22:59 +08:00
osu-lazer/osu.Game/Overlays/Music/PlaylistItem.cs

184 lines
5.3 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;
using System.Linq;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-12-12 18:04:11 +08:00
using osu.Framework.Graphics.Sprites;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Music
{
public class PlaylistItem : Container, IFilterable, IDraggable
{
private const float fade_duration = 100;
private Color4 hoverColour;
private Color4 artistColour;
private SpriteIcon handle;
private TextFlowContainer text;
private IEnumerable<Drawable> titleSprites;
private ILocalisedBindableString titleBind;
private ILocalisedBindableString artistBind;
2018-04-13 17:19:50 +08:00
public readonly BeatmapSetInfo BeatmapSetInfo;
public Action<BeatmapSetInfo> OnSelect;
public bool IsDraggable { get; private set; }
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 17:19:50 +08:00
{
IsDraggable = handle.IsHovered;
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseUp(MouseUpEvent e)
2018-04-13 17:19:50 +08:00
{
IsDraggable = false;
2018-10-02 11:02:47 +08:00
return base.OnMouseUp(e);
2018-04-13 17:19:50 +08:00
}
private bool selected;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public bool Selected
{
get => selected;
2018-04-13 17:19:50 +08:00
set
{
if (value == selected) return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
selected = value;
FinishTransforms(true);
foreach (Drawable s in titleSprites)
2018-04-13 17:19:50 +08:00
s.FadeColour(Selected ? hoverColour : Color4.White, fade_duration);
}
}
public PlaylistItem(BeatmapSetInfo setInfo)
{
BeatmapSetInfo = setInfo;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Top = 3, Bottom = 3 };
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, LocalisationManager localisation)
2018-04-13 17:19:50 +08:00
{
hoverColour = colours.Yellow;
artistColour = colours.Gray9;
var metadata = BeatmapSetInfo.Metadata;
FilterTerms = metadata.SearchableTerms;
Children = new Drawable[]
{
handle = new PlaylistItemHandle
{
Colour = colours.Gray5
},
text = new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = 20 },
ContentIndent = 10f,
},
};
titleBind = localisation.GetLocalisedString(new LocalisedString((metadata.TitleUnicode, metadata.Title)));
artistBind = localisation.GetLocalisedString(new LocalisedString((metadata.ArtistUnicode, metadata.Artist)));
2018-04-13 17:19:50 +08:00
artistBind.BindValueChanged(_ => recreateText(), true);
2018-04-13 17:19:50 +08:00
}
private void recreateText()
{
text.Clear();
//space after the title to put a space between the title and artist
2019-02-20 15:52:36 +08:00
titleSprites = text.AddText(titleBind.Value + @" ", sprite => sprite.Font = OsuFont.GetFont(weight: FontWeight.Regular)).OfType<SpriteText>();
2018-04-13 17:19:50 +08:00
text.AddText(artistBind.Value, sprite =>
{
sprite.Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold);
2018-04-13 17:19:50 +08:00
sprite.Colour = artistColour;
sprite.Padding = new MarginPadding { Top = 1 };
});
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
handle.FadeIn(fade_duration);
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
handle.FadeOut(fade_duration);
}
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
OnSelect?.Invoke(BeatmapSetInfo);
return true;
}
public IEnumerable<string> FilterTerms { get; private set; }
private bool matching = true;
public bool MatchingFilter
{
get => matching;
2018-04-13 17:19:50 +08:00
set
{
if (matching == value) return;
matching = value;
this.FadeTo(matching ? 1 : 0, 200);
}
}
2019-03-28 23:29:07 +08:00
public bool FilteringActive { get; set; }
2018-04-13 17:19:50 +08:00
private class PlaylistItemHandle : SpriteIcon
{
public PlaylistItemHandle()
{
Anchor = Anchor.TopLeft;
Origin = Anchor.TopLeft;
Size = new Vector2(12);
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.Bars;
2018-04-13 17:19:50 +08:00
Alpha = 0f;
Margin = new MarginPadding { Left = 5, Top = 2 };
}
public override bool HandlePositionalInput => IsPresent;
2018-04-13 17:19:50 +08:00
}
}
public interface IDraggable : IDrawable
{
/// <summary>
/// Whether this <see cref="IDraggable"/> can be dragged in its current state.
/// </summary>
bool IsDraggable { get; }
}
}