1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 05:52:56 +08:00
osu-lazer/osu.Game/Screens/Play/PlayerLoader.cs

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

682 lines
24 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
2018-12-26 21:16:35 +08:00
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using ManagedBass.Fx;
2017-02-22 20:43:29 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
2019-10-01 23:39:01 +08:00
using osu.Framework.Bindables;
2017-02-22 20:43:29 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2020-10-20 06:22:30 +08:00
using osu.Framework.Graphics.Transforms;
2019-03-20 18:35:40 +08:00
using osu.Framework.Input;
2017-02-22 20:43:29 +08:00
using osu.Framework.Screens;
using osu.Framework.Threading;
2024-02-16 16:13:41 +08:00
using osu.Framework.Utils;
using osu.Game.Audio;
2021-10-07 13:15:16 +08:00
using osu.Game.Audio.Effects;
using osu.Game.Beatmaps;
2019-10-01 23:39:01 +08:00
using osu.Game.Configuration;
2017-02-22 20:43:29 +08:00
using osu.Game.Graphics;
2019-03-22 18:01:32 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Input;
2023-01-14 04:11:25 +08:00
using osu.Game.Localisation;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Performance;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Play.PlayerSettings;
using osu.Game.Skinning;
using osu.Game.Users;
using osu.Game.Utils;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2017-02-22 20:43:29 +08:00
namespace osu.Game.Screens.Play
{
public partial class PlayerLoader : ScreenWithBeatmapBackground
2017-02-22 20:43:29 +08:00
{
protected const float BACKGROUND_BLUR = 15;
protected const double CONTENT_OUT_DURATION = 300;
protected virtual double PlayerPushDelay => 1800 + disclaimers.Count * 500;
2020-02-14 17:22:57 +08:00
public override bool HideOverlaysOnEnter => hideOverlays;
2018-04-13 17:19:50 +08:00
2020-02-14 17:22:57 +08:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
2019-01-24 18:55:42 +08:00
public override bool? AllowGlobalTrackControl => false;
2020-02-14 17:22:57 +08:00
// Here because IsHovered will not update unless we do so.
public override bool HandlePositionalInput => true;
2018-04-13 17:19:50 +08:00
2020-02-14 17:22:57 +08:00
// We show the previous screen status
2022-03-17 18:36:33 +08:00
protected override UserActivity? InitialActivity => null;
2018-04-13 17:19:50 +08:00
2022-03-17 18:36:33 +08:00
protected BeatmapMetadataDisplay MetadataInfo { get; private set; } = null!;
2019-02-01 14:42:15 +08:00
/// <summary>
/// A fill flow containing the player settings groups, exposed for the ability to hide it from inheritors of the player loader.
/// </summary>
2022-03-17 18:36:33 +08:00
protected FillFlowContainer<PlayerSettingsGroup> PlayerSettings { get; private set; } = null!;
2022-03-17 18:36:33 +08:00
protected VisualSettings VisualSettings { get; private set; } = null!;
2022-03-17 18:36:33 +08:00
protected AudioSettings AudioSettings { get; private set; } = null!;
2022-03-17 18:36:33 +08:00
protected Task? LoadTask { get; private set; }
2019-12-06 12:47:34 +08:00
2022-03-17 18:36:33 +08:00
protected Task? DisposalTask { get; private set; }
2018-04-13 17:19:50 +08:00
private FillFlowContainer disclaimers = null!;
private OsuScrollContainer settingsScroll = null!;
private Bindable<bool> showStoryboards = null!;
2020-02-14 17:22:57 +08:00
private bool backgroundBrightnessReduction;
2020-10-25 19:33:35 +08:00
private readonly BindableDouble volumeAdjustment = new BindableDouble(1);
private AudioFilter? lowPassFilter;
private AudioFilter? highPassFilter;
2021-10-07 13:15:16 +08:00
private SkinnableSound sampleRestart = null!;
2022-10-13 15:04:38 +08:00
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
2020-02-14 17:22:57 +08:00
protected bool BackgroundBrightnessReduction
{
set
{
if (value == backgroundBrightnessReduction)
return;
backgroundBrightnessReduction = value;
ApplyToBackground(b => b.FadeColour(OsuColour.Gray(backgroundBrightnessReduction ? 0.8f : 1), 200));
2020-02-14 17:22:57 +08:00
}
}
private bool readyForPush =>
!playerConsumed
// don't push unless the player is completely loaded
&& CurrentPlayer?.LoadState == LoadState.Ready
// don't push unless the player is ready to start gameplay
&& ReadyForGameplay;
protected virtual bool ReadyForGameplay =>
// not ready if the user is hovering one of the panes (logo is excluded), unless they are idle.
(IsHovered || osuLogo?.IsHovered == true || idleTracker.IsIdle.Value)
// not ready if the user is dragging a slider or otherwise.
&& (inputManager.DraggedDrawable == null || inputManager.DraggedDrawable is OsuLogo)
// not ready if a focused overlay is visible, like settings.
2022-03-17 18:36:33 +08:00
&& inputManager.FocusedDrawable == null;
2020-02-14 17:22:57 +08:00
private readonly Func<Player> createPlayer;
/// <summary>
/// The <see cref="Player"/> instance being loaded by this screen.
/// </summary>
2022-03-17 18:36:33 +08:00
public Player? CurrentPlayer { get; private set; }
2020-02-14 17:22:57 +08:00
/// <summary>
/// Whether the current player instance has been consumed via <see cref="consumePlayer"/>.
/// </summary>
private bool playerConsumed;
2022-03-17 18:36:33 +08:00
private LogoTrackingContainer content = null!;
2020-02-14 17:22:57 +08:00
private bool hideOverlays;
2022-03-17 18:36:33 +08:00
private InputManager inputManager = null!;
2020-02-14 17:22:57 +08:00
2022-03-17 18:36:33 +08:00
private IdleTracker idleTracker = null!;
2022-03-17 18:36:33 +08:00
private ScheduledDelegate? scheduledPushPlayer;
2020-02-14 17:22:57 +08:00
private PlayerLoaderDisclaimer? epilepsyWarning;
2020-10-20 06:08:05 +08:00
private bool quickRestart;
private IDisposable? highPerformanceSession;
[Resolved]
private INotificationOverlay? notificationOverlay { get; set; }
2019-10-03 18:16:31 +08:00
[Resolved]
2022-03-17 18:36:33 +08:00
private VolumeOverlay? volumeOverlay { get; set; }
2019-10-03 18:16:31 +08:00
[Resolved]
2022-03-17 18:36:33 +08:00
private AudioManager audioManager { get; set; } = null!;
2019-10-03 18:16:31 +08:00
[Resolved]
2022-03-17 18:36:33 +08:00
private BatteryInfo? batteryInfo { get; set; }
[Resolved]
2024-02-27 18:36:03 +08:00
private IHighPerformanceSessionManager? highPerformanceSessionManager { get; set; }
2018-12-26 21:16:35 +08:00
public PlayerLoader(Func<Player> createPlayer)
2017-02-22 20:43:29 +08:00
{
2018-12-26 21:16:35 +08:00
this.createPlayer = createPlayer;
}
2018-04-13 17:19:50 +08:00
2017-02-22 20:43:29 +08:00
[BackgroundDependencyLoader]
private void load(SessionStatics sessionStatics, OsuConfigManager config)
2017-02-22 20:43:29 +08:00
{
2019-10-01 23:39:01 +08:00
muteWarningShownOnce = sessionStatics.GetBindable<bool>(Static.MutedAudioNotificationShownOnce);
batteryWarningShownOnce = sessionStatics.GetBindable<bool>(Static.LowBatteryNotificationShownOnce);
showStoryboards = config.GetBindable<bool>(OsuSetting.ShowStoryboard);
2019-10-01 23:39:01 +08:00
const float padding = 25;
InternalChildren = new Drawable[]
2019-03-27 17:11:12 +08:00
{
(content = new LogoTrackingContainer
{
2019-03-27 17:11:12 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
}).WithChildren(new Drawable[]
2019-03-27 17:11:12 +08:00
{
MetadataInfo = new BeatmapMetadataDisplay(Beatmap.Value, Mods, content.LogoFacade)
2019-01-24 18:55:42 +08:00
{
Alpha = 0,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
}),
disclaimers = new FillFlowContainer
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
Width = SettingsToolboxGroup.CONTAINER_WIDTH + padding * 2,
AutoSizeAxes = Axes.Y,
AutoSizeDuration = 250,
AutoSizeEasing = Easing.OutQuint,
Direction = FillDirection.Vertical,
Padding = new MarginPadding(padding),
2024-03-15 18:38:51 +08:00
Spacing = new Vector2(20),
},
settingsScroll = new OsuScrollContainer
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
RelativeSizeAxes = Axes.Y,
Width = SettingsToolboxGroup.CONTAINER_WIDTH + padding * 2,
Padding = new MarginPadding { Vertical = padding },
Masking = false,
Child = PlayerSettings = new FillFlowContainer<PlayerSettingsGroup>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 20),
Padding = new MarginPadding { Horizontal = padding },
Children = new PlayerSettingsGroup[]
{
VisualSettings = new VisualSettings(),
AudioSettings = new AudioSettings(),
new InputSettings()
}
},
},
idleTracker = new IdleTracker(750),
sampleRestart = new SkinnableSound(new SampleInfo(@"Gameplay/restart", @"pause-retry-click"))
};
2020-10-20 06:08:05 +08:00
if (Beatmap.Value.BeatmapInfo.EpilepsyWarning)
{
2024-03-15 17:58:00 +08:00
disclaimers.Add(epilepsyWarning = new PlayerLoaderDisclaimer(PlayerLoaderStrings.EpilepsyWarningTitle, PlayerLoaderStrings.EpilepsyWarningContent));
2020-10-20 06:08:05 +08:00
}
switch (Beatmap.Value.BeatmapInfo.Status)
{
case BeatmapOnlineStatus.Loved:
2024-03-15 17:58:00 +08:00
disclaimers.Add(new PlayerLoaderDisclaimer(PlayerLoaderStrings.LovedBeatmapDisclaimerTitle, PlayerLoaderStrings.LovedBeatmapDisclaimerContent));
break;
case BeatmapOnlineStatus.Qualified:
2024-03-15 17:58:00 +08:00
disclaimers.Add(new PlayerLoaderDisclaimer(PlayerLoaderStrings.QualifiedBeatmapDisclaimerTitle, PlayerLoaderStrings.QualifiedBeatmapDisclaimerContent));
break;
}
2017-02-22 20:43:29 +08:00
}
2018-04-13 17:19:50 +08:00
2019-10-03 18:16:31 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager()!;
showStoryboards.BindValueChanged(val => epilepsyWarning?.FadeTo(val.NewValue ? 1 : 0, 250, Easing.OutQuint), true);
epilepsyWarning?.FinishTransforms(true);
}
2020-02-14 17:28:58 +08:00
#region Screen handling
public override void OnEntering(ScreenTransitionEvent e)
{
base.OnEntering(e);
2020-10-25 19:33:35 +08:00
Beatmap.Value.Track.AddAdjustment(AdjustableProperty.Volume, volumeAdjustment);
// Start side content off-screen.
disclaimers.MoveToX(-disclaimers.DrawWidth);
settingsScroll.MoveToX(settingsScroll.DrawWidth);
content.ScaleTo(0.7f);
const double metadata_delay = 500;
2024-03-15 18:38:51 +08:00
MetadataInfo.Delay(metadata_delay).FadeIn(500, Easing.OutQuint);
contentIn(metadata_delay + 250);
// after an initial delay, start the debounced load check.
// this will continue to execute even after resuming back on restart.
Scheduler.Add(new ScheduledDelegate(pushWhenLoaded, Clock.CurrentTime + PlayerPushDelay, 0));
2019-10-03 18:16:31 +08:00
2020-02-14 17:28:58 +08:00
showMuteWarningIfNeeded();
showBatteryWarningIfNeeded();
2019-10-03 18:16:31 +08:00
}
public override void OnResuming(ScreenTransitionEvent e)
{
base.OnResuming(e);
2018-04-13 17:19:50 +08:00
2022-03-17 18:36:33 +08:00
Debug.Assert(CurrentPlayer != null);
highPerformanceSession?.Dispose();
highPerformanceSession = null;
// prepare for a retry.
CurrentPlayer = null;
playerConsumed = false;
cancelLoad();
sampleRestart.Play();
contentIn();
}
2018-04-13 17:19:50 +08:00
public override void OnSuspending(ScreenTransitionEvent e)
2018-12-26 21:16:35 +08:00
{
base.OnSuspending(e);
2018-12-26 21:16:35 +08:00
2020-02-14 17:22:57 +08:00
BackgroundBrightnessReduction = false;
2020-10-25 19:33:35 +08:00
// we're moving to player, so a period of silence is upcoming.
// stop the track before removing adjustment to avoid a volume spike.
Beatmap.Value.Track.Stop();
Beatmap.Value.Track.RemoveAdjustment(AdjustableProperty.Volume, volumeAdjustment);
lowPassFilter?.RemoveAndDisposeImmediately();
highPassFilter?.RemoveAndDisposeImmediately();
2018-12-26 21:16:35 +08:00
}
public override bool OnExiting(ScreenExitEvent e)
{
2020-02-14 17:22:57 +08:00
cancelLoad();
ContentOut();
2018-04-13 17:19:50 +08:00
// Ensure the screen doesn't expire until all the outwards fade operations have completed.
this.Delay(CONTENT_OUT_DURATION).FadeOut();
ApplyToBackground(b => b.IgnoreUserSettings.Value = true);
2020-02-14 17:22:57 +08:00
BackgroundBrightnessReduction = false;
2020-10-25 19:33:35 +08:00
Beatmap.Value.Track.RemoveAdjustment(AdjustableProperty.Volume, volumeAdjustment);
2020-02-14 17:22:57 +08:00
highPerformanceSession?.Dispose();
highPerformanceSession = null;
return base.OnExiting(e);
}
2018-04-13 17:19:50 +08:00
private OsuLogo? osuLogo;
2017-11-09 16:38:20 +08:00
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
2017-11-09 16:38:20 +08:00
base.LogoArriving(logo, resuming);
2018-04-13 17:19:50 +08:00
osuLogo = logo;
2019-03-22 19:01:58 +08:00
const double duration = 300;
2018-04-13 17:19:50 +08:00
if (!resuming) logo.MoveTo(new Vector2(0.5f), duration, Easing.OutQuint);
logo.ScaleTo(new Vector2(0.15f), duration, Easing.OutQuint);
2017-11-02 20:52:01 +08:00
logo.FadeIn(350);
2018-04-13 17:19:50 +08:00
Scheduler.AddDelayed(() =>
{
if (this.IsCurrentScreen())
content.StartTracking(logo, resuming ? 0 : 500, Easing.InOutExpo);
}, resuming ? 0 : 250);
2019-03-26 16:18:35 +08:00
}
protected override void LogoExiting(OsuLogo logo)
{
base.LogoExiting(logo);
2019-04-08 14:24:09 +08:00
content.StopTracking();
osuLogo = null;
}
2018-04-13 17:19:50 +08:00
protected override void LogoSuspending(OsuLogo logo)
{
base.LogoSuspending(logo);
content.StopTracking();
logo
.FadeOut(CONTENT_OUT_DURATION / 2, Easing.OutQuint)
.ScaleTo(logo.Scale * 0.8f, CONTENT_OUT_DURATION * 2, Easing.OutQuint);
osuLogo = null;
}
2020-02-14 17:28:58 +08:00
#endregion
2020-02-14 17:22:57 +08:00
protected override void Update()
{
base.Update();
2018-04-13 17:19:50 +08:00
2020-02-14 17:22:57 +08:00
if (!this.IsCurrentScreen())
return;
// We need to perform this check here rather than in OnHover as any number of children of VisualSettings
// may also be handling the hover events.
if (inputManager.HoveredDrawables.Contains(VisualSettings))
{
// Preview user-defined background dim and blur when hovered on the visual settings panel.
ApplyToBackground(b =>
{
b.IgnoreUserSettings.Value = false;
b.BlurAmount.Value = 0;
});
2020-02-14 17:22:57 +08:00
BackgroundBrightnessReduction = false;
}
else
{
ApplyToBackground(b =>
{
// Returns background dim and blur to the values specified by PlayerLoader.
b.IgnoreUserSettings.Value = true;
b.BlurAmount.Value = BACKGROUND_BLUR;
});
2020-02-14 17:22:57 +08:00
BackgroundBrightnessReduction = true;
}
}
private Player consumePlayer()
{
Debug.Assert(!playerConsumed);
2022-03-17 18:36:33 +08:00
Debug.Assert(CurrentPlayer != null);
playerConsumed = true;
return CurrentPlayer;
}
2020-02-14 17:22:57 +08:00
private void prepareNewPlayer()
{
if (!this.IsCurrentScreen())
return;
CurrentPlayer = createPlayer();
2022-08-31 18:50:16 +08:00
CurrentPlayer.Configuration.AutomaticallySkipIntro |= quickRestart;
CurrentPlayer.RestartCount = restartCount++;
CurrentPlayer.RestartRequested = restartRequested;
LoadTask = LoadComponentAsync(CurrentPlayer, _ =>
{
MetadataInfo.Loading = false;
OnPlayerLoaded();
});
}
protected virtual void OnPlayerLoaded()
{
2020-02-14 17:22:57 +08:00
}
private void restartRequested(bool quickRestartRequested)
2020-02-14 17:22:57 +08:00
{
quickRestart = quickRestartRequested;
2020-02-14 17:22:57 +08:00
hideOverlays = true;
ValidForResume = true;
this.MakeCurrent();
2020-02-14 17:22:57 +08:00
}
private void contentIn(double delayBeforeSideDisplays = 0)
2020-02-14 17:22:57 +08:00
{
MetadataInfo.Loading = true;
content.FadeInFromZero(500, Easing.OutQuint);
2022-08-06 21:12:36 +08:00
content.ScaleTo(1, 650, Easing.OutQuint).Then().Schedule(prepareNewPlayer);
using (BeginDelayedSequence(delayBeforeSideDisplays))
{
settingsScroll.FadeInFromZero(500, Easing.Out)
.MoveToX(0, 500, Easing.OutQuint);
disclaimers.FadeInFromZero(500, Easing.Out)
.MoveToX(0, 500, Easing.OutQuint);
}
AddRangeInternal(new[]
{
lowPassFilter = new AudioFilter(audioManager.TrackMixer),
highPassFilter = new AudioFilter(audioManager.TrackMixer, BQFType.HighPass),
});
lowPassFilter.CutoffTo(1000, 650, Easing.OutQuint);
2021-11-12 13:22:43 +08:00
highPassFilter.CutoffTo(300).Then().CutoffTo(0, 1250); // 1250 is to line up with the appearance of MetadataInfo (750 delay + 500 fade-in)
ApplyToBackground(b => b.FadeColour(Color4.White, 800, Easing.OutQuint));
2020-02-14 17:22:57 +08:00
}
protected virtual void ContentOut()
2020-02-14 17:22:57 +08:00
{
// Ensure the logo is no longer tracking before we scale the content
content.StopTracking();
content.ScaleTo(0.7f, CONTENT_OUT_DURATION * 2, Easing.OutQuint);
content.FadeOut(CONTENT_OUT_DURATION, Easing.OutQuint)
// Safety for filter potentially getting stuck in applied state due to
// transforms on `this` causing children to no longer be updated.
.OnComplete(_ =>
{
highPassFilter?.RemoveAndDisposeImmediately();
highPassFilter = null;
lowPassFilter?.RemoveAndDisposeImmediately();
lowPassFilter = null;
});
disclaimers.FadeOut(CONTENT_OUT_DURATION, Easing.Out)
.MoveToX(-disclaimers.DrawWidth, CONTENT_OUT_DURATION * 2, Easing.OutQuint);
settingsScroll.FadeOut(CONTENT_OUT_DURATION, Easing.OutQuint)
.MoveToX(settingsScroll.DrawWidth, CONTENT_OUT_DURATION * 2, Easing.OutQuint);
lowPassFilter?.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF, CONTENT_OUT_DURATION);
highPassFilter?.CutoffTo(0, CONTENT_OUT_DURATION);
2020-02-14 17:22:57 +08:00
}
2018-04-13 17:19:50 +08:00
private void pushWhenLoaded()
{
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen()) return;
2018-04-13 17:19:50 +08:00
if (!readyForPush)
{
// as the pushDebounce below has a delay, we need to keep checking and cancel a future debounce
// if we become unready for push during the delay.
cancelLoad();
return;
}
2018-04-13 17:19:50 +08:00
// if a push has already been scheduled, no further action is required.
// this value is reset via cancelLoad() to allow a second usage of the same PlayerLoader screen.
if (scheduledPushPlayer != null)
return;
2018-04-13 17:19:50 +08:00
// Now that everything's been loaded, we can safely switch to a higher performance session without incurring too much overhead.
// Doing this prior to the game being pushed gives us a bit of time to stabilise into the high performance mode before gameplay starts.
highPerformanceSession ??= highPerformanceSessionManager?.BeginSession();
scheduledPushPlayer = Scheduler.AddDelayed(() =>
{
// ensure that once we have reached this "point of no return", readyForPush will be false for all future checks (until a new player instance is prepared).
var consumedPlayer = consumePlayer();
2018-04-13 17:19:50 +08:00
ContentOut();
2020-10-20 06:22:30 +08:00
TransformSequence<PlayerLoader> pushSequence = this.Delay(0);
// This goes hand-in-hand with the restoration of low pass filter in contentOut().
this.TransformBindableTo(volumeAdjustment, 0, CONTENT_OUT_DURATION, Easing.OutCubic);
2018-04-13 17:19:50 +08:00
pushSequence.Schedule(() =>
{
if (!this.IsCurrentScreen()) return;
2018-04-13 17:19:50 +08:00
LoadTask = null;
// By default, we want to load the player and never be returned to.
// Note that this may change if the player we load requested a re-run.
ValidForResume = false;
if (consumedPlayer.LoadedBeatmapSuccessfully)
this.Push(consumedPlayer);
else
this.Exit();
});
2022-08-06 21:12:36 +08:00
}, 500);
2017-02-22 20:43:29 +08:00
}
2018-04-13 17:19:50 +08:00
private void cancelLoad()
{
2020-02-14 17:22:57 +08:00
scheduledPushPlayer?.Cancel();
scheduledPushPlayer = null;
}
2018-04-13 17:19:50 +08:00
2020-02-14 17:22:57 +08:00
#region Disposal
2018-04-13 17:19:50 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2018-04-13 17:19:50 +08:00
if (isDisposing)
{
// if the player never got pushed, we should explicitly dispose it.
DisposalTask = LoadTask?.ContinueWith(_ => CurrentPlayer?.Dispose());
}
}
2018-04-13 17:19:50 +08:00
2020-02-14 17:22:57 +08:00
#endregion
2020-02-14 17:28:58 +08:00
#region Mute warning
2022-03-17 18:36:33 +08:00
private Bindable<bool> muteWarningShownOnce = null!;
2020-02-14 17:28:58 +08:00
private int restartCount;
private const double volume_requirement = 0.01;
2020-02-14 17:28:58 +08:00
private void showMuteWarningIfNeeded()
{
if (!muteWarningShownOnce.Value)
{
double aggregateVolumeTrack = audioManager.Volume.Value * audioManager.VolumeTrack.Value;
2020-05-05 09:31:11 +08:00
// Checks if the notification has not been shown yet and also if master volume is muted, track/music volume is muted or if the whole game is muted.
2024-02-16 16:13:41 +08:00
if (volumeOverlay?.IsMuted.Value == true || Precision.AlmostBigger(volume_requirement, aggregateVolumeTrack))
2020-02-14 17:28:58 +08:00
{
notificationOverlay?.Post(new MutedNotification());
muteWarningShownOnce.Value = true;
}
}
}
private partial class MutedNotification : SimpleNotification
{
2020-02-14 17:22:57 +08:00
public override bool IsImportant => true;
public MutedNotification()
{
2023-01-14 04:11:25 +08:00
Text = NotificationsStrings.GameVolumeTooLow;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, AudioManager audioManager, INotificationOverlay notificationOverlay, VolumeOverlay volumeOverlay)
{
Icon = FontAwesome.Solid.VolumeMute;
IconContent.Colour = colours.RedDark;
Activated = delegate
{
notificationOverlay.Hide();
2019-09-15 22:50:01 +08:00
volumeOverlay.IsMuted.Value = false;
// Check values before resetting, as the user may have only had mute enabled, in which case we might not need to adjust volumes.
2024-02-16 06:31:52 +08:00
// Note that we only restore halfway to ensure the user isn't suddenly overloaded by unexpectedly high volume.
audioManager.Volume.Value = Math.Max(audioManager.Volume.Value, 0.5);
audioManager.VolumeTrack.Value = Math.Max(audioManager.VolumeTrack.Value, 0.5);
return true;
};
}
}
2020-02-14 17:28:58 +08:00
#endregion
#region Low battery warning
2021-04-09 08:28:23 +08:00
2022-07-30 20:26:19 +08:00
private const double low_battery_threshold = 0.25;
2022-03-17 18:36:33 +08:00
private Bindable<bool> batteryWarningShownOnce = null!;
private void showBatteryWarningIfNeeded()
{
if (batteryInfo == null) return;
if (!batteryWarningShownOnce.Value)
{
2022-07-30 20:26:19 +08:00
if (batteryInfo.OnBattery && batteryInfo.ChargeLevel <= low_battery_threshold)
{
notificationOverlay?.Post(new BatteryWarningNotification());
batteryWarningShownOnce.Value = true;
}
}
}
private partial class BatteryWarningNotification : SimpleNotification
{
public override bool IsImportant => true;
public BatteryWarningNotification()
{
2023-01-14 07:30:10 +08:00
Text = NotificationsStrings.BatteryLow;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, INotificationOverlay notificationOverlay)
{
Icon = FontAwesome.Solid.BatteryQuarter;
IconContent.Colour = colours.RedDark;
Activated = delegate
{
notificationOverlay.Hide();
return true;
};
}
}
#endregion
2017-02-22 20:43:29 +08:00
}
}