1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 08:32:54 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/DialogButton.cs

283 lines
9.8 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
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Sprites;
using osu.Framework.Extensions.Color4Extensions;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class DialogButton : OsuClickableContainer
{
private const float idle_width = 0.8f;
2018-04-13 17:19:50 +08:00
private const float hover_width = 0.9f;
2019-12-09 13:11:44 +08:00
2018-04-13 17:19:50 +08:00
private const float hover_duration = 500;
private const float click_duration = 200;
public readonly BindableBool Selected = new BindableBool();
private readonly Container backgroundContainer;
private readonly Container colourContainer;
private readonly Container glowContainer;
private readonly Box leftGlow;
private readonly Box centerGlow;
private readonly Box rightGlow;
private readonly Box background;
private readonly SpriteText spriteText;
private Vector2 hoverSpacing => new Vector2(3f, 0f);
public DialogButton()
{
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
backgroundContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 1f,
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = backgroundColour,
},
},
},
glowContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 1f,
Alpha = 0f,
Children = new Drawable[]
{
leftGlow = new Box
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopLeft,
Anchor = Anchor.TopLeft,
Width = 0.125f,
},
centerGlow = new Box
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Width = 0.75f,
},
rightGlow = new Box
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
Width = 0.125f,
},
},
},
new Container
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Masking = true,
Children = new Drawable[]
{
colourContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Width = idle_width,
2018-04-13 17:19:50 +08:00
Masking = true,
MaskingSmoothness = 2,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.2f),
Radius = 5,
},
Colour = ButtonColour,
Shear = new Vector2(0.2f, 0),
Children = new Drawable[]
{
new Box
{
EdgeSmoothness = new Vector2(2, 0),
RelativeSizeAxes = Axes.Both,
},
new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
MaskingSmoothness = 0,
Children = new[]
{
new Triangles
{
RelativeSizeAxes = Axes.Both,
TriangleScale = 4,
ColourDark = OsuColour.Gray(0.88f),
Shear = new Vector2(-0.2f, 0),
},
},
},
},
},
},
},
spriteText = new OsuSpriteText
{
Text = Text,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 28, weight: FontWeight.Bold),
2018-04-13 17:19:50 +08:00
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.1f),
Colour = Color4.White,
},
};
updateGlow();
Selected.ValueChanged += selectionChanged;
}
private Color4 buttonColour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Color4 ButtonColour
{
get => buttonColour;
2018-04-13 17:19:50 +08:00
set
{
buttonColour = value;
updateGlow();
colourContainer.Colour = value;
}
}
private Color4 backgroundColour = OsuColour.Gray(34);
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Color4 BackgroundColour
{
get => backgroundColour;
2018-04-13 17:19:50 +08:00
set
{
backgroundColour = value;
background.Colour = value;
}
}
private string text;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public string Text
{
get => text;
2018-04-13 17:19:50 +08:00
set
{
text = value;
spriteText.Text = Text;
}
}
public float TextSize
{
get => spriteText.Font.Size;
set => spriteText.Font = spriteText.Font.With(size: value);
2018-04-13 17:19:50 +08:00
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => backgroundContainer.ReceivePositionalInputAt(screenSpacePos);
2018-04-13 17:19:50 +08:00
2019-12-09 13:11:44 +08:00
private bool clickAnimating;
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
2019-12-09 13:11:44 +08:00
var flash = new Box
2018-04-13 17:19:50 +08:00
{
2019-12-09 13:11:44 +08:00
RelativeSizeAxes = Axes.Both,
Colour = ButtonColour,
Blending = BlendingParameters.Additive,
Alpha = 0.05f
};
colourContainer.Add(flash);
flash.FadeOutFromOne(100).Expire();
clickAnimating = true;
colourContainer.ResizeWidthTo(colourContainer.Width * 1.05f, 100, Easing.OutQuint)
.OnComplete(_ =>
{
clickAnimating = false;
Selected.TriggerChange();
});
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
return base.OnClick(e);
2018-04-13 17:19:50 +08:00
}
2019-12-09 13:11:44 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
{
colourContainer.ResizeWidthTo(hover_width * 0.98f, click_duration * 4, Easing.OutQuad);
return base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseUpEvent e)
2019-12-09 13:11:44 +08:00
{
if (Selected.Value)
colourContainer.ResizeWidthTo(hover_width, click_duration, Easing.In);
base.OnMouseUp(e);
2019-12-09 13:11:44 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
2018-10-02 11:02:47 +08:00
base.OnHover(e);
2018-04-13 17:19:50 +08:00
Selected.Value = true;
2019-12-09 13:11:44 +08:00
2018-04-13 17:19:50 +08:00
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
Selected.Value = false;
}
2019-02-21 17:56:34 +08:00
private void selectionChanged(ValueChangedEvent<bool> args)
2018-04-13 17:19:50 +08:00
{
2019-12-09 13:11:44 +08:00
if (clickAnimating)
return;
2019-02-21 17:56:34 +08:00
if (args.NewValue)
2018-04-13 17:19:50 +08:00
{
spriteText.TransformSpacingTo(hoverSpacing, hover_duration, Easing.OutElastic);
2019-12-09 13:11:44 +08:00
colourContainer.ResizeWidthTo(hover_width, hover_duration, Easing.OutElastic);
glowContainer.FadeIn(hover_duration, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
else
{
2019-12-09 13:11:44 +08:00
colourContainer.ResizeWidthTo(idle_width, hover_duration, Easing.OutElastic);
2018-04-13 17:19:50 +08:00
spriteText.TransformSpacingTo(Vector2.Zero, hover_duration, Easing.OutElastic);
2019-12-09 13:11:44 +08:00
glowContainer.FadeOut(hover_duration, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
}
private void updateGlow()
{
leftGlow.Colour = ColourInfo.GradientHorizontal(new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f), ButtonColour);
centerGlow.Colour = ButtonColour;
rightGlow.Colour = ColourInfo.GradientHorizontal(ButtonColour, new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f));
}
}
}