1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 17:17:26 +08:00
osu-lazer/osu.Game/Screens/Play/GameplayMenuOverlay.cs

303 lines
10 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
using System;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Sprites;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Framework.Allocation;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Graphics.Shapes;
2018-11-20 15:51:59 +08:00
using osuTK.Input;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Game.Input.Bindings;
using Humanizer;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play
{
public abstract class GameplayMenuOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
2018-04-13 17:19:50 +08:00
{
private const int transition_duration = 200;
private const int button_height = 70;
private const float background_alpha = 0.75f;
protected override bool BlockNonPositionalInput => true;
2018-04-13 17:19:50 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-04-13 17:19:50 +08:00
public Action OnRetry;
public Action OnQuit;
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Back"/> is triggered.
/// </summary>
protected virtual Action BackAction => () => InternalButtons.Children.Last().Click();
2018-04-13 17:19:50 +08:00
public abstract string Header { get; }
public abstract string Description { get; }
protected internal FillFlowContainer<DialogButton> InternalButtons;
public IReadOnlyList<DialogButton> Buttons => InternalButtons;
private FillFlowContainer retryCounterContainer;
protected GameplayMenuOverlay()
{
RelativeSizeAxes = Axes.Both;
StateChanged += s => selectionIndex = -1;
}
[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
{
Text = Header,
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 30),
2018-04-13 17:19:50 +08:00
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
{
Text = Description,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f)
}
}
},
InternalButtons = new FillFlowContainer<DialogButton>
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Masking = true,
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,
}
}
},
};
updateRetryCount();
}
private int retries;
public int Retries
{
set
{
if (value == retries)
return;
retries = value;
if (retryCounterContainer != null)
updateRetryCount();
}
}
protected override void PopIn() => this.FadeIn(transition_duration, Easing.In);
protected override void PopOut() => this.FadeOut(transition_duration, Easing.In);
// 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 OnMouseUp(MouseUpEvent 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
protected void AddButton(string text, Color4 colour, Action action)
{
var button = new Button
{
Text = text,
ButtonColour = colour,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Height = button_height,
Action = delegate
{
action?.Invoke();
Hide();
}
};
button.Selected.ValueChanged += selected => buttonSelectionChanged(button, selected.NewValue);
2018-04-13 17:19:50 +08:00
InternalButtons.Add(button);
}
private int _selectionIndex = -1;
private int selectionIndex
{
get => _selectionIndex;
2018-04-13 17:19:50 +08:00
set
{
if (_selectionIndex == value)
return;
// Deselect the previously-selected button
if (_selectionIndex != -1)
InternalButtons[_selectionIndex].Selected.Value = false;
_selectionIndex = value;
// Select the newly-selected button
if (_selectionIndex != -1)
InternalButtons[_selectionIndex].Selected.Value = true;
}
}
2018-10-02 11:02:47 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 17:19:50 +08:00
{
2018-10-02 11:02:47 +08:00
if (!e.Repeat)
2018-04-13 17:19:50 +08:00
{
2018-10-02 11:02:47 +08:00
switch (e.Key)
2018-04-13 17:19:50 +08:00
{
case Key.Up:
if (selectionIndex == -1 || selectionIndex == 0)
selectionIndex = InternalButtons.Count - 1;
else
selectionIndex--;
return true;
case Key.Down:
if (selectionIndex == -1 || selectionIndex == InternalButtons.Count - 1)
selectionIndex = 0;
else
selectionIndex++;
return true;
}
}
2018-10-02 11:02:47 +08:00
return base.OnKeyDown(e);
2018-04-13 17:19:50 +08:00
}
public bool OnPressed(GlobalAction action)
{
if (action == GlobalAction.Back)
{
BackAction.Invoke();
return true;
}
return false;
}
public bool OnReleased(GlobalAction action) => action == GlobalAction.Back;
2018-04-13 17:19:50 +08:00
private void buttonSelectionChanged(DialogButton button, bool isSelected)
{
if (!isSelected)
selectionIndex = -1;
else
selectionIndex = InternalButtons.IndexOf(button);
}
private void updateRetryCount()
{
// "You've retried 1,065 times in this session"
// "You've retried 1 time in this session"
retryCounterContainer.Children = new Drawable[]
{
new OsuSpriteText
{
Text = "You've retried ",
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f),
Font = OsuFont.GetFont(size: 18),
2018-04-13 17:19:50 +08:00
},
new OsuSpriteText
{
Text = "time".ToQuantity(retries),
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 18),
2018-04-13 17:19:50 +08:00
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f),
},
new OsuSpriteText
{
Text = " in this session",
2018-04-13 17:19:50 +08:00
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.25f),
Font = OsuFont.GetFont(size: 18),
2018-04-13 17:19:50 +08:00
}
};
}
private class Button : DialogButton
{
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e) => true;
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 17:19:50 +08:00
{
Selected.Value = true;
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 17:19:50 +08:00
{
2019-02-21 17:56:34 +08:00
if (e.Repeat || e.Key != Key.Enter || !Selected.Value)
2018-04-13 17:19:50 +08:00
return false;
Click();
2018-04-13 17:19:50 +08:00
return true;
}
}
}
}