1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 13:27:23 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/LoadingButton.cs

98 lines
2.4 KiB
C#
Raw Normal View History

2019-10-17 18:57:17 +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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Graphics.Containers;
using osuTK;
namespace osu.Game.Graphics.UserInterface
{
public abstract class LoadingButton : OsuHoverContainer
{
private const float fade_duration = 200;
private bool isLoading;
public bool IsLoading
{
get => isLoading;
set
{
isLoading = value;
Enabled.Value = !isLoading;
if (value)
{
loading.Show();
2019-10-23 18:39:42 +08:00
text.FadeOut(fade_duration, Easing.OutQuint);
2019-10-17 18:57:17 +08:00
OnLoadingStart();
}
else
{
loading.Hide();
2019-10-23 18:39:42 +08:00
text.FadeIn(fade_duration, Easing.OutQuint);
2019-10-17 18:57:17 +08:00
OnLoadingFinished();
}
}
}
public Vector2 LoadingAnimationSize
{
get => loading.Size;
set => loading.Size = value;
}
private readonly LoadingAnimation loading;
2019-10-23 18:39:42 +08:00
private readonly Drawable text;
2019-10-17 18:57:17 +08:00
2019-10-17 20:24:51 +08:00
protected LoadingButton()
2019-10-17 18:57:17 +08:00
{
2019-10-23 18:39:42 +08:00
Container content;
2019-10-17 20:24:51 +08:00
2019-10-23 18:39:42 +08:00
Child = content = CreateContent();
2019-10-17 18:57:17 +08:00
2019-10-23 18:39:42 +08:00
content.AddRange(new[]
2019-10-17 18:57:17 +08:00
{
2019-10-23 18:39:42 +08:00
text = CreateText(),
2019-10-17 18:57:17 +08:00
loading = new LoadingAnimation
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(12)
}
});
}
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;
}
}
protected virtual void OnLoadingStart()
{
}
protected virtual void OnLoadingFinished()
{
}
2019-10-23 18:39:42 +08:00
protected abstract Container CreateContent();
2019-10-17 18:57:17 +08:00
2019-10-23 18:39:42 +08:00
protected abstract Drawable CreateText();
2019-10-17 18:57:17 +08:00
}
}