1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-28 23:42:56 +08:00
osu-lazer/osu.Game/Screens/Menu/ButtonSystem.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

404 lines
13 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2016-08-26 11:28:23 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
2016-11-14 16:23:33 +08:00
using osu.Framework;
2016-12-05 20:09:41 +08:00
using osu.Framework.Allocation;
2018-04-30 01:15:09 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2019-03-26 16:18:35 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
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.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Framework.Platform;
2018-04-30 01:15:09 +08:00
using osu.Framework.Threading;
2016-08-26 11:28:23 +08:00
using osu.Game.Graphics;
2019-03-26 16:18:35 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Input;
using osu.Game.Input.Bindings;
2021-04-20 16:06:26 +08:00
using osu.Game.Localisation;
using osu.Game.Online.API;
2018-06-06 15:17:51 +08:00
using osu.Game.Overlays;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens.Menu
2016-08-26 11:28:23 +08:00
{
public class ButtonSystem : Container, IStateful<ButtonSystemState>, IKeyBindingHandler<GlobalAction>
2016-08-26 11:28:23 +08:00
{
public event Action<ButtonSystemState> StateChanged;
2018-04-13 17:19:50 +08:00
2018-11-26 16:07:30 +08:00
private readonly IBindable<bool> isIdle = new BindableBool();
2016-09-29 19:13:58 +08:00
public Action OnEdit;
public Action OnExit;
public Action OnBeatmapListing;
2016-09-29 19:13:58 +08:00
public Action OnSolo;
public Action OnSettings;
2020-12-22 13:55:25 +08:00
public Action OnMultiplayer;
2020-12-25 12:11:21 +08:00
public Action OnPlaylists;
2018-04-13 17:19:50 +08:00
public const float BUTTON_WIDTH = 140f;
public const float WEDGE_WIDTH = 20;
2018-04-13 17:19:50 +08:00
[CanBeNull]
private OsuLogo logo;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Assign the <see cref="OsuLogo"/> that this ButtonSystem should manage the position of.
/// </summary>
/// <param name="logo">The instance of the logo to be assigned. If null, we are suspending from the screen that uses this ButtonSystem.</param>
public void SetOsuLogo(OsuLogo logo)
{
this.logo = logo;
2018-04-13 17:19:50 +08:00
if (this.logo != null)
{
this.logo.Action = onOsuLogo;
2018-04-13 17:19:50 +08:00
// osuLogo.SizeForFlow relies on loading to be complete.
buttonArea.Flow.Position = new Vector2(WEDGE_WIDTH * 2 - (BUTTON_WIDTH + this.logo.SizeForFlow / 4), 0);
2018-04-13 17:19:50 +08:00
updateLogoState();
}
2019-03-26 16:18:35 +08:00
else
{
2019-03-27 10:37:16 +08:00
// We should stop tracking as the facade is now out of scope.
2019-04-08 14:24:09 +08:00
logoTrackingContainer.StopTracking();
2019-03-26 16:18:35 +08:00
}
}
2018-04-13 17:19:50 +08:00
private readonly ButtonArea buttonArea;
2018-04-13 17:19:50 +08:00
private readonly MainMenuButton backButton;
2018-04-13 17:19:50 +08:00
private readonly List<MainMenuButton> buttonsTopLevel = new List<MainMenuButton>();
private readonly List<MainMenuButton> buttonsPlay = new List<MainMenuButton>();
2018-04-13 17:19:50 +08:00
2021-01-19 16:11:40 +08:00
private Sample sampleBack;
2018-04-13 17:19:50 +08:00
private readonly LogoTrackingContainer logoTrackingContainer;
2019-03-26 16:18:35 +08:00
2022-04-19 14:46:03 +08:00
public bool ReturnToTopOnIdle { get; set; } = true;
public ButtonSystem()
{
RelativeSizeAxes = Axes.Both;
2018-04-13 17:19:50 +08:00
Child = logoTrackingContainer = new LogoTrackingContainer
2019-03-26 16:18:35 +08:00
{
RelativeSizeAxes = Axes.Both,
Child = buttonArea = new ButtonArea()
};
buttonArea.AddRange(new Drawable[]
2016-08-26 11:28:23 +08:00
{
new MainMenuButton(ButtonSystemStrings.Settings, string.Empty, FontAwesome.Solid.Cog, new Color4(85, 85, 85, 255), () => OnSettings?.Invoke(), -WEDGE_WIDTH, Key.O),
2022-04-19 14:46:03 +08:00
backButton = new MainMenuButton(ButtonSystemStrings.Back, @"button-back-select", OsuIcon.LeftCircle, new Color4(51, 58, 94, 255), () => State = ButtonSystemState.TopLevel,
-WEDGE_WIDTH)
{
VisibleState = ButtonSystemState.Play,
},
2019-04-17 16:24:09 +08:00
logoTrackingContainer.LogoFacade.With(d => d.Scale = new Vector2(0.74f))
});
buttonArea.Flow.CentreTarget = logoTrackingContainer.LogoFacade;
2016-08-26 11:28:23 +08:00
}
2018-04-13 17:19:50 +08:00
2018-12-26 12:18:36 +08:00
[Resolved(CanBeNull = true)]
private OsuGame game { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
[Resolved(CanBeNull = true)]
private LoginOverlay loginOverlay { get; set; }
2017-02-09 16:47:11 +08:00
[BackgroundDependencyLoader(true)]
2022-01-15 08:06:39 +08:00
private void load(AudioManager audio, IdleTracker idleTracker, GameHost host)
2016-12-05 20:09:41 +08:00
{
buttonsPlay.Add(new MainMenuButton(ButtonSystemStrings.Solo, @"button-solo-select", FontAwesome.Solid.User, new Color4(102, 68, 204, 255), () => OnSolo?.Invoke(), WEDGE_WIDTH, Key.P));
buttonsPlay.Add(new MainMenuButton(ButtonSystemStrings.Multi, @"button-generic-select", FontAwesome.Solid.Users, new Color4(94, 63, 186, 255), onMultiplayer, 0, Key.M));
buttonsPlay.Add(new MainMenuButton(ButtonSystemStrings.Playlists, @"button-generic-select", OsuIcon.Charts, new Color4(94, 63, 186, 255), onPlaylists, 0, Key.L));
buttonsPlay.ForEach(b => b.VisibleState = ButtonSystemState.Play);
2022-04-19 14:46:03 +08:00
buttonsTopLevel.Add(new MainMenuButton(ButtonSystemStrings.Play, @"button-play-select", OsuIcon.Logo, new Color4(102, 68, 204, 255), () => State = ButtonSystemState.Play, WEDGE_WIDTH,
Key.P));
buttonsTopLevel.Add(new MainMenuButton(ButtonSystemStrings.Edit, @"button-edit-select", OsuIcon.EditCircle, new Color4(238, 170, 0, 255), () => OnEdit?.Invoke(), 0, Key.E));
2022-04-19 14:46:03 +08:00
buttonsTopLevel.Add(new MainMenuButton(ButtonSystemStrings.Browse, @"button-direct-select", OsuIcon.ChevronDownCircle, new Color4(165, 204, 0, 255), () => OnBeatmapListing?.Invoke(), 0,
Key.D));
if (host.CanExit)
buttonsTopLevel.Add(new MainMenuButton(ButtonSystemStrings.Exit, string.Empty, OsuIcon.CrossCircle, new Color4(238, 51, 153, 255), () => OnExit?.Invoke(), 0, Key.Q));
buttonArea.AddRange(buttonsPlay);
buttonArea.AddRange(buttonsTopLevel);
2019-03-26 16:18:35 +08:00
buttonArea.ForEach(b =>
{
if (b is MainMenuButton)
2019-03-26 16:18:35 +08:00
{
b.Origin = Anchor.CentreLeft;
b.Anchor = Anchor.CentreLeft;
}
});
isIdle.ValueChanged += idle => updateIdleState(idle.NewValue);
2018-11-26 15:50:41 +08:00
if (idleTracker != null) isIdle.BindTo(idleTracker.IsIdle);
sampleBack = audio.Samples.Get(@"Menu/button-back-select");
2016-12-05 20:09:41 +08:00
}
2018-04-13 17:19:50 +08:00
2020-12-22 13:55:25 +08:00
private void onMultiplayer()
{
2020-12-29 17:56:29 +08:00
if (api.State.Value != APIState.Online)
{
loginOverlay?.Show();
return;
}
2018-12-26 12:18:36 +08:00
2020-12-22 13:55:25 +08:00
OnMultiplayer?.Invoke();
}
2020-12-25 12:11:21 +08:00
private void onPlaylists()
2020-12-22 13:55:25 +08:00
{
2020-12-29 17:56:29 +08:00
if (api.State.Value != APIState.Online)
2020-12-22 13:55:25 +08:00
{
loginOverlay?.Show();
2020-12-22 13:55:25 +08:00
return;
}
2020-12-25 12:11:21 +08:00
OnPlaylists?.Invoke();
}
private void updateIdleState(bool isIdle)
{
2022-04-19 14:46:03 +08:00
if (!ReturnToTopOnIdle)
return;
if (isIdle && State != ButtonSystemState.Exit && State != ButtonSystemState.EnteringMode)
State = ButtonSystemState.Initial;
}
2022-07-05 17:15:37 +08:00
/// <summary>
/// Triggers the <see cref="logo"/> if the current <see cref="State"/> is <see cref="ButtonSystemState.Initial"/>.
/// </summary>
/// <returns><c>true</c> if the <see cref="logo"/> was triggered, <c>false</c> otherwise.</returns>
private bool triggerInitialOsuLogo()
{
if (State == ButtonSystemState.Initial)
{
logo?.TriggerClick();
return true;
}
2022-07-05 17:15:37 +08:00
return false;
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Repeat || e.ControlPressed || e.ShiftPressed || e.AltPressed || e.SuperPressed)
return false;
if (triggerInitialOsuLogo())
return true;
return base.OnKeyDown(e);
}
protected override bool OnJoystickPress(JoystickPressEvent e)
{
2022-07-05 17:15:37 +08:00
if (triggerInitialOsuLogo())
return true;
return base.OnJoystickPress(e);
}
protected override bool OnMidiDown(MidiDownEvent e)
{
2022-07-05 17:15:37 +08:00
if (triggerInitialOsuLogo())
return true;
return base.OnMidiDown(e);
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
2018-04-30 01:15:09 +08:00
{
if (e.Repeat)
return false;
2021-09-16 17:26:12 +08:00
switch (e.Action)
{
case GlobalAction.Back:
return goBack();
2019-04-01 11:16:05 +08:00
case GlobalAction.Select:
2021-08-04 16:27:44 +08:00
logo?.TriggerClick();
return true;
2019-04-01 11:16:05 +08:00
default:
return false;
}
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
private bool goBack()
{
switch (State)
{
case ButtonSystemState.TopLevel:
State = ButtonSystemState.Initial;
sampleBack?.Play();
return true;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.Play:
2021-08-04 16:27:44 +08:00
backButton.TriggerClick();
return true;
2019-04-01 11:16:05 +08:00
default:
return false;
}
2018-04-30 01:15:09 +08:00
}
2018-04-13 17:19:50 +08:00
private bool onOsuLogo()
2016-08-26 11:28:23 +08:00
{
switch (state)
{
default:
2021-06-26 00:21:26 +08:00
return false;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.Initial:
State = ButtonSystemState.TopLevel;
return true;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.TopLevel:
2021-08-04 16:27:44 +08:00
buttonsTopLevel.First().TriggerClick();
return false;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.Play:
2021-08-04 16:27:44 +08:00
buttonsPlay.First().TriggerClick();
return false;
2016-08-26 11:28:23 +08:00
}
}
2018-04-13 17:19:50 +08:00
private ButtonSystemState state = ButtonSystemState.Initial;
2018-04-13 17:19:50 +08:00
public override bool HandleNonPositionalInput => state != ButtonSystemState.Exit;
public override bool HandlePositionalInput => state != ButtonSystemState.Exit;
2018-04-13 17:19:50 +08:00
public ButtonSystemState State
2016-08-26 11:28:23 +08:00
{
get => state;
2018-04-13 17:19:50 +08:00
2016-08-26 11:28:23 +08:00
set
{
if (state == value) return;
2018-04-13 17:19:50 +08:00
ButtonSystemState lastState = state;
2016-08-26 11:28:23 +08:00
state = value;
2018-04-13 17:19:50 +08:00
updateLogoState(lastState);
2018-04-13 17:19:50 +08:00
Logger.Log($"{nameof(ButtonSystem)}'s state changed from {lastState} to {state}");
2018-04-13 17:19:50 +08:00
2021-07-05 23:52:39 +08:00
using (buttonArea.BeginDelayedSequence(lastState == ButtonSystemState.Initial ? 150 : 0))
{
buttonArea.ButtonSystemState = state;
2018-04-13 17:19:50 +08:00
foreach (var b in buttonArea.Children.OfType<MainMenuButton>())
b.ButtonSystemState = state;
2016-08-26 11:28:23 +08:00
}
2018-04-13 17:19:50 +08:00
2017-09-04 08:10:04 +08:00
StateChanged?.Invoke(State);
2016-08-26 11:28:23 +08:00
}
}
2018-04-13 17:19:50 +08:00
private ScheduledDelegate logoDelayedAction;
2018-04-13 17:19:50 +08:00
private void updateLogoState(ButtonSystemState lastState = ButtonSystemState.Initial)
{
if (logo == null) return;
2018-04-13 17:19:50 +08:00
switch (state)
{
case ButtonSystemState.Exit:
case ButtonSystemState.Initial:
logoDelayedAction?.Cancel();
logoDelayedAction = Scheduler.AddDelayed(() =>
{
logoTrackingContainer.StopTracking();
game?.Toolbar.Hide();
2018-06-06 15:17:51 +08:00
logo?.ClearTransforms(targetMember: nameof(Position));
logo?.MoveTo(new Vector2(0.5f), 800, Easing.OutExpo);
logo?.ScaleTo(1, 800, Easing.OutExpo);
}, buttonArea.Alpha * 150);
break;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.TopLevel:
case ButtonSystemState.Play:
switch (lastState)
{
case ButtonSystemState.TopLevel: // coming from toplevel to play
break;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.Initial:
logo.ClearTransforms(targetMember: nameof(Position));
bool impact = logo.Scale.X > 0.6f;
logo.ScaleTo(0.5f, 200, Easing.In);
2018-04-13 17:19:50 +08:00
logoTrackingContainer.StartTracking(logo, 200, Easing.In);
2018-04-13 17:19:50 +08:00
logoDelayedAction?.Cancel();
logoDelayedAction = Scheduler.AddDelayed(() =>
{
2018-05-31 14:17:59 +08:00
if (impact)
logo?.Impact();
2018-06-06 15:17:51 +08:00
game?.Toolbar.Show();
}, 200);
break;
2019-04-01 11:16:05 +08:00
default:
logo.ClearTransforms(targetMember: nameof(Position));
2019-04-08 14:24:09 +08:00
logoTrackingContainer.StartTracking(logo, 0, Easing.In);
logo.ScaleTo(0.5f, 200, Easing.OutQuint);
break;
}
2018-04-30 01:15:09 +08:00
break;
2019-04-01 11:16:05 +08:00
case ButtonSystemState.EnteringMode:
2019-07-05 15:07:17 +08:00
logoTrackingContainer.StartTracking(logo, lastState == ButtonSystemState.Initial ? MainMenu.FADE_OUT_DURATION : 0, Easing.InSine);
break;
}
}
}
2018-04-13 17:19:50 +08:00
public enum ButtonSystemState
{
Exit,
Initial,
TopLevel,
Play,
EnteringMode,
2016-08-26 11:28:23 +08:00
}
}