osu/osu.Game/Overlays/DialogOverlay.cs

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

135 lines
4.3 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 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Overlays.Dialog;
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
2021-09-16 09:26:12 +00:00
using osu.Framework.Input.Events;
using osu.Game.Audio.Effects;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays
{
public partial class DialogOverlay : OsuFocusedOverlayContainer, IDialogOverlay
{
private readonly Container dialogContainer;
2019-09-19 11:17:58 +00:00
protected override string PopInSampleName => "UI/dialog-pop-in";
protected override string PopOutSampleName => "UI/dialog-pop-out";
2021-10-07 09:49:30 +00:00
private AudioFilter lowPassFilter;
2019-09-19 11:17:58 +00:00
public PopupDialog CurrentDialog { get; private set; }
2018-04-13 09:19:50 +00:00
public DialogOverlay()
{
RelativeSizeAxes = Axes.Both;
Child = dialogContainer = new Container
{
RelativeSizeAxes = Axes.Both,
};
Width = 0.4f;
Anchor = Anchor.BottomCentre;
Origin = Anchor.BottomCentre;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
2021-10-07 09:49:30 +00:00
AddInternal(lowPassFilter = new AudioFilter(audio.TrackMixer));
}
public void Push(PopupDialog dialog)
{
if (dialog == CurrentDialog || dialog.State.Value == Visibility.Hidden) return;
// Immediately update the externally accessible property as this may be used for checks even before
// a DialogOverlay instance has finished loading.
var lastDialog = CurrentDialog;
2019-09-19 11:17:58 +00:00
CurrentDialog = dialog;
2018-04-13 09:19:50 +00:00
2022-07-14 04:06:03 +00:00
Schedule(() =>
{
// if any existing dialog is being displayed, dismiss it before showing a new one.
lastDialog?.Hide();
2022-07-14 04:04:46 +00:00
2022-07-15 02:56:49 +00:00
// if the new dialog is hidden before added to the dialogContainer, bypass any further operations.
if (dialog.State.Value == Visibility.Hidden)
{
dismiss();
return;
}
dialogContainer.Add(dialog);
Show();
2018-04-13 09:19:50 +00:00
dialog.State.BindValueChanged(state =>
{
if (state.NewValue != Visibility.Hidden) return;
// Trigger the demise of the dialog as soon as it hides.
dialog.Delay(PopupDialog.EXIT_DURATION).Expire();
dismiss();
});
});
2018-04-13 09:19:50 +00:00
void dismiss()
2019-09-19 11:17:58 +00:00
{
if (dialog != CurrentDialog) return;
// Handle the case where the dialog is the currently displayed dialog.
// In this scenario, the overlay itself should also be hidden.
Hide();
2019-09-19 11:17:58 +00:00
CurrentDialog = null;
}
}
2018-04-13 09:19:50 +00:00
public override bool IsPresent => Scheduler.HasPendingTasks || dialogContainer.Children.Count > 0;
protected override bool BlockNonPositionalInput => true;
protected override void PopIn()
{
2021-10-07 09:49:30 +00:00
lowPassFilter.CutoffTo(300, 100, Easing.OutCubic);
}
2018-04-13 09:19:50 +00:00
protected override void PopOut()
{
base.PopOut();
lowPassFilter.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF, 100, Easing.InCubic);
// PopOut gets called initially, but we only want to hide dialog when we have been loaded and are present.
if (IsLoaded && CurrentDialog?.State.Value == Visibility.Visible)
CurrentDialog.Hide();
}
2021-09-16 09:26:12 +00:00
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat)
return false;
2021-09-16 09:26:12 +00:00
switch (e.Action)
{
case GlobalAction.Select:
var clickableButton =
CurrentDialog?.Buttons.OfType<PopupDialogOkButton>().FirstOrDefault() ??
CurrentDialog?.Buttons.First();
clickableButton?.TriggerClick();
return true;
}
2021-09-16 09:26:12 +00:00
return base.OnPressed(e);
}
}
}