1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:07:26 +08:00
osu-lazer/osu.Game/Screens/Select/FooterButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

218 lines
6.6 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 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2017-03-14 16:58:34 +08:00
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select
{
public partial class FooterButton : OsuClickableContainer, IKeyBindingHandler<GlobalAction>
{
2019-11-12 20:07:01 +08:00
public const float SHEAR_WIDTH = 7.5f;
2019-08-07 11:20:49 +08:00
protected static readonly Vector2 SHEAR = new Vector2(SHEAR_WIDTH / Footer.HEIGHT, 0);
2018-04-13 17:19:50 +08:00
public LocalisableString Text
{
2021-02-25 13:06:21 +08:00
get => SpriteText?.Text ?? default;
set
{
2019-04-14 07:55:15 +08:00
if (SpriteText != null)
SpriteText.Text = value;
}
}
2018-04-13 17:19:50 +08:00
private Color4 deselectedColour;
2019-02-28 12:31:40 +08:00
public Color4 DeselectedColour
{
get => deselectedColour;
set
{
deselectedColour = value;
2017-03-14 16:58:34 +08:00
if (light.Colour != SelectedColour)
light.Colour = value;
}
}
2018-04-13 17:19:50 +08:00
private Color4 selectedColour;
2019-02-28 12:31:40 +08:00
public Color4 SelectedColour
{
get => selectedColour;
set
{
selectedColour = value;
box.Colour = selectedColour;
}
}
2018-04-13 17:19:50 +08:00
protected FillFlowContainer ButtonContentContainer;
protected readonly Container TextContainer;
2019-04-14 07:55:15 +08:00
protected readonly SpriteText SpriteText;
private readonly Box box;
private readonly Box light;
2018-04-13 17:19:50 +08:00
public FooterButton()
{
AutoSizeAxes = Axes.Both;
2019-08-07 11:20:49 +08:00
Shear = SHEAR;
Children = new Drawable[]
{
box = new Box
{
RelativeSizeAxes = Axes.Both,
EdgeSmoothness = new Vector2(2, 0),
Colour = Color4.White,
Alpha = 0,
},
light = new Box
{
Height = 4,
EdgeSmoothness = new Vector2(2, 0),
RelativeSizeAxes = Axes.X,
},
new Container
2019-08-06 09:14:41 +08:00
{
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
2019-08-06 09:14:41 +08:00
{
ButtonContentContainer = new FillFlowContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Direction = FillDirection.Horizontal,
Shear = -SHEAR,
AutoSizeAxes = Axes.X,
Height = 50,
Spacing = new Vector2(15, 0),
Children = new Drawable[]
{
TextContainer = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Child = SpriteText = new OsuSpriteText
{
AlwaysPresent = true,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
},
},
},
},
2019-08-06 09:14:41 +08:00
},
};
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
Enabled.BindValueChanged(_ => updateDisplay(), true);
}
public Action Hovered;
public Action HoverLost;
public GlobalAction? Hotkey;
2018-04-13 17:19:50 +08:00
private bool mouseDown;
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
float horizontalMargin = (100 - TextContainer.Width) / 2;
ButtonContentContainer.Padding = new MarginPadding
{
Left = horizontalMargin,
// right side margin offset to compensate for shear
Right = horizontalMargin - SHEAR_WIDTH / 2
};
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
{
Hovered?.Invoke();
updateDisplay();
return true;
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
{
HoverLost?.Invoke();
updateDisplay();
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
{
if (!Enabled.Value)
return true;
mouseDown = true;
updateDisplay();
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
}
2018-04-13 17:19:50 +08:00
protected override void OnMouseUp(MouseUpEvent e)
{
mouseDown = false;
updateDisplay();
base.OnMouseUp(e);
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
{
if (!Enabled.Value)
return true;
2017-02-25 20:12:39 +08:00
box.ClearTransforms();
box.Alpha = 1;
2017-07-23 02:50:25 +08:00
box.FadeOut(Footer.TRANSITION_LENGTH * 3, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
return base.OnClick(e);
}
2018-04-13 17:19:50 +08:00
2021-09-16 17:26:12 +08:00
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
2017-03-14 16:58:34 +08:00
{
if (e.Action == Hotkey && !e.Repeat)
2017-03-14 16:58:34 +08:00
{
2021-08-04 16:27:44 +08:00
TriggerClick();
2017-03-14 16:58:34 +08:00
return true;
}
2020-06-04 12:59:04 +08:00
return false;
}
2018-04-13 17:19:50 +08:00
2021-09-16 17:26:12 +08:00
public virtual void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) { }
private void updateDisplay()
{
this.FadeTo(Enabled.Value ? 1 : 0.25f, Footer.TRANSITION_LENGTH, Easing.OutQuint);
light.ScaleTo(Enabled.Value && IsHovered ? new Vector2(1, 2) : new Vector2(1), Footer.TRANSITION_LENGTH, Easing.OutQuint);
light.FadeColour(Enabled.Value && IsHovered ? SelectedColour : DeselectedColour, Footer.TRANSITION_LENGTH, Easing.OutQuint);
box.FadeTo(Enabled.Value & mouseDown ? 0.3f : 0f, Footer.TRANSITION_LENGTH * 2, Easing.OutQuint);
if (Enabled.Value && IsHovered)
Hovered?.Invoke();
else
HoverLost?.Invoke();
}
}
}