1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 10:57:30 +08:00
osu-lazer/osu.Game/Overlays/Comments/DrawableComment.cs

454 lines
19 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;
using osu.Game.Utils;
2019-10-09 00:09:02 +08:00
using osu.Framework.Graphics.Cursor;
2019-10-09 00:18:46 +08:00
using osu.Framework.Bindables;
2019-10-09 16:07:56 +08:00
using System.Linq;
2019-11-25 10:30:55 +08:00
using osu.Game.Graphics.Sprites;
2019-10-14 21:56:07 +08:00
using osu.Game.Online.Chat;
using osu.Framework.Allocation;
using osuTK.Graphics;
using System.Collections.Generic;
using System;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Extensions.IEnumerableExtensions;
using System.Collections.Specialized;
namespace osu.Game.Overlays.Comments
{
public class DrawableComment : CompositeDrawable
{
private const int avatar_size = 40;
private const int margin = 10;
2019-10-09 17:18:49 +08:00
public Action<DrawableComment, int> RepliesRequested;
public readonly Comment Comment;
2019-10-09 17:18:49 +08:00
public readonly BindableBool ShowDeleted = new BindableBool();
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
2020-02-13 07:37:41 +08:00
private readonly Dictionary<long, Comment> loadedReplies = new Dictionary<long, Comment>();
2019-10-08 21:00:34 +08:00
2020-02-13 07:47:13 +08:00
public readonly BindableList<DrawableComment> Replies = new BindableList<DrawableComment>();
2019-10-15 05:32:21 +08:00
private readonly BindableBool childrenExpanded = new BindableBool(true);
2019-10-08 21:00:34 +08:00
private int currentPage;
private FillFlowContainer childCommentsVisibilityContainer;
private FillFlowContainer childCommentsContainer;
private LoadMoreCommentsButton loadMoreCommentsButton;
private ShowMoreButton showMoreButton;
private RepliesButton repliesButton;
private ChevronButton chevronButton;
private DeletedCommentsCounter deletedCommentsCounter;
public DrawableComment(Comment comment)
{
Comment = comment;
}
[BackgroundDependencyLoader]
private void load()
{
LinkFlowContainer username;
2019-10-09 16:32:17 +08:00
FillFlowContainer info;
2019-10-14 21:56:07 +08:00
LinkFlowContainer message;
2019-10-09 17:18:49 +08:00
GridContainer content;
VotePill votePill;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
{
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new Container
2019-10-08 20:39:03 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding(margin) { Left = margin + 5 },
Child = content = new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
},
RowDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize)
},
Content = new[]
{
new Drawable[]
2019-10-09 00:56:43 +08:00
{
new FillFlowContainer
2019-10-09 00:56:43 +08:00
{
AutoSizeAxes = Axes.Both,
Margin = new MarginPadding { Horizontal = margin },
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0),
Children = new Drawable[]
{
new Container
2019-10-17 20:24:51 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Width = 40,
AutoSizeAxes = Axes.Y,
Child = votePill = new VotePill(Comment)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
}
},
new UpdateableAvatar(Comment.User)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(avatar_size),
Masking = true,
CornerRadius = avatar_size / 2f,
CornerExponent = 2,
},
}
},
new FillFlowContainer
2019-10-08 20:39:03 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0, 3),
Children = new Drawable[]
2019-10-09 00:09:02 +08:00
{
new FillFlowContainer
2019-10-09 16:07:56 +08:00
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(7, 0),
Children = new Drawable[]
{
username = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true))
{
AutoSizeAxes = Axes.Both,
},
new ParentUsername(Comment),
new OsuSpriteText
{
Alpha = Comment.IsDeleted ? 1 : 0,
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true),
Text = @"deleted",
}
}
},
message = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 14))
2019-10-10 16:43:45 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Right = 40 }
},
info = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10, 0),
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12),
Colour = OsuColour.Gray(0.7f),
Text = HumanizerUtils.Humanize(Comment.CreatedAt)
},
repliesButton = new RepliesButton(Comment.RepliesCount)
{
Expanded = { BindTarget = childrenExpanded }
},
loadMoreCommentsButton = new LoadMoreCommentsButton
{
Action = () => RepliesRequested(this, ++currentPage)
}
}
}
2019-10-09 17:18:49 +08:00
}
}
2019-10-08 21:00:34 +08:00
}
}
}
},
childCommentsVisibilityContainer = new FillFlowContainer
2019-10-08 21:00:34 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
2019-10-10 16:43:45 +08:00
{
childCommentsContainer = new FillFlowContainer
{
Padding = new MarginPadding { Left = 20 },
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical
},
deletedCommentsCounter = new DeletedCommentsCounter
{
ShowDeleted = { BindTarget = ShowDeleted }
},
showMoreButton = new ShowMoreButton
{
Action = () => RepliesRequested(this, ++currentPage)
}
}
},
}
},
chevronButton = new ChevronButton
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding { Right = 30, Top = margin },
Expanded = { BindTarget = childrenExpanded },
Alpha = 0
}
};
if (Comment.UserId.HasValue)
username.AddUserLink(Comment.User);
2019-10-14 21:43:43 +08:00
else
username.AddText(Comment.LegacyName);
2019-10-08 20:39:03 +08:00
if (Comment.EditedAt.HasValue)
2019-10-09 16:32:17 +08:00
{
2019-11-25 10:30:55 +08:00
info.Add(new OsuSpriteText
2019-10-09 16:32:17 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12),
Text = $@"edited {HumanizerUtils.Humanize(Comment.EditedAt.Value)} by {Comment.EditedUser.Username}"
2019-10-09 16:32:17 +08:00
});
}
if (Comment.HasMessage)
2019-10-14 21:56:07 +08:00
{
var formattedSource = MessageFormatter.FormatText(Comment.Message);
2019-10-14 21:56:07 +08:00
message.AddLinks(formattedSource.Text, formattedSource.Links);
}
if (Comment.IsDeleted)
2019-10-08 20:39:03 +08:00
{
2019-10-09 17:18:49 +08:00
content.FadeColour(OsuColour.Gray(0.5f));
votePill.Hide();
}
if (Comment.IsTopLevel)
2019-10-09 17:18:49 +08:00
{
AddInternal(new Box
2019-10-09 17:18:49 +08:00
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
Height = 1.5f,
Colour = OsuColour.Gray(0.1f)
2019-10-09 17:18:49 +08:00
});
}
2020-02-13 07:47:13 +08: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-09 00:18:46 +08:00
}
2019-10-08 21:00:34 +08:00
2019-10-09 00:18:46 +08:00
protected override void LoadComplete()
{
2019-10-15 16:30:50 +08:00
ShowDeleted.BindValueChanged(show =>
{
if (Comment.IsDeleted)
2019-10-15 16:30:50 +08:00
this.FadeTo(show.NewValue ? 1 : 0);
}, true);
2019-10-15 05:32:21 +08:00
childrenExpanded.BindValueChanged(expanded => childCommentsVisibilityContainer.FadeTo(expanded.NewValue ? 1 : 0), true);
updateButtonsState();
2019-10-09 00:18:46 +08:00
base.LoadComplete();
}
2020-02-13 07:37:41 +08:00
public bool ContainsReply(long replyId) => loadedReplies.ContainsKey(replyId);
2020-02-13 07:47:13 +08: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 07:37:41 +08: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()
{
2020-02-13 07:37:41 +08:00
var loadedReplesCount = loadedReplies.Count;
var hasUnloadedReplies = loadedReplesCount != Comment.RepliesCount;
loadMoreCommentsButton.FadeTo(hasUnloadedReplies && loadedReplesCount == 0 ? 1 : 0);
showMoreButton.FadeTo(hasUnloadedReplies && loadedReplesCount > 0 ? 1 : 0);
repliesButton.FadeTo(loadedReplesCount != 0 ? 1 : 0);
if (Comment.IsTopLevel)
chevronButton.FadeTo(loadedReplesCount != 0 ? 1 : 0);
showMoreButton.IsLoading = loadMoreCommentsButton.IsLoading = false;
}
2019-10-15 05:32:21 +08:00
private class ChevronButton : ShowChildrenButton
2019-10-09 16:07:56 +08:00
{
private readonly SpriteIcon icon;
public ChevronButton()
2019-10-09 16:07:56 +08:00
{
Child = icon = new SpriteIcon
{
Size = new Vector2(12),
};
}
protected override void OnExpandedChanged(ValueChangedEvent<bool> expanded)
{
icon.Icon = expanded.NewValue ? FontAwesome.Solid.ChevronUp : FontAwesome.Solid.ChevronDown;
}
}
2019-10-15 05:32:21 +08:00
private class RepliesButton : ShowChildrenButton
2019-10-08 21:00:34 +08:00
{
private readonly SpriteText text;
private readonly int count;
public RepliesButton(int count)
{
this.count = count;
2019-11-25 10:30:55 +08:00
Child = text = new OsuSpriteText
2019-10-08 21:00:34 +08:00
{
2019-10-09 01:44:01 +08:00
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
2019-10-08 21:00:34 +08:00
};
2019-10-09 00:18:46 +08:00
}
2019-10-08 21:00:34 +08:00
2019-10-09 16:07:56 +08:00
protected override void OnExpandedChanged(ValueChangedEvent<bool> expanded)
2019-10-09 00:18:46 +08:00
{
text.Text = $@"{(expanded.NewValue ? "[-]" : "[+]")} replies ({count})";
2019-10-08 21:00:34 +08:00
}
}
2019-10-09 00:09:02 +08:00
private class LoadMoreCommentsButton : GetCommentRepliesButton
{
public LoadMoreCommentsButton()
{
IdleColour = OsuColour.Gray(0.7f);
HoverColour = Color4.White;
}
protected override string GetText() => @"[+] load replies";
}
private class ShowMoreButton : GetCommentRepliesButton
{
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
Margin = new MarginPadding { Vertical = 10, Left = 80 };
IdleColour = colourProvider.Light2;
HoverColour = colourProvider.Light1;
}
protected override string GetText() => @"Show More";
}
2019-10-09 00:09:02 +08:00
private class ParentUsername : FillFlowContainer, IHasTooltip
{
public string TooltipText => getParentMessage();
2019-10-09 00:09:02 +08:00
private readonly Comment parentComment;
2019-10-09 00:09:02 +08:00
public ParentUsername(Comment comment)
{
parentComment = comment.ParentComment;
2019-10-09 00:09:02 +08:00
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Horizontal;
2019-10-14 22:33:14 +08:00
Spacing = new Vector2(3, 0);
2019-10-09 00:09:02 +08:00
Alpha = comment.ParentId == null ? 0 : 1;
Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.Solid.Reply,
Size = new Vector2(14),
},
2019-11-25 10:30:55 +08:00
new OsuSpriteText
2019-10-09 00:09:02 +08:00
{
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true),
Text = parentComment?.User?.Username ?? parentComment?.LegacyName
2019-10-09 00:09:02 +08:00
}
};
}
private string getParentMessage()
{
if (parentComment == null)
return string.Empty;
2020-01-29 11:44:39 +08:00
return parentComment.HasMessage ? parentComment.Message : parentComment.IsDeleted ? @"deleted" : string.Empty;
}
2019-10-09 00:09:02 +08:00
}
}
}