osu/osu.Game/Graphics/Containers/OsuFocusedOverlayContainer.cs

143 lines
4.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 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Containers;
2018-11-20 07:51:59 +00:00
using osuTK;
using osu.Framework.Input.Bindings;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
using osu.Game.Audio;
using osu.Game.Input.Bindings;
using osu.Game.Overlays;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Graphics.Containers
{
2019-08-21 09:16:30 +00:00
[Cached(typeof(IPreviewTrackOwner))]
public abstract class OsuFocusedOverlayContainer : FocusedOverlayContainer, IPreviewTrackOwner, IKeyBindingHandler<GlobalAction>
2018-04-13 09:19:50 +00:00
{
private SampleChannel samplePopIn;
private SampleChannel samplePopOut;
protected override bool BlockNonPositionalInput => true;
2018-07-13 11:40:49 +00:00
2019-03-02 05:48:05 +00:00
/// <summary>
/// Temporary to allow for overlays in the main screen content to not dim theirselves.
/// Should be eventually replaced by dimming which is aware of the target dim container (traverse parent for certain interface type?).
/// </summary>
protected virtual bool DimMainContent => true;
[Resolved(CanBeNull = true)]
private OsuGame game { get; set; }
[Resolved]
private PreviewTrackManager previewTrackManager { get; set; }
2018-06-06 06:49:27 +00:00
protected readonly Bindable<OverlayActivation> OverlayActivationMode = new Bindable<OverlayActivation>(OverlayActivation.All);
[BackgroundDependencyLoader(true)]
private void load(AudioManager audio)
{
samplePopIn = audio.Samples.Get(@"UI/overlay-pop-in");
samplePopOut = audio.Samples.Get(@"UI/overlay-pop-out");
}
protected override void LoadComplete()
2018-04-13 09:19:50 +00:00
{
2019-09-11 10:12:55 +00:00
if (game != null)
OverlayActivationMode.BindTo(game.OverlayActivationMode);
OverlayActivationMode.BindValueChanged(mode =>
{
if (mode.NewValue == OverlayActivation.Disabled)
State.Value = Visibility.Hidden;
2019-09-11 10:12:55 +00:00
}, true);
base.LoadComplete();
}
2018-04-13 09:19:50 +00:00
/// <summary>
/// Whether mouse input should be blocked screen-wide while this overlay is visible.
2018-06-15 08:07:07 +00:00
/// Performing mouse actions outside of the valid extents will hide the overlay.
2018-04-13 09:19:50 +00:00
/// </summary>
public virtual bool BlockScreenWideMouse => BlockPositionalInput;
2018-04-13 09:19:50 +00:00
// receive input outside our bounds so we can trigger a close event on ourselves.
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => BlockScreenWideMouse || base.ReceivePositionalInputAt(screenSpacePos);
2018-04-13 09:19:50 +00:00
private bool closeOnMouseUp;
2018-04-13 09:19:50 +00:00
protected override bool OnMouseDown(MouseDownEvent e)
{
2019-12-01 20:54:37 +00:00
closeOnMouseUp = !base.ReceivePositionalInputAt(e.ScreenSpaceMousePosition);
return base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseUpEvent e)
{
if (closeOnMouseUp && !base.ReceivePositionalInputAt(e.ScreenSpaceMousePosition))
Hide();
2018-04-13 09:19:50 +00:00
base.OnMouseUp(e);
2018-04-13 09:19:50 +00:00
}
public virtual bool OnPressed(GlobalAction action)
{
switch (action)
{
case GlobalAction.Back:
Hide();
return true;
2019-04-01 03:44:46 +00:00
case GlobalAction.Select:
return true;
}
return false;
}
public void OnReleased(GlobalAction action)
{
}
2019-08-06 05:21:34 +00:00
protected override void UpdateState(ValueChangedEvent<Visibility> state)
2018-04-13 09:19:50 +00:00
{
switch (state.NewValue)
{
2018-06-06 06:10:09 +00:00
case Visibility.Visible:
if (OverlayActivationMode.Value == OverlayActivation.Disabled)
{
State.Value = Visibility.Hidden;
return;
}
2019-02-28 04:31:40 +00:00
samplePopIn?.Play();
if (BlockScreenWideMouse && DimMainContent) game?.AddBlockingOverlay(this);
2018-06-06 06:10:09 +00:00
break;
2019-04-01 03:44:46 +00:00
2018-06-06 06:10:09 +00:00
case Visibility.Hidden:
samplePopOut?.Play();
if (BlockScreenWideMouse) game?.RemoveBlockingOverlay(this);
2018-06-06 06:10:09 +00:00
break;
2018-04-13 09:19:50 +00:00
}
base.UpdateState(state);
2018-04-13 09:19:50 +00:00
}
protected override void PopOut()
{
base.PopOut();
previewTrackManager.StopAnyPlaying(this);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
game?.RemoveBlockingOverlay(this);
}
2018-04-13 09:19:50 +00:00
}
}