1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 14:07:25 +08:00
osu-lazer/osu.Game/Overlays/Comments/CommentEditor.cs

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

228 lines
8.1 KiB
C#
Raw Normal View History

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;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2020-02-12 01:08:24 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites;
using osuTK.Graphics;
2020-02-12 05:57:06 +08:00
using osu.Game.Graphics.UserInterface;
using System;
using osuTK;
using osu.Framework.Bindables;
2022-11-29 10:13:54 +08:00
using osu.Framework.Localisation;
2022-11-29 11:17:02 +08:00
using osu.Game.Graphics.UserInterfaceV2;
2020-02-11 23:46:49 +08:00
namespace osu.Game.Overlays.Comments
{
public abstract partial class CommentEditor : CompositeDrawable
{
2020-02-12 01:08:24 +08:00
private const int side_padding = 8;
2022-11-29 10:17:44 +08:00
public Action<string>? OnCommit;
2020-02-12 05:57:06 +08:00
/// <summary>
2022-11-29 19:39:59 +08:00
/// Whether editor is waiting for submit action to complete.
/// </summary>
public bool IsSubmitting => CommitButton.IsLoading;
2020-02-12 07:05:45 +08:00
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:13:54 +08:00
protected abstract LocalisableString CommitButtonText { get; }
2020-02-12 01:08:24 +08:00
2022-11-29 10:13:54 +08:00
protected abstract LocalisableString TextBoxPlaceholder { get; }
2020-02-11 23:46:49 +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
2020-02-13 09:06:34 +08:00
protected readonly Bindable<string> Current = new Bindable<string>();
protected EditorCommitButton CommitButton = null!;
2020-02-11 23:46:49 +08:00
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
2020-02-14 04:04:53 +08:00
EditorTextBox textBox;
2020-02-12 05:57:06 +08:00
2020-02-11 23:46:49 +08:00
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[]
{
2020-02-14 04:04:53 +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,
2020-02-14 04:04:53 +08:00
PlaceholderText = TextBoxPlaceholder,
2020-02-13 09:06:34 +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,
Height = 40,
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,
Font = OsuFont.GetFont(size: 14, weight: FontWeight.SemiBold),
2020-02-12 01:08:24 +08:00
Text = FooterText
2020-02-12 01:47:51 +08:00
},
ButtonsContainer = new FillFlowContainer
{
2022-11-29 10:17:44 +08:00
Name = @"Buttons",
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),
Child = CommitButton = new EditorCommitButton
2020-02-12 05:57:06 +08:00
{
2022-11-29 11:17:02 +08:00
Text = CommitButtonText,
2020-02-12 05:57:06 +08:00
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
2020-02-12 18:43:56 +08:00
Action = () =>
{
2020-02-13 09:06:34 +08:00
OnCommit?.Invoke(Current.Value);
Current.Value = string.Empty;
2020-02-12 18:43:56 +08:00
}
2020-02-12 05:57:06 +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
2022-06-24 20:25:23 +08:00
textBox.OnCommit += (_, _) =>
{
if (CommitButton.IsLoading)
return;
CommitButton.TriggerClick();
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Current.BindValueChanged(text => CommitButton.IsBlocked.Value = string.IsNullOrEmpty(text.NewValue), true);
2020-02-11 23:46:49 +08:00
}
2020-02-14 04:04:53 +08:00
private partial class EditorTextBox : BasicTextBox
2020-02-12 01:08:24 +08:00
{
protected override float LeftRightPadding => side_padding;
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;
BackgroundCommit = colourProvider.Background3;
2020-02-12 01:08:24 +08:00
}
protected override SpriteText CreatePlaceholder() => placeholder = new OsuSpriteText
{
Font = OsuFont.GetFont(weight: FontWeight.Regular),
};
protected override Drawable GetDrawableCharacter(char c) => new FallingDownContainer
{
AutoSizeAxes = Axes.Both,
Child = new OsuSpriteText { Text = c.ToString(), Font = OsuFont.GetFont(size: CalculatedTextSize) },
};
2020-02-12 01:08:24 +08:00
}
2020-02-12 05:57:06 +08:00
protected sealed partial class EditorCommitButton : RoundedButton
2020-02-12 05:57:06 +08:00
{
private const int duration = 200;
2022-11-29 11:23:25 +08:00
2022-11-29 11:17:02 +08:00
private readonly LoadingSpinner spinner;
private OsuSpriteText text = null!;
2022-11-29 11:23:25 +08:00
public readonly BindableBool IsBlocked = new BindableBool();
private bool isLoading;
2022-11-29 19:39:59 +08:00
/// <summary>
/// Whether loading spinner shown.
/// </summary>
2022-11-29 11:23:25 +08:00
public bool IsLoading
{
get => isLoading;
set
{
isLoading = value;
Enabled.Value = !value && !IsBlocked.Value;
spinner.FadeTo(value ? 1f : 0f, duration, Easing.OutQuint);
text.FadeTo(value ? 0f : 1f, duration, Easing.OutQuint);
}
}
public EditorCommitButton()
2020-02-12 05:57:06 +08:00
{
2022-11-30 18:11:54 +08:00
Width = 90;
2022-11-29 11:17:02 +08:00
Height = 25;
Add(spinner = new LoadingSpinner
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2022-11-29 11:19:54 +08:00
Size = new Vector2(14),
2022-11-29 11:17:02 +08:00
Depth = -2,
});
2020-02-12 05:57:06 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2022-11-29 11:17:02 +08:00
IsBlocked.BindValueChanged(e =>
{
Enabled.Value = !IsLoading && !e.NewValue;
}, true);
}
2022-11-29 11:17:02 +08:00
protected override SpriteText CreateText() => text = new OsuSpriteText
{
2022-11-29 11:17:02 +08:00
AlwaysPresent = true,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
Margin = new MarginPadding { Horizontal = 20 },
};
2020-02-12 05:57:06 +08:00
}
2020-02-11 23:46:49 +08:00
}
}