1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 06:42:54 +08:00
osu-lazer/osu.Game/Screens/Menu/Button.cs

287 lines
10 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-12-06 17:56:20 +08:00
using System;
using osu.Framework;
2016-12-05 20:09:41 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2016-11-14 16:23:33 +08:00
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
using osu.Framework.Extensions.Color4Extensions;
using osu.Game.Graphics.Containers;
using osu.Framework.Audio.Track;
using osu.Game.Beatmaps.ControlPoints;
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens.Menu
{
/// <summary>
/// Button designed specifically for the osu!next main menu.
/// In order to correctly flow, we have to use a negative margin on the parent container (due to the parallelogram shape).
/// </summary>
public class Button : BeatSyncedContainer, IStateful<ButtonState>
{
2017-09-04 08:10:04 +08:00
public event Action<ButtonState> StateChanged;
private readonly Container iconText;
private readonly Container box;
private readonly Box boxHoverLayer;
private readonly SpriteIcon icon;
private readonly string sampleName;
private readonly Action clickAction;
private readonly Key triggerKey;
private SampleChannel sampleClick;
private SampleChannel sampleHover;
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => box.ReceiveMouseInputAt(screenSpacePos);
public Button(string text, string sampleName, FontAwesome symbol, Color4 colour, Action clickAction = null, float extraWidth = 0, Key triggerKey = Key.Unknown)
{
this.sampleName = sampleName;
this.clickAction = clickAction;
this.triggerKey = triggerKey;
2016-10-22 16:50:42 +08:00
AutoSizeAxes = Axes.Both;
Alpha = 0;
2017-02-07 15:15:45 +08:00
Vector2 boxSize = new Vector2(ButtonSystem.BUTTON_WIDTH + Math.Abs(extraWidth), ButtonSystem.BUTTON_AREA_HEIGHT);
Children = new Drawable[]
{
2016-11-30 13:38:45 +08:00
box = new Container
{
2016-11-30 13:38:45 +08:00
Masking = true,
MaskingSmoothness = 2,
2017-06-12 11:48:47 +08:00
EdgeEffect = new EdgeEffectParameters
2016-11-30 13:38:45 +08:00
{
Type = EdgeEffectType.Shadow,
2017-01-11 02:44:40 +08:00
Colour = Color4.Black.Opacity(0.2f),
2016-11-30 13:38:45 +08:00
Roundness = 5,
Radius = 8,
},
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Scale = new Vector2(0, 1),
Size = boxSize,
2017-02-07 15:15:45 +08:00
Shear = new Vector2(ButtonSystem.WEDGE_WIDTH / boxSize.Y, 0),
2017-06-07 19:53:37 +08:00
Children = new[]
2016-11-30 13:38:45 +08:00
{
new Box
{
EdgeSmoothness = new Vector2(1.5f, 0),
RelativeSizeAxes = Axes.Both,
Colour = colour,
},
boxHoverLayer = new Box
2016-11-30 13:38:45 +08:00
{
EdgeSmoothness = new Vector2(1.5f, 0),
2016-11-30 13:38:45 +08:00
RelativeSizeAxes = Axes.Both,
2017-09-07 21:46:21 +08:00
Blending = BlendingMode.Additive,
Colour = Color4.White,
Alpha = 0,
2016-11-30 13:38:45 +08:00
},
}
},
iconText = new Container
{
AutoSizeAxes = Axes.Both,
Position = new Vector2(extraWidth / 2, 0),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
{
icon = new SpriteIcon
{
Shadow = true,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(30),
Position = new Vector2(0, 0),
Icon = symbol
},
new OsuSpriteText
{
Shadow = true,
AllowMultiline = false,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 16,
Position = new Vector2(0, 35),
Text = text
}
}
}
};
}
private bool rightward;
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
{
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes);
if (!IsHovered) return;
double duration = timingPoint.BeatLength / 2;
2017-07-23 02:50:25 +08:00
icon.RotateTo(rightward ? 10 : -10, duration * 2, Easing.InOutSine);
icon.Animate(
2017-07-23 02:50:25 +08:00
i => i.MoveToY(-10, duration, Easing.Out),
i => i.ScaleTo(1, duration, Easing.Out)
).Then(
2017-07-23 02:50:25 +08:00
i => i.MoveToY(0, duration, Easing.In),
i => i.ScaleTo(new Vector2(1, 0.9f), duration, Easing.In)
);
rightward = !rightward;
}
protected override bool OnHover(InputState state)
{
if (State != ButtonState.Expanded) return true;
sampleHover?.Play();
2017-07-23 02:50:25 +08:00
box.ScaleTo(new Vector2(1.5f, 1), 500, Easing.OutElastic);
double duration = TimeUntilNextBeat;
icon.ClearTransforms();
icon.RotateTo(rightward ? -10 : 10, duration, Easing.InOutSine);
2017-07-23 02:50:25 +08:00
icon.ScaleTo(new Vector2(1, 0.9f), duration, Easing.Out);
return true;
}
protected override void OnHoverLost(InputState state)
{
2017-02-25 20:12:39 +08:00
icon.ClearTransforms();
2017-07-23 02:50:25 +08:00
icon.RotateTo(0, 500, Easing.Out);
icon.MoveTo(Vector2.Zero, 500, Easing.Out);
icon.ScaleTo(Vector2.One, 200, Easing.Out);
if (State == ButtonState.Expanded)
2017-07-23 02:50:25 +08:00
box.ScaleTo(new Vector2(1, 1), 500, Easing.OutElastic);
}
2016-12-05 20:09:41 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
2017-11-25 22:11:18 +08:00
sampleHover = audio.Sample.Get(@"Menu/button-hover");
if (!string.IsNullOrEmpty(sampleName))
2017-07-01 22:10:04 +08:00
sampleClick = audio.Sample.Get($@"Menu/{sampleName}");
2016-12-05 20:09:41 +08:00
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
2017-07-23 02:50:25 +08:00
boxHoverLayer.FadeTo(0.1f, 1000, Easing.OutQuint);
return base.OnMouseDown(state, args);
}
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
2017-07-23 02:50:25 +08:00
boxHoverLayer.FadeTo(0, 1000, Easing.OutQuint);
return base.OnMouseUp(state, args);
}
protected override bool OnClick(InputState state)
{
trigger();
return true;
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (args.Repeat || state.Keyboard.ControlPressed || state.Keyboard.ShiftPressed || state.Keyboard.AltPressed)
return false;
if (triggerKey == args.Key && triggerKey != Key.Unknown)
{
trigger();
return true;
}
return false;
}
private void trigger()
{
sampleClick?.Play();
clickAction?.Invoke();
2017-02-25 20:12:39 +08:00
boxHoverLayer.ClearTransforms();
boxHoverLayer.Alpha = 0.9f;
2017-07-23 02:50:25 +08:00
boxHoverLayer.FadeOut(800, Easing.OutExpo);
}
public override bool HandleInput => state != ButtonState.Exploded && box.Scale.X >= 0.8f;
protected override void Update()
{
iconText.Alpha = MathHelper.Clamp((box.Scale.X - 0.5f) / 0.3f, 0, 1);
base.Update();
}
public int ContractStyle;
2017-03-07 09:59:19 +08:00
private ButtonState state;
2016-12-05 20:09:41 +08:00
public ButtonState State
{
2017-06-08 14:35:10 +08:00
get { return state; }
2017-06-07 19:53:37 +08:00
set
{
if (state == value)
return;
state = value;
switch (state)
{
case ButtonState.Contracted:
switch (ContractStyle)
{
default:
2017-07-23 02:50:25 +08:00
box.ScaleTo(new Vector2(0, 1), 500, Easing.OutExpo);
this.FadeOut(500);
break;
case 1:
2017-07-23 02:50:25 +08:00
box.ScaleTo(new Vector2(0, 1), 400, Easing.InSine);
this.FadeOut(800);
break;
}
break;
case ButtonState.Expanded:
const int expand_duration = 500;
2017-07-23 02:50:25 +08:00
box.ScaleTo(new Vector2(1, 1), expand_duration, Easing.OutExpo);
this.FadeIn(expand_duration / 6f);
break;
case ButtonState.Exploded:
const int explode_duration = 200;
2017-07-23 02:50:25 +08:00
box.ScaleTo(new Vector2(2, 1), explode_duration, Easing.OutExpo);
this.FadeOut(explode_duration / 4f * 3);
break;
}
2017-09-04 08:10:04 +08:00
StateChanged?.Invoke(State);
}
}
}
public enum ButtonState
{
Contracted,
Expanded,
Exploded
}
}