2017-02-07 04:59:30 +00:00
|
|
|
|
// 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 09:56:20 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
using osu.Framework;
|
2016-12-05 12:09:41 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-06-20 05:54:23 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Game.Graphics;
|
2017-01-31 09:53:52 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2016-11-14 08:23:33 +00:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using OpenTK.Input;
|
2017-03-04 18:42:37 +00:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2017-07-09 09:23:34 +00:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Audio.Track;
|
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2016-11-14 08:23:33 +00:00
|
|
|
|
namespace osu.Game.Screens.Menu
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
|
|
|
|
/// <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>
|
2017-07-09 09:23:34 +00:00
|
|
|
|
public class Button : BeatSyncedContainer, IStateful<ButtonState>
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly Container iconText;
|
|
|
|
|
private readonly Container box;
|
|
|
|
|
private readonly Box boxHoverLayer;
|
|
|
|
|
private readonly TextAwesome icon;
|
2017-06-28 17:19:04 +00:00
|
|
|
|
private readonly string sampleName;
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly Action clickAction;
|
|
|
|
|
private readonly Key triggerKey;
|
2017-02-18 08:35:04 +00:00
|
|
|
|
private SampleChannel sampleClick;
|
2017-06-28 17:19:04 +00:00
|
|
|
|
private SampleChannel sampleHover;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-06-30 06:54:03 +00:00
|
|
|
|
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => box.ReceiveMouseInputAt(screenSpacePos);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-06-28 17:19:04 +00:00
|
|
|
|
public Button(string text, string sampleName, FontAwesome symbol, Color4 colour, Action clickAction = null, float extraWidth = 0, Key triggerKey = Key.Unknown)
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
2017-06-28 17:19:04 +00:00
|
|
|
|
this.sampleName = sampleName;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
this.clickAction = clickAction;
|
|
|
|
|
this.triggerKey = triggerKey;
|
2016-10-22 08:50:42 +00:00
|
|
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
Alpha = 0;
|
|
|
|
|
|
2017-02-07 07:15:45 +00:00
|
|
|
|
Vector2 boxSize = new Vector2(ButtonSystem.BUTTON_WIDTH + Math.Abs(extraWidth), ButtonSystem.BUTTON_AREA_HEIGHT);
|
2016-10-16 12:10:24 +00:00
|
|
|
|
|
2016-10-08 03:53:46 +00:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2016-11-30 05:38:45 +00:00
|
|
|
|
box = new Container
|
2016-11-12 17:34:36 +00:00
|
|
|
|
{
|
2016-11-30 05:38:45 +00:00
|
|
|
|
Masking = true,
|
2017-02-04 10:06:26 +00:00
|
|
|
|
MaskingSmoothness = 2,
|
2017-06-12 03:48:47 +00:00
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
2016-11-30 05:38:45 +00:00
|
|
|
|
{
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
2017-01-10 18:44:40 +00:00
|
|
|
|
Colour = Color4.Black.Opacity(0.2f),
|
2016-11-30 05:38:45 +00:00
|
|
|
|
Roundness = 5,
|
|
|
|
|
Radius = 8,
|
|
|
|
|
},
|
2016-11-12 17:34:36 +00:00
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Scale = new Vector2(0, 1),
|
|
|
|
|
Size = boxSize,
|
2017-02-07 07:15:45 +00:00
|
|
|
|
Shear = new Vector2(ButtonSystem.WEDGE_WIDTH / boxSize.Y, 0),
|
2017-06-07 11:53:37 +00:00
|
|
|
|
Children = new[]
|
2016-11-30 05:38:45 +00:00
|
|
|
|
{
|
2017-03-09 06:52:40 +00:00
|
|
|
|
new Box
|
2017-02-07 11:29:26 +00:00
|
|
|
|
{
|
|
|
|
|
EdgeSmoothness = new Vector2(1.5f, 0),
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = colour,
|
|
|
|
|
},
|
|
|
|
|
boxHoverLayer = new Box
|
2016-11-30 05:38:45 +00:00
|
|
|
|
{
|
2017-02-04 10:06:26 +00:00
|
|
|
|
EdgeSmoothness = new Vector2(1.5f, 0),
|
2016-11-30 05:38:45 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-02-07 11:29:26 +00:00
|
|
|
|
BlendingMode = BlendingMode.Additive,
|
|
|
|
|
Colour = Color4.White,
|
|
|
|
|
Alpha = 0,
|
2016-11-30 05:38:45 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
2016-11-12 17:34:36 +00:00
|
|
|
|
},
|
|
|
|
|
iconText = new Container
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Position = new Vector2(extraWidth / 2, 0),
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Children = new Drawable[]
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
2016-11-12 17:34:36 +00:00
|
|
|
|
icon = new TextAwesome
|
|
|
|
|
{
|
2016-11-30 04:15:34 +00:00
|
|
|
|
Shadow = true,
|
2016-11-12 17:34:36 +00:00
|
|
|
|
Anchor = Anchor.Centre,
|
2017-03-06 08:06:48 +00:00
|
|
|
|
Origin = Anchor.Centre,
|
2016-11-12 17:34:36 +00:00
|
|
|
|
TextSize = 30,
|
|
|
|
|
Position = new Vector2(0, 0),
|
|
|
|
|
Icon = symbol
|
|
|
|
|
},
|
2017-01-31 09:53:52 +00:00
|
|
|
|
new OsuSpriteText
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
2016-11-30 04:15:34 +00:00
|
|
|
|
Shadow = true,
|
2017-02-26 20:32:43 +00:00
|
|
|
|
AllowMultiline = false,
|
2016-11-12 17:34:36 +00:00
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
TextSize = 16,
|
|
|
|
|
Position = new Vector2(0, 35),
|
|
|
|
|
Text = text
|
2016-10-08 03:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-12 17:34:36 +00:00
|
|
|
|
}
|
2016-10-08 03:53:46 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
2017-07-09 09:23:34 +00:00
|
|
|
|
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
if (!IsHovered) return;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
bool rightward = beatIndex % 2 == 1;
|
|
|
|
|
double duration = timingPoint.BeatLength / 2;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
icon.RotateTo(rightward ? 10 : -10, duration * 2, EasingTypes.InOutSine);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
icon.MoveToY(-10, duration, EasingTypes.Out);
|
|
|
|
|
icon.ScaleTo(Vector2.One, duration, EasingTypes.Out);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
using (icon.BeginDelayedSequence(duration))
|
|
|
|
|
{
|
|
|
|
|
icon.MoveToY(0, duration, EasingTypes.In);
|
|
|
|
|
icon.ScaleTo(new Vector2(1, 0.9f), duration, EasingTypes.In);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
|
{
|
|
|
|
|
if (State != ButtonState.Expanded) return true;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
sampleHover?.Play();
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
box.ScaleTo(new Vector2(1.5f, 1), 500, EasingTypes.OutElastic);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
double duration = TimeUntilNextBeat;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-07-09 09:23:34 +00:00
|
|
|
|
icon.ClearTransforms();
|
|
|
|
|
icon.RotateTo(10, duration, EasingTypes.InOutSine);
|
|
|
|
|
icon.ScaleTo(new Vector2(1, 0.9f), duration, EasingTypes.Out);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
|
{
|
2017-02-25 12:12:39 +00:00
|
|
|
|
icon.ClearTransforms();
|
2016-10-08 03:53:46 +00:00
|
|
|
|
icon.RotateTo(0, 500, EasingTypes.Out);
|
|
|
|
|
icon.MoveTo(Vector2.Zero, 500, EasingTypes.Out);
|
|
|
|
|
icon.ScaleTo(Vector2.One, 200, EasingTypes.Out);
|
|
|
|
|
|
|
|
|
|
if (State == ButtonState.Expanded)
|
|
|
|
|
box.ScaleTo(new Vector2(1, 1), 500, EasingTypes.OutElastic);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 12:09:41 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
|
{
|
2017-06-28 17:19:04 +00:00
|
|
|
|
sampleHover = audio.Sample.Get(@"Menu/hover");
|
|
|
|
|
if (!string.IsNullOrEmpty(sampleName))
|
2017-07-01 14:10:04 +00:00
|
|
|
|
sampleClick = audio.Sample.Get($@"Menu/{sampleName}");
|
2016-12-05 12:09:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 03:53:46 +00:00
|
|
|
|
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
2017-02-07 11:29:26 +00:00
|
|
|
|
{
|
|
|
|
|
boxHoverLayer.FadeTo(0.1f, 1000, EasingTypes.OutQuint);
|
|
|
|
|
return base.OnMouseDown(state, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
boxHoverLayer.FadeTo(0, 1000, EasingTypes.OutQuint);
|
|
|
|
|
return base.OnMouseUp(state, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnClick(InputState state)
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
|
|
|
|
trigger();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
|
|
|
|
{
|
2017-03-03 05:08:37 +00:00
|
|
|
|
if (args.Repeat || state.Keyboard.ControlPressed || state.Keyboard.ShiftPressed || state.Keyboard.AltPressed)
|
|
|
|
|
return false;
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-03-03 05:08:37 +00:00
|
|
|
|
if (triggerKey == args.Key && triggerKey != Key.Unknown)
|
2016-10-08 03:53:46 +00:00
|
|
|
|
{
|
|
|
|
|
trigger();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void trigger()
|
|
|
|
|
{
|
2017-06-28 17:19:04 +00:00
|
|
|
|
sampleClick?.Play();
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-02-07 13:19:36 +00:00
|
|
|
|
clickAction?.Invoke();
|
|
|
|
|
|
2017-02-25 12:12:39 +00:00
|
|
|
|
boxHoverLayer.ClearTransforms();
|
2017-02-07 11:29:26 +00:00
|
|
|
|
boxHoverLayer.Alpha = 0.9f;
|
|
|
|
|
boxHoverLayer.FadeOut(800, EasingTypes.OutExpo);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 01:59:19 +00:00
|
|
|
|
private ButtonState state;
|
2016-12-05 12:09:41 +00:00
|
|
|
|
|
2016-10-08 03:53:46 +00:00
|
|
|
|
public ButtonState State
|
|
|
|
|
{
|
2017-06-08 06:35:10 +00:00
|
|
|
|
get { return state; }
|
2016-10-08 03:53:46 +00:00
|
|
|
|
|
2017-06-07 11:53:37 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
2016-10-08 03:53:46 +00:00
|
|
|
|
if (state == value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
state = value;
|
|
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ButtonState.Contracted:
|
|
|
|
|
switch (ContractStyle)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
box.ScaleTo(new Vector2(0, 1), 500, EasingTypes.OutExpo);
|
|
|
|
|
FadeOut(500);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
box.ScaleTo(new Vector2(0, 1), 400, EasingTypes.InSine);
|
|
|
|
|
FadeOut(800);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ButtonState.Expanded:
|
|
|
|
|
const int expand_duration = 500;
|
|
|
|
|
box.ScaleTo(new Vector2(1, 1), expand_duration, EasingTypes.OutExpo);
|
2017-03-09 06:52:40 +00:00
|
|
|
|
FadeIn(expand_duration / 6f);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
break;
|
|
|
|
|
case ButtonState.Exploded:
|
|
|
|
|
const int explode_duration = 200;
|
|
|
|
|
box.ScaleTo(new Vector2(2, 1), explode_duration, EasingTypes.OutExpo);
|
2017-03-09 06:52:40 +00:00
|
|
|
|
FadeOut(explode_duration / 4f * 3);
|
2016-10-08 03:53:46 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum ButtonState
|
|
|
|
|
{
|
|
|
|
|
Contracted,
|
|
|
|
|
Expanded,
|
|
|
|
|
Exploded
|
|
|
|
|
}
|
|
|
|
|
}
|