2021-03-15 17:37:46 +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.
|
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
using System.ComponentModel;
|
2021-03-15 17:37:46 +08:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input.Handlers.Tablet;
|
2021-03-16 16:57:50 +08:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Framework.Threading;
|
2021-03-15 17:37:46 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Settings.Sections.Input
|
|
|
|
|
{
|
|
|
|
|
public class TabletSettings : SettingsSubsection
|
|
|
|
|
{
|
|
|
|
|
private readonly ITabletHandler tabletHandler;
|
|
|
|
|
|
|
|
|
|
private readonly BindableSize areaOffset = new BindableSize();
|
|
|
|
|
private readonly BindableSize areaSize = new BindableSize();
|
|
|
|
|
private readonly BindableSize tabletSize = new BindableSize();
|
|
|
|
|
|
|
|
|
|
private readonly BindableNumber<int> offsetX = new BindableNumber<int> { MinValue = 0 };
|
|
|
|
|
private readonly BindableNumber<int> offsetY = new BindableNumber<int> { MinValue = 0 };
|
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
private readonly BindableNumber<int> sizeX = new BindableNumber<int> { MinValue = 10 };
|
|
|
|
|
private readonly BindableNumber<int> sizeY = new BindableNumber<int> { MinValue = 10 };
|
2021-03-15 17:37:46 +08:00
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private GameHost host { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Based on the longest available smartphone.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const float largest_feasible_aspect_ratio = 20f / 9;
|
2021-03-16 16:04:22 +08:00
|
|
|
|
|
|
|
|
|
private readonly BindableNumber<float> aspectRatio = new BindableFloat(1)
|
|
|
|
|
{
|
2021-03-16 16:57:50 +08:00
|
|
|
|
MinValue = 1 / largest_feasible_aspect_ratio,
|
|
|
|
|
MaxValue = largest_feasible_aspect_ratio,
|
2021-03-16 16:04:22 +08:00
|
|
|
|
Precision = 0.01f,
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
private readonly BindableBool aspectLock = new BindableBool();
|
|
|
|
|
|
|
|
|
|
private ScheduledDelegate aspectRatioApplication;
|
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
|
protected override string Header => "Tablet";
|
|
|
|
|
|
|
|
|
|
public TabletSettings(ITabletHandler tabletHandler)
|
|
|
|
|
{
|
|
|
|
|
this.tabletHandler = tabletHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2021-03-16 14:57:29 +08:00
|
|
|
|
private void load()
|
2021-03-15 17:37:46 +08:00
|
|
|
|
{
|
|
|
|
|
areaOffset.BindTo(tabletHandler.AreaOffset);
|
|
|
|
|
areaOffset.BindValueChanged(val =>
|
|
|
|
|
{
|
|
|
|
|
offsetX.Value = val.NewValue.Width;
|
|
|
|
|
offsetY.Value = val.NewValue.Height;
|
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
|
|
offsetX.BindValueChanged(val => areaOffset.Value = new Size(val.NewValue, areaOffset.Value.Height));
|
|
|
|
|
offsetY.BindValueChanged(val => areaOffset.Value = new Size(areaOffset.Value.Width, val.NewValue));
|
|
|
|
|
|
|
|
|
|
areaSize.BindTo(tabletHandler.AreaSize);
|
|
|
|
|
areaSize.BindValueChanged(val =>
|
|
|
|
|
{
|
|
|
|
|
sizeX.Value = val.NewValue.Width;
|
|
|
|
|
sizeY.Value = val.NewValue.Height;
|
2021-03-16 16:04:22 +08:00
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
aspectRatioApplication?.Cancel();
|
|
|
|
|
aspectRatioApplication = Schedule(() => applyAspectRatio(val));
|
2021-03-15 17:37:46 +08:00
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
|
|
sizeX.BindValueChanged(val => areaSize.Value = new Size(val.NewValue, areaSize.Value.Height));
|
|
|
|
|
sizeY.BindValueChanged(val => areaSize.Value = new Size(areaSize.Value.Width, val.NewValue));
|
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
aspectRatio.BindValueChanged(aspect =>
|
|
|
|
|
{
|
|
|
|
|
forceAspectRatio(aspect.NewValue);
|
|
|
|
|
});
|
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
|
((IBindable<Size>)tabletSize).BindTo(tabletHandler.TabletSize);
|
|
|
|
|
tabletSize.BindValueChanged(val =>
|
|
|
|
|
{
|
2021-03-16 14:57:29 +08:00
|
|
|
|
if (tabletSize.Value == System.Drawing.Size.Empty)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
|
// todo: these should propagate from a TabletChanged event or similar.
|
|
|
|
|
offsetX.MaxValue = val.NewValue.Width;
|
|
|
|
|
sizeX.Default = sizeX.MaxValue = val.NewValue.Width;
|
|
|
|
|
|
|
|
|
|
offsetY.MaxValue = val.NewValue.Height;
|
|
|
|
|
sizeY.Default = sizeY.MaxValue = val.NewValue.Height;
|
|
|
|
|
|
2021-03-16 15:24:51 +08:00
|
|
|
|
areaSize.Default = new Size(sizeX.Default, sizeY.Default);
|
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
|
updateDisplay();
|
|
|
|
|
}, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 16:57:50 +08:00
|
|
|
|
private void applyAspectRatio(ValueChangedEvent<Size> sizeChanged)
|
|
|
|
|
{
|
|
|
|
|
float proposedAspectRatio = (float)sizeX.Value / sizeY.Value;
|
|
|
|
|
|
|
|
|
|
if (!aspectLock.Value)
|
|
|
|
|
{
|
|
|
|
|
aspectRatio.Value = proposedAspectRatio;
|
|
|
|
|
|
|
|
|
|
// aspect ratio was in a valid range.
|
|
|
|
|
if (proposedAspectRatio >= aspectRatio.MinValue && proposedAspectRatio <= aspectRatio.MaxValue)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sizeChanged.NewValue.Width != sizeChanged.OldValue.Width)
|
|
|
|
|
{
|
|
|
|
|
areaSize.Value = new Size(areaSize.Value.Width, (int)(areaSize.Value.Width / aspectRatio.Value));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
areaSize.Value = new Size((int)(areaSize.Value.Height * aspectRatio.Value), areaSize.Value.Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cancel any event which may have fired while updating variables as a result of aspect ratio limitations.
|
|
|
|
|
// this avoids a potential feedback loop.
|
|
|
|
|
aspectRatioApplication?.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
|
private void updateDisplay()
|
|
|
|
|
{
|
2021-03-16 14:57:29 +08:00
|
|
|
|
if (Children.Count > 0)
|
2021-03-15 17:37:46 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2021-03-16 15:24:51 +08:00
|
|
|
|
new TabletAreaSelection(tabletHandler)
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
Height = 300,
|
|
|
|
|
},
|
2021-03-16 16:57:50 +08:00
|
|
|
|
new DangerousSettingsButton
|
2021-03-16 15:24:51 +08:00
|
|
|
|
{
|
|
|
|
|
Text = "Reset to full area",
|
|
|
|
|
Action = () =>
|
|
|
|
|
{
|
2021-03-16 16:57:50 +08:00
|
|
|
|
aspectLock.Value = false;
|
|
|
|
|
|
2021-03-16 15:24:51 +08:00
|
|
|
|
areaOffset.SetDefault();
|
|
|
|
|
areaSize.SetDefault();
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-16 16:57:50 +08:00
|
|
|
|
new SettingsButton
|
2021-03-16 16:04:22 +08:00
|
|
|
|
{
|
2021-03-16 16:57:50 +08:00
|
|
|
|
Text = "Conform to current game aspect ratio",
|
|
|
|
|
Action = () =>
|
|
|
|
|
{
|
|
|
|
|
forceAspectRatio((float)host.Window.ClientSize.Width / host.Window.ClientSize.Height);
|
|
|
|
|
}
|
2021-03-16 16:04:22 +08:00
|
|
|
|
},
|
|
|
|
|
new SettingsSlider<float>
|
|
|
|
|
{
|
|
|
|
|
LabelText = "Aspect Ratio",
|
|
|
|
|
Current = aspectRatio
|
|
|
|
|
},
|
2021-03-15 17:37:46 +08:00
|
|
|
|
new SettingsSlider<int>
|
|
|
|
|
{
|
2021-03-16 15:24:51 +08:00
|
|
|
|
LabelText = "X Offset",
|
2021-03-15 17:37:46 +08:00
|
|
|
|
Current = offsetX
|
|
|
|
|
},
|
|
|
|
|
new SettingsSlider<int>
|
|
|
|
|
{
|
2021-03-16 15:24:51 +08:00
|
|
|
|
LabelText = "Y Offset",
|
2021-03-15 17:37:46 +08:00
|
|
|
|
Current = offsetY
|
|
|
|
|
},
|
2021-03-16 16:57:50 +08:00
|
|
|
|
new SettingsCheckbox
|
|
|
|
|
{
|
|
|
|
|
LabelText = "Lock aspect ratio",
|
|
|
|
|
Current = aspectLock
|
|
|
|
|
},
|
2021-03-15 17:37:46 +08:00
|
|
|
|
new SettingsSlider<int>
|
|
|
|
|
{
|
2021-03-16 15:24:51 +08:00
|
|
|
|
LabelText = "Width",
|
2021-03-15 17:37:46 +08:00
|
|
|
|
Current = sizeX
|
|
|
|
|
},
|
|
|
|
|
new SettingsSlider<int>
|
|
|
|
|
{
|
2021-03-16 15:24:51 +08:00
|
|
|
|
LabelText = "Height",
|
2021-03-15 17:37:46 +08:00
|
|
|
|
Current = sizeY
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-03-16 16:57:50 +08:00
|
|
|
|
|
|
|
|
|
private void forceAspectRatio(float aspectRatio)
|
|
|
|
|
{
|
|
|
|
|
aspectLock.Value = false;
|
|
|
|
|
|
|
|
|
|
int proposedHeight = (int)(sizeX.Value / aspectRatio);
|
|
|
|
|
|
|
|
|
|
if (proposedHeight < sizeY.MaxValue)
|
|
|
|
|
sizeY.Value = proposedHeight;
|
|
|
|
|
else
|
|
|
|
|
sizeX.Value = (int)(sizeY.Value * aspectRatio);
|
|
|
|
|
|
|
|
|
|
aspectRatioApplication?.Cancel();
|
|
|
|
|
aspectLock.Value = true;
|
|
|
|
|
}
|
2021-03-15 17:37:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|