osu/osu.Game/Graphics/UserInterfaceV2/ReportPopover.cs

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

134 lines
5.1 KiB
C#
Raw Normal View History

2022-12-20 16:37:16 +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 System;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Resources.Localisation.Web;
using osuTK;
namespace osu.Game.Graphics.UserInterfaceV2
{
2023-05-06 12:52:33 +00:00
/// <summary>
/// A generic popover for sending an online report about something.
/// </summary>
/// <typeparam name="TReportReason">An enumeration type with all valid reasons for the report.</typeparam>
public abstract partial class ReportPopover<TReportReason> : OsuPopover
where TReportReason : struct, Enum
2022-12-20 16:37:16 +00:00
{
2023-05-06 12:52:33 +00:00
/// <summary>
/// The action to run when the report is finalised.
/// The arguments to this action are: the reason for the report, and an optional additional comment.
/// </summary>
public Action<TReportReason, string>? Action;
2022-12-20 16:37:16 +00:00
2023-05-06 12:52:33 +00:00
private OsuEnumDropdown<TReportReason> reasonDropdown = null!;
2022-12-20 16:37:16 +00:00
private OsuTextBox commentsTextBox = null!;
private RoundedButton submitButton = null!;
2023-05-03 09:59:39 +00:00
private readonly LocalisableString header;
2022-12-20 16:37:16 +00:00
2023-05-06 12:52:33 +00:00
/// <summary>
/// Creates a new <see cref="ReportPopover{TReportReason}"/>.
/// </summary>
/// <param name="headerString">The text to display in the header of the popover.</param>
2022-12-20 16:37:16 +00:00
protected ReportPopover(LocalisableString headerString)
{
2023-05-03 09:59:39 +00:00
header = headerString;
2022-12-20 16:37:16 +00:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Child = new ReverseChildIDFillFlowContainer<Drawable>
{
Direction = FillDirection.Vertical,
Width = 500,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(7),
Children = new Drawable[]
{
new SpriteIcon
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Icon = FontAwesome.Solid.ExclamationTriangle,
Size = new Vector2(36),
},
new OsuSpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
2023-05-03 09:59:39 +00:00
Text = header,
2022-12-20 16:37:16 +00:00
Font = OsuFont.Torus.With(size: 25),
Margin = new MarginPadding { Bottom = 10 }
},
new OsuSpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Text = UsersStrings.ReportReason,
},
new Container
{
RelativeSizeAxes = Axes.X,
Height = 40,
2023-05-06 12:52:33 +00:00
Child = reasonDropdown = new OsuEnumDropdown<TReportReason>
2022-12-20 16:37:16 +00:00
{
RelativeSizeAxes = Axes.X
}
},
new OsuSpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Text = UsersStrings.ReportComments,
},
commentsTextBox = new OsuTextBox
{
RelativeSizeAxes = Axes.X,
PlaceholderText = UsersStrings.ReportPlaceholder,
},
submitButton = new RoundedButton
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Width = 200,
BackgroundColour = colours.Red3,
Text = UsersStrings.ReportActionsSend,
Action = () =>
{
Action?.Invoke(reasonDropdown.Current.Value, commentsTextBox.Text);
this.HidePopover();
},
Margin = new MarginPadding { Bottom = 5, Top = 10 },
}
}
};
commentsTextBox.Current.BindValueChanged(_ => updateStatus());
reasonDropdown.Current.BindValueChanged(_ => updateStatus());
updateStatus();
}
private void updateStatus()
{
2023-05-06 13:34:55 +00:00
submitButton.Enabled.Value = !string.IsNullOrWhiteSpace(commentsTextBox.Current.Value) || !IsCommentRequired(reasonDropdown.Current.Value);
}
2023-05-06 12:52:33 +00:00
/// <summary>
/// Determines whether an additional comment is required for submitting the report with the supplied <paramref name="reason"/>.
/// </summary>
2023-05-06 13:34:55 +00:00
protected virtual bool IsCommentRequired(TReportReason reason) => true;
2022-12-20 16:37:16 +00:00
}
}