1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-31 05:32:57 +08:00

Rebase CommitButton to RoundedButton

This commit is contained in:
ansel 2022-11-29 06:17:02 +03:00
parent a874345da0
commit 894fb98fa2

View File

@ -11,11 +11,12 @@ using osu.Framework.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osuTK.Graphics; using osuTK.Graphics;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using System.Collections.Generic;
using System; using System;
using osuTK; using osuTK;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Input.Events;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Game.Graphics.UserInterfaceV2;
namespace osu.Game.Overlays.Comments namespace osu.Game.Overlays.Comments
{ {
@ -99,8 +100,9 @@ namespace osu.Game.Overlays.Comments
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0), Spacing = new Vector2(5, 0),
Child = commitButton = new CommitButton(CommitButtonText) Child = commitButton = new CommitButton
{ {
Text = CommitButtonText,
Anchor = Anchor.CentreRight, Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight, Origin = Anchor.CentreRight,
Action = () => Action = () =>
@ -118,7 +120,7 @@ namespace osu.Game.Overlays.Comments
textBox.OnCommit += (_, _) => textBox.OnCommit += (_, _) =>
{ {
if (commitButton.IsBlocked.Value) if (commitButton.IsLoading)
return; return;
commitButton.TriggerClick(); commitButton.TriggerClick();
@ -166,84 +168,72 @@ namespace osu.Game.Overlays.Comments
}; };
} }
private partial class CommitButton : LoadingButton private sealed partial class CommitButton : RoundedButton
{ {
private const int duration = 200; private const int duration = 200;
private bool isLoading;
private readonly LoadingSpinner spinner;
public readonly BindableBool IsBlocked = new BindableBool(); public readonly BindableBool IsBlocked = new BindableBool();
private OsuSpriteText text = null!;
public override bool PropagatePositionalInputSubTree => !IsBlocked.Value && base.PropagatePositionalInputSubTree; public CommitButton()
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
private readonly LocalisableString text;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
private OsuSpriteText drawableText = null!;
private Box background = null!;
private Box blockedBackground = null!;
public CommitButton(LocalisableString text)
{ {
this.text = text; Height = 25;
AutoSizeAxes = Axes.X;
AutoSizeAxes = Axes.Both; Add(spinner = new LoadingSpinner
LoadingAnimationSize = new Vector2(10);
}
[BackgroundDependencyLoader]
private void load()
{ {
IdleColour = colourProvider.Light4; Anchor = Anchor.Centre,
HoverColour = colourProvider.Light3; Origin = Anchor.Centre,
blockedBackground.Colour = colourProvider.Background5; Size = new Vector2(12),
Depth = -2,
});
} }
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
IsBlocked.BindValueChanged(onBlockedStateChanged, true); IsBlocked.BindValueChanged(e =>
{
Enabled.Value = !IsLoading && !e.NewValue;
}, true);
} }
private void onBlockedStateChanged(ValueChangedEvent<bool> isBlocked) protected override SpriteText CreateText() => text = new OsuSpriteText
{
drawableText.FadeColour(isBlocked.NewValue ? colourProvider.Foreground1 : Color4.White, duration, Easing.OutQuint);
background.FadeTo(isBlocked.NewValue ? 0 : 1, duration, Easing.OutQuint);
}
protected override Drawable CreateContent() => new CircularContainer
{
Masking = true,
Height = 25,
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
blockedBackground = new Box
{
RelativeSizeAxes = Axes.Both
},
background = new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0
},
drawableText = new OsuSpriteText
{ {
AlwaysPresent = true, AlwaysPresent = true,
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold), Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
Margin = new MarginPadding { Horizontal = 20 }, Margin = new MarginPadding { Horizontal = 20 },
Text = text,
}
}
}; };
protected override void OnLoadStarted() => drawableText.FadeOut(duration, Easing.OutQuint); 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);
}
}
protected override void OnLoadFinished() => drawableText.FadeIn(duration, Easing.OutQuint); protected override bool OnClick(ClickEvent e)
{
if (!Enabled.Value)
return false;
try
{
return base.OnClick(e);
}
finally
{
// run afterwards as this will disable this button.
IsLoading = true;
}
}
} }
} }
} }