2019-10-17 19:18:31 +08: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.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using osuTK;
|
2019-10-17 20:04:30 +08:00
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
using System.Linq;
|
2019-10-17 19:18:31 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Comments
|
|
|
|
|
{
|
|
|
|
|
public class VotePill : LoadingButton, IHasAccentColour
|
|
|
|
|
{
|
2019-10-24 22:49:34 +08:00
|
|
|
|
private const int duration = 200;
|
|
|
|
|
|
2019-10-17 19:18:31 +08:00
|
|
|
|
public Color4 AccentColour { get; set; }
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<Drawable> EffectTargets => null;
|
|
|
|
|
|
2019-10-17 20:04:30 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
2021-01-23 02:47:53 +08:00
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
|
private LoginOverlay login { get; set; }
|
|
|
|
|
|
2020-02-27 00:35:20 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OverlayColourProvider colourProvider { get; set; }
|
|
|
|
|
|
2021-01-23 03:49:49 +08:00
|
|
|
|
protected Box Background { get; private set; }
|
|
|
|
|
|
2019-10-17 19:18:31 +08:00
|
|
|
|
private readonly Comment comment;
|
2021-01-23 03:49:49 +08:00
|
|
|
|
|
2019-10-17 19:18:31 +08:00
|
|
|
|
private Box hoverLayer;
|
|
|
|
|
private CircularContainer borderContainer;
|
|
|
|
|
private SpriteText sideNumber;
|
|
|
|
|
private OsuSpriteText votesCounter;
|
2019-10-17 20:04:30 +08:00
|
|
|
|
private CommentVoteRequest request;
|
|
|
|
|
|
|
|
|
|
private readonly BindableBool isVoted = new BindableBool();
|
2019-10-17 21:28:32 +08:00
|
|
|
|
private readonly BindableInt votesCount = new BindableInt();
|
2019-10-17 19:18:31 +08:00
|
|
|
|
|
|
|
|
|
public VotePill(Comment comment)
|
|
|
|
|
{
|
|
|
|
|
this.comment = comment;
|
2019-10-17 20:04:30 +08:00
|
|
|
|
|
2019-10-17 19:18:31 +08:00
|
|
|
|
AutoSizeAxes = Axes.X;
|
|
|
|
|
Height = 20;
|
|
|
|
|
LoadingAnimationSize = new Vector2(10);
|
2019-10-17 20:04:30 +08:00
|
|
|
|
}
|
2019-10-17 19:18:31 +08:00
|
|
|
|
|
2019-10-17 20:04:30 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
AccentColour = borderContainer.BorderColour = sideNumber.Colour = colours.GreenLight;
|
|
|
|
|
hoverLayer.Colour = Color4.Black.Opacity(0.5f);
|
2019-10-30 04:43:16 +08:00
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
|
bool ownComment = api.LocalUser.Value.Id == comment.UserId;
|
2021-01-23 03:49:49 +08:00
|
|
|
|
|
|
|
|
|
if (!ownComment)
|
|
|
|
|
Action = onAction;
|
|
|
|
|
|
|
|
|
|
Background.Alpha = ownComment ? 0 : 1;
|
2019-10-17 20:04:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
isVoted.Value = comment.IsVoted;
|
2019-10-17 21:28:32 +08:00
|
|
|
|
votesCount.Value = comment.VotesCount;
|
2021-01-23 03:49:49 +08:00
|
|
|
|
isVoted.BindValueChanged(voted => Background.Colour = voted.NewValue ? AccentColour : colourProvider.Background6, true);
|
2019-10-17 21:28:32 +08:00
|
|
|
|
votesCount.BindValueChanged(count => votesCounter.Text = $"+{count.NewValue}", true);
|
2019-10-17 20:04:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onAction()
|
|
|
|
|
{
|
2021-01-23 02:47:53 +08:00
|
|
|
|
if (!api.IsLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
login?.Show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-17 20:04:30 +08:00
|
|
|
|
request = new CommentVoteRequest(comment.Id, isVoted.Value ? CommentVoteAction.UnVote : CommentVoteAction.Vote);
|
|
|
|
|
request.Success += onSuccess;
|
|
|
|
|
api.Queue(request);
|
|
|
|
|
}
|
2019-10-17 19:18:31 +08:00
|
|
|
|
|
2019-10-17 20:04:30 +08:00
|
|
|
|
private void onSuccess(CommentBundle response)
|
|
|
|
|
{
|
2019-10-18 04:20:01 +08:00
|
|
|
|
var receivedComment = response.Comments.Single();
|
|
|
|
|
isVoted.Value = receivedComment.IsVoted;
|
|
|
|
|
votesCount.Value = receivedComment.VotesCount;
|
2019-10-17 20:04:30 +08:00
|
|
|
|
IsLoading = false;
|
2019-10-17 19:18:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 22:49:34 +08:00
|
|
|
|
protected override Drawable CreateContent() => new Container
|
2019-10-17 19:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
|
AutoSizeAxes = Axes.X,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
borderContainer = new CircularContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Masking = true,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2021-01-23 03:49:49 +08:00
|
|
|
|
Background = new Box
|
2019-10-17 19:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
},
|
|
|
|
|
hoverLayer = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Alpha = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-11-25 10:30:55 +08:00
|
|
|
|
sideNumber = new OsuSpriteText
|
2019-10-17 19:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
Text = "+1",
|
|
|
|
|
Font = OsuFont.GetFont(size: 14),
|
|
|
|
|
Margin = new MarginPadding { Right = 3 },
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
},
|
2019-10-24 22:49:34 +08:00
|
|
|
|
votesCounter = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Margin = new MarginPadding { Horizontal = 10 },
|
|
|
|
|
Font = OsuFont.GetFont(size: 14),
|
|
|
|
|
AlwaysPresent = true,
|
|
|
|
|
}
|
2019-10-17 19:18:31 +08:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-24 22:49:34 +08:00
|
|
|
|
protected override void OnLoadStarted()
|
2019-10-17 19:18:31 +08:00
|
|
|
|
{
|
2019-10-24 22:49:34 +08:00
|
|
|
|
votesCounter.FadeOut(duration, Easing.OutQuint);
|
2021-01-23 03:49:49 +08:00
|
|
|
|
updateDisplay();
|
2019-10-24 22:49:34 +08:00
|
|
|
|
}
|
2019-10-17 19:18:31 +08:00
|
|
|
|
|
2019-10-24 22:49:34 +08:00
|
|
|
|
protected override void OnLoadFinished()
|
2019-10-17 19:18:31 +08:00
|
|
|
|
{
|
2019-10-24 22:49:34 +08:00
|
|
|
|
votesCounter.FadeIn(duration, Easing.OutQuint);
|
2021-01-23 03:49:49 +08:00
|
|
|
|
|
|
|
|
|
if (IsHovered)
|
|
|
|
|
onHoverAction();
|
2019-10-17 19:18:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
|
{
|
2021-01-23 03:49:49 +08:00
|
|
|
|
onHoverAction();
|
2019-10-17 19:18:31 +08:00
|
|
|
|
return base.OnHover(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
|
{
|
2021-01-23 03:49:49 +08:00
|
|
|
|
updateDisplay();
|
2019-10-17 19:18:31 +08:00
|
|
|
|
base.OnHoverLost(e);
|
2019-10-17 20:04:30 +08:00
|
|
|
|
}
|
2019-10-17 19:18:31 +08:00
|
|
|
|
|
2021-01-23 03:49:49 +08:00
|
|
|
|
private void updateDisplay()
|
2019-10-17 20:04:30 +08:00
|
|
|
|
{
|
2021-01-23 03:49:49 +08:00
|
|
|
|
if (Action == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-10-31 07:10:00 +08:00
|
|
|
|
if (isVoted.Value)
|
|
|
|
|
{
|
2021-01-23 03:49:49 +08:00
|
|
|
|
hoverLayer.FadeTo(IsHovered ? 1 : 0);
|
2019-10-31 07:10:00 +08:00
|
|
|
|
sideNumber.Hide();
|
2019-10-30 08:09:14 +08:00
|
|
|
|
}
|
2019-10-31 07:10:00 +08:00
|
|
|
|
else
|
2021-01-23 03:49:49 +08:00
|
|
|
|
sideNumber.FadeTo(IsHovered ? 1 : 0);
|
2019-10-31 07:10:00 +08:00
|
|
|
|
|
2021-01-23 03:49:49 +08:00
|
|
|
|
borderContainer.BorderThickness = IsHovered ? 3 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onHoverAction()
|
|
|
|
|
{
|
|
|
|
|
if (!IsLoading)
|
|
|
|
|
updateDisplay();
|
2019-10-17 20:04:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
2021-01-23 03:27:26 +08:00
|
|
|
|
base.Dispose(isDisposing);
|
2021-01-23 03:49:49 +08:00
|
|
|
|
request?.Cancel();
|
2019-10-17 20:04:30 +08:00
|
|
|
|
}
|
2019-10-17 19:18:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|