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

197 lines
6.1 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(canBeNull: true)]
private LoginOverlay login { get; set; }
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
2021-01-23 03:27:26 +08:00
private bool isOwnComment;
2019-10-17 19:18:31 +08:00
private readonly Comment comment;
2021-01-23 03:27:26 +08:00
private Box background;
2019-10-17 19:18:31 +08: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 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)
{
2021-01-23 03:27:26 +08:00
isOwnComment = api.LocalUser.Value.Id == comment.UserId;
Action = onAction;
AccentColour = borderContainer.BorderColour = sideNumber.Colour = colours.GreenLight;
hoverLayer.Colour = Color4.Black.Opacity(0.5f);
2021-01-23 03:27:26 +08:00
background.Alpha = isOwnComment ? 0 : 1;
2019-10-30 04:43:16 +08:00
2021-01-23 03:27:26 +08:00
Enabled.Value = !isOwnComment;
}
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:27:26 +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);
}
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 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[]
{
2021-01-23 03:27:26 +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:27:26 +08:00
updateDisplay(false);
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:27:26 +08:00
updateDisplay(IsHovered);
2019-10-17 19:18:31 +08:00
}
protected override bool OnHover(HoverEvent e)
{
2021-01-23 03:27:26 +08:00
if (!isOwnComment && !IsLoading)
updateDisplay(true);
2019-10-17 19:18:31 +08:00
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
2021-01-23 03:27:26 +08:00
if (!isOwnComment && !IsLoading)
updateDisplay(false);
2019-10-17 19:18:31 +08:00
base.OnHoverLost(e);
}
2019-10-17 19:18:31 +08:00
2021-01-23 03:27:26 +08:00
private void updateDisplay(bool isHovered)
{
2019-10-31 07:10:00 +08:00
if (isVoted.Value)
{
2021-01-23 03:27:26 +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:27:26 +08:00
sideNumber.FadeTo(isHovered ? 1 : 0);
2019-10-31 07:10:00 +08:00
2021-01-23 03:27:26 +08:00
borderContainer.BorderThickness = isHovered ? 3 : 0;
}
protected override void Dispose(bool isDisposing)
{
request?.Cancel();
2021-01-23 03:27:26 +08:00
base.Dispose(isDisposing);
}
2019-10-17 19:18:31 +08:00
}
}