1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 08:33:20 +08:00
osu-lazer/osu.Game/Overlays/Comments/DrawableComment.cs

351 lines
15 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 00:56:43 +08:00
using osu.Framework.Graphics.Shapes;
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;
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 readonly BindableBool ShowDeleted = new BindableBool();
2019-10-08 21:00:34 +08:00
2019-10-15 05:32:21 +08:00
private readonly BindableBool childrenExpanded = new BindableBool(true);
2019-10-08 21:00:34 +08:00
private FillFlowContainer childCommentsVisibilityContainer;
2019-10-14 21:43:43 +08:00
private readonly Comment comment;
public DrawableComment(Comment comment)
{
this.comment = comment;
}
[BackgroundDependencyLoader]
private void load()
{
LinkFlowContainer username;
2019-10-08 20:39:03 +08:00
FillFlowContainer childCommentsContainer;
2019-10-15 05:32:21 +08:00
DeletedChildrenPlaceholder deletedChildrenPlaceholder;
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;
2019-10-08 20:39:03 +08:00
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-10-08 20:39:03 +08:00
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new Container
{
2019-10-08 20:39:03 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-10-17 18:10:28 +08:00
Padding = new MarginPadding(margin) { Left = margin + 5 },
Child = content = new GridContainer
2019-10-08 20:39:03 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
},
RowDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize)
},
Content = new[]
{
new Drawable[]
{
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[]
2019-10-09 00:56:43 +08:00
{
2019-10-17 20:24:51 +08:00
new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-10-17 20:24:51 +08:00
Width = 40,
AutoSizeAxes = Axes.Y,
2019-10-17 20:40:09 +08:00
Child = votePill = new VotePill(comment)
2019-10-17 20:24:51 +08:00
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
}
},
new UpdateableAvatar(comment.User)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(avatar_size),
Masking = true,
2019-10-14 22:33:14 +08:00
CornerRadius = avatar_size / 2f,
2019-11-22 18:49:20 +08:00
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-08 20:39:03 +08:00
{
new FillFlowContainer
2019-10-09 00:09:02 +08:00
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(7, 0),
Children = new Drawable[]
2019-10-09 16:07:56 +08:00
{
username = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true))
{
AutoSizeAxes = Axes.Both,
},
new ParentUsername(comment),
2019-11-25 10:30:55 +08:00
new OsuSpriteText
{
2019-10-14 22:33:14 +08:00
Alpha = comment.IsDeleted ? 1 : 0,
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true),
Text = @"deleted",
}
2019-10-09 16:07:56 +08:00
}
},
message = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 14))
2019-10-09 17:18:49 +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),
Colour = OsuColour.Gray(0.7f),
Children = new Drawable[]
2019-10-10 16:43:45 +08:00
{
2019-11-25 10:30:55 +08:00
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12),
Text = HumanizerUtils.Humanize(comment.CreatedAt)
},
new RepliesButton(comment.RepliesCount)
{
2019-10-15 05:32:21 +08:00
Expanded = { BindTarget = childrenExpanded }
},
}
2019-10-09 17:18:49 +08:00
}
}
2019-10-08 21:00:34 +08:00
}
}
}
}
},
2019-10-10 16:43:45 +08:00
childCommentsVisibilityContainer = new FillFlowContainer
{
2019-10-08 20:39:03 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-10-10 16:43:45 +08:00
Direction = FillDirection.Vertical,
Children = new Drawable[]
2019-10-08 21:00:34 +08:00
{
2019-10-10 16:43:45 +08:00
childCommentsContainer = new FillFlowContainer
{
Padding = new MarginPadding { Left = 20 },
2019-10-10 16:43:45 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical
},
2019-10-15 05:32:21 +08:00
deletedChildrenPlaceholder = new DeletedChildrenPlaceholder
2019-10-10 16:43:45 +08:00
{
ShowDeleted = { BindTarget = ShowDeleted }
}
2019-10-08 21:00:34 +08:00
}
}
}
};
2019-10-15 16:25:58 +08:00
deletedChildrenPlaceholder.DeletedCount.Value = comment.DeletedChildrenCount;
2019-10-13 17:38:50 +08:00
2019-10-14 21:43:43 +08:00
if (comment.UserId.HasValue)
2019-10-09 03:46:42 +08:00
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
2019-10-09 16:32:17 +08:00
if (comment.EditedAt.HasValue)
{
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}"
});
}
if (comment.HasMessage)
2019-10-14 21:56:07 +08:00
{
var formattedSource = MessageFormatter.FormatText(comment.GetMessage);
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)
{
AddInternal(new Container
{
RelativeSizeAxes = Axes.X,
2019-10-14 21:43:43 +08:00
Height = 1.5f,
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
2019-10-09 17:18:49 +08:00
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(0.1f)
}
});
if (comment.ChildComments.Any())
{
AddInternal(new ChevronButton(comment)
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
2019-10-14 21:43:43 +08:00
Margin = new MarginPadding { Right = 30, Top = margin },
2019-10-15 05:32:21 +08:00
Expanded = { BindTarget = childrenExpanded }
2019-10-09 17:18:49 +08:00
});
}
}
comment.ChildComments.ForEach(c => childCommentsContainer.Add(new DrawableComment(c)
2019-10-10 16:43:45 +08:00
{
ShowDeleted = { BindTarget = ShowDeleted }
}));
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)
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);
2019-10-09 00:18:46 +08:00
base.LoadComplete();
}
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(Comment comment)
{
Alpha = comment.IsTopLevel && comment.ChildComments.Any() ? 1 : 0;
Child = icon = new SpriteIcon
{
Size = new Vector2(12),
2019-10-13 16:50:27 +08:00
Colour = OsuColour.Gray(0.7f)
2019-10-09 16:07:56 +08:00
};
}
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;
Alpha = count == 0 ? 0 : 1;
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 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;
return parentComment.HasMessage ? parentComment.GetMessage : parentComment.IsDeleted ? @"deleted" : string.Empty;
}
2019-10-09 00:09:02 +08:00
}
}
}