1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 02:07:24 +08:00
osu-lazer/osu.Game/Overlays/Comments/VotePill.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

207 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.
2022-06-17 15:37:17 +08:00
#nullable disable
2019-10-17 19:18:31 +08:00
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 partial 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; }
protected Box Background { get; private set; }
2019-10-17 19:18:31 +08:00
private readonly Comment comment;
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)
{
AccentColour = borderContainer.BorderColour = sideNumber.Colour = colours.GreenLight;
hoverLayer.Colour = Color4.Black.Opacity(0.5f);
2019-10-30 04:43:16 +08:00
bool 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 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()
{
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[]
{
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.TopCentre,
Origin = Anchor.BottomCentre,
2019-10-17 19:18:31 +08:00
Text = "+1",
Font = OsuFont.GetFont(size: 14),
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-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);
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)
{
updateDisplay();
2019-10-17 19:18:31 +08:00
base.OnHoverLost(e);
}
2019-10-17 19:18:31 +08:00
private void updateDisplay()
{
if (Action == null)
return;
2019-10-31 07:10:00 +08:00
if (isVoted.Value)
{
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
sideNumber.FadeTo(IsHovered ? 1 : 0);
2019-10-31 07:10:00 +08:00
borderContainer.BorderThickness = IsHovered ? 2 : 0;
}
private void onHoverAction()
{
if (!IsLoading)
updateDisplay();
}
protected override void Dispose(bool isDisposing)
{
2021-01-23 03:27:26 +08:00
base.Dispose(isDisposing);
request?.Cancel();
}
2019-10-17 19:18:31 +08:00
}
}