Improve code quality of main menu button system

This commit is contained in:
Dean Herbert
2018-07-03 18:18:04 +09:00
parent 5f33ab579e
commit 2aae528e1c
5 changed files with 263 additions and 161 deletions

View File

@ -10,9 +10,9 @@ 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.Framework.Input.Bindings;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Game.Graphics;
using osu.Game.Input.Bindings;
@ -23,9 +23,9 @@ using OpenTK.Input;
namespace osu.Game.Screens.Menu
{
public class ButtonSystem : Container, IStateful<MenuState>, IKeyBindingHandler<GlobalAction>
public class ButtonSystem : Container, IStateful<ButtonSystemState>, IKeyBindingHandler<GlobalAction>
{
public event Action<MenuState> StateChanged;
public event Action<ButtonSystemState> StateChanged;
public Action OnEdit;
public Action OnExit;
@ -34,12 +34,6 @@ namespace osu.Game.Screens.Menu
public Action OnSettings;
public Action OnMulti;
public Action OnChart;
public Action OnTest;
private readonly FlowContainerWithOrigin buttonFlow;
//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;
@ -55,18 +49,16 @@ namespace osu.Game.Screens.Menu
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);
buttonArea.Flow.Position = new Vector2(WEDGE_WIDTH * 2 - (BUTTON_WIDTH + this.logo.SizeForFlow / 4), 0);
updateLogoState();
}
}
private readonly Drawable iconFacade;
private readonly Container buttonArea;
private readonly Box buttonAreaBackground;
private readonly ButtonArea buttonArea;
private readonly Button backButton;
private readonly Button settingsButton;
private readonly List<Button> buttonsTopLevel = new List<Button>();
private readonly List<Button> buttonsPlay = new List<Button>();
@ -77,57 +69,39 @@ namespace osu.Game.Screens.Menu
{
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
Child = buttonArea = new ButtonArea();
buttonArea.AddRange(new[]
{
buttonArea = new Container
new Button(@"settings", string.Empty, FontAwesome.fa_gear, new Color4(85, 85, 85, 255), () => OnSettings?.Invoke(), -WEDGE_WIDTH, Key.O)
{
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 = OsuColour.Gray(50),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
buttonFlow = new FlowContainerWithOrigin
{
Direction = FillDirection.Horizontal,
Spacing = new Vector2(-WEDGE_WIDTH, 0),
Anchor = Anchor.Centre,
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.
{
Size = new Vector2(0, BUTTON_AREA_HEIGHT)
}
},
CentreTarget = iconFacade
}
}
VisibleState = ButtonSystemState.TopLevel,
},
};
backButton = new Button(@"back", @"button-back-select", FontAwesome.fa_osu_left_o, new Color4(51, 58, 94, 255), () => State = ButtonSystemState.TopLevel, -WEDGE_WIDTH)
{
VisibleState = ButtonSystemState.Play,
},
iconFacade = new Container //need a container to make the osu! icon flow properly.
{
Size = new Vector2(0, ButtonArea.BUTTON_AREA_HEIGHT)
}
});
buttonArea.Flow.CentreTarget = iconFacade;
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()));
buttonsPlay.ForEach(b => b.VisibleState = ButtonSystemState.Play);
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(@"play", @"button-play-select", FontAwesome.fa_osu_logo, new Color4(102, 68, 204, 255), () => State = ButtonSystemState.Play, 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));
buttonsTopLevel.Add(new Button(@"exit", string.Empty, FontAwesome.fa_osu_cross_o, new Color4(238, 51, 153, 255), () => OnExit?.Invoke(), 0, Key.Q));
buttonsTopLevel.ForEach(b => b.VisibleState = ButtonSystemState.TopLevel);
buttonFlow.AddRange(buttonsPlay);
buttonFlow.AddRange(buttonsTopLevel);
buttonArea.AddRange(buttonsPlay);
buttonArea.AddRange(buttonsTopLevel);
}
private OsuGame game;
@ -164,14 +138,17 @@ namespace osu.Game.Screens.Menu
}
}
public bool OnReleased(GlobalAction action) => false;
private bool goBack()
{
switch (State)
{
case MenuState.TopLevel:
State = MenuState.Initial;
case ButtonSystemState.TopLevel:
State = ButtonSystemState.Initial;
sampleBack?.Play();
return true;
case MenuState.Play:
case ButtonSystemState.Play:
backButton.TriggerOnClick();
return true;
default:
@ -179,48 +156,30 @@ namespace osu.Game.Screens.Menu
}
}
public bool OnReleased(GlobalAction action) => false;
private void onPlay()
{
State = MenuState.Play;
}
private void onExit()
{
OnExit?.Invoke();
}
private void onBack()
{
sampleBack?.Play();
State = MenuState.TopLevel;
}
private bool onOsuLogo()
{
switch (state)
{
default:
return true;
case MenuState.Initial:
State = MenuState.TopLevel;
case ButtonSystemState.Initial:
State = ButtonSystemState.TopLevel;
return true;
case MenuState.TopLevel:
case ButtonSystemState.TopLevel:
buttonsTopLevel.First().TriggerOnClick();
return false;
case MenuState.Play:
case ButtonSystemState.Play:
buttonsPlay.First().TriggerOnClick();
return false;
}
}
private MenuState state;
private ButtonSystemState state = ButtonSystemState.Initial;
public override bool HandleKeyboardInput => state != MenuState.Exit;
public override bool HandleMouseInput => state != MenuState.Exit;
public override bool HandleKeyboardInput => state != ButtonSystemState.Exit;
public override bool HandleMouseInput => state != ButtonSystemState.Exit;
public MenuState State
public ButtonSystemState State
{
get { return state; }
@ -228,71 +187,19 @@ namespace osu.Game.Screens.Menu
{
if (state == value) return;
MenuState lastState = state;
ButtonSystemState 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;
updateLogoState(lastState);
using (buttonArea.BeginDelayedSequence(lastState == MenuState.Initial ? 150 : 0, true))
Logger.Log($"{nameof(ButtonSystem)}'s state changed from {lastState} to {state}");
using (buttonArea.BeginDelayedSequence(lastState == ButtonSystemState.Initial ? 150 : 0, true))
{
switch (state)
{
case MenuState.Exit:
case MenuState.Initial:
buttonAreaBackground.ScaleTo(Vector2.One, 500, Easing.Out);
buttonArea.FadeOut(300);
buttonArea.ButtonSystemState = state;
foreach (Button b in buttonsTopLevel)
b.State = ButtonState.Contracted;
foreach (Button b in buttonsPlay)
b.State = ButtonState.Contracted;
if (state != MenuState.Exit && lastState == MenuState.TopLevel)
sampleBack?.Play();
break;
case MenuState.TopLevel:
buttonAreaBackground.ScaleTo(Vector2.One, 200, Easing.Out);
buttonArea.FadeIn(300);
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:
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;
foreach (var b in buttonArea.Children.OfType<Button>())
b.ButtonSystemState = state;
}
StateChanged?.Invoke(State);
@ -301,14 +208,14 @@ namespace osu.Game.Screens.Menu
private ScheduledDelegate logoDelayedAction;
private void updateLogoState(MenuState lastState = MenuState.Initial)
private void updateLogoState(ButtonSystemState lastState = ButtonSystemState.Initial)
{
if (logo == null) return;
switch (state)
{
case MenuState.Exit:
case MenuState.Initial:
case ButtonSystemState.Exit:
case ButtonSystemState.Initial:
logoDelayedAction?.Cancel();
logoDelayedAction = Scheduler.AddDelayed(() =>
{
@ -316,7 +223,7 @@ namespace osu.Game.Screens.Menu
if (game != null)
{
game.OverlayActivationMode.Value = state == MenuState.Exit ? OverlayActivation.Disabled : OverlayActivation.All;
game.OverlayActivationMode.Value = state == ButtonSystemState.Exit ? OverlayActivation.Disabled : OverlayActivation.All;
game.Toolbar.Hide();
}
@ -327,22 +234,22 @@ namespace osu.Game.Screens.Menu
logo.ScaleTo(1, 800, Easing.OutExpo);
}, buttonArea.Alpha * 150);
break;
case MenuState.TopLevel:
case MenuState.Play:
case ButtonSystemState.TopLevel:
case ButtonSystemState.Play:
switch (lastState)
{
case MenuState.TopLevel: // coming from toplevel to play
case ButtonSystemState.TopLevel: // coming from toplevel to play
break;
case MenuState.Initial:
case ButtonSystemState.Initial:
logo.ClearTransforms(targetMember: nameof(Position));
logo.RelativePositionAxes = Axes.None;
bool impact = logo.Scale.X > 0.6f;
if (lastState == MenuState.Initial)
if (lastState == ButtonSystemState.Initial)
logo.ScaleTo(0.5f, 200, Easing.In);
logo.MoveTo(logoTrackingPosition, lastState == MenuState.EnteringMode ? 0 : 200, Easing.In);
logo.MoveTo(logoTrackingPosition, lastState == ButtonSystemState.EnteringMode ? 0 : 200, Easing.In);
logoDelayedAction?.Cancel();
logoDelayedAction = Scheduler.AddDelayed(() =>
@ -368,7 +275,7 @@ namespace osu.Game.Screens.Menu
}
break;
case MenuState.EnteringMode:
case ButtonSystemState.EnteringMode:
logoTracking = true;
break;
}
@ -387,7 +294,7 @@ namespace osu.Game.Screens.Menu
if (logo != null)
{
if (logoTracking)
if (logoTracking && iconFacade.IsLoaded)
logo.Position = logoTrackingPosition;
iconFacade.Width = logo.SizeForFlow * 0.5f;
@ -395,12 +302,12 @@ namespace osu.Game.Screens.Menu
}
}
public enum MenuState
public enum ButtonSystemState
{
Exit,
Initial,
TopLevel,
Play,
EnteringMode,
Exit,
}
}