1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 21:27:23 +08:00
osu-lazer/osu.Game/Overlays/Comments/VotePill.cs

191 lines
5.8 KiB
C#
Raw Normal View History

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;
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;
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
2019-10-17 19:18:31 +08:00
private readonly Comment comment;
private Box background;
private Box hoverLayer;
private CircularContainer borderContainer;
private SpriteText sideNumber;
private OsuSpriteText votesCounter;
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 19:18:31 +08:00
AutoSizeAxes = Axes.X;
Height = 20;
LoadingAnimationSize = new Vector2(10);
}
2019-10-17 19:18:31 +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
2019-11-09 15:23:22 +08:00
if (api.IsLoggedIn && api.LocalUser.Value.Id != comment.UserId)
2019-10-30 08:09:14 +08:00
Action = onAction;
}
protected override void LoadComplete()
{
base.LoadComplete();
isVoted.Value = comment.IsVoted;
2019-10-17 21:28:32 +08:00
votesCount.Value = comment.VotesCount;
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);
}
private void onAction()
{
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
private void onSuccess(CommentBundle response)
{
var receivedComment = response.Comments.Single();
isVoted.Value = receivedComment.IsVoted;
votesCount.Value = receivedComment.VotesCount;
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[]
{
background = new Box
{
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);
updateDisplay();
}
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);
if (IsHovered)
onHoverAction();
2019-10-17 19:18:31 +08:00
}
protected override bool OnHover(HoverEvent e)
{
onHoverAction();
2019-10-17 19:18:31 +08:00
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
2019-10-18 08:06:01 +08:00
updateDisplay();
2019-10-17 19:18:31 +08:00
base.OnHoverLost(e);
}
2019-10-17 19:18:31 +08:00
2019-10-18 08:06:01 +08:00
private void updateDisplay()
{
2019-10-31 07:10:00 +08:00
if (Action == null)
return;
2019-10-17 19:18:31 +08:00
2019-10-31 07:10:00 +08:00
if (isVoted.Value)
{
hoverLayer.FadeTo(IsHovered ? 1 : 0);
sideNumber.Hide();
2019-10-30 08:09:14 +08:00
}
2019-10-31 07:10:00 +08:00
else
sideNumber.FadeTo(IsHovered ? 1 : 0);
borderContainer.BorderThickness = IsHovered ? 3 : 0;
2019-10-17 19:18:31 +08:00
}
private void onHoverAction()
{
if (!IsLoading)
2019-10-18 08:06:01 +08:00
updateDisplay();
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
request?.Cancel();
}
2019-10-17 19:18:31 +08:00
}
}