2019-01-24 16:43:03 +08:00
|
|
|
// 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.
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-01-04 12:29:37 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-03-02 19:04:53 +08:00
|
|
|
using osu.Framework.Graphics.Primitives;
|
2019-03-25 12:28:51 +08:00
|
|
|
using osu.Framework.Screens;
|
2019-01-04 12:29:37 +08:00
|
|
|
using osu.Game.Configuration;
|
2019-03-25 05:49:57 +08:00
|
|
|
using osu.Game.Screens;
|
|
|
|
using osu.Game.Screens.Backgrounds;
|
2019-01-04 12:29:37 +08:00
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Handles user-defined scaling, allowing application at multiple levels defined by <see cref="ScalingMode"/>.
|
|
|
|
/// </summary>
|
|
|
|
public class ScalingContainer : Container
|
|
|
|
{
|
|
|
|
private Bindable<float> sizeX;
|
|
|
|
private Bindable<float> sizeY;
|
|
|
|
private Bindable<float> posX;
|
|
|
|
private Bindable<float> posY;
|
|
|
|
|
2022-02-04 18:19:44 +08:00
|
|
|
private Bindable<MarginPadding> safeAreaPadding;
|
|
|
|
|
2019-01-04 14:28:35 +08:00
|
|
|
private readonly ScalingMode? targetMode;
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
private Bindable<ScalingMode> scalingMode;
|
|
|
|
|
|
|
|
private readonly Container content;
|
|
|
|
protected override Container<Drawable> Content => content;
|
|
|
|
|
2019-01-16 16:21:26 +08:00
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
|
|
|
|
2019-01-04 12:29:37 +08:00
|
|
|
private readonly Container sizableContainer;
|
|
|
|
|
2019-03-25 12:28:51 +08:00
|
|
|
private BackgroundScreenStack backgroundStack;
|
2019-01-04 12:29:37 +08:00
|
|
|
|
2022-03-02 19:04:53 +08:00
|
|
|
private RectangleF? customScale;
|
|
|
|
private bool customScaleIsRelativePosition;
|
2021-04-29 16:19:47 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2022-03-02 19:04:53 +08:00
|
|
|
/// Set a custom position and scale which overrides any user specification.
|
2021-04-29 16:19:47 +08:00
|
|
|
/// </summary>
|
2022-03-02 19:04:53 +08:00
|
|
|
public void SetCustomScale(RectangleF? scale, bool relativePosition = false)
|
2021-04-29 16:19:47 +08:00
|
|
|
{
|
2022-03-02 19:04:53 +08:00
|
|
|
customScale = scale;
|
|
|
|
customScaleIsRelativePosition = relativePosition;
|
2021-04-29 16:19:47 +08:00
|
|
|
|
2022-03-02 19:04:53 +08:00
|
|
|
if (IsLoaded) Scheduler.AddOnce(updateSize);
|
2021-04-29 16:19:47 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:41:47 +08:00
|
|
|
private const float corner_radius = 10;
|
|
|
|
|
2019-01-04 12:29:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Create a new instance.
|
|
|
|
/// </summary>
|
2019-01-04 14:28:35 +08:00
|
|
|
/// <param name="targetMode">The mode which this container should be handling. Handles all modes if null.</param>
|
|
|
|
public ScalingContainer(ScalingMode? targetMode = null)
|
2019-01-04 12:29:37 +08:00
|
|
|
{
|
|
|
|
this.targetMode = targetMode;
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
2019-01-16 16:21:26 +08:00
|
|
|
InternalChild = sizableContainer = new AlwaysInputContainer
|
2019-01-04 12:29:37 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
RelativePositionAxes = Axes.Both,
|
2022-03-02 18:41:47 +08:00
|
|
|
CornerRadius = corner_radius,
|
2019-01-09 18:01:33 +08:00
|
|
|
Child = content = new ScalingDrawSizePreservingFillContainer(targetMode != ScalingMode.Gameplay)
|
2019-01-04 12:29:37 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-09 18:01:33 +08:00
|
|
|
private class ScalingDrawSizePreservingFillContainer : DrawSizePreservingFillContainer
|
|
|
|
{
|
|
|
|
private readonly bool applyUIScale;
|
|
|
|
private Bindable<float> uiScale;
|
|
|
|
|
2019-01-16 16:21:26 +08:00
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
|
|
|
|
2019-01-09 18:01:33 +08:00
|
|
|
public ScalingDrawSizePreservingFillContainer(bool applyUIScale)
|
|
|
|
{
|
|
|
|
this.applyUIScale = applyUIScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuConfigManager osuConfig)
|
|
|
|
{
|
|
|
|
if (applyUIScale)
|
|
|
|
{
|
|
|
|
uiScale = osuConfig.GetBindable<float>(OsuSetting.UIScale);
|
|
|
|
uiScale.BindValueChanged(scaleChanged, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
private void scaleChanged(ValueChangedEvent<float> args)
|
2019-01-09 18:01:33 +08:00
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
this.ScaleTo(new Vector2(args.NewValue), 500, Easing.Out);
|
|
|
|
this.ResizeTo(new Vector2(1 / args.NewValue), 500, Easing.Out);
|
2019-01-09 18:01:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:29:37 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2022-02-04 18:19:44 +08:00
|
|
|
private void load(OsuConfigManager config, ISafeArea safeArea)
|
2019-01-04 12:29:37 +08:00
|
|
|
{
|
|
|
|
scalingMode = config.GetBindable<ScalingMode>(OsuSetting.Scaling);
|
2022-02-04 19:33:15 +08:00
|
|
|
scalingMode.ValueChanged += _ => Scheduler.AddOnce(updateSize);
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
sizeX = config.GetBindable<float>(OsuSetting.ScalingSizeX);
|
2022-02-04 19:33:15 +08:00
|
|
|
sizeX.ValueChanged += _ => Scheduler.AddOnce(updateSize);
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
sizeY = config.GetBindable<float>(OsuSetting.ScalingSizeY);
|
2022-02-04 19:33:15 +08:00
|
|
|
sizeY.ValueChanged += _ => Scheduler.AddOnce(updateSize);
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
posX = config.GetBindable<float>(OsuSetting.ScalingPositionX);
|
2022-02-04 19:33:15 +08:00
|
|
|
posX.ValueChanged += _ => Scheduler.AddOnce(updateSize);
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
posY = config.GetBindable<float>(OsuSetting.ScalingPositionY);
|
2022-02-04 19:33:15 +08:00
|
|
|
posY.ValueChanged += _ => Scheduler.AddOnce(updateSize);
|
2022-02-04 15:07:05 +08:00
|
|
|
|
2022-02-04 18:19:44 +08:00
|
|
|
safeAreaPadding = safeArea.SafeAreaPadding.GetBoundCopy();
|
2022-02-04 19:33:15 +08:00
|
|
|
safeAreaPadding.BindValueChanged(_ => Scheduler.AddOnce(updateSize));
|
2019-01-04 12:29:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-02-04 21:31:41 +08:00
|
|
|
updateSize();
|
2019-01-04 14:28:35 +08:00
|
|
|
sizableContainer.FinishTransforms();
|
2019-01-04 12:29:37 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
private bool requiresBackgroundVisible => (scalingMode.Value == ScalingMode.Everything || scalingMode.Value == ScalingMode.ExcludeOverlays) && (sizeX.Value != 1 || sizeY.Value != 1);
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
private void updateSize()
|
|
|
|
{
|
2019-03-25 12:28:51 +08:00
|
|
|
const float fade_time = 500;
|
|
|
|
|
2019-01-04 12:29:37 +08:00
|
|
|
if (targetMode == ScalingMode.Everything)
|
|
|
|
{
|
|
|
|
// the top level scaling container manages the background to be displayed while scaling.
|
|
|
|
if (requiresBackgroundVisible)
|
|
|
|
{
|
2019-03-25 12:28:51 +08:00
|
|
|
if (backgroundStack == null)
|
|
|
|
{
|
|
|
|
AddInternal(backgroundStack = new BackgroundScreenStack
|
2019-01-04 12:29:37 +08:00
|
|
|
{
|
|
|
|
Colour = OsuColour.Gray(0.1f),
|
|
|
|
Alpha = 0,
|
|
|
|
Depth = float.MaxValue
|
|
|
|
});
|
2019-03-25 12:28:51 +08:00
|
|
|
|
2019-03-25 12:38:50 +08:00
|
|
|
backgroundStack.Push(new ScalingBackgroundScreen());
|
2019-03-25 12:28:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
backgroundStack.FadeIn(fade_time);
|
2019-01-04 12:29:37 +08:00
|
|
|
}
|
|
|
|
else
|
2019-03-25 12:28:51 +08:00
|
|
|
backgroundStack?.FadeOut(fade_time);
|
2019-01-04 12:29:37 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 19:04:53 +08:00
|
|
|
RectangleF targetSize = new RectangleF(Vector2.Zero, Vector2.One);
|
|
|
|
|
|
|
|
if (customScale != null)
|
|
|
|
{
|
|
|
|
sizableContainer.RelativePositionAxes = customScaleIsRelativePosition ? Axes.Both : Axes.None;
|
|
|
|
|
|
|
|
targetSize = customScale.Value;
|
|
|
|
}
|
|
|
|
else if (targetMode == null || scalingMode.Value == targetMode)
|
|
|
|
{
|
|
|
|
sizableContainer.RelativePositionAxes = Axes.Both;
|
|
|
|
|
|
|
|
Vector2 scale = new Vector2(sizeX.Value, sizeY.Value);
|
|
|
|
Vector2 pos = new Vector2(posX.Value, posY.Value) * (Vector2.One - scale);
|
|
|
|
|
|
|
|
targetSize = new RectangleF(pos, scale);
|
|
|
|
}
|
2019-01-04 12:29:37 +08:00
|
|
|
|
2022-03-02 19:04:53 +08:00
|
|
|
bool requiresMasking = targetSize.Size != Vector2.One
|
2022-02-04 15:07:05 +08:00
|
|
|
// For the top level scaling container, for now we apply masking if safe areas are in use.
|
|
|
|
// In the future this can likely be removed as more of the actual UI supports overflowing into the safe areas.
|
2022-02-04 18:19:44 +08:00
|
|
|
|| (targetMode == ScalingMode.Everything && safeAreaPadding.Value.Total != Vector2.Zero);
|
2019-01-04 12:29:37 +08:00
|
|
|
|
|
|
|
if (requiresMasking)
|
|
|
|
sizableContainer.Masking = true;
|
|
|
|
|
2022-03-02 19:04:53 +08:00
|
|
|
sizableContainer.MoveTo(targetSize.Location, 500, Easing.OutQuart);
|
|
|
|
sizableContainer.ResizeTo(targetSize.Size, 500, Easing.OutQuart).OnComplete(_ => { sizableContainer.Masking = requiresMasking; });
|
2022-03-02 18:41:47 +08:00
|
|
|
sizableContainer.TransformTo(nameof(CornerRadius), requiresMasking ? corner_radius : 0, 500, requiresMasking ? Easing.OutQuart : Easing.None);
|
2019-01-04 12:29:37 +08:00
|
|
|
}
|
2019-01-16 16:21:26 +08:00
|
|
|
|
2019-03-25 12:38:50 +08:00
|
|
|
private class ScalingBackgroundScreen : BackgroundScreenDefault
|
2019-03-25 12:28:51 +08:00
|
|
|
{
|
2021-06-03 13:24:21 +08:00
|
|
|
protected override bool AllowStoryboardBackground => false;
|
|
|
|
|
2019-03-25 12:28:51 +08:00
|
|
|
public override void OnEntering(IScreen last)
|
|
|
|
{
|
|
|
|
this.FadeInFromZero(4000, Easing.OutQuint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 16:21:26 +08:00
|
|
|
private class AlwaysInputContainer : Container
|
|
|
|
{
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
|
|
|
|
|
|
|
public AlwaysInputContainer()
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 12:29:37 +08:00
|
|
|
}
|
|
|
|
}
|