osu/osu.Game/Overlays/Pause/PauseButton.cs

234 lines
8.4 KiB
C#
Raw Normal View History

using OpenTK;
2017-01-27 09:24:49 +00:00
using OpenTK.Graphics;
2017-01-27 18:18:57 +00:00
using osu.Framework.Graphics;
2017-01-27 09:24:49 +00:00
using osu.Framework.Graphics.Transformations;
2017-01-27 18:18:57 +00:00
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Containers;
2017-01-27 09:24:49 +00:00
using osu.Framework.Audio.Sample;
using osu.Game.Graphics;
2017-01-27 18:18:57 +00:00
using osu.Game.Graphics.Backgrounds;
2017-01-27 09:24:49 +00:00
2017-01-27 18:18:57 +00:00
namespace osu.Game.Overlays.Pause
{
public class PauseButton : ClickableContainer
2017-01-27 09:24:49 +00:00
{
private const float hoverWidth = 0.9f;
private const float hoverDuration = 500;
private const float glowFadeDuration = 250;
private const float clickDuration = 200;
2017-01-27 09:24:49 +00:00
2017-01-27 18:18:57 +00:00
private Color4 backgroundColour = OsuColour.Gray(34);
2017-01-27 09:24:49 +00:00
private Color4 buttonColour;
public Color4 ButtonColour
{
get
{
return buttonColour;
}
set
{
buttonColour = value;
2017-01-31 13:17:47 +00:00
updateGlow();
if (colourContainer == null) return;
colourContainer.Colour = ButtonColour;
}
}
2017-01-27 18:18:57 +00:00
private string text;
2017-01-27 18:18:57 +00:00
public string Text
2017-01-27 09:24:49 +00:00
{
2017-01-27 18:18:57 +00:00
get
{
return text;
}
set
{
text = value;
if (spriteText == null) return;
spriteText.Text = Text;
2017-01-27 18:18:57 +00:00
}
}
public AudioSample SampleClick, SampleHover;
private Container backgroundContainer, colourContainer, glowContainer;
2017-01-30 12:19:44 +00:00
private Box leftGlow, centerGlow, rightGlow;
private SpriteText spriteText;
2017-01-27 18:18:57 +00:00
private bool didClick; // Used for making sure that the OnMouseDown animation can call instead of OnHoverLost's when clicking
2017-01-27 18:18:57 +00:00
public override bool Contains(Vector2 screenSpacePos) => backgroundContainer.Contains(screenSpacePos);
2017-01-27 09:24:49 +00:00
protected override bool OnMouseDown(Framework.Input.InputState state, MouseDownEventArgs args)
2017-01-27 18:18:57 +00:00
{
didClick = true;
colourContainer.ResizeTo(new Vector2(1.5f, 1f), clickDuration, EasingTypes.In);
flash();
SampleClick?.Play();
Action?.Invoke();
Delay(clickDuration);
Schedule(delegate {
colourContainer.ResizeTo(new Vector2(0.8f, 1f), 0, EasingTypes.None);
spriteText.Spacing = Vector2.Zero;
2017-01-30 20:18:11 +00:00
glowContainer.FadeOut();
});
2017-01-27 09:24:49 +00:00
return true;
}
protected override bool OnClick(Framework.Input.InputState state) => false;
2017-01-27 09:24:49 +00:00
protected override bool OnHover(Framework.Input.InputState state)
{
colourContainer.ResizeTo(new Vector2(hoverWidth, 1f), hoverDuration, EasingTypes.OutElastic);
spriteText.TransformSpacingTo(new Vector2(3f, 0f), hoverDuration, EasingTypes.OutElastic);
glowContainer.FadeIn(glowFadeDuration, EasingTypes.Out);
SampleHover?.Play();
2017-01-27 09:24:49 +00:00
return true;
}
protected override void OnHoverLost(Framework.Input.InputState state)
{
if (!didClick)
{
colourContainer.ResizeTo(new Vector2(0.8f, 1f), hoverDuration, EasingTypes.OutElastic);
spriteText.TransformSpacingTo(Vector2.Zero, hoverDuration, EasingTypes.OutElastic);
glowContainer.FadeOut(glowFadeDuration, EasingTypes.Out);
}
didClick = false;
2017-01-27 09:24:49 +00:00
}
2017-01-27 18:18:57 +00:00
private void flash()
{
var flash = new Box
{
RelativeSizeAxes = Axes.Both
};
colourContainer.Add(flash);
flash.Colour = ButtonColour;
flash.BlendingMode = BlendingMode.Additive;
flash.Alpha = 0.3f;
flash.FadeOutFromOne(clickDuration);
flash.Expire();
}
2017-01-31 13:17:47 +00:00
private void updateGlow()
2017-01-30 12:19:44 +00:00
{
leftGlow.ColourInfo = ColourInfo.GradientHorizontal(new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f), ButtonColour);
centerGlow.Colour = ButtonColour;
rightGlow.ColourInfo = ColourInfo.GradientHorizontal(ButtonColour, new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f));
}
public PauseButton()
2017-01-27 09:39:15 +00:00
{
Children = new Drawable[]
2017-01-27 18:18:57 +00:00
{
backgroundContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 1f,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = backgroundColour
}
2017-01-27 18:18:57 +00:00
}
},
glowContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 1f,
Alpha = 0f,
Children = new Drawable[]
{
2017-01-30 12:19:44 +00:00
leftGlow = new Box
2017-01-27 18:18:57 +00:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopLeft,
Anchor = Anchor.TopLeft,
Width = 0.125f
},
2017-01-30 12:19:44 +00:00
centerGlow = new Box
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Width = 0.75f
2017-01-27 18:18:57 +00:00
},
2017-01-30 12:19:44 +00:00
rightGlow = new Box
2017-01-27 18:18:57 +00:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
Width = 0.125f
}
2017-01-27 18:18:57 +00:00
}
},
new Container
{
2017-01-30 08:43:06 +00:00
RelativeSizeAxes = Axes.Both,
2017-01-27 18:18:57 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Masking = true,
Children = new Drawable[]
{
colourContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Width = 0.8f,
2017-01-27 18:18:57 +00:00
Masking = true,
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.2f),
Radius = 5
2017-01-27 18:18:57 +00:00
},
Colour = ButtonColour,
Shear = new Vector2(0.2f, 0),
2017-01-27 18:18:57 +00:00
Children = new Drawable[]
{
new Box
{
EdgeSmoothness = new Vector2(2, 0),
RelativeSizeAxes = Axes.Both
2017-01-27 18:18:57 +00:00
},
new Triangles
{
BlendingMode = BlendingMode.Additive,
RelativeSizeAxes = Axes.Both,
TriangleScale = 4,
Alpha = 0.05f,
Shear = new Vector2(-0.2f, 0)
}
2017-01-27 18:18:57 +00:00
}
}
2017-01-27 18:18:57 +00:00
}
},
spriteText = new SpriteText
2017-01-27 18:18:57 +00:00
{
Text = Text,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 28,
2017-01-27 18:18:57 +00:00
Font = "Exo2.0-Bold",
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.1f),
Colour = Color4.White
}
};
2017-01-30 12:19:44 +00:00
2017-01-31 13:17:47 +00:00
updateGlow();
2017-01-27 18:18:57 +00:00
}
}
}