1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 16:33:21 +08:00
osu-lazer/osu.Game/Screens/Play/GameplayMenuOverlay.cs

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

281 lines
9.6 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
using System;
using System.Collections.Generic;
2021-07-07 18:59:31 +08:00
using System.Linq;
using Humanizer;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play
{
public abstract partial class GameplayMenuOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
{
protected const int TRANSITION_DURATION = 200;
private const int button_height = 70;
private const float background_alpha = 0.75f;
2018-04-13 17:19:50 +08:00
protected override bool BlockNonPositionalInput => true;
2018-04-13 17:19:50 +08:00
protected override bool BlockScrollInput => false;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-04-13 17:19:50 +08:00
public Action OnRetry;
public Action OnQuit;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Back"/> is triggered.
/// </summary>
protected virtual Action BackAction => () => InternalButtons.LastOrDefault()?.TriggerClick();
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Select"/> is triggered.
/// </summary>
2021-08-04 16:27:44 +08:00
protected virtual Action SelectAction => () => InternalButtons.Selected?.TriggerClick();
2019-03-24 11:03:06 +08:00
2017-04-06 03:36:03 +08:00
public abstract string Header { get; }
2019-03-24 11:03:06 +08:00
2017-04-06 03:36:03 +08:00
public abstract string Description { get; }
2018-04-13 17:19:50 +08:00
protected SelectionCycleFillFlowContainer<DialogButton> InternalButtons;
2017-12-18 18:13:08 +08:00
public IReadOnlyList<DialogButton> Buttons => InternalButtons;
2018-04-13 17:19:50 +08:00
private FillFlowContainer retryCounterContainer;
2018-04-13 17:19:50 +08:00
2017-12-18 15:40:50 +08:00
protected GameplayMenuOverlay()
{
2017-12-18 13:05:12 +08:00
RelativeSizeAxes = Axes.Both;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
Alpha = background_alpha,
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 50),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new Drawable[]
{
new FillFlowContainer
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 20),
Children = new Drawable[]
{
new OsuSpriteText
{
2017-04-06 03:36:03 +08:00
Text = Header,
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 30),
Spacing = new Vector2(5, 0),
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Colour = colours.Yellow,
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f)
},
new OsuSpriteText
{
2017-04-02 02:50:25 +08:00
Text = Description,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f)
}
}
},
InternalButtons = new SelectionCycleFillFlowContainer<DialogButton>
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Masking = true,
2017-06-12 11:48:47 +08:00
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.6f),
Radius = 50
},
},
retryCounterContainer = new FillFlowContainer
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
AutoSizeAxes = Axes.Both,
}
}
},
};
2018-04-13 17:19:50 +08:00
2022-06-24 20:25:23 +08:00
State.ValueChanged += _ => InternalButtons.Deselect();
updateRetryCount();
}
2018-04-13 17:19:50 +08:00
private int retries;
2018-04-13 17:19:50 +08:00
2017-12-18 13:05:12 +08:00
public int Retries
{
2017-12-18 13:05:12 +08:00
set
{
if (value == retries)
return;
2018-04-13 17:19:50 +08:00
retries = value;
if (retryCounterContainer != null)
updateRetryCount();
2017-12-18 13:05:12 +08:00
}
}
2018-04-13 17:19:50 +08:00
protected override void PopIn() => this.FadeIn(TRANSITION_DURATION, Easing.In);
protected override void PopOut() => this.FadeOut(TRANSITION_DURATION, Easing.In);
2018-04-13 17:19:50 +08:00
2017-12-18 13:05:12 +08:00
// Don't let mouse down events through the overlay or people can click circles while paused.
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e) => true;
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e) => true;
2018-04-13 17:19:50 +08:00
2017-12-18 13:05:12 +08:00
protected void AddButton(string text, Color4 colour, Action action)
{
2017-12-18 15:40:50 +08:00
var button = new Button
2017-12-18 13:05:12 +08:00
{
Text = text,
ButtonColour = colour,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Height = button_height,
Action = delegate
{
action?.Invoke();
Hide();
}
};
2018-04-13 17:19:50 +08:00
2017-12-18 18:13:08 +08:00
InternalButtons.Add(button);
}
2018-04-13 17:19:50 +08:00
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
2021-09-16 17:26:12 +08:00
switch (e.Action)
{
case GlobalAction.SelectPrevious:
InternalButtons.SelectPrevious();
return true;
case GlobalAction.SelectNext:
InternalButtons.SelectNext();
return true;
2019-03-24 11:03:06 +08:00
case GlobalAction.Back:
BackAction.Invoke();
return true;
2019-04-01 11:44:46 +08:00
2019-03-24 11:03:06 +08:00
case GlobalAction.Select:
SelectAction.Invoke();
return true;
}
2019-03-24 11:19:09 +08:00
return false;
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
2019-03-24 11:19:09 +08:00
{
}
2021-07-06 20:11:46 +08:00
private void updateRetryCount()
{
// "You've retried 1,065 times in this session"
// "You've retried 1 time in this session"
2018-04-13 17:19:50 +08:00
retryCounterContainer.Children = new Drawable[]
{
2017-12-21 20:46:57 +08:00
new OsuSpriteText
{
Text = "You've retried ",
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f),
Font = OsuFont.GetFont(size: 18),
2017-12-21 20:46:57 +08:00
},
new OsuSpriteText
{
Text = "time".ToQuantity(retries),
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 18),
2017-12-21 20:46:57 +08:00
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f),
},
new OsuSpriteText
{
Text = " in this session",
2017-12-21 20:46:57 +08:00
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f),
Font = OsuFont.GetFont(size: 18),
2017-12-21 20:46:57 +08:00
}
};
}
2018-04-13 17:19:50 +08:00
2017-12-18 15:40:50 +08:00
private partial class Button : DialogButton
{
// required to ensure keyboard navigation always starts from an extremity (unless the cursor is moved)
protected override bool OnHover(HoverEvent e) => true;
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
{
State = SelectionState.Selected;
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
}
}
[Resolved]
private GlobalActionContainer globalAction { get; set; }
protected override bool Handle(UIEvent e)
{
switch (e)
{
2022-06-24 20:25:23 +08:00
case ScrollEvent:
if (ReceivePositionalInputAt(e.ScreenSpaceMousePosition))
return globalAction.TriggerEvent(e);
break;
}
return base.Handle(e);
}
}
}