osu/osu.Game/Overlays/Comments/CommentEditor.cs

249 lines
8.9 KiB
C#
Raw Normal View History

2020-02-11 15:46:49 +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 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-11 17:08:24 +00:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites;
using osuTK.Graphics;
2020-02-11 21:57:06 +00:00
using osu.Game.Graphics.UserInterface;
using System.Collections.Generic;
using System;
using osuTK;
using osu.Framework.Bindables;
2020-02-11 15:46:49 +00:00
namespace osu.Game.Overlays.Comments
{
public abstract class CommentEditor : CompositeDrawable
{
2020-02-11 17:08:24 +00:00
private const int side_padding = 8;
2020-02-11 21:57:06 +00:00
public Action<string> OnCommit;
2020-02-11 23:05:45 +00:00
public bool IsLoading
{
get => commitButton.IsLoading;
set => commitButton.IsLoading = value;
}
2020-02-11 17:08:24 +00:00
protected abstract string FooterText { get; }
protected abstract string CommitButtonText { get; }
2020-02-13 20:04:53 +00:00
protected abstract string TextBoxPlaceholder { get; }
2020-02-11 15:46:49 +00:00
2020-02-21 03:33:49 +00:00
protected FillFlowContainer ButtonsContainer { get; private set; }
2020-02-11 17:47:51 +00:00
2020-02-13 01:06:34 +00:00
protected readonly Bindable<string> Current = new Bindable<string>();
private CommitButton commitButton;
2020-02-11 15:46:49 +00:00
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
2020-02-13 20:04:53 +00:00
EditorTextBox textBox;
2020-02-11 21:57:06 +00:00
2020-02-11 15:46:49 +00:00
RelativeSizeAxes = Axes.X;
2020-02-11 17:08:24 +00:00
AutoSizeAxes = Axes.Y;
2020-02-11 15:46:49 +00:00
Masking = true;
CornerRadius = 6;
2020-02-11 17:08:24 +00:00
BorderThickness = 3;
BorderColour = colourProvider.Background3;
2020-02-11 15:46:49 +00:00
AddRangeInternal(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background3
},
2020-02-11 17:08:24 +00:00
new FillFlowContainer
2020-02-11 15:46:49 +00:00
{
2020-02-11 17:08:24 +00:00
AutoSizeAxes = Axes.Y,
2020-02-11 15:46:49 +00:00
RelativeSizeAxes = Axes.X,
2020-02-11 17:08:24 +00:00
Direction = FillDirection.Vertical,
2020-02-11 15:46:49 +00:00
Children = new Drawable[]
{
2020-02-13 20:04:53 +00:00
textBox = new EditorTextBox
2020-02-11 17:08:24 +00:00
{
2020-02-11 17:11:22 +00:00
Height = 40,
2020-02-11 17:08:24 +00:00
RelativeSizeAxes = Axes.X,
2020-02-13 20:04:53 +00:00
PlaceholderText = TextBoxPlaceholder,
2020-02-13 01:06:34 +00:00
Current = Current
2020-02-11 17:08:24 +00:00
},
new Container
2020-02-11 15:46:49 +00:00
{
2020-02-11 17:08:24 +00:00
Name = "Footer",
RelativeSizeAxes = Axes.X,
2020-02-11 17:11:22 +00:00
Height = 35,
2020-02-11 17:08:24 +00:00
Padding = new MarginPadding { Horizontal = side_padding },
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold),
Text = FooterText
2020-02-11 17:47:51 +00:00
},
ButtonsContainer = new FillFlowContainer
{
Name = "Buttons",
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
2020-02-11 21:57:06 +00:00
Spacing = new Vector2(5, 0),
Child = commitButton = new CommitButton(CommitButtonText)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
2020-02-12 10:43:56 +00:00
Action = () =>
{
2020-02-13 01:06:34 +00:00
OnCommit?.Invoke(Current.Value);
Current.Value = string.Empty;
2020-02-12 10:43:56 +00:00
}
2020-02-11 21:57:06 +00:00
}
2020-02-11 17:08:24 +00:00
}
}
2020-02-11 15:46:49 +00:00
}
}
}
});
2020-02-11 21:57:06 +00:00
2020-02-13 20:04:53 +00:00
textBox.OnCommit += (u, v) =>
{
2020-02-11 23:05:45 +00:00
if (commitButton.IsBlocked.Value)
return;
2021-08-04 08:27:44 +00:00
commitButton.TriggerClick();
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-02-13 01:06:34 +00:00
Current.BindValueChanged(text => commitButton.IsBlocked.Value = string.IsNullOrEmpty(text.NewValue), true);
2020-02-11 15:46:49 +00:00
}
2020-02-13 20:04:53 +00:00
private class EditorTextBox : BasicTextBox
2020-02-11 17:08:24 +00:00
{
protected override float LeftRightPadding => side_padding;
protected override Color4 SelectionColour => Color4.Gray;
2020-02-11 15:46:49 +00:00
2020-02-11 17:08:24 +00:00
private OsuSpriteText placeholder;
2020-02-13 20:04:53 +00:00
public EditorTextBox()
2020-02-11 17:08:24 +00: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-11 17:08:24 +00: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-11 17:08:24 +00:00
}
2020-02-11 21:57:06 +00:00
private class CommitButton : LoadingButton
{
private const int duration = 200;
2020-02-11 23:05:45 +00:00
public readonly BindableBool IsBlocked = new BindableBool();
2020-02-11 23:05:45 +00:00
public override bool PropagatePositionalInputSubTree => !IsBlocked.Value && base.PropagatePositionalInputSubTree;
2020-02-11 21:57:06 +00:00
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
private readonly string text;
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
2020-02-11 21:57:06 +00:00
private OsuSpriteText drawableText;
private Box background;
2020-02-12 10:43:56 +00:00
private Box blockedBackground;
2020-02-11 21:57:06 +00:00
public CommitButton(string text)
{
this.text = text;
2020-02-11 21:57:06 +00:00
AutoSizeAxes = Axes.Both;
LoadingAnimationSize = new Vector2(10);
}
[BackgroundDependencyLoader]
private void load()
2020-02-11 21:57:06 +00:00
{
2020-02-12 10:28:49 +00:00
IdleColour = colourProvider.Light4;
HoverColour = colourProvider.Light3;
2020-02-12 10:43:56 +00:00
blockedBackground.Colour = colourProvider.Background5;
2020-02-11 21:57:06 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-02-11 23:05:45 +00:00
IsBlocked.BindValueChanged(onBlockedStateChanged, true);
}
2020-02-11 23:05:45 +00:00
private void onBlockedStateChanged(ValueChangedEvent<bool> isBlocked)
{
2020-02-11 23:05:45 +00:00
drawableText.FadeColour(isBlocked.NewValue ? colourProvider.Foreground1 : Color4.White, duration, Easing.OutQuint);
2020-02-12 10:43:56 +00:00
background.FadeTo(isBlocked.NewValue ? 0 : 1, duration, Easing.OutQuint);
}
2020-02-11 21:57:06 +00:00
protected override Drawable CreateContent() => new CircularContainer
{
Masking = true,
Height = 25,
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
2020-02-12 10:43:56 +00:00
blockedBackground = new Box
{
RelativeSizeAxes = Axes.Both
},
2020-02-11 21:57:06 +00:00
background = new Box
{
2020-02-13 01:06:34 +00:00
RelativeSizeAxes = Axes.Both,
Alpha = 0
2020-02-11 21:57:06 +00:00
},
drawableText = new OsuSpriteText
{
AlwaysPresent = true,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
Margin = new MarginPadding { Horizontal = 20 },
2021-11-05 07:12:41 +00:00
Text = text,
2020-02-11 21:57:06 +00:00
}
}
};
protected override void OnLoadStarted() => drawableText.FadeOut(duration, Easing.OutQuint);
protected override void OnLoadFinished() => drawableText.FadeIn(duration, Easing.OutQuint);
}
2020-02-11 15:46:49 +00:00
}
}