1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-06 22:47:20 +08:00
osu-lazer/osu.Game/Overlays/Comments/DrawableComment.cs

566 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 osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Framework.Graphics.Sprites;
using osuTK;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Users.Drawables;
using osu.Game.Graphics.Containers;
2019-10-08 19:09:02 +03:00
using osu.Framework.Graphics.Cursor;
2019-10-08 19:18:46 +03:00
using osu.Framework.Bindables;
2019-10-09 11:07:56 +03:00
using System.Linq;
2019-11-25 11:30:55 +09:00
using osu.Game.Graphics.Sprites;
using osu.Framework.Allocation;
using System.Collections.Generic;
using System;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Extensions.IEnumerableExtensions;
using System.Collections.Specialized;
using osu.Framework.Localisation;
2022-10-13 22:18:26 +03:00
using osu.Framework.Platform;
using osu.Game.Graphics.UserInterface;
2022-09-27 18:23:15 +03:00
using osu.Game.Online.API;
2022-09-27 18:33:16 +03:00
using osu.Game.Online.API.Requests;
2020-07-11 08:01:11 +03:00
using osu.Game.Overlays.Comments.Buttons;
2022-09-27 18:33:16 +03:00
using osu.Game.Overlays.Dialog;
2022-10-16 19:57:21 +03:00
using osu.Game.Overlays.OSD;
2021-08-08 17:21:29 +07:00
using osu.Game.Resources.Localisation.Web;
namespace osu.Game.Overlays.Comments
{
[Cached]
public class DrawableComment : CompositeDrawable
{
private const int avatar_size = 40;
2019-10-09 12:18:49 +03:00
2022-10-08 17:11:09 +03:00
public Action<DrawableComment, int> RepliesRequested = null!;
public readonly Comment Comment;
2019-10-09 12:18:49 +03:00
public readonly BindableBool ShowDeleted = new BindableBool();
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
2020-02-13 02:37:41 +03:00
private readonly Dictionary<long, Comment> loadedReplies = new Dictionary<long, Comment>();
2019-10-08 16:00:34 +03:00
2020-02-13 02:47:13 +03:00
public readonly BindableList<DrawableComment> Replies = new BindableList<DrawableComment>();
2019-10-15 00:32:21 +03:00
private readonly BindableBool childrenExpanded = new BindableBool(true);
2019-10-08 16:00:34 +03:00
private int currentPage;
/// <summary>
/// Local field for tracking comment state. Initialized from Comment.IsDeleted, may change when deleting was requested by user.
/// </summary>
public bool WasDeleted { get; protected set; }
2022-10-08 17:11:09 +03:00
private FillFlowContainer childCommentsVisibilityContainer = null!;
private FillFlowContainer childCommentsContainer = null!;
private LoadRepliesButton loadRepliesButton = null!;
private ShowMoreRepliesButton showMoreButton = null!;
private ShowRepliesButton showRepliesButton = null!;
private ChevronButton chevronButton = null!;
private LinkFlowContainer actionsContainer = null!;
private LoadingSpinner actionsLoading = null!;
private DeletedCommentsCounter deletedCommentsCounter = null!;
private OsuSpriteText deletedLabel = null!;
private GridContainer content = null!;
private VotePill votePill = null!;
2022-10-08 17:11:09 +03:00
[Resolved(canBeNull: true)]
private IDialogOverlay? dialogOverlay { get; set; }
2022-09-27 22:46:23 +03:00
[Resolved]
2022-10-08 17:11:09 +03:00
private IAPIProvider api { get; set; } = null!;
2022-09-27 22:46:23 +03:00
2022-10-13 22:18:26 +03:00
[Resolved]
private GameHost host { get; set; } = null!;
2022-10-14 16:26:25 +03:00
[Resolved(canBeNull: true)]
2022-10-16 19:57:21 +03:00
private OnScreenDisplay? onScreenDisplay { get; set; }
2022-10-14 16:26:25 +03:00
public DrawableComment(Comment comment)
{
Comment = comment;
}
[BackgroundDependencyLoader]
2022-09-27 22:46:23 +03:00
private void load(OverlayColourProvider colourProvider)
{
LinkFlowContainer username;
2019-10-09 11:32:17 +03:00
FillFlowContainer info;
CommentMarkdownContainer message;
2019-10-09 12:18:49 +03:00
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
{
2020-07-28 02:36:25 +03:00
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2020-07-28 02:36:25 +03:00
Padding = getPadding(Comment.IsTopLevel),
Child = new FillFlowContainer
{
2020-07-28 02:36:25 +03:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
2019-10-08 15:39:03 +03:00
{
2020-07-28 02:36:25 +03:00
content = new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
ColumnDimensions = new[]
{
2020-07-28 02:36:25 +03:00
new Dimension(GridSizeMode.Absolute, size: avatar_size + 10),
new Dimension(),
},
RowDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize)
},
Content = new[]
{
new Drawable[]
2019-10-08 19:56:43 +03:00
{
2020-07-28 02:36:25 +03:00
new Container
2019-10-08 19:56:43 +03:00
{
2020-07-28 02:36:25 +03:00
Size = new Vector2(avatar_size),
Children = new Drawable[]
{
new UpdateableAvatar(Comment.User)
{
Size = new Vector2(avatar_size),
Masking = true,
CornerRadius = avatar_size / 2f,
CornerExponent = 2,
},
2020-07-28 02:36:25 +03:00
votePill = new VotePill(Comment)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreRight,
Margin = new MarginPadding
{
Right = 5
}
}
}
},
new FillFlowContainer
2019-10-08 15:39:03 +03:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2020-07-28 02:36:25 +03:00
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 4),
Margin = new MarginPadding
{
Vertical = 2
},
Children = new Drawable[]
2019-10-08 19:09:02 +03:00
{
new FillFlowContainer
2019-10-09 11:07:56 +03:00
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
2020-07-28 02:36:25 +03:00
Spacing = new Vector2(10, 0),
Children = new[]
{
2020-07-28 02:36:25 +03:00
username = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold))
{
2020-07-28 02:36:25 +03:00
AutoSizeAxes = Axes.Both
},
Comment.Pinned ? new PinnedCommentNotice() : Empty(),
new ParentUsername(Comment),
deletedLabel = new OsuSpriteText
{
Alpha = 0f,
2020-07-28 02:36:25 +03:00
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold),
Text = CommentsStrings.Deleted
}
}
},
message = new CommentMarkdownContainer
2019-10-10 11:43:45 +03:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
DocumentMargin = new MarginPadding(0),
DocumentPadding = new MarginPadding(0),
},
2022-09-27 18:23:15 +03:00
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
2020-07-28 02:36:25 +03:00
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10, 0),
Children = new Drawable[]
{
2022-09-27 18:23:15 +03:00
info = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10, 0),
Children = new Drawable[]
{
new DrawableDate(Comment.CreatedAt, 12, false)
{
Colour = colourProvider.Foreground1
}
}
},
actionsContainer = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold))
{
Name = @"Actions buttons",
2022-09-27 18:23:15 +03:00
AutoSizeAxes = Axes.Both,
},
actionsLoading = new LoadingSpinner
{
Size = new Vector2(12f),
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft
2020-07-28 02:36:25 +03:00
}
}
},
new Container
{
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
2020-07-11 08:01:11 +03:00
showRepliesButton = new ShowRepliesButton(Comment.RepliesCount)
{
Expanded = { BindTarget = childrenExpanded }
},
2020-07-11 08:01:11 +03:00
loadRepliesButton = new LoadRepliesButton
{
Action = () => RepliesRequested(this, ++currentPage)
}
}
}
2019-10-09 12:18:49 +03:00
}
}
2019-10-08 16:00:34 +03:00
}
}
2020-07-28 02:36:25 +03:00
},
childCommentsVisibilityContainer = new FillFlowContainer
2019-10-10 11:43:45 +03:00
{
2020-07-28 02:36:25 +03:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Padding = new MarginPadding { Left = 20 },
Children = new Drawable[]
{
2020-07-28 02:36:25 +03:00
childCommentsContainer = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical
},
deletedCommentsCounter = new DeletedCommentsCounter
{
ShowDeleted = { BindTarget = ShowDeleted },
Margin = new MarginPadding
{
Top = 10
}
},
showMoreButton = new ShowMoreRepliesButton
2020-07-28 02:36:25 +03:00
{
Action = () => RepliesRequested(this, ++currentPage)
}
}
2020-07-28 02:36:25 +03:00
},
}
}
},
2020-07-28 00:29:17 +03:00
new Container
{
2020-07-28 00:29:17 +03:00
Size = new Vector2(70, 40),
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
2020-07-28 00:29:17 +03:00
Margin = new MarginPadding { Horizontal = 5 },
Child = chevronButton = new ChevronButton
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Expanded = { BindTarget = childrenExpanded },
Alpha = 0
}
}
};
if (Comment.UserId.HasValue)
username.AddUserLink(Comment.User);
2019-10-14 16:43:43 +03:00
else
2022-10-08 17:11:09 +03:00
username.AddText(Comment.LegacyName!);
2019-10-08 15:39:03 +03:00
2022-09-27 22:45:05 +03:00
if (Comment.EditedAt.HasValue && Comment.EditedUser != null)
2019-10-09 11:32:17 +03:00
{
var font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular);
var colour = colourProvider.Foreground1;
info.Add(new FillFlowContainer
2019-10-09 11:32:17 +03:00
{
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
new OsuSpriteText
{
Font = font,
Text = "edited ",
Colour = colour
},
new DrawableDate(Comment.EditedAt.Value)
{
Font = font,
Colour = colour
},
new OsuSpriteText
{
Font = font,
Text = $@" by {Comment.EditedUser.Username}",
Colour = colour
},
}
2019-10-09 11:32:17 +03:00
});
}
if (Comment.HasMessage)
message.Text = Comment.Message;
WasDeleted = Comment.IsDeleted;
if (WasDeleted)
makeDeleted();
2019-10-09 12:18:49 +03:00
2022-10-13 22:47:48 +03:00
actionsContainer.AddLink("Copy link", copyUrl);
actionsContainer.AddArbitraryDrawable(Empty().With(d => d.Width = 10));
2022-10-13 22:18:26 +03:00
2022-09-27 18:23:15 +03:00
if (Comment.UserId.HasValue && Comment.UserId.Value == api.LocalUser.Value.Id)
actionsContainer.AddLink("Delete", deleteComment);
2022-10-14 15:52:09 +03:00
else
actionsContainer.AddArbitraryDrawable(new CommentReportButton(Comment));
2022-09-27 18:23:15 +03:00
if (Comment.IsTopLevel)
2019-10-09 12:18:49 +03:00
{
AddInternal(new Box
2019-10-09 12:18:49 +03:00
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
Height = 1.5f,
Colour = OsuColour.Gray(0.1f)
2019-10-09 12:18:49 +03:00
});
}
2020-02-13 02:47:13 +03:00
if (Replies.Any())
onRepliesAdded(Replies);
Replies.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
onRepliesAdded(args.NewItems.Cast<DrawableComment>());
break;
default:
throw new NotSupportedException(@"You can only add replies to this list. Other actions are not supported.");
}
};
2019-10-08 19:18:46 +03:00
}
2019-10-08 16:00:34 +03:00
/// <summary>
/// Transforms some comment's components to show it as deleted. Invoked both from loading and deleting.
/// </summary>
private void makeDeleted()
{
2022-10-12 15:43:05 +09:00
deletedLabel.Show();
content.FadeColour(OsuColour.Gray(0.5f));
votePill.Hide();
actionsContainer.Expire();
}
/// <summary>
/// Invokes comment deletion with confirmation.
/// </summary>
private void deleteComment()
{
2022-10-08 17:11:09 +03:00
if (dialogOverlay == null)
deleteCommentRequest();
else
dialogOverlay.Push(new ConfirmDialog("Do you really want to delete your comment?", deleteCommentRequest));
}
/// <summary>
/// Invokes comment deletion directly.
/// </summary>
2022-10-08 17:11:09 +03:00
private void deleteCommentRequest()
{
actionsContainer.Hide();
actionsLoading.Show();
var request = new CommentDeleteRequest(Comment.Id);
request.Success += _ => Schedule(() =>
{
2022-10-08 17:11:09 +03:00
actionsLoading.Hide();
makeDeleted();
WasDeleted = true;
if (!ShowDeleted.Value)
Hide();
});
request.Failure += _ => Schedule(() =>
2022-10-08 17:11:09 +03:00
{
actionsLoading.Hide();
actionsContainer.Show();
});
2022-10-08 17:11:09 +03:00
api.Queue(request);
}
2022-10-13 22:18:26 +03:00
private void copyUrl()
{
2022-10-16 20:20:34 +03:00
host.GetClipboard()?.SetText($@"{api.APIEndpointUrl}/comments/{Comment.Id}");
2022-10-13 22:18:26 +03:00
onScreenDisplay?.Display(new CopyUrlToast());
}
2019-10-08 19:18:46 +03:00
protected override void LoadComplete()
{
2019-10-15 11:30:50 +03:00
ShowDeleted.BindValueChanged(show =>
{
if (WasDeleted)
2019-10-15 11:30:50 +03:00
this.FadeTo(show.NewValue ? 1 : 0);
}, true);
2019-10-15 00:32:21 +03:00
childrenExpanded.BindValueChanged(expanded => childCommentsVisibilityContainer.FadeTo(expanded.NewValue ? 1 : 0), true);
updateButtonsState();
2019-10-08 19:18:46 +03:00
base.LoadComplete();
}
2020-02-13 02:37:41 +03:00
public bool ContainsReply(long replyId) => loadedReplies.ContainsKey(replyId);
2020-02-13 02:47:13 +03:00
private void onRepliesAdded(IEnumerable<DrawableComment> replies)
{
var page = createRepliesPage(replies);
if (LoadState == LoadState.Loading)
{
addRepliesPage(page, replies);
return;
}
LoadComponentAsync(page, loaded => addRepliesPage(loaded, replies));
}
private void addRepliesPage(FillFlowContainer<DrawableComment> page, IEnumerable<DrawableComment> replies)
{
childCommentsContainer.Add(page);
var newReplies = replies.Select(reply => reply.Comment);
2020-02-13 02:37:41 +03:00
newReplies.ForEach(reply => loadedReplies.Add(reply.Id, reply));
deletedCommentsCounter.Count.Value += newReplies.Count(reply => reply.IsDeleted);
updateButtonsState();
}
private FillFlowContainer<DrawableComment> createRepliesPage(IEnumerable<DrawableComment> replies) => new FillFlowContainer<DrawableComment>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = replies.ToList()
};
private void updateButtonsState()
{
2021-12-09 21:04:31 -08:00
int loadedRepliesCount = loadedReplies.Count;
bool hasUnloadedReplies = loadedRepliesCount != Comment.RepliesCount;
2021-12-09 21:04:31 -08:00
loadRepliesButton.FadeTo(hasUnloadedReplies && loadedRepliesCount == 0 ? 1 : 0);
showMoreButton.FadeTo(hasUnloadedReplies && loadedRepliesCount > 0 ? 1 : 0);
showRepliesButton.FadeTo(loadedRepliesCount != 0 ? 1 : 0);
if (Comment.IsTopLevel)
2021-12-09 21:04:31 -08:00
chevronButton.FadeTo(loadedRepliesCount != 0 ? 1 : 0);
showMoreButton.IsLoading = loadRepliesButton.IsLoading = false;
}
2020-07-28 02:36:25 +03:00
private MarginPadding getPadding(bool isTopLevel)
{
if (isTopLevel)
{
return new MarginPadding
{
Horizontal = 70,
Vertical = 15
};
}
return new MarginPadding
{
Top = 10
};
}
private class PinnedCommentNotice : FillFlowContainer
{
public PinnedCommentNotice()
{
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Horizontal;
Spacing = new Vector2(2, 0);
Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.Solid.Thumbtack,
Size = new Vector2(14),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
new OsuSpriteText
{
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold),
Text = CommentsStrings.Pinned,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
}
};
}
}
2019-10-08 19:09:02 +03:00
private class ParentUsername : FillFlowContainer, IHasTooltip
{
public LocalisableString TooltipText => getParentMessage();
2019-10-08 19:09:02 +03:00
2022-10-08 17:11:09 +03:00
private readonly Comment? parentComment;
2019-10-08 19:09:02 +03:00
public ParentUsername(Comment comment)
{
parentComment = comment.ParentComment;
2019-10-08 19:09:02 +03:00
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Horizontal;
2019-10-14 17:33:14 +03:00
Spacing = new Vector2(3, 0);
2019-10-08 19:09:02 +03:00
Alpha = comment.ParentId == null ? 0 : 1;
Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.Solid.Reply,
Size = new Vector2(14),
},
2019-11-25 11:30:55 +09:00
new OsuSpriteText
2019-10-08 19:09:02 +03:00
{
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true),
2022-10-08 17:11:09 +03:00
Text = parentComment?.User?.Username ?? parentComment?.LegacyName!
2019-10-08 19:09:02 +03:00
}
};
}
private string getParentMessage()
{
if (parentComment == null)
return string.Empty;
return parentComment.HasMessage ? parentComment.Message : parentComment.IsDeleted ? "deleted" : string.Empty;
}
2019-10-08 19:09:02 +03:00
}
}
}