osu/osu.Game/Overlays/Comments/VotePill.cs

206 lines
6.1 KiB
C#
Raw Normal View History

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