osu/osu.Game/Screens/Select/FooterButton.cs

146 lines
4.2 KiB
C#
Raw Normal View History

// 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.
2018-04-13 09:19:50 +00:00
using System;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.Containers;
namespace osu.Game.Screens.Select
{
public class FooterButton : OsuClickableContainer
{
private static readonly Vector2 shearing = new Vector2(0.15f, 0);
public string Text
{
get => spriteText?.Text;
2018-04-13 09:19:50 +00:00
set
{
if (spriteText != null)
spriteText.Text = value;
}
}
private Color4 deselectedColour;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
public Color4 DeselectedColour
{
get => deselectedColour;
2018-04-13 09:19:50 +00:00
set
{
deselectedColour = value;
if (light.Colour != SelectedColour)
light.Colour = value;
}
}
private Color4 selectedColour;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
public Color4 SelectedColour
{
get => selectedColour;
2018-04-13 09:19:50 +00:00
set
{
selectedColour = value;
box.Colour = selectedColour;
}
}
private readonly SpriteText spriteText;
private readonly Box box;
private readonly Box light;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => box.ReceivePositionalInputAt(screenSpacePos);
2018-07-03 05:03:17 +00:00
2018-04-13 09:19:50 +00:00
public FooterButton()
{
AutoSizeAxes = Axes.Both;
2018-04-13 09:19:50 +00:00
Children = new Drawable[]
{
new Container
{
Size = new Vector2(100, 50),
Child = spriteText = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
},
2018-04-13 09:19:50 +00:00
box = new Box
{
RelativeSizeAxes = Axes.Both,
Shear = shearing,
EdgeSmoothness = new Vector2(2, 0),
Colour = Color4.White,
Alpha = 0,
},
light = new Box
{
Shear = shearing,
Height = 4,
EdgeSmoothness = new Vector2(2, 0),
RelativeSizeAxes = Axes.X,
},
};
}
public Action Hovered;
public Action HoverLost;
public Key? Hotkey;
2018-10-02 03:02:47 +00:00
protected override bool OnHover(HoverEvent e)
2018-04-13 09:19:50 +00:00
{
Hovered?.Invoke();
light.ScaleTo(new Vector2(1, 2), Footer.TRANSITION_LENGTH, Easing.OutQuint);
light.FadeColour(SelectedColour, Footer.TRANSITION_LENGTH, Easing.OutQuint);
return true;
}
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 09:19:50 +00:00
{
HoverLost?.Invoke();
light.ScaleTo(new Vector2(1, 1), Footer.TRANSITION_LENGTH, Easing.OutQuint);
light.FadeColour(DeselectedColour, Footer.TRANSITION_LENGTH, Easing.OutQuint);
}
2018-10-02 03:02:47 +00:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 09:19:50 +00:00
{
box.FadeTo(0.3f, Footer.TRANSITION_LENGTH * 2, Easing.OutQuint);
2018-10-02 03:02:47 +00:00
return base.OnMouseDown(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnMouseUp(MouseUpEvent e)
2018-04-13 09:19:50 +00:00
{
box.FadeOut(Footer.TRANSITION_LENGTH, Easing.OutQuint);
2018-10-02 03:02:47 +00:00
return base.OnMouseUp(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnClick(ClickEvent e)
2018-04-13 09:19:50 +00:00
{
box.ClearTransforms();
box.Alpha = 1;
box.FadeOut(Footer.TRANSITION_LENGTH * 3, Easing.OutQuint);
2018-10-02 03:02:47 +00:00
return base.OnClick(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 09:19:50 +00:00
{
2018-10-02 03:02:47 +00:00
if (!e.Repeat && e.Key == Hotkey)
2018-04-13 09:19:50 +00:00
{
Click();
2018-04-13 09:19:50 +00:00
return true;
}
2018-10-02 03:02:47 +00:00
return base.OnKeyDown(e);
2018-04-13 09:19:50 +00:00
}
}
}