2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-05-17 15:36:24 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-06-20 13:54:23 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-05-17 15:36:24 +08:00
|
|
|
|
using osu.Framework.Input;
|
2017-06-27 20:05:49 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2017-05-17 15:36:24 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
2017-06-27 20:05:49 +08:00
|
|
|
|
public class IconButton : OsuClickableContainer
|
2017-05-17 15:36:24 +08:00
|
|
|
|
{
|
2017-10-10 16:20:23 +08:00
|
|
|
|
private const float button_size = 30;
|
|
|
|
|
|
2017-10-10 17:04:41 +08:00
|
|
|
|
private Color4? flashColour;
|
2017-10-10 16:25:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The colour that should be flashed when the <see cref="IconButton"/> is clicked.
|
|
|
|
|
/// </summary>
|
2017-10-10 17:04:41 +08:00
|
|
|
|
public Color4 FlashColour
|
|
|
|
|
{
|
|
|
|
|
get { return flashColour ?? Color4.White; }
|
|
|
|
|
set { flashColour = value; }
|
|
|
|
|
}
|
2017-05-17 15:36:24 +08:00
|
|
|
|
|
2017-10-12 17:17:23 +08:00
|
|
|
|
private Color4? iconColour;
|
2017-10-10 16:25:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon colour. This does not affect <see cref="IconButton.Colour"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color4 IconColour
|
|
|
|
|
{
|
2017-10-12 17:17:23 +08:00
|
|
|
|
get { return iconColour ?? Color4.White; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
iconColour = value;
|
|
|
|
|
icon.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Color4? iconHoverColour;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon colour while the <see cref="IconButton"/> is hovered.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color4 IconHoverColour
|
|
|
|
|
{
|
|
|
|
|
get { return iconHoverColour ?? IconColour; }
|
|
|
|
|
set { iconHoverColour = value; }
|
2017-10-10 16:25:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 17:04:41 +08:00
|
|
|
|
private Color4? hoverColour;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The background colour of the <see cref="IconButton"/> while it is hovered.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color4 HoverColour
|
|
|
|
|
{
|
|
|
|
|
get { return hoverColour ?? Color4.White; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
hoverColour = value;
|
|
|
|
|
hover.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:25:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon.
|
|
|
|
|
/// </summary>
|
2017-05-17 15:36:24 +08:00
|
|
|
|
public FontAwesome Icon
|
|
|
|
|
{
|
|
|
|
|
get { return icon.Icon; }
|
|
|
|
|
set { icon.Icon = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:25:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon scale. This does not affect <see cref="IconButton.Scale"/>.
|
|
|
|
|
/// </summary>
|
2017-05-17 15:36:24 +08:00
|
|
|
|
public Vector2 IconScale
|
|
|
|
|
{
|
|
|
|
|
get { return icon.Scale; }
|
|
|
|
|
set { icon.Scale = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:25:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The size of the <see cref="IconButton"/> while it is not being pressed.
|
|
|
|
|
/// </summary>
|
2017-10-10 16:20:23 +08:00
|
|
|
|
public Vector2 ButtonSize
|
|
|
|
|
{
|
|
|
|
|
get { return content.Size; }
|
|
|
|
|
set { content.Size = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Container content;
|
|
|
|
|
private readonly SpriteIcon icon;
|
|
|
|
|
private readonly Box hover;
|
|
|
|
|
|
2017-05-30 17:23:53 +08:00
|
|
|
|
public IconButton()
|
2017-05-17 15:36:24 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-05-19 21:08:33 +08:00
|
|
|
|
content = new Container
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
2017-08-03 13:36:21 +08:00
|
|
|
|
Size = new Vector2(button_size),
|
2017-05-30 00:00:29 +08:00
|
|
|
|
CornerRadius = 5,
|
|
|
|
|
Masking = true,
|
2017-06-12 11:48:47 +08:00
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
2017-05-19 21:08:33 +08:00
|
|
|
|
{
|
|
|
|
|
Colour = Color4.Black.Opacity(0.04f),
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Radius = 5,
|
|
|
|
|
},
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
hover = new Box
|
2017-05-17 15:36:24 +08:00
|
|
|
|
{
|
2017-05-19 21:08:33 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Alpha = 0,
|
2017-05-17 15:36:24 +08:00
|
|
|
|
},
|
2017-08-03 13:36:21 +08:00
|
|
|
|
icon = new SpriteIcon
|
2017-05-17 15:36:24 +08:00
|
|
|
|
{
|
2017-05-19 21:08:33 +08:00
|
|
|
|
Origin = Anchor.Centre,
|
2017-05-30 00:00:29 +08:00
|
|
|
|
Anchor = Anchor.Centre,
|
2017-08-03 13:36:21 +08:00
|
|
|
|
Size = new Vector2(18),
|
2017-05-17 15:36:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-19 21:08:33 +08:00
|
|
|
|
}
|
2017-05-17 15:36:24 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
2017-10-10 17:04:41 +08:00
|
|
|
|
if (hoverColour == null)
|
2017-10-10 17:31:56 +08:00
|
|
|
|
HoverColour = colours.Yellow.Opacity(0.6f);
|
2017-10-10 17:04:41 +08:00
|
|
|
|
|
|
|
|
|
if (flashColour == null)
|
2017-10-10 17:31:56 +08:00
|
|
|
|
FlashColour = colours.Yellow;
|
2017-06-21 18:17:59 +08:00
|
|
|
|
|
2017-08-02 10:56:29 +08:00
|
|
|
|
Enabled.ValueChanged += enabled => this.FadeColour(enabled ? Color4.White : colours.Gray9, 200, Easing.OutQuint);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
hover.FadeIn(500, Easing.OutQuint);
|
2017-10-12 17:17:23 +08:00
|
|
|
|
icon.FadeColour(IconHoverColour, 500, Easing.OutQuint);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
return base.OnHover(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
hover.FadeOut(500, Easing.OutQuint);
|
2017-10-12 17:17:23 +08:00
|
|
|
|
icon.FadeColour(IconColour, 500, Easing.OutQuint);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
base.OnHoverLost(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnClick(InputState state)
|
|
|
|
|
{
|
2017-10-10 16:20:23 +08:00
|
|
|
|
hover.FlashColour(FlashColour, 800, Easing.OutQuint);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
return base.OnClick(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
content.ScaleTo(0.75f, 2000, Easing.OutQuint);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
return base.OnMouseDown(state, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
content.ScaleTo(1, 1000, Easing.OutElastic);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
return base.OnMouseUp(state, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|