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

295 lines
11 KiB
C#
Raw Normal View History

2016-08-26 11:28:23 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
2016-08-26 16:27:49 +08:00
//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-08-26 11:28:23 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Framework.Input;
using osu.Game.Graphics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens.Menu
2016-08-26 11:28:23 +08:00
{
public partial class ButtonSystem : Container, IStateful<MenuState>
2016-08-26 11:28:23 +08:00
{
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;
2016-08-26 11:28:23 +08:00
private FlowContainerWithOrigin buttonFlow;
//todo: make these non-internal somehow.
internal const float button_area_height = 100;
internal const float button_width = 140f;
internal const float wedge_width = 20;
2016-08-26 11:28:23 +08:00
2016-09-30 12:31:05 +08:00
public const int EXIT_DELAY = 3000;
2016-08-26 11:28:23 +08:00
private OsuLogo osuLogo;
private Drawable iconFacade;
private Container buttonArea;
private Box buttonAreaBackground;
2016-08-26 11:28:23 +08:00
private Button backButton;
private Button settingsButton;
List<Button> buttonsTopLevel = new List<Button>();
List<Button> buttonsPlay = new List<Button>();
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,
Size = new Vector2(1, button_area_height),
Alpha = 0,
Children = new Drawable[]
{
buttonAreaBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(2, 1),
Colour = new Color4(50, 50, 50, 255),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
buttonFlow = new FlowContainerWithOrigin
{
2016-10-12 14:33:04 +08:00
Direction = FlowDirection.HorizontalOnly,
Anchor = Anchor.Centre,
2016-10-22 17:05:46 +08:00
AutoSizeAxes = Axes.Both,
2016-10-12 14:58:12 +08:00
Spacing = new Vector2(-wedge_width, 0),
2016-10-22 17:05:46 +08:00
Children = new[]
{
2016-11-07 16:58:32 +08:00
settingsButton = new Button(@"settings", @"options", FontAwesome.fa_gear, new Color4(85, 85, 85, 255), () => OnSettings?.Invoke(), -wedge_width, Key.O),
backButton = new Button(@"back", @"back", FontAwesome.fa_osu_left_o, new Color4(51, 58, 94, 255), onBack, -wedge_width, Key.Escape),
iconFacade = new Container //need a container to make the osu! icon flow properly.
{
2016-09-24 14:35:27 +08:00
Size = new Vector2(0, button_area_height)
}
},
CentreTarget = iconFacade
}
}
},
2016-10-07 16:07:23 +08:00
osuLogo = new OsuLogo
2016-09-24 14:35:27 +08:00
{
2016-10-07 16:07:23 +08:00
Action = onOsuLogo,
2016-09-24 14:35:27 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre
}
};
2016-08-26 11:28:23 +08:00
buttonsPlay.Add(new Button(@"solo", @"freeplay", FontAwesome.fa_user, new Color4(102, 68, 204, 255), () => OnSolo?.Invoke(), wedge_width, Key.P));
buttonsPlay.Add(new Button(@"multi", @"multiplayer", FontAwesome.fa_users, new Color4(94, 63, 186, 255), () => OnMulti?.Invoke(), 0, Key.M));
buttonsPlay.Add(new Button(@"chart", @"charts", FontAwesome.fa_osu_charts, new Color4(80, 53, 160, 255), () => OnChart?.Invoke()));
buttonsTopLevel.Add(new Button(@"play", @"play", FontAwesome.fa_osu_logo, new Color4(102, 68, 204, 255), onPlay, wedge_width, Key.P));
buttonsTopLevel.Add(new Button(@"osu!editor", @"edit", FontAwesome.fa_osu_edit_o, new Color4(238, 170, 0, 255), () => OnEdit?.Invoke(), 0, Key.E));
buttonsTopLevel.Add(new Button(@"osu!direct", @"direct", FontAwesome.fa_osu_chevron_down_o, new Color4(165, 204, 0, 255), () => OnDirect?.Invoke(), 0, Key.D));
buttonsTopLevel.Add(new Button(@"exit", @"exit", FontAwesome.fa_osu_cross_o, new Color4(238, 51, 153, 255), onExit, 0, Key.Q));
buttonFlow.Add(buttonsPlay);
buttonFlow.Add(buttonsTopLevel);
2016-08-26 11:28:23 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
// osuLogo.SizeForFlow relies on loading to be complete.
buttonFlow.Position = new Vector2(wedge_width * 2 - (button_width + osuLogo.SizeForFlow / 4), 0);
}
2016-08-26 11:28:23 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
2016-10-07 23:41:44 +08:00
switch (args.Key)
{
2016-10-07 23:41:44 +08:00
case Key.Space:
osuLogo.TriggerClick(state);
return true;
case Key.Escape:
if (State == MenuState.Initial)
return false;
2016-10-07 23:41:44 +08:00
State = MenuState.Initial;
return true;
}
return false;
2016-08-26 11:28:23 +08:00
}
private void onPlay()
{
State = MenuState.Play;
}
private void onExit()
{
State = MenuState.Exit;
2016-09-29 19:13:58 +08:00
OnExit?.Invoke();
2016-08-26 11:28:23 +08:00
}
private void onBack()
{
State = MenuState.TopLevel;
}
private void onOsuLogo()
{
switch (state)
{
case MenuState.Initial:
//Game.Audio.PlaySamplePositional(@"menuhit");
State = MenuState.TopLevel;
return;
case MenuState.TopLevel:
buttonsTopLevel.First().TriggerMouseDown();
return;
case MenuState.Play:
buttonsPlay.First().TriggerMouseDown();
return;
}
}
MenuState state;
2016-09-20 13:41:55 +08:00
public override bool HandleInput => state != MenuState.Exit;
2016-08-26 11:28:23 +08:00
public MenuState State
{
get
{
return state;
}
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;
2016-08-26 11:28:23 +08:00
switch (state)
{
case MenuState.Initial:
buttonAreaBackground.ScaleTo(Vector2.One, 500, EasingTypes.Out);
2016-08-26 11:28:23 +08:00
buttonArea.FadeOut(500);
osuLogo.Delay(150);
osuLogo.MoveTo(Vector2.Zero, 800, EasingTypes.OutExpo);
osuLogo.ScaleTo(1, 800, EasingTypes.OutExpo);
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
break;
case MenuState.TopLevel:
buttonAreaBackground.ScaleTo(Vector2.One, 200, EasingTypes.Out);
2016-08-26 11:28:23 +08:00
osuLogo.MoveTo(buttonFlow.DrawPosition, 200, EasingTypes.In);
2016-08-26 11:28:23 +08:00
osuLogo.ScaleTo(0.5f, 200, EasingTypes.In);
buttonArea.FadeIn(300);
if (lastState == MenuState.Initial)
//todo: this propagates to invisible children and causes delays later down the track (on first MenuState.Play)
buttonArea.Delay(150, true);
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Expanded;
2016-08-26 11:28:23 +08:00
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
break;
case MenuState.Play:
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Exploded;
2016-08-26 11:28:23 +08:00
foreach (Button b in buttonsPlay)
b.State = ButtonState.Expanded;
2016-08-26 11:28:23 +08:00
break;
case MenuState.EnteringMode:
buttonAreaBackground.ScaleTo(new Vector2(2, 0), 300, EasingTypes.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;
2016-08-26 11:28:23 +08:00
case MenuState.Exit:
buttonArea.FadeOut(200);
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
2016-08-26 11:28:23 +08:00
osuLogo.Delay(150);
2016-09-30 12:31:05 +08:00
osuLogo.ScaleTo(1f, EXIT_DELAY * 1.5f);
osuLogo.RotateTo(20, EXIT_DELAY * 1.5f);
osuLogo.FadeOut(EXIT_DELAY);
2016-08-26 11:28:23 +08:00
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
if (lastState == MenuState.Initial)
buttonArea.DelayReset();
}
}
protected override void Update()
{
//if (OsuGame.IdleTime > 6000 && State != MenuState.Exit)
// State = MenuState.Initial;
iconFacade.Width = osuLogo.SizeForFlow * 0.5f;
base.Update();
}
}
2016-08-26 11:28:23 +08:00
public enum MenuState
{
Initial,
TopLevel,
Play,
EnteringMode,
Exit,
2016-08-26 11:28:23 +08:00
}
}