2020-02-11 23:46:49 +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.Allocation;
|
2023-01-09 14:52:08 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2023-01-09 14:52:08 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2023-01-09 14:52:08 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
|
|
|
|
using osu.Framework.Localisation;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2020-02-12 05:57:06 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2022-11-29 11:17:02 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
2023-06-20 05:08:04 +08:00
|
|
|
|
using osu.Game.Online.API;
|
2024-09-30 17:02:00 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
|
|
|
using osu.Game.Resources.Localisation.Web;
|
2023-01-09 14:52:08 +08:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Comments
|
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public abstract partial class CommentEditor : CompositeDrawable
|
2020-02-11 23:46:49 +08:00
|
|
|
|
{
|
2024-09-30 17:02:00 +08:00
|
|
|
|
public Bindable<CommentableMeta?> CommentableMeta { get; set; } = new Bindable<CommentableMeta?>();
|
|
|
|
|
|
2020-02-12 01:08:24 +08:00
|
|
|
|
private const int side_padding = 8;
|
|
|
|
|
|
2022-11-29 10:13:54 +08:00
|
|
|
|
protected abstract LocalisableString FooterText { get; }
|
2020-02-12 01:08:24 +08:00
|
|
|
|
|
2022-11-29 10:17:44 +08:00
|
|
|
|
protected FillFlowContainer ButtonsContainer { get; private set; } = null!;
|
2020-02-12 01:47:51 +08:00
|
|
|
|
|
2023-01-11 07:26:25 +08:00
|
|
|
|
protected readonly Bindable<string> Current = new Bindable<string>(string.Empty);
|
2020-02-12 06:35:08 +08:00
|
|
|
|
|
2023-01-10 04:37:36 +08:00
|
|
|
|
private RoundedButton commitButton = null!;
|
2023-06-21 12:58:43 +08:00
|
|
|
|
private RoundedButton logInButton = null!;
|
2023-01-10 04:37:36 +08:00
|
|
|
|
private LoadingSpinner loadingSpinner = null!;
|
|
|
|
|
|
2023-01-19 15:08:27 +08:00
|
|
|
|
protected TextBox TextBox { get; private set; } = null!;
|
|
|
|
|
|
2023-06-20 05:08:04 +08:00
|
|
|
|
[Resolved]
|
2023-06-20 05:36:40 +08:00
|
|
|
|
protected IAPIProvider API { get; private set; } = null!;
|
2023-06-20 05:08:04 +08:00
|
|
|
|
|
2023-06-20 06:10:37 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private LoginOverlay? loginOverlay { get; set; }
|
|
|
|
|
|
2023-06-23 13:05:02 +08:00
|
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
|
|
|
|
|
2023-06-23 05:00:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the text content of the main action button.
|
|
|
|
|
/// When <paramref name="isLoggedIn"/> is <see langword="true"/>, the text will apply to a button that posts a comment.
|
|
|
|
|
/// When <paramref name="isLoggedIn"/> is <see langword="false"/>, the text will apply to a button that directs the user to the login overlay.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected abstract LocalisableString GetButtonText(bool isLoggedIn);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the placeholder text for the comment box.
|
|
|
|
|
/// </summary>
|
2024-09-30 17:02:00 +08:00
|
|
|
|
protected abstract LocalisableString GetPlaceholderText();
|
2023-06-20 05:08:04 +08:00
|
|
|
|
|
2023-01-10 04:37:36 +08:00
|
|
|
|
protected bool ShowLoadingSpinner
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
loadingSpinner.Show();
|
|
|
|
|
else
|
|
|
|
|
loadingSpinner.Hide();
|
2023-01-10 22:57:38 +08:00
|
|
|
|
|
2024-09-30 20:05:23 +08:00
|
|
|
|
updateState();
|
2023-01-10 04:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 06:35:08 +08:00
|
|
|
|
|
2020-02-11 23:46:49 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OverlayColourProvider colourProvider)
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
2020-02-12 01:08:24 +08:00
|
|
|
|
AutoSizeAxes = Axes.Y;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
Masking = true;
|
|
|
|
|
CornerRadius = 6;
|
2020-02-12 01:08:24 +08:00
|
|
|
|
BorderThickness = 3;
|
|
|
|
|
BorderColour = colourProvider.Background3;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
|
|
|
|
|
AddRangeInternal(new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = colourProvider.Background3
|
|
|
|
|
},
|
2020-02-12 01:08:24 +08:00
|
|
|
|
new FillFlowContainer
|
2020-02-11 23:46:49 +08:00
|
|
|
|
{
|
2020-02-12 01:08:24 +08:00
|
|
|
|
AutoSizeAxes = Axes.Y,
|
2020-02-11 23:46:49 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2020-02-12 01:08:24 +08:00
|
|
|
|
Direction = FillDirection.Vertical,
|
2020-02-11 23:46:49 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2023-01-19 15:08:27 +08:00
|
|
|
|
TextBox = new EditorTextBox
|
2020-02-12 01:08:24 +08:00
|
|
|
|
{
|
2020-02-12 01:11:22 +08:00
|
|
|
|
Height = 40,
|
2020-02-12 01:08:24 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2023-06-20 06:20:16 +08:00
|
|
|
|
Current = Current
|
2020-02-12 01:08:24 +08:00
|
|
|
|
},
|
|
|
|
|
new Container
|
2020-02-11 23:46:49 +08:00
|
|
|
|
{
|
2022-11-29 10:17:44 +08:00
|
|
|
|
Name = @"Footer",
|
2020-02-12 01:08:24 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2023-01-13 00:48:11 +08:00
|
|
|
|
Height = 35,
|
2020-02-12 01:08:24 +08:00
|
|
|
|
Padding = new MarginPadding { Horizontal = side_padding },
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
2023-01-13 00:48:11 +08:00
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold),
|
2020-02-12 01:08:24 +08:00
|
|
|
|
Text = FooterText
|
2020-02-12 01:47:51 +08:00
|
|
|
|
},
|
2023-01-10 04:37:36 +08:00
|
|
|
|
new FillFlowContainer
|
2020-02-12 01:47:51 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
2020-02-12 05:57:06 +08:00
|
|
|
|
Spacing = new Vector2(5, 0),
|
2023-01-10 04:37:36 +08:00
|
|
|
|
Children = new Drawable[]
|
2020-02-12 05:57:06 +08:00
|
|
|
|
{
|
2023-01-10 04:37:36 +08:00
|
|
|
|
ButtonsContainer = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
Name = @"Buttons",
|
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
Spacing = new Vector2(5, 0),
|
2023-06-21 12:58:43 +08:00
|
|
|
|
Children = new Drawable[]
|
2023-01-10 04:37:36 +08:00
|
|
|
|
{
|
2023-06-21 12:58:43 +08:00
|
|
|
|
commitButton = new EditorButton
|
|
|
|
|
{
|
|
|
|
|
Action = () => OnCommit(Current.Value),
|
2023-06-23 05:00:52 +08:00
|
|
|
|
Text = GetButtonText(true)
|
2023-06-21 12:58:43 +08:00
|
|
|
|
},
|
|
|
|
|
logInButton = new EditorButton
|
|
|
|
|
{
|
|
|
|
|
Width = 100,
|
|
|
|
|
Action = () => loginOverlay?.Show(),
|
2023-06-23 05:00:52 +08:00
|
|
|
|
Text = GetButtonText(false)
|
2023-06-21 12:58:43 +08:00
|
|
|
|
}
|
2023-01-10 04:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
loadingSpinner = new LoadingSpinner
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
Size = new Vector2(18),
|
|
|
|
|
},
|
2020-02-12 05:57:06 +08:00
|
|
|
|
}
|
2023-01-10 04:37:36 +08:00
|
|
|
|
},
|
2020-02-12 01:08:24 +08:00
|
|
|
|
}
|
2020-02-11 23:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-02-12 05:57:06 +08:00
|
|
|
|
|
2023-01-19 15:08:27 +08:00
|
|
|
|
TextBox.OnCommit += (_, _) => commitButton.TriggerClick();
|
2023-06-23 13:05:02 +08:00
|
|
|
|
apiState.BindTo(API.State);
|
2020-02-12 06:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2024-09-30 20:05:23 +08:00
|
|
|
|
Current.BindValueChanged(_ => updateState());
|
|
|
|
|
apiState.BindValueChanged(_ => Scheduler.AddOnce(updateState));
|
|
|
|
|
CommentableMeta.BindValueChanged(_ => Scheduler.AddOnce(updateState));
|
|
|
|
|
updateState();
|
2020-02-11 23:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 03:51:57 +08:00
|
|
|
|
protected abstract void OnCommit(string text);
|
|
|
|
|
|
2024-09-30 20:05:23 +08:00
|
|
|
|
private void updateState()
|
2023-06-20 05:08:04 +08:00
|
|
|
|
{
|
2024-09-30 17:02:00 +08:00
|
|
|
|
bool isOnline = apiState.Value > APIState.Offline;
|
2024-10-01 16:01:31 +08:00
|
|
|
|
LocalisableString? canNewCommentReason = CommentEditor.canNewCommentReason(CommentableMeta.Value);
|
2024-09-30 17:02:00 +08:00
|
|
|
|
bool commentsDisabled = canNewCommentReason != null;
|
|
|
|
|
bool canComment = isOnline && !commentsDisabled;
|
|
|
|
|
|
|
|
|
|
if (!isOnline)
|
|
|
|
|
TextBox.PlaceholderText = AuthorizationStrings.RequireLogin;
|
|
|
|
|
else if (canNewCommentReason != null)
|
|
|
|
|
TextBox.PlaceholderText = canNewCommentReason.Value;
|
|
|
|
|
else
|
|
|
|
|
TextBox.PlaceholderText = GetPlaceholderText();
|
|
|
|
|
TextBox.ReadOnly = !canComment;
|
2023-06-21 12:58:43 +08:00
|
|
|
|
|
2024-09-30 17:02:00 +08:00
|
|
|
|
if (isOnline)
|
2023-06-21 12:58:43 +08:00
|
|
|
|
{
|
|
|
|
|
commitButton.Show();
|
2024-09-30 20:05:23 +08:00
|
|
|
|
commitButton.Enabled.Value = !commentsDisabled && loadingSpinner.State.Value == Visibility.Hidden && !string.IsNullOrEmpty(Current.Value);
|
2023-06-21 12:58:43 +08:00
|
|
|
|
logInButton.Hide();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
commitButton.Hide();
|
|
|
|
|
logInButton.Show();
|
|
|
|
|
}
|
2024-09-30 20:05:23 +08:00
|
|
|
|
}
|
2023-06-20 05:08:04 +08:00
|
|
|
|
|
2024-09-30 17:02:00 +08:00
|
|
|
|
// https://github.com/ppy/osu-web/blob/83816dbe24ad2927273cba968f2fcd2694a121a9/resources/js/components/comment-editor.tsx#L54-L60
|
|
|
|
|
// careful here, logic is VERY finicky.
|
|
|
|
|
private static LocalisableString? canNewCommentReason(CommentableMeta? meta)
|
|
|
|
|
{
|
|
|
|
|
if (meta == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (meta.CurrentUserAttributes != null)
|
|
|
|
|
{
|
|
|
|
|
if (meta.CurrentUserAttributes.Value.CanNewCommentReason is string reason)
|
|
|
|
|
return reason;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return AuthorizationStrings.CommentStoreDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 09:08:42 +08:00
|
|
|
|
private partial class EditorTextBox : OsuTextBox
|
2020-02-12 01:08:24 +08:00
|
|
|
|
{
|
|
|
|
|
protected override float LeftRightPadding => side_padding;
|
|
|
|
|
|
2020-02-12 06:35:08 +08:00
|
|
|
|
protected override Color4 SelectionColour => Color4.Gray;
|
2020-02-11 23:46:49 +08:00
|
|
|
|
|
2022-11-29 10:17:44 +08:00
|
|
|
|
private OsuSpriteText placeholder = null!;
|
2020-02-12 01:08:24 +08:00
|
|
|
|
|
2020-02-14 04:04:53 +08:00
|
|
|
|
public EditorTextBox()
|
2020-02-12 01:08:24 +08:00
|
|
|
|
{
|
|
|
|
|
Masking = false;
|
|
|
|
|
TextContainer.Height = 0.4f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OverlayColourProvider colourProvider)
|
|
|
|
|
{
|
|
|
|
|
BackgroundUnfocused = BackgroundFocused = colourProvider.Background5;
|
|
|
|
|
placeholder.Colour = colourProvider.Background3;
|
2020-02-12 06:35:08 +08:00
|
|
|
|
BackgroundCommit = colourProvider.Background3;
|
2020-02-12 01:08:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override SpriteText CreatePlaceholder() => placeholder = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Regular),
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-01-13 00:48:11 +08:00
|
|
|
|
|
|
|
|
|
protected partial class EditorButton : RoundedButton
|
|
|
|
|
{
|
|
|
|
|
public EditorButton()
|
|
|
|
|
{
|
|
|
|
|
Width = 80;
|
|
|
|
|
Height = 25;
|
|
|
|
|
Anchor = Anchor.CentreRight;
|
|
|
|
|
Origin = Anchor.CentreRight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override SpriteText CreateText()
|
|
|
|
|
{
|
|
|
|
|
var t = base.CreateText();
|
|
|
|
|
t.Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 12);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-11 23:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|