2017-05-17 15:36:24 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// 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-21 18:17:59 +08:00
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
2017-05-17 15:36:24 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
2017-05-30 17:23:53 +08:00
|
|
|
|
public class IconButton : ClickableContainer
|
2017-05-17 15:36:24 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly TextAwesome icon;
|
|
|
|
|
private readonly Box hover;
|
|
|
|
|
private readonly Container content;
|
|
|
|
|
|
|
|
|
|
public FontAwesome Icon
|
|
|
|
|
{
|
|
|
|
|
get { return icon.Icon; }
|
|
|
|
|
set { icon.Icon = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const float button_size = 30;
|
|
|
|
|
private Color4 flashColour;
|
|
|
|
|
|
|
|
|
|
public Vector2 IconScale
|
|
|
|
|
{
|
|
|
|
|
get { return icon.Scale; }
|
|
|
|
|
set { icon.Scale = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-21 06:51:32 +08:00
|
|
|
|
private Color4 disabledColour;
|
|
|
|
|
|
2017-05-30 17:23:53 +08:00
|
|
|
|
public IconButton()
|
2017-05-17 15:36:24 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
|
|
Origin = Anchor.Centre;
|
|
|
|
|
Anchor = Anchor.Centre;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-05-19 21:08:33 +08:00
|
|
|
|
content = new Container
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
2017-05-30 00:00:29 +08:00
|
|
|
|
Size = new Vector2 (button_size),
|
|
|
|
|
|
|
|
|
|
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-05-19 21:08:33 +08:00
|
|
|
|
icon = new TextAwesome
|
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,
|
|
|
|
|
TextSize = 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)
|
|
|
|
|
{
|
|
|
|
|
hover.Colour = colours.Yellow.Opacity(0.6f);
|
|
|
|
|
flashColour = colours.Yellow;
|
2017-06-21 06:51:32 +08:00
|
|
|
|
disabledColour = colours.Gray9;
|
2017-06-21 18:17:59 +08:00
|
|
|
|
|
|
|
|
|
Enabled.ValueChanged += enabledChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void enabledChanged(bool newEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (newEnabled)
|
|
|
|
|
{
|
|
|
|
|
// Only fade the colour to white if there is no colour transformation pending
|
|
|
|
|
bool colorTransformation = false;
|
|
|
|
|
foreach (ITransform t in Transforms)
|
|
|
|
|
{
|
|
|
|
|
if (t is TransformColour)
|
|
|
|
|
{
|
|
|
|
|
colorTransformation = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!colorTransformation)
|
|
|
|
|
FadeColour(Color4.White, 200, EasingTypes.OutQuint);
|
|
|
|
|
|
|
|
|
|
if (Hovering)
|
|
|
|
|
OnHover(new InputState());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FadeColour(disabledColour, 200, EasingTypes.OutQuint);
|
|
|
|
|
content.ScaleTo(1, 200, EasingTypes.OutElastic);
|
|
|
|
|
|
|
|
|
|
if (Hovering)
|
|
|
|
|
OnHoverLost(new InputState());
|
|
|
|
|
}
|
2017-05-17 15:36:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
|
{
|
2017-06-21 18:17:59 +08:00
|
|
|
|
if (!Enabled.Value)
|
2017-06-21 06:51:32 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
2017-05-17 15:36:24 +08:00
|
|
|
|
hover.FadeIn(500, EasingTypes.OutQuint);
|
|
|
|
|
return base.OnHover(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
|
{
|
2017-06-21 18:17:59 +08:00
|
|
|
|
if (!Enabled.Value)
|
2017-06-21 06:51:32 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-05-17 15:36:24 +08:00
|
|
|
|
hover.FadeOut(500, EasingTypes.OutQuint);
|
|
|
|
|
base.OnHoverLost(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnClick(InputState state)
|
|
|
|
|
{
|
2017-06-21 08:47:11 +08:00
|
|
|
|
hover.FlashColour(flashColour, 800, EasingTypes.OutQuint);
|
2017-05-17 15:36:24 +08:00
|
|
|
|
return base.OnClick(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
|
|
|
|
{
|
2017-06-21 18:17:59 +08:00
|
|
|
|
if (!Enabled.Value)
|
2017-06-21 06:51:32 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
2017-05-17 15:36:24 +08:00
|
|
|
|
content.ScaleTo(0.75f, 2000, EasingTypes.OutQuint);
|
|
|
|
|
return base.OnMouseDown(state, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
|
|
|
|
|
{
|
2017-06-21 18:17:59 +08:00
|
|
|
|
if (!Enabled.Value)
|
2017-06-21 06:51:32 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
2017-05-17 15:36:24 +08:00
|
|
|
|
content.ScaleTo(1, 1000, EasingTypes.OutElastic);
|
|
|
|
|
return base.OnMouseUp(state, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|