2017-02-07 12:59:30 +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
|
2016-12-06 17:56:20 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
using osu.Framework;
|
2016-12-05 20:09:41 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2017-02-25 20:12:39 +08:00
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Game.Graphics;
|
2017-01-31 17:53:52 +08:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2016-11-14 16:23:33 +08:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using OpenTK.Input;
|
2017-03-05 02:42:37 +08:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
2016-11-14 16:23:33 +08:00
|
|
|
|
namespace osu.Game.Screens.Menu
|
2016-10-08 11:53:46 +08: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>
|
2016-10-22 16:50:42 +08:00
|
|
|
|
public class Button : Container, IStateful<ButtonState>
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
private Container iconText;
|
2016-11-30 13:38:45 +08:00
|
|
|
|
private Container box;
|
2017-02-07 19:29:26 +08:00
|
|
|
|
private Box boxColourLayer;
|
|
|
|
|
private Box boxHoverLayer;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
private Color4 colour;
|
|
|
|
|
private TextAwesome icon;
|
|
|
|
|
private string internalName;
|
|
|
|
|
private readonly FontAwesome symbol;
|
|
|
|
|
private Action clickAction;
|
|
|
|
|
private readonly float extraWidth;
|
|
|
|
|
private Key triggerKey;
|
|
|
|
|
private string text;
|
2017-02-18 16:35:04 +08:00
|
|
|
|
private SampleChannel sampleClick;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
2016-10-16 20:10:24 +08:00
|
|
|
|
public override bool Contains(Vector2 screenSpacePos)
|
|
|
|
|
{
|
|
|
|
|
return box.Contains(screenSpacePos);
|
|
|
|
|
}
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
|
|
|
|
public Button(string text, string internalName, FontAwesome symbol, Color4 colour, Action clickAction = null, float extraWidth = 0, Key triggerKey = Key.Unknown)
|
|
|
|
|
{
|
|
|
|
|
this.internalName = internalName;
|
|
|
|
|
this.symbol = symbol;
|
|
|
|
|
this.colour = colour;
|
|
|
|
|
this.clickAction = clickAction;
|
|
|
|
|
this.extraWidth = extraWidth;
|
|
|
|
|
this.triggerKey = triggerKey;
|
|
|
|
|
this.text = text;
|
2016-10-22 16:50:42 +08:00
|
|
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
Alpha = 0;
|
|
|
|
|
|
2017-02-07 15:15:45 +08:00
|
|
|
|
Vector2 boxSize = new Vector2(ButtonSystem.BUTTON_WIDTH + Math.Abs(extraWidth), ButtonSystem.BUTTON_AREA_HEIGHT);
|
2016-10-16 20:10:24 +08:00
|
|
|
|
|
2016-10-08 11:53:46 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2016-11-30 13:38:45 +08:00
|
|
|
|
box = new Container
|
2016-11-13 01:34:36 +08:00
|
|
|
|
{
|
2016-11-30 13:38:45 +08:00
|
|
|
|
Masking = true,
|
2017-02-04 18:06:26 +08:00
|
|
|
|
MaskingSmoothness = 2,
|
2016-11-30 13:38:45 +08:00
|
|
|
|
EdgeEffect = new EdgeEffect
|
|
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
},
|
2016-11-13 01:34:36 +08:00
|
|
|
|
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-02-07 19:29:26 +08:00
|
|
|
|
Children = new []
|
2016-11-30 13:38:45 +08:00
|
|
|
|
{
|
2017-02-07 19:29:26 +08:00
|
|
|
|
boxColourLayer = new Box
|
|
|
|
|
{
|
|
|
|
|
EdgeSmoothness = new Vector2(1.5f, 0),
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = colour,
|
|
|
|
|
},
|
|
|
|
|
boxHoverLayer = new Box
|
2016-11-30 13:38:45 +08:00
|
|
|
|
{
|
2017-02-04 18:06:26 +08:00
|
|
|
|
EdgeSmoothness = new Vector2(1.5f, 0),
|
2016-11-30 13:38:45 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-02-07 19:29:26 +08:00
|
|
|
|
BlendingMode = BlendingMode.Additive,
|
|
|
|
|
Colour = Color4.White,
|
|
|
|
|
Alpha = 0,
|
2016-11-30 13:38:45 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
2016-11-13 01:34:36 +08: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 11:53:46 +08:00
|
|
|
|
{
|
2016-11-13 01:34:36 +08:00
|
|
|
|
icon = new TextAwesome
|
|
|
|
|
{
|
2016-11-30 12:15:34 +08:00
|
|
|
|
Shadow = true,
|
2016-11-13 01:34:36 +08:00
|
|
|
|
Anchor = Anchor.Centre,
|
2017-03-06 16:06:48 +08:00
|
|
|
|
Origin = Anchor.Centre,
|
2016-11-13 01:34:36 +08:00
|
|
|
|
TextSize = 30,
|
|
|
|
|
Position = new Vector2(0, 0),
|
|
|
|
|
Icon = symbol
|
|
|
|
|
},
|
2017-01-31 17:53:52 +08:00
|
|
|
|
new OsuSpriteText
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
2016-11-30 12:15:34 +08:00
|
|
|
|
Shadow = true,
|
2017-02-27 04:32:43 +08:00
|
|
|
|
AllowMultiline = false,
|
2016-11-13 01:34:36 +08:00
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
TextSize = 16,
|
|
|
|
|
Position = new Vector2(0, 35),
|
|
|
|
|
Text = text
|
2016-10-08 11:53:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-13 01:34:36 +08:00
|
|
|
|
}
|
2016-10-08 11:53:46 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
|
{
|
|
|
|
|
if (State != ButtonState.Expanded) return true;
|
|
|
|
|
|
|
|
|
|
//if (OsuGame.Instance.IsActive)
|
|
|
|
|
// Game.Audio.PlaySamplePositional($@"menu-{internalName}-hover", @"menuclick");
|
|
|
|
|
|
|
|
|
|
box.ScaleTo(new Vector2(1.5f, 1), 500, EasingTypes.OutElastic);
|
|
|
|
|
|
|
|
|
|
int duration = 0; //(int)(Game.Audio.BeatLength / 2);
|
|
|
|
|
if (duration == 0) duration = 250;
|
|
|
|
|
|
2017-02-25 20:12:39 +08:00
|
|
|
|
icon.ClearTransforms();
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
|
|
|
|
icon.ScaleTo(1, 500, EasingTypes.OutElasticHalf);
|
|
|
|
|
|
|
|
|
|
double offset = 0; //(1 - Game.Audio.SyncBeatProgress) * duration;
|
2016-11-06 15:25:21 +08:00
|
|
|
|
double startTime = Time.Current + offset;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
|
|
|
|
icon.RotateTo(10, offset, EasingTypes.InOutSine);
|
|
|
|
|
icon.ScaleTo(new Vector2(1, 0.9f), offset, EasingTypes.Out);
|
|
|
|
|
|
2016-10-28 16:42:00 +08:00
|
|
|
|
icon.Transforms.Add(new TransformRotation
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
StartValue = -10,
|
|
|
|
|
EndValue = 10,
|
|
|
|
|
StartTime = startTime,
|
|
|
|
|
EndTime = startTime + duration * 2,
|
|
|
|
|
Easing = EasingTypes.InOutSine,
|
|
|
|
|
LoopCount = -1,
|
|
|
|
|
LoopDelay = duration * 2
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-29 02:44:27 +08:00
|
|
|
|
icon.Transforms.Add(new TransformPosition
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
StartValue = Vector2.Zero,
|
|
|
|
|
EndValue = new Vector2(0, -10),
|
|
|
|
|
StartTime = startTime,
|
|
|
|
|
EndTime = startTime + duration,
|
|
|
|
|
Easing = EasingTypes.Out,
|
|
|
|
|
LoopCount = -1,
|
|
|
|
|
LoopDelay = duration
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-29 02:44:27 +08:00
|
|
|
|
icon.Transforms.Add(new TransformScale
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
StartValue = new Vector2(1, 0.9f),
|
|
|
|
|
EndValue = Vector2.One,
|
|
|
|
|
StartTime = startTime,
|
|
|
|
|
EndTime = startTime + duration,
|
|
|
|
|
Easing = EasingTypes.Out,
|
|
|
|
|
LoopCount = -1,
|
|
|
|
|
LoopDelay = duration
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-29 02:44:27 +08:00
|
|
|
|
icon.Transforms.Add(new TransformPosition
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
StartValue = new Vector2(0, -10),
|
|
|
|
|
EndValue = Vector2.Zero,
|
|
|
|
|
StartTime = startTime + duration,
|
|
|
|
|
EndTime = startTime + duration * 2,
|
|
|
|
|
Easing = EasingTypes.In,
|
|
|
|
|
LoopCount = -1,
|
|
|
|
|
LoopDelay = duration
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-29 02:44:27 +08:00
|
|
|
|
icon.Transforms.Add(new TransformScale
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
StartValue = Vector2.One,
|
|
|
|
|
EndValue = new Vector2(1, 0.9f),
|
|
|
|
|
StartTime = startTime + duration,
|
|
|
|
|
EndTime = startTime + duration * 2,
|
|
|
|
|
Easing = EasingTypes.In,
|
|
|
|
|
LoopCount = -1,
|
|
|
|
|
LoopDelay = duration
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-28 16:42:00 +08:00
|
|
|
|
icon.Transforms.Add(new TransformRotation
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
StartValue = 10,
|
|
|
|
|
EndValue = -10,
|
|
|
|
|
StartTime = startTime + duration * 2,
|
|
|
|
|
EndTime = startTime + duration * 4,
|
|
|
|
|
Easing = EasingTypes.InOutSine,
|
|
|
|
|
LoopCount = -1,
|
|
|
|
|
LoopDelay = duration * 2
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
|
{
|
2017-02-25 20:12:39 +08:00
|
|
|
|
icon.ClearTransforms();
|
2016-10-08 11:53:46 +08:00
|
|
|
|
icon.RotateTo(0, 500, EasingTypes.Out);
|
|
|
|
|
icon.MoveTo(Vector2.Zero, 500, EasingTypes.Out);
|
|
|
|
|
icon.ScaleTo(0.7f, 500, EasingTypes.OutElasticHalf);
|
|
|
|
|
icon.ScaleTo(Vector2.One, 200, EasingTypes.Out);
|
|
|
|
|
|
|
|
|
|
if (State == ButtonState.Expanded)
|
|
|
|
|
box.ScaleTo(new Vector2(1, 1), 500, EasingTypes.OutElastic);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 20:09:41 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
|
{
|
2017-03-09 13:24:16 +08:00
|
|
|
|
sampleClick = audio.Sample.Get($@"Menu/menu-{internalName}-click") ?? audio.Sample.Get(internalName.Contains(@"back") ? @"Menu/menuback" : @"Menu/menuhit");
|
2016-12-05 20:09:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 11:53:46 +08:00
|
|
|
|
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
2017-02-07 19:29:26 +08: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 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
trigger();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
|
|
|
|
{
|
2017-03-03 13:08:37 +08:00
|
|
|
|
if (args.Repeat || state.Keyboard.ControlPressed || state.Keyboard.ShiftPressed || state.Keyboard.AltPressed)
|
|
|
|
|
return false;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
2017-03-03 13:08:37 +08:00
|
|
|
|
if (triggerKey == args.Key && triggerKey != Key.Unknown)
|
2016-10-08 11:53:46 +08:00
|
|
|
|
{
|
|
|
|
|
trigger();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void trigger()
|
|
|
|
|
{
|
2016-12-05 20:09:41 +08:00
|
|
|
|
sampleClick.Play();
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
2017-02-07 21:19:36 +08:00
|
|
|
|
clickAction?.Invoke();
|
|
|
|
|
|
2017-02-25 20:12:39 +08:00
|
|
|
|
boxHoverLayer.ClearTransforms();
|
2017-02-07 19:29:26 +08:00
|
|
|
|
boxHoverLayer.Alpha = 0.9f;
|
|
|
|
|
boxHoverLayer.FadeOut(800, EasingTypes.OutExpo);
|
2016-10-08 11:53:46 +08: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 09:59:19 +08:00
|
|
|
|
private ButtonState state;
|
2016-12-05 20:09:41 +08:00
|
|
|
|
|
2016-10-08 11:53:46 +08:00
|
|
|
|
public ButtonState State
|
|
|
|
|
{
|
|
|
|
|
get { return state; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
FadeIn(expand_duration / 6);
|
|
|
|
|
break;
|
|
|
|
|
case ButtonState.Exploded:
|
|
|
|
|
const int explode_duration = 200;
|
|
|
|
|
box.ScaleTo(new Vector2(2, 1), explode_duration, EasingTypes.OutExpo);
|
|
|
|
|
FadeOut(explode_duration / 4 * 3);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum ButtonState
|
|
|
|
|
{
|
|
|
|
|
Contracted,
|
|
|
|
|
Expanded,
|
|
|
|
|
Exploded
|
|
|
|
|
}
|
|
|
|
|
}
|