osu/osu.Game/Overlays/Comments/DrawableComment.cs

244 lines
9.7 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-08 13:00:34 +00:00
using osu.Framework.Input.Events;
2019-10-08 16:09:02 +00:00
using osu.Framework.Graphics.Cursor;
2019-10-08 16:18:46 +00:00
using osu.Framework.Bindables;
namespace osu.Game.Overlays.Comments
{
public class DrawableComment : CompositeDrawable
{
private const int avatar_size = 40;
private const int margin = 10;
2019-10-08 12:39:03 +00:00
private const int child_margin = 20;
2019-10-08 13:00:34 +00:00
private const int duration = 200;
2019-10-08 16:18:46 +00:00
private readonly BindableBool childExpanded = new BindableBool(true);
2019-10-08 13:00:34 +00:00
private readonly Container childCommentsVisibilityContainer;
public DrawableComment(Comment comment)
{
LinkFlowContainer username;
2019-10-08 12:39:03 +00:00
FillFlowContainer childCommentsContainer;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2019-10-08 12:39:03 +00:00
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-10-08 12:39:03 +00:00
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
2019-10-08 12:39:03 +00:00
new GridContainer
{
2019-10-08 12:39:03 +00:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding(margin),
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
},
RowDimensions = new[]
{
2019-10-08 12:39:03 +00:00
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.AutoSize)
},
2019-10-08 12:39:03 +00:00
Content = new[]
{
2019-10-08 12:39:03 +00:00
new Drawable[]
{
2019-10-08 12:39:03 +00:00
new UpdateableAvatar(comment.User)
{
2019-10-08 12:39:03 +00:00
Size = new Vector2(avatar_size),
Margin = new MarginPadding { Horizontal = margin },
Masking = true,
CornerRadius = avatar_size / 2,
},
2019-10-08 12:39:03 +00:00
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-10-08 12:39:03 +00:00
Margin = new MarginPadding { Top = margin / 2 },
Spacing = new Vector2(0, 2),
Children = new Drawable[]
{
2019-10-08 16:09:02 +00:00
new FillFlowContainer
2019-10-08 12:39:03 +00:00
{
AutoSizeAxes = Axes.Both,
2019-10-08 16:09:02 +00:00
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)
}
2019-10-08 12:39:03 +00:00
},
new TextFlowContainer(s => s.Font = OsuFont.GetFont(size: 14))
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Text = comment.GetMessage()
}
}
}
},
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
},
2019-10-08 13:00:34 +00:00
new FillFlowContainer
2019-10-08 12:39:03 +00:00
{
2019-10-08 13:00:34 +00:00
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
2019-10-08 13:04:52 +00:00
Spacing = new Vector2(10, 0),
2019-10-08 13:00:34 +00:00
Children = new Drawable[]
{
new SpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12),
Text = HumanizerUtils.Humanize(comment.CreatedAt)
},
2019-10-08 16:18:46 +00:00
new RepliesButton(comment.RepliesCount)
{ Expanded = { BindTarget = childExpanded } },
2019-10-08 13:00:34 +00:00
}
}
}
}
},
2019-10-08 13:00:34 +00:00
childCommentsVisibilityContainer = new Container
{
2019-10-08 12:39:03 +00:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-10-08 13:00:34 +00:00
AutoSizeDuration = duration,
AutoSizeEasing = Easing.OutQuint,
Masking = true,
Child = childCommentsContainer = new FillFlowContainer
{
Margin = new MarginPadding { Left = child_margin },
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical
}
}
}
};
username.AddUserLink(comment.User);
2019-10-08 12:39:03 +00:00
comment.ChildComments.ForEach(c =>
{
if (!c.IsDeleted)
childCommentsContainer.Add(new DrawableComment(c));
});
2019-10-08 16:18:46 +00:00
}
2019-10-08 13:00:34 +00:00
2019-10-08 16:18:46 +00:00
protected override void LoadComplete()
{
childExpanded.BindValueChanged(onChildExpandedChanged, true);
base.LoadComplete();
}
private void onChildExpandedChanged(ValueChangedEvent<bool> expanded)
{
childCommentsVisibilityContainer.ClearTransforms();
if (expanded.NewValue)
childCommentsVisibilityContainer.AutoSizeAxes = Axes.Y;
else
{
childCommentsVisibilityContainer.AutoSizeAxes = Axes.None;
childCommentsVisibilityContainer.ResizeHeightTo(0, duration, Easing.OutQuint);
}
2019-10-08 13:00:34 +00:00
}
private class RepliesButton : Container
{
private readonly SpriteText text;
private readonly int count;
2019-10-08 16:18:46 +00:00
public readonly BindableBool Expanded = new BindableBool(true);
2019-10-08 13:00:34 +00:00
public RepliesButton(int count)
{
this.count = count;
AutoSizeAxes = Axes.Both;
Alpha = count == 0 ? 0 : 1;
Child = text = new SpriteText
{
Font = OsuFont.GetFont(size: 12),
};
2019-10-08 16:18:46 +00:00
}
2019-10-08 13:00:34 +00:00
2019-10-08 16:18:46 +00:00
protected override void LoadComplete()
{
Expanded.BindValueChanged(onExpandedChanged, true);
base.LoadComplete();
}
private void onExpandedChanged(ValueChangedEvent<bool> expanded)
{
text.Text = $@"{(expanded.NewValue ? "[+]" : "[-]")} replies ({count})";
2019-10-08 13:00:34 +00:00
}
protected override bool OnClick(ClickEvent e)
{
2019-10-08 16:18:46 +00:00
Expanded.Value = !Expanded.Value;
2019-10-08 13:00:34 +00:00
return base.OnClick(e);
}
}
2019-10-08 16:09:02 +00:00
private class ParentUsername : FillFlowContainer, IHasTooltip
{
private const int spacing = 3;
public string TooltipText => comment.ParentComment?.GetMessage() ?? "";
private readonly Comment comment;
public ParentUsername(Comment comment)
{
this.comment = comment;
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Horizontal;
Spacing = new Vector2(spacing, 0);
Alpha = comment.ParentId == null ? 0 : 1;
Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.Solid.Reply,
Size = new Vector2(14),
},
new SpriteText
{
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold, italics: true),
Text = comment.ParentComment?.User?.Username
}
};
}
}
}
}