osu/osu.Game/Overlays/CommentsContainer.cs

207 lines
6.8 KiB
C#
Raw Normal View History

2019-10-07 14:49:20 +00:00
// 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.Allocation;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Framework.Graphics;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Framework.Graphics.Sprites;
using osuTK;
using osu.Game.Online.API.Requests.Responses;
2019-10-07 14:49:20 +00:00
namespace osu.Game.Overlays
{
public class CommentsContainer : CompositeDrawable
{
private readonly CommentableType type;
private readonly long id;
public readonly Bindable<SortCommentsBy> Sort = new Bindable<SortCommentsBy>();
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private OsuColour colours { get; set; }
private GetCommentsRequest request;
2019-10-07 14:49:20 +00:00
private readonly Box background;
private readonly FillFlowContainer content;
2019-10-07 14:49:20 +00:00
public CommentsContainer(CommentableType type, long id)
{
this.type = type;
this.id = id;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AddRangeInternal(new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
2019-10-07 15:45:22 +00:00
new CommentsHeader
{
Sort = { BindTarget = Sort }
},
content = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
}
}
}
});
}
protected override void LoadComplete()
{
Sort.BindValueChanged(onSortChanged, true);
base.LoadComplete();
}
private void onSortChanged(ValueChangedEvent<SortCommentsBy> sort) => getComments();
private void getComments()
{
request?.Cancel();
request = new GetCommentsRequest(type, id, Sort.Value);
request.Success += onSuccess;
api.Queue(request);
}
private void onSuccess(APIComments response)
{
content.Clear();
foreach (var c in response.Comments)
{
2019-10-07 15:45:22 +00:00
if (!c.IsDeleted)
createDrawableComment(c);
}
}
private void createDrawableComment(Comment comment)
{
content.Add(new Container
{
RelativeSizeAxes = Axes.X,
Height = 70,
Children = new Drawable[]
2019-10-07 14:49:20 +00:00
{
new FillFlowContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 3),
Children = new[]
{
new SpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Text = $"user: {comment.User.Username}",
},
new SpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Text = $"message: {comment.GetMessage()}",
},
}
},
new Container
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
Height = 1,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1,
}
}
2019-10-07 14:49:20 +00:00
}
});
}
[BackgroundDependencyLoader]
private void load()
2019-10-07 14:49:20 +00:00
{
background.Colour = colours.Gray3;
}
private class CommentsHeader : CompositeDrawable
{
private const int height = 40;
private const int spacing = 10;
private const int padding = 50;
public readonly Bindable<SortCommentsBy> Sort = new Bindable<SortCommentsBy>();
private readonly Box background;
public CommentsHeader()
{
RelativeSizeAxes = Axes.X;
Height = height;
AddRangeInternal(new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Horizontal = padding },
Children = new Drawable[]
{
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(spacing, 0),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Children = new Drawable[]
{
new SpriteText
{
Font = OsuFont.GetFont(size: 14),
Text = @"Sort by"
}
}
}
}
}
});
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
background.Colour = colours.Gray4;
}
}
}
}