mirror of
https://github.com/osukey/osukey.git
synced 2025-07-01 08:20:00 +09:00
Implement LoadingButton component
This commit is contained in:
96
osu.Game/Graphics/UserInterface/LoadingButton.cs
Normal file
96
osu.Game/Graphics/UserInterface/LoadingButton.cs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
// 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();
|
||||||
|
content.FadeOut(fade_duration, Easing.OutQuint);
|
||||||
|
OnLoadingStart();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
loading.Hide();
|
||||||
|
content.FadeIn(fade_duration, Easing.OutQuint);
|
||||||
|
OnLoadingFinished();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector2 LoadingAnimationSize
|
||||||
|
{
|
||||||
|
get => loading.Size;
|
||||||
|
set => loading.Size = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly Container background;
|
||||||
|
private readonly LoadingAnimation loading;
|
||||||
|
private readonly Drawable content;
|
||||||
|
|
||||||
|
public LoadingButton()
|
||||||
|
{
|
||||||
|
Child = background = CreateBackground();
|
||||||
|
|
||||||
|
background.AddRange(new Drawable[]
|
||||||
|
{
|
||||||
|
content = CreateContent(),
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Container CreateBackground();
|
||||||
|
|
||||||
|
protected abstract Drawable CreateContent();
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,6 @@ using osu.Framework.Graphics;
|
|||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
using osu.Framework.Graphics.Sprites;
|
using osu.Framework.Graphics.Sprites;
|
||||||
using osu.Framework.Input.Events;
|
|
||||||
using osu.Game.Graphics.Containers;
|
|
||||||
using osu.Game.Graphics.Sprites;
|
using osu.Game.Graphics.Sprites;
|
||||||
using osuTK;
|
using osuTK;
|
||||||
using osuTK.Graphics;
|
using osuTK.Graphics;
|
||||||
@ -14,10 +12,8 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace osu.Game.Graphics.UserInterface
|
namespace osu.Game.Graphics.UserInterface
|
||||||
{
|
{
|
||||||
public class ShowMoreButton : OsuHoverContainer
|
public class ShowMoreButton : LoadingButton
|
||||||
{
|
{
|
||||||
private const float fade_duration = 200;
|
|
||||||
|
|
||||||
private Color4 chevronIconColour;
|
private Color4 chevronIconColour;
|
||||||
|
|
||||||
protected Color4 ChevronIconColour
|
protected Color4 ChevronIconColour
|
||||||
@ -32,100 +28,53 @@ namespace osu.Game.Graphics.UserInterface
|
|||||||
set => text.Text = value;
|
set => text.Text = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool isLoading;
|
|
||||||
|
|
||||||
public bool IsLoading
|
|
||||||
{
|
|
||||||
get => isLoading;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
isLoading = value;
|
|
||||||
|
|
||||||
Enabled.Value = !isLoading;
|
|
||||||
|
|
||||||
if (value)
|
|
||||||
{
|
|
||||||
loading.Show();
|
|
||||||
content.FadeOut(fade_duration, Easing.OutQuint);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
loading.Hide();
|
|
||||||
content.FadeIn(fade_duration, Easing.OutQuint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly Box background;
|
|
||||||
private readonly LoadingAnimation loading;
|
|
||||||
private readonly FillFlowContainer content;
|
|
||||||
private readonly ChevronIcon leftChevron;
|
|
||||||
private readonly ChevronIcon rightChevron;
|
|
||||||
private readonly SpriteText text;
|
|
||||||
|
|
||||||
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
|
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
|
||||||
|
|
||||||
|
private ChevronIcon leftChevron;
|
||||||
|
private ChevronIcon rightChevron;
|
||||||
|
private SpriteText text;
|
||||||
|
private Box background;
|
||||||
|
|
||||||
public ShowMoreButton()
|
public ShowMoreButton()
|
||||||
{
|
{
|
||||||
AutoSizeAxes = Axes.Both;
|
AutoSizeAxes = Axes.Both;
|
||||||
|
AddInternal(new CircularContainer
|
||||||
|
{
|
||||||
|
Masking = true,
|
||||||
|
Size = new Vector2(140, 30)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Container CreateBackground() => new CircularContainer
|
||||||
|
{
|
||||||
|
Masking = true,
|
||||||
|
Size = new Vector2(140, 30),
|
||||||
|
Child = background = new Box
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
protected override Drawable CreateContent() => new FillFlowContainer
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
AutoSizeAxes = Axes.Both,
|
||||||
|
Direction = FillDirection.Horizontal,
|
||||||
|
Spacing = new Vector2(7),
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
new CircularContainer
|
leftChevron = new ChevronIcon(),
|
||||||
|
text = new OsuSpriteText
|
||||||
{
|
{
|
||||||
Masking = true,
|
Anchor = Anchor.Centre,
|
||||||
Size = new Vector2(140, 30),
|
Origin = Anchor.Centre,
|
||||||
Children = new Drawable[]
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
|
||||||
{
|
Text = "show more".ToUpper(),
|
||||||
background = new Box
|
},
|
||||||
{
|
rightChevron = new ChevronIcon(),
|
||||||
RelativeSizeAxes = Axes.Both,
|
|
||||||
},
|
|
||||||
content = new FillFlowContainer
|
|
||||||
{
|
|
||||||
Anchor = Anchor.Centre,
|
|
||||||
Origin = Anchor.Centre,
|
|
||||||
AutoSizeAxes = Axes.Both,
|
|
||||||
Direction = FillDirection.Horizontal,
|
|
||||||
Spacing = new Vector2(7),
|
|
||||||
Children = new Drawable[]
|
|
||||||
{
|
|
||||||
leftChevron = new ChevronIcon(),
|
|
||||||
text = new OsuSpriteText
|
|
||||||
{
|
|
||||||
Anchor = Anchor.Centre,
|
|
||||||
Origin = Anchor.Centre,
|
|
||||||
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
|
|
||||||
Text = "show more".ToUpper(),
|
|
||||||
},
|
|
||||||
rightChevron = new ChevronIcon(),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ChevronIcon : SpriteIcon
|
private class ChevronIcon : SpriteIcon
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user