1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 19:12:57 +08:00
osu-lazer/osu.Game/Graphics/Containers/ScalingContainer.cs

194 lines
6.7 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.
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;
using osu.Framework.Screens;
2019-01-04 12:29:37 +08:00
using osu.Game.Configuration;
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;
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;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2019-01-04 12:29:37 +08:00
private readonly Container sizableContainer;
private BackgroundScreenStack backgroundStack;
2019-01-04 12:29:37 +08:00
private bool allowScaling = true;
/// <summary>
/// Whether user scaling preferences should be applied. Enabled by default.
/// </summary>
public bool AllowScaling
{
get => allowScaling;
set
{
if (value == allowScaling)
return;
allowScaling = value;
if (IsLoaded) updateSize();
}
}
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;
InternalChild = sizableContainer = new AlwaysInputContainer
2019-01-04 12:29:37 +08:00
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both,
CornerRadius = 10,
Child = content = new ScalingDrawSizePreservingFillContainer(targetMode != ScalingMode.Gameplay)
2019-01-04 12:29:37 +08:00
};
}
private class ScalingDrawSizePreservingFillContainer : DrawSizePreservingFillContainer
{
private readonly bool applyUIScale;
private Bindable<float> uiScale;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
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-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-04 12:29:37 +08:00
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
scalingMode = config.GetBindable<ScalingMode>(OsuSetting.Scaling);
scalingMode.ValueChanged += _ => updateSize();
sizeX = config.GetBindable<float>(OsuSetting.ScalingSizeX);
sizeX.ValueChanged += _ => updateSize();
sizeY = config.GetBindable<float>(OsuSetting.ScalingSizeY);
sizeY.ValueChanged += _ => updateSize();
posX = config.GetBindable<float>(OsuSetting.ScalingPositionX);
posX.ValueChanged += _ => updateSize();
posY = config.GetBindable<float>(OsuSetting.ScalingPositionY);
posY.ValueChanged += _ => updateSize();
}
protected override void LoadComplete()
{
base.LoadComplete();
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()
{
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)
{
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:38:50 +08:00
backgroundStack.Push(new ScalingBackgroundScreen());
}
backgroundStack.FadeIn(fade_time);
2019-01-04 12:29:37 +08:00
}
else
backgroundStack?.FadeOut(fade_time);
2019-01-04 12:29:37 +08:00
}
bool scaling = AllowScaling && (targetMode == null || scalingMode.Value == targetMode);
2019-01-04 12:29:37 +08:00
2019-02-21 17:56:34 +08:00
var targetSize = scaling ? new Vector2(sizeX.Value, sizeY.Value) : Vector2.One;
var targetPosition = scaling ? new Vector2(posX.Value, posY.Value) * (Vector2.One - targetSize) : Vector2.Zero;
bool requiresMasking = scaling && targetSize != Vector2.One;
2019-01-04 12:29:37 +08:00
if (requiresMasking)
sizableContainer.Masking = true;
sizableContainer.MoveTo(targetPosition, 500, Easing.OutQuart);
sizableContainer.ResizeTo(targetSize, 500, Easing.OutQuart).OnComplete(_ => { sizableContainer.Masking = requiresMasking; });
2019-01-04 12:29:37 +08:00
}
2019-03-25 12:38:50 +08:00
private class ScalingBackgroundScreen : BackgroundScreenDefault
{
protected override bool AllowStoryboardBackground => false;
public override void OnEntering(IScreen last)
{
this.FadeInFromZero(4000, Easing.OutQuint);
}
}
private class AlwaysInputContainer : Container
{
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
public AlwaysInputContainer()
{
RelativeSizeAxes = Axes.Both;
}
}
2019-01-04 12:29:37 +08:00
}
}