osu/osu.Game/Screens/OnlinePlay/DrawableRoomPlaylistItem.cs

600 lines
24 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.
using System;
using System.Collections.Generic;
using System.Linq;
2021-11-25 14:15:28 +00:00
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
2021-11-25 14:15:28 +00:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
2021-11-25 14:15:28 +00:00
using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
2021-11-25 14:15:28 +00:00
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osu.Game.Online.Chat;
2020-12-25 04:38:11 +00:00
using osu.Game.Online.Rooms;
2021-01-13 08:33:00 +00:00
using osu.Game.Overlays.BeatmapSet;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Play.HUD;
2021-11-25 14:15:28 +00:00
using osu.Game.Users.Drawables;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.OnlinePlay
{
public class DrawableRoomPlaylistItem : OsuRearrangeableListItem<PlaylistItem>
{
public const float HEIGHT = 50;
2021-11-25 14:15:28 +00:00
public const float ICON_HEIGHT = 34;
/// <summary>
/// Invoked when this item requests to be deleted.
/// </summary>
public Action<PlaylistItem> RequestDeletion;
/// <summary>
/// Invoked when this item requests its results to be shown.
/// </summary>
2021-12-08 16:52:59 +00:00
public Action<PlaylistItem> RequestResults;
/// <summary>
/// The currently-selected item, used to show a border around this item.
/// May be updated by this item if <see cref="AllowSelection"/> is <c>true</c>.
/// </summary>
2020-02-14 07:55:05 +00:00
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
public readonly PlaylistItem Item;
private readonly DelayedLoadWrapper onScreenLoader = new DelayedLoadWrapper(Empty) { RelativeSizeAxes = Axes.Both };
private readonly IBindable<bool> valid = new Bindable<bool>();
private readonly Bindable<IBeatmapInfo> beatmap = new Bindable<IBeatmapInfo>();
private readonly Bindable<IRulesetInfo> ruleset = new Bindable<IRulesetInfo>();
private readonly BindableList<Mod> requiredMods = new BindableList<Mod>();
private Container maskingContainer;
private Container difficultyIconContainer;
private LinkFlowContainer beatmapText;
private LinkFlowContainer authorText;
2021-01-17 13:40:24 +00:00
private ExplicitContentBeatmapPill explicitContentPill;
private ModDisplay modDisplay;
private FillFlowContainer buttonsFlow;
2021-11-25 14:15:28 +00:00
private UpdateableAvatar ownerAvatar;
private Drawable removeButton;
private Drawable showResultsButton;
private PanelBackground panelBackground;
private FillFlowContainer mainFillFlow;
2021-11-25 14:15:28 +00:00
[Resolved]
private OsuColour colours { get; set; }
[Resolved]
private UserLookupCache userLookupCache { get; set; }
[Resolved]
private BeatmapLookupCache beatmapLookupCache { get; set; }
protected override bool ShouldBeConsideredForInput(Drawable child) => AllowReordering || AllowDeletion || !AllowSelection || SelectedItem.Value == Model;
public DrawableRoomPlaylistItem(PlaylistItem item)
: base(item)
{
Item = item;
beatmap.BindTo(item.Beatmap);
valid.BindTo(item.Valid);
ruleset.BindTo(item.Ruleset);
requiredMods.BindTo(item.RequiredMods);
2020-09-08 07:36:36 +00:00
if (item.Expired)
Colour = OsuColour.Gray(0.5f);
}
[BackgroundDependencyLoader]
private void load()
{
maskingContainer.BorderColour = colours.Yellow;
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-11-17 02:28:43 +00:00
SelectedItem.BindValueChanged(selected =>
{
bool isCurrent = selected.NewValue == Model;
if (!valid.Value)
{
// Don't allow selection when not valid.
if (isCurrent)
{
SelectedItem.Value = selected.OldValue;
}
// Don't update border when not valid (the border is displaying this fact).
return;
}
2020-02-14 07:55:05 +00:00
2021-11-17 02:28:43 +00:00
maskingContainer.BorderThickness = isCurrent ? 5 : 0;
}, true);
beatmap.BindValueChanged(_ => Scheduler.AddOnce(refresh));
ruleset.BindValueChanged(_ => Scheduler.AddOnce(refresh));
valid.BindValueChanged(_ => Scheduler.AddOnce(refresh));
requiredMods.CollectionChanged += (_, __) => Scheduler.AddOnce(refresh);
onScreenLoader.DelayedLoadStarted += _ =>
{
Task.Run(async () =>
{
try
{
if (showItemOwner)
{
var foundUser = await userLookupCache.GetUserAsync(Item.OwnerID).ConfigureAwait(false);
Schedule(() => ownerAvatar.User = foundUser);
}
if (Item.Beatmap.Value == null)
{
var foundBeatmap = await beatmapLookupCache.GetBeatmapAsync(Item.BeatmapID).ConfigureAwait(false);
Schedule(() => Item.Beatmap.Value = foundBeatmap);
}
}
catch (Exception e)
{
Logger.Log($"Error while populating playlist item {e}");
}
});
};
refresh();
}
/// <summary>
/// Whether this item can be selected.
/// </summary>
public bool AllowSelection { get; set; }
/// <summary>
/// Whether this item can be reordered in the playlist.
/// </summary>
public bool AllowReordering
{
get => ShowDragHandle.Value;
set => ShowDragHandle.Value = value;
}
private bool allowDeletion;
/// <summary>
/// Whether this item can be deleted.
/// </summary>
public bool AllowDeletion
{
get => allowDeletion;
set
{
allowDeletion = value;
if (removeButton != null)
removeButton.Alpha = value ? 1 : 0;
}
}
private bool allowShowingResults;
/// <summary>
/// Whether this item can have results shown.
/// </summary>
public bool AllowShowingResults
{
get => allowShowingResults;
set
{
allowShowingResults = value;
if (showResultsButton != null)
showResultsButton.Alpha = value ? 1 : 0;
}
}
private bool showItemOwner;
/// <summary>
/// Whether to display the avatar of the user which owns this playlist item.
/// </summary>
public bool ShowItemOwner
{
get => showItemOwner;
set
{
showItemOwner = value;
if (ownerAvatar != null)
ownerAvatar.Alpha = value ? 1 : 0;
}
}
2020-02-14 07:55:05 +00:00
private void refresh()
{
if (!valid.Value)
{
maskingContainer.BorderThickness = 5;
maskingContainer.BorderColour = colours.Red;
}
if (Item.Beatmap.Value != null)
difficultyIconContainer.Child = new DifficultyIcon(Item.Beatmap.Value, ruleset.Value, requiredMods, performBackgroundDifficultyLookup: false) { Size = new Vector2(ICON_HEIGHT) };
else
difficultyIconContainer.Clear();
panelBackground.Beatmap.Value = Item.Beatmap.Value;
2020-02-14 07:55:05 +00:00
beatmapText.Clear();
if (Item.Beatmap.Value != null)
2021-07-22 04:18:37 +00:00
{
beatmapText.AddLink(Item.Beatmap.Value.GetDisplayTitleRomanisable(), LinkAction.OpenBeatmap, Item.Beatmap.Value.OnlineID.ToString(), null, text =>
{
text.Truncate = true;
});
}
2020-02-14 07:55:05 +00:00
authorText.Clear();
if (!string.IsNullOrEmpty(Item.Beatmap.Value?.Metadata.Author.Username))
2020-02-14 07:55:05 +00:00
{
authorText.AddText("mapped by ");
authorText.AddUserLink(Item.Beatmap.Value.Metadata.Author);
2020-02-14 07:55:05 +00:00
}
bool hasExplicitContent = (Item.Beatmap.Value?.BeatmapSet as IBeatmapSetOnlineInfo)?.HasExplicitContent == true;
2021-01-17 13:40:24 +00:00
explicitContentPill.Alpha = hasExplicitContent ? 1 : 0;
2021-01-13 08:33:00 +00:00
2020-02-14 07:55:05 +00:00
modDisplay.Current.Value = requiredMods.ToArray();
buttonsFlow.Clear();
buttonsFlow.ChildrenEnumerable = createButtons();
difficultyIconContainer.FadeInFromZero(500, Easing.OutQuint);
mainFillFlow.FadeInFromZero(500, Easing.OutQuint);
2020-02-14 07:55:05 +00:00
}
2021-02-02 08:09:59 +00:00
protected override Drawable CreateContent()
{
2021-02-02 08:09:59 +00:00
Action<SpriteText> fontParameters = s => s.Font = OsuFont.Default.With(weight: FontWeight.SemiBold);
return maskingContainer = new Container
{
2021-02-02 08:09:59 +00:00
RelativeSizeAxes = Axes.X,
Height = HEIGHT,
2021-02-02 08:09:59 +00:00
Masking = true,
CornerRadius = 10,
Children = new Drawable[]
{
2021-02-02 08:09:59 +00:00
new Box // A transparent box that forces the border to be drawn if the panel background is opaque
{
2021-02-02 08:09:59 +00:00
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true
},
onScreenLoader,
panelBackground = new PanelBackground
2021-02-02 08:09:59 +00:00
{
RelativeSizeAxes = Axes.Both,
},
2021-07-22 04:18:37 +00:00
new GridContainer
2021-02-02 08:09:59 +00:00
{
RelativeSizeAxes = Axes.Both,
2021-07-22 04:18:37 +00:00
ColumnDimensions = new[]
{
2021-07-22 04:18:37 +00:00
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
2021-11-26 08:17:06 +00:00
new Dimension(GridSizeMode.AutoSize)
2021-07-22 04:18:37 +00:00
},
Content = new[]
{
new Drawable[]
2021-02-02 08:09:59 +00:00
{
2021-07-22 04:18:37 +00:00
difficultyIconContainer = new Container
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
2021-11-26 08:17:06 +00:00
Margin = new MarginPadding { Left = 8, Right = 8 },
2021-07-22 04:18:37 +00:00
},
mainFillFlow = new FillFlowContainer
{
2021-07-22 04:18:37 +00:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
2021-07-22 04:18:37 +00:00
beatmapText = new LinkFlowContainer(fontParameters)
2021-01-13 08:33:00 +00:00
{
2021-07-22 04:18:37 +00:00
RelativeSizeAxes = Axes.X,
// workaround to ensure only the first line of text shows, emulating truncation (but without ellipsis at the end).
// TODO: remove when text/link flow can support truncation with ellipsis natively.
Height = OsuFont.DEFAULT_FONT_SIZE,
Masking = true
2021-07-22 04:18:37 +00:00
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10f, 0),
Children = new Drawable[]
2021-01-13 08:33:00 +00:00
{
2021-07-22 04:18:37 +00:00
new FillFlowContainer
2021-01-13 08:33:00 +00:00
{
2021-07-22 04:18:37 +00:00
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10f, 0),
Children = new Drawable[]
2021-02-02 08:09:59 +00:00
{
2021-07-22 04:18:37 +00:00
authorText = new LinkFlowContainer(fontParameters) { AutoSizeAxes = Axes.Both },
explicitContentPill = new ExplicitContentBeatmapPill
{
Alpha = 0f,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Margin = new MarginPadding { Top = 3f },
}
},
2021-02-02 08:09:59 +00:00
},
2021-07-22 04:18:37 +00:00
new Container
2021-02-02 08:09:59 +00:00
{
2021-07-22 04:18:37 +00:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
Child = modDisplay = new ModDisplay
{
Scale = new Vector2(0.4f),
ExpansionMode = ExpansionMode.AlwaysExpanded
}
2021-02-02 08:09:59 +00:00
}
2021-01-13 08:33:00 +00:00
}
}
}
2021-07-22 04:18:37 +00:00
},
buttonsFlow = new FillFlowContainer
2021-07-22 04:18:37 +00:00
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Direction = FillDirection.Horizontal,
2021-11-26 08:40:45 +00:00
Margin = new MarginPadding { Horizontal = 8 },
2021-07-22 04:18:37 +00:00
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(5),
ChildrenEnumerable = createButtons().Select(button => button.With(b =>
2021-07-22 04:18:37 +00:00
{
b.Anchor = Anchor.Centre;
b.Origin = Anchor.Centre;
}))
2021-11-26 08:17:06 +00:00
},
ownerAvatar = new OwnerAvatar
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(ICON_HEIGHT),
2021-11-26 08:40:45 +00:00
Margin = new MarginPadding { Right = 8 },
2021-11-26 08:17:06 +00:00
Masking = true,
CornerRadius = 4,
Alpha = ShowItemOwner ? 1 : 0
2021-11-26 08:17:06 +00:00
},
}
}
2021-02-02 08:09:59 +00:00
},
}
2021-02-02 08:09:59 +00:00
};
}
private IEnumerable<Drawable> createButtons() =>
new[]
{
showResultsButton = new ShowResultsButton
{
2021-12-08 16:52:59 +00:00
Action = () => RequestResults?.Invoke(Item),
Alpha = AllowShowingResults ? 1 : 0,
},
Item.Beatmap.Value == null ? Empty() : new PlaylistDownloadButton(Item),
removeButton = new PlaylistRemoveButton
{
Size = new Vector2(30, 30),
Alpha = AllowDeletion ? 1 : 0,
Action = () => RequestDeletion?.Invoke(Item),
},
};
public class PlaylistRemoveButton : GrayButton
{
public PlaylistRemoveButton()
: base(FontAwesome.Solid.MinusSquare)
{
TooltipText = "Remove from playlist";
}
[BackgroundDependencyLoader]
private void load()
{
Icon.Scale = new Vector2(0.8f);
}
}
protected override bool OnClick(ClickEvent e)
{
if (AllowSelection && valid.Value)
2020-02-14 07:55:05 +00:00
SelectedItem.Value = Model;
return true;
}
private sealed class PlaylistDownloadButton : BeatmapDownloadButton
{
private readonly PlaylistItem playlistItem;
[Resolved]
private BeatmapManager beatmapManager { get; set; }
2021-10-27 12:26:26 +00:00
// required for download tracking, as this button hides itself. can probably be removed with a bit of consideration.
public override bool IsPresent => true;
private const float width = 50;
public PlaylistDownloadButton(PlaylistItem playlistItem)
: base(playlistItem.Beatmap.Value.BeatmapSet)
{
this.playlistItem = playlistItem;
Size = new Vector2(width, 30);
Alpha = 0;
}
protected override void LoadComplete()
{
State.BindValueChanged(stateChanged, true);
// base implementation calls FinishTransforms, so should be run after the above state update.
base.LoadComplete();
}
private void stateChanged(ValueChangedEvent<DownloadState> state)
{
switch (state.NewValue)
{
case DownloadState.LocallyAvailable:
// Perform a local query of the beatmap by beatmap checksum, and reset the state if not matching.
if (beatmapManager.QueryBeatmap(b => b.MD5Hash == playlistItem.Beatmap.Value.MD5Hash) == null)
State.Value = DownloadState.NotDownloaded;
else
{
this.FadeTo(0, 500)
.ResizeWidthTo(0, 500, Easing.OutQuint);
}
break;
default:
this.ResizeWidthTo(width, 500, Easing.OutQuint)
.FadeTo(1, 500);
break;
}
}
}
private class ShowResultsButton : IconButton
{
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Icon = FontAwesome.Solid.ChartPie;
TooltipText = "View results";
Add(new Box
{
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue,
Colour = colours.Gray4,
});
}
}
// For now, this is the same implementation as in PanelBackground, but supports a beatmap info rather than a working beatmap
private class PanelBackground : Container // todo: should be a buffered container (https://github.com/ppy/osu-framework/issues/3222)
{
public readonly Bindable<IBeatmapInfo> Beatmap = new Bindable<IBeatmapInfo>();
public PanelBackground()
{
UpdateableBeatmapBackgroundSprite backgroundSprite;
InternalChildren = new Drawable[]
{
backgroundSprite = new UpdateableBeatmapBackgroundSprite
{
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fill,
},
new FillFlowContainer
{
Depth = -1,
RelativeSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
// This makes the gradient not be perfectly horizontal, but diagonal at a ~40° angle
Shear = new Vector2(0.8f, 0),
Alpha = 0.5f,
Children = new[]
{
// The left half with no gradient applied
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
Width = 0.4f,
},
2021-02-02 21:05:25 +00:00
// Piecewise-linear gradient with 2 segments to make it appear smoother
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = ColourInfo.GradientHorizontal(Color4.Black, new Color4(0f, 0f, 0f, 0.7f)),
Width = 0.4f,
},
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = ColourInfo.GradientHorizontal(new Color4(0f, 0f, 0f, 0.7f), new Color4(0, 0, 0, 0.4f)),
Width = 0.4f,
},
}
}
};
// manual binding required as playlists don't expose IBeatmapInfo currently.
// may be removed in the future if this changes.
Beatmap.BindValueChanged(beatmap => backgroundSprite.Beatmap.Value = beatmap.NewValue);
}
}
2021-11-25 14:15:28 +00:00
private class OwnerAvatar : UpdateableAvatar, IHasTooltip
{
public OwnerAvatar()
{
AddInternal(new TooltipArea(this)
{
RelativeSizeAxes = Axes.Both,
Depth = -1
});
}
public LocalisableString TooltipText => User == null ? string.Empty : $"queued by {User.Username}";
2021-11-25 14:15:28 +00:00
private class TooltipArea : Component, IHasTooltip
{
private readonly OwnerAvatar avatar;
public TooltipArea(OwnerAvatar avatar)
{
this.avatar = avatar;
}
public LocalisableString TooltipText => avatar.TooltipText;
}
}
}
}