// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Game.Screens.Play; namespace osu.Game.Mobile { /// /// A that manages the device orientations a game can display in. /// public abstract partial class OrientationManager : Component { /// /// Whether the current orientation of the game is portrait. /// protected abstract bool IsCurrentOrientationPortrait { get; } /// /// Whether the mobile device is considered a tablet. /// protected abstract bool IsTablet { get; } [Resolved] private OsuGame game { get; set; } = null!; [Resolved] private ILocalUserPlayInfo localUserPlayInfo { get; set; } = null!; private IBindable requiresPortraitOrientation = null!; private IBindable localUserPlaying = null!; protected override void LoadComplete() { base.LoadComplete(); requiresPortraitOrientation = game.RequiresPortraitOrientation.GetBoundCopy(); requiresPortraitOrientation.BindValueChanged(_ => updateOrientations()); localUserPlaying = localUserPlayInfo.PlayingState.GetBoundCopy(); localUserPlaying.BindValueChanged(_ => updateOrientations()); updateOrientations(); } private void updateOrientations() { bool lockCurrentOrientation = localUserPlaying.Value == LocalUserPlayingState.Playing; bool lockToPortrait = requiresPortraitOrientation.Value; if (lockCurrentOrientation) { if (lockToPortrait && !IsCurrentOrientationPortrait) SetAllowedOrientations(GameOrientation.Portrait); else if (!lockToPortrait && IsCurrentOrientationPortrait && !IsTablet) SetAllowedOrientations(GameOrientation.Landscape); else SetAllowedOrientations(GameOrientation.Locked); return; } if (lockToPortrait) { if (IsTablet) SetAllowedOrientations(GameOrientation.FullPortrait); else SetAllowedOrientations(GameOrientation.Portrait); return; } SetAllowedOrientations(null); } /// /// Sets the allowed orientations the device can rotate to. /// /// The allowed orientations, or null to return back to default. protected abstract void SetAllowedOrientations(GameOrientation? orientation); } }