1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 05:07:50 +08:00
osu-lazer/osu.Game/Screens/Menu/ButtonSystem.cs

377 lines
14 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-08-26 11:28:23 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2016-11-14 16:23:33 +08:00
using osu.Framework;
2016-12-05 20:09:41 +08:00
using osu.Framework.Allocation;
2016-08-26 11:28:23 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2016-08-26 11:28:23 +08:00
using osu.Framework.Input;
using osu.Game.Graphics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio;
using osu.Framework.Configuration;
using osu.Framework.Threading;
2016-08-26 11:28:23 +08:00
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens.Menu
2016-08-26 11:28:23 +08:00
{
2017-03-09 13:24:16 +08:00
public class ButtonSystem : Container, IStateful<MenuState>
2016-08-26 11:28:23 +08:00
{
2017-09-04 08:10:04 +08:00
public event Action<MenuState> StateChanged;
private readonly BindableBool showOverlays = new BindableBool();
2016-09-29 19:13:58 +08:00
public Action OnEdit;
public Action OnExit;
public Action OnDirect;
public Action OnSolo;
public Action OnSettings;
public Action OnMulti;
public Action OnChart;
public Action OnTest;
private readonly FlowContainerWithOrigin buttonFlow;
2016-08-26 11:28:23 +08:00
//todo: make these non-internal somehow.
public const float BUTTON_AREA_HEIGHT = 100;
public const float BUTTON_WIDTH = 140f;
public const float WEDGE_WIDTH = 20;
2016-08-26 11:28:23 +08:00
private OsuLogo logo;
public void SetOsuLogo(OsuLogo logo)
{
this.logo = logo;
if (this.logo != null)
{
this.logo.Action = onOsuLogo;
// osuLogo.SizeForFlow relies on loading to be complete.
buttonFlow.Position = new Vector2(WEDGE_WIDTH * 2 - (BUTTON_WIDTH + this.logo.SizeForFlow / 4), 0);
updateLogoState();
}
}
2016-09-30 12:31:05 +08:00
private readonly Drawable iconFacade;
private readonly Container buttonArea;
private readonly Box buttonAreaBackground;
2016-08-26 11:28:23 +08:00
private readonly Button backButton;
private readonly Button settingsButton;
2016-08-26 11:28:23 +08:00
private readonly List<Button> buttonsTopLevel = new List<Button>();
private readonly List<Button> buttonsPlay = new List<Button>();
2016-08-26 11:28:23 +08:00
private SampleChannel sampleBack;
public ButtonSystem()
{
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
2016-08-26 11:28:23 +08:00
{
buttonArea = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
2017-02-07 15:15:45 +08:00
Size = new Vector2(1, BUTTON_AREA_HEIGHT),
Alpha = 0,
Children = new Drawable[]
{
buttonAreaBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(2, 1),
Colour = OsuColour.Gray(50),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
buttonFlow = new FlowContainerWithOrigin
{
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Horizontal,
2017-03-02 02:33:01 +08:00
Spacing = new Vector2(-WEDGE_WIDTH, 0),
Anchor = Anchor.Centre,
2016-10-22 17:05:46 +08:00
AutoSizeAxes = Axes.Both,
Children = new[]
{
settingsButton = new Button(@"settings", string.Empty, FontAwesome.fa_gear, new Color4(85, 85, 85, 255), () => OnSettings?.Invoke(), -WEDGE_WIDTH, Key.O),
backButton = new Button(@"back", string.Empty, FontAwesome.fa_osu_left_o, new Color4(51, 58, 94, 255), onBack, -WEDGE_WIDTH),
iconFacade = new Container //need a container to make the osu! icon flow properly.
{
2017-02-07 15:15:45 +08:00
Size = new Vector2(0, BUTTON_AREA_HEIGHT)
2016-09-24 14:35:27 +08:00
}
},
CentreTarget = iconFacade
}
}
},
};
2016-08-26 11:28:23 +08:00
2017-11-25 22:11:18 +08:00
buttonsPlay.Add(new Button(@"solo", @"button-solo-select", FontAwesome.fa_user, new Color4(102, 68, 204, 255), () => OnSolo?.Invoke(), WEDGE_WIDTH, Key.P));
buttonsPlay.Add(new Button(@"multi", @"button-generic-select", FontAwesome.fa_users, new Color4(94, 63, 186, 255), () => OnMulti?.Invoke(), 0, Key.M));
buttonsPlay.Add(new Button(@"chart", @"button-generic-select", FontAwesome.fa_osu_charts, new Color4(80, 53, 160, 255), () => OnChart?.Invoke()));
2017-11-25 22:11:18 +08:00
buttonsTopLevel.Add(new Button(@"play", @"button-play-select", FontAwesome.fa_osu_logo, new Color4(102, 68, 204, 255), onPlay, WEDGE_WIDTH, Key.P));
buttonsTopLevel.Add(new Button(@"osu!editor", @"button-generic-select", FontAwesome.fa_osu_edit_o, new Color4(238, 170, 0, 255), () => OnEdit?.Invoke(), 0, Key.E));
buttonsTopLevel.Add(new Button(@"osu!direct", @"button-direct-select", FontAwesome.fa_osu_chevron_down_o, new Color4(165, 204, 0, 255), () => OnDirect?.Invoke(), 0, Key.D));
buttonsTopLevel.Add(new Button(@"exit", string.Empty, FontAwesome.fa_osu_cross_o, new Color4(238, 51, 153, 255), onExit, 0, Key.Q));
2017-07-11 21:58:06 +08:00
buttonFlow.AddRange(buttonsPlay);
buttonFlow.AddRange(buttonsTopLevel);
2016-08-26 11:28:23 +08:00
}
2017-02-09 16:47:11 +08:00
[BackgroundDependencyLoader(true)]
private void load(AudioManager audio, OsuGame game)
2016-12-05 20:09:41 +08:00
{
if (game != null) showOverlays.BindTo(game.ShowOverlays);
2017-11-25 22:11:18 +08:00
sampleBack = audio.Sample.Get(@"Menu/button-back-select");
2016-12-05 20:09:41 +08:00
}
2016-08-26 11:28:23 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
2017-02-07 20:46:13 +08:00
if (args.Repeat) return false;
2016-10-07 23:41:44 +08:00
switch (args.Key)
{
2016-10-07 23:41:44 +08:00
case Key.Space:
2017-11-02 20:52:12 +08:00
logo?.TriggerOnClick(state);
2016-10-07 23:41:44 +08:00
return true;
case Key.Escape:
switch (State)
{
case MenuState.TopLevel:
State = MenuState.Initial;
return true;
case MenuState.Play:
backButton.TriggerOnClick();
return true;
}
return false;
}
return false;
2016-08-26 11:28:23 +08:00
}
private void onPlay()
{
State = MenuState.Play;
}
private void onExit()
{
2016-09-29 19:13:58 +08:00
OnExit?.Invoke();
2016-08-26 11:28:23 +08:00
}
private void onBack()
{
sampleBack?.Play();
2016-08-26 11:28:23 +08:00
State = MenuState.TopLevel;
}
private bool onOsuLogo()
2016-08-26 11:28:23 +08:00
{
switch (state)
{
default:
return true;
2016-08-26 11:28:23 +08:00
case MenuState.Initial:
State = MenuState.TopLevel;
return true;
2016-08-26 11:28:23 +08:00
case MenuState.TopLevel:
buttonsTopLevel.First().TriggerOnClick();
return false;
2016-08-26 11:28:23 +08:00
case MenuState.Play:
buttonsPlay.First().TriggerOnClick();
return false;
2016-08-26 11:28:23 +08:00
}
}
2017-03-07 09:59:19 +08:00
private MenuState state;
2016-08-26 11:28:23 +08:00
public override bool HandleKeyboardInput => state != MenuState.Exit;
public override bool HandleMouseInput => state != MenuState.Exit;
2016-09-20 13:41:55 +08:00
2016-08-26 11:28:23 +08:00
public MenuState State
{
2017-06-08 14:35:10 +08:00
get { return state; }
2016-08-26 11:28:23 +08:00
set
{
if (state == value) return;
MenuState lastState = state;
state = value;
//todo: figure a more elegant way of doing this.
buttonsTopLevel.ForEach(b => b.ContractStyle = 0);
buttonsPlay.ForEach(b => b.ContractStyle = 0);
backButton.ContractStyle = 0;
settingsButton.ContractStyle = 0;
if (state == MenuState.TopLevel)
2017-07-21 23:24:09 +08:00
buttonArea.FinishTransforms(true);
2016-08-26 11:28:23 +08:00
updateLogoState(lastState);
using (buttonArea.BeginDelayedSequence(lastState == MenuState.Initial ? 150 : 0, true))
{
switch (state)
{
case MenuState.Exit:
case MenuState.Initial:
2017-07-23 02:50:25 +08:00
buttonAreaBackground.ScaleTo(Vector2.One, 500, Easing.Out);
buttonArea.FadeOut(300);
2016-08-26 11:28:23 +08:00
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Contracted;
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
if (state != MenuState.Exit && lastState == MenuState.TopLevel)
sampleBack?.Play();
break;
case MenuState.TopLevel:
2017-07-23 02:50:25 +08:00
buttonAreaBackground.ScaleTo(Vector2.One, 200, Easing.Out);
2016-08-26 11:28:23 +08:00
buttonArea.FadeIn(300);
2016-08-26 11:28:23 +08:00
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Expanded;
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
break;
case MenuState.Play:
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Exploded;
foreach (Button b in buttonsPlay)
b.State = ButtonState.Expanded;
break;
case MenuState.EnteringMode:
2017-07-23 02:50:25 +08:00
buttonAreaBackground.ScaleTo(new Vector2(2, 0), 300, Easing.InSine);
buttonsTopLevel.ForEach(b => b.ContractStyle = 1);
buttonsPlay.ForEach(b => b.ContractStyle = 1);
backButton.ContractStyle = 1;
settingsButton.ContractStyle = 1;
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Contracted;
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
break;
}
backButton.State = state == MenuState.Play ? ButtonState.Expanded : ButtonState.Contracted;
settingsButton.State = state == MenuState.TopLevel ? ButtonState.Expanded : ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
}
2017-09-04 08:10:04 +08:00
StateChanged?.Invoke(State);
2016-08-26 11:28:23 +08:00
}
}
private ScheduledDelegate logoDelayedAction;
private void updateLogoState(MenuState lastState = MenuState.Initial)
{
if (logo == null) return;
logoDelayedAction?.Cancel();
switch (state)
{
case MenuState.Exit:
case MenuState.Initial:
2017-11-18 09:35:17 +08:00
logoTracking = false;
logoDelayedAction = Scheduler.AddDelayed(() =>
{
showOverlays.Value = false;
logo.ClearTransforms(targetMember: nameof(Position));
logo.RelativePositionAxes = Axes.Both;
logo.MoveTo(new Vector2(0.5f), 800, Easing.OutExpo);
logo.ScaleTo(1, 800, Easing.OutExpo);
}, 150);
break;
case MenuState.TopLevel:
case MenuState.Play:
logo.ClearTransforms(targetMember: nameof(Position));
logo.RelativePositionAxes = Axes.None;
switch (lastState)
{
case MenuState.TopLevel: // coming from toplevel to play
case MenuState.Initial:
logoTracking = false;
logo.ScaleTo(0.5f, 200, Easing.In);
logo.MoveTo(logoTrackingPosition, lastState == MenuState.EnteringMode ? 0 : 200, Easing.In);
logoDelayedAction = Scheduler.AddDelayed(() =>
{
logoTracking = true;
logo.Impact();
showOverlays.Value = true;
}, 200);
break;
default:
logoTracking = true;
logo.ScaleTo(0.5f, 200, Easing.OutQuint);
break;
}
break;
case MenuState.EnteringMode:
2017-11-18 09:35:17 +08:00
logoTracking = true;
break;
}
}
2017-11-18 09:35:17 +08:00
private Vector2 logoTrackingPosition => logo.Parent.ToLocalSpace(iconFacade.ScreenSpaceDrawQuad.Centre);
2017-11-18 09:35:17 +08:00
private bool logoTracking;
2016-08-26 11:28:23 +08:00
protected override void Update()
{
//if (OsuGame.IdleTime > 6000 && State != MenuState.Exit)
// State = MenuState.Initial;
base.Update();
if (logo != null)
{
2017-11-18 09:35:17 +08:00
if (logoTracking)
logo.Position = logoTrackingPosition;
iconFacade.Width = logo.SizeForFlow * 0.5f;
}
2016-08-26 11:28:23 +08:00
}
}
2016-08-26 11:28:23 +08:00
public enum MenuState
{
Initial,
TopLevel,
Play,
EnteringMode,
Exit,
2016-08-26 11:28:23 +08:00
}
}