Update to use new bindables and centered area offset

This commit is contained in:
Dean Herbert 2021-03-17 12:50:02 +09:00
parent 9d0c8902a6
commit 196f95ae54
3 changed files with 89 additions and 71 deletions

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Drawing;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
@ -11,6 +10,7 @@ using osu.Framework.Platform;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Settings.Sections.Input; using osu.Game.Overlays.Settings.Sections.Input;
using osuTK;
namespace osu.Game.Tests.Visual.Settings namespace osu.Game.Tests.Visual.Settings
{ {
@ -22,9 +22,6 @@ namespace osu.Game.Tests.Visual.Settings
{ {
var tabletHandler = new TestTabletHandler(); var tabletHandler = new TestTabletHandler();
tabletHandler.AreaOffset.Value = new Size(10, 10);
tabletHandler.AreaSize.Value = new Size(100, 80);
AddRange(new Drawable[] AddRange(new Drawable[]
{ {
new TabletSettings(tabletHandler) new TabletSettings(tabletHandler)
@ -36,27 +33,40 @@ namespace osu.Game.Tests.Visual.Settings
} }
}); });
AddStep("Test with wide tablet", () => tabletHandler.SetTabletSize(new Size(160, 100))); AddStep("Test with wide tablet", () => tabletHandler.SetTabletSize(new Vector2(160, 100)));
AddStep("Test with square tablet", () => tabletHandler.SetTabletSize(new Size(300, 300))); AddStep("Test with square tablet", () => tabletHandler.SetTabletSize(new Vector2(300, 300)));
AddStep("Test with tall tablet", () => tabletHandler.SetTabletSize(new Size(100, 300))); AddStep("Test with tall tablet", () => tabletHandler.SetTabletSize(new Vector2(100, 300)));
AddStep("Test with very tall tablet", () => tabletHandler.SetTabletSize(new Size(100, 700))); AddStep("Test with very tall tablet", () => tabletHandler.SetTabletSize(new Vector2(100, 700)));
AddStep("Test no tablet present", () => tabletHandler.SetTabletSize(System.Drawing.Size.Empty)); AddStep("Test no tablet present", () => tabletHandler.SetTabletSize(Vector2.Zero));
} }
public class TestTabletHandler : ITabletHandler public class TestTabletHandler : ITabletHandler
{ {
private readonly Bindable<Size> tabletSize = new Bindable<Size>(); public Bindable<Vector2> AreaOffset { get; } = new Bindable<Vector2>();
public Bindable<Vector2> AreaSize { get; } = new Bindable<Vector2>();
public IBindable<TabletInfo> Tablet => tablet;
private readonly Bindable<TabletInfo> tablet = new Bindable<TabletInfo>();
public BindableSize AreaOffset { get; } = new BindableSize();
public BindableSize AreaSize { get; } = new BindableSize();
public IBindable<Size> TabletSize => tabletSize;
public string DeviceName { get; private set; }
public BindableBool Enabled { get; } = new BindableBool(true); public BindableBool Enabled { get; } = new BindableBool(true);
public void SetTabletSize(Size size) public void SetTabletSize(Vector2 size)
{ {
DeviceName = size != System.Drawing.Size.Empty ? $"test tablet T-{RNG.Next(999):000}" : string.Empty; tablet.Value = size != Vector2.Zero ? new TabletInfo($"test tablet T-{RNG.Next(999):000}", size) : null;
tabletSize.Value = size;
AreaSize.Default = new Vector2(size.X, size.Y);
// if it's clear the user has not configured the area, take the full area from the tablet that was just found.
if (AreaSize.Value == Vector2.Zero)
AreaSize.SetDefault();
AreaOffset.Default = new Vector2(size.X / 2, size.Y / 2);
// likewise with the position, use the centre point if it has not been configured.
// it's safe to assume no user would set their centre point to 0,0 for now.
if (AreaOffset.Value == Vector2.Zero)
AreaOffset.SetDefault();
} }
} }
} }

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Drawing;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -23,9 +22,10 @@ namespace osu.Game.Overlays.Settings.Sections.Input
private Container tabletContainer; private Container tabletContainer;
private Container usableAreaContainer; private Container usableAreaContainer;
private readonly Bindable<Size> areaOffset = new BindableSize(); private readonly Bindable<Vector2> areaOffset = new Bindable<Vector2>();
private readonly Bindable<Size> areaSize = new BindableSize(); private readonly Bindable<Vector2> areaSize = new Bindable<Vector2>();
private readonly IBindable<Size> tabletSize = new BindableSize();
private readonly IBindable<TabletInfo> tablet = new Bindable<TabletInfo>();
private OsuSpriteText tabletName; private OsuSpriteText tabletName;
@ -56,6 +56,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
}, },
usableAreaContainer = new Container usableAreaContainer = new Container
{ {
Origin = Anchor.Centre,
Children = new Drawable[] Children = new Drawable[]
{ {
new Box new Box
@ -89,24 +90,27 @@ namespace osu.Game.Overlays.Settings.Sections.Input
areaOffset.BindTo(handler.AreaOffset); areaOffset.BindTo(handler.AreaOffset);
areaOffset.BindValueChanged(val => areaOffset.BindValueChanged(val =>
{ {
usableAreaContainer.MoveTo(new Vector2(val.NewValue.Width, val.NewValue.Height), 100, Easing.OutQuint); usableAreaContainer.MoveTo(val.NewValue, 100, Easing.OutQuint)
checkBounds(); .OnComplete(_ => checkBounds()); // required as we are using SSDQ.
}, true); }, true);
areaSize.BindTo(handler.AreaSize); areaSize.BindTo(handler.AreaSize);
areaSize.BindValueChanged(val => areaSize.BindValueChanged(val =>
{ {
usableAreaContainer.ResizeTo(new Vector2(val.NewValue.Width, val.NewValue.Height), 100, Easing.OutQuint); usableAreaContainer.ResizeTo(val.NewValue, 100, Easing.OutQuint)
.OnComplete(_ => checkBounds()); // required as we are using SSDQ.
}, true);
tablet.BindTo(handler.Tablet);
tablet.BindValueChanged(val =>
{
tabletContainer.Size = val.NewValue?.Size ?? Vector2.Zero;
tabletName.Text = val.NewValue?.Name ?? string.Empty;
checkBounds(); checkBounds();
}, true); }, true);
tabletSize.BindTo(handler.TabletSize); // initial animation should be instant.
tabletSize.BindValueChanged(val => FinishTransforms(true);
{
tabletContainer.Size = new Vector2(val.NewValue.Width, val.NewValue.Height);
tabletName.Text = handler.DeviceName;
checkBounds();
}, true);
} }
[Resolved] [Resolved]
@ -114,10 +118,13 @@ namespace osu.Game.Overlays.Settings.Sections.Input
private void checkBounds() private void checkBounds()
{ {
Size areaExtent = areaOffset.Value + areaSize.Value; if (tablet.Value == null)
return;
bool isWithinBounds = areaExtent.Width <= tabletSize.Value.Width var usableSsdq = usableAreaContainer.ScreenSpaceDrawQuad;
&& areaExtent.Height <= tabletSize.Value.Height;
bool isWithinBounds = tabletContainer.ScreenSpaceDrawQuad.Contains(usableSsdq.TopLeft) &&
tabletContainer.ScreenSpaceDrawQuad.Contains(usableSsdq.BottomRight);
usableAreaContainer.FadeColour(isWithinBounds ? colour.Blue : colour.RedLight, 100); usableAreaContainer.FadeColour(isWithinBounds ? colour.Blue : colour.RedLight, 100);
} }
@ -126,13 +133,11 @@ namespace osu.Game.Overlays.Settings.Sections.Input
{ {
base.Update(); base.Update();
var size = tabletSize.Value; if (!(tablet.Value?.Size is Vector2 size))
if (size == System.Drawing.Size.Empty)
return; return;
float fitX = size.Width / (DrawWidth - Padding.Left - Padding.Right); float fitX = size.X / (DrawWidth - Padding.Left - Padding.Right);
float fitY = size.Height / DrawHeight; float fitY = size.Y / DrawHeight;
float adjust = MathF.Max(fitX, fitY); float adjust = MathF.Max(fitX, fitY);

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Drawing;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -18,15 +17,15 @@ namespace osu.Game.Overlays.Settings.Sections.Input
{ {
private readonly ITabletHandler tabletHandler; private readonly ITabletHandler tabletHandler;
private readonly BindableSize areaOffset = new BindableSize(); private readonly Bindable<Vector2> areaOffset = new Bindable<Vector2>();
private readonly BindableSize areaSize = new BindableSize(); private readonly Bindable<Vector2> areaSize = new Bindable<Vector2>();
private readonly IBindable<Size> tabletSize = new BindableSize(); private readonly IBindable<TabletInfo> tablet = new Bindable<TabletInfo>();
private readonly BindableNumber<int> offsetX = new BindableNumber<int> { MinValue = 0 }; private readonly BindableNumber<float> offsetX = new BindableNumber<float> { MinValue = 0 };
private readonly BindableNumber<int> offsetY = new BindableNumber<int> { MinValue = 0 }; private readonly BindableNumber<float> offsetY = new BindableNumber<float> { MinValue = 0 };
private readonly BindableNumber<int> sizeX = new BindableNumber<int> { MinValue = 10 }; private readonly BindableNumber<float> sizeX = new BindableNumber<float> { MinValue = 10 };
private readonly BindableNumber<int> sizeY = new BindableNumber<int> { MinValue = 10 }; private readonly BindableNumber<float> sizeY = new BindableNumber<float> { MinValue = 10 };
[Resolved] [Resolved]
private GameHost host { get; set; } private GameHost host { get; set; }
@ -108,12 +107,12 @@ namespace osu.Game.Overlays.Settings.Sections.Input
LabelText = "Aspect Ratio", LabelText = "Aspect Ratio",
Current = aspectRatio Current = aspectRatio
}, },
new SettingsSlider<int> new SettingsSlider<float>
{ {
LabelText = "X Offset", LabelText = "X Offset",
Current = offsetX Current = offsetX
}, },
new SettingsSlider<int> new SettingsSlider<float>
{ {
LabelText = "Y Offset", LabelText = "Y Offset",
Current = offsetY Current = offsetY
@ -123,12 +122,12 @@ namespace osu.Game.Overlays.Settings.Sections.Input
LabelText = "Lock aspect ratio", LabelText = "Lock aspect ratio",
Current = aspectLock Current = aspectLock
}, },
new SettingsSlider<int> new SettingsSlider<float>
{ {
LabelText = "Width", LabelText = "Width",
Current = sizeX Current = sizeX
}, },
new SettingsSlider<int> new SettingsSlider<float>
{ {
LabelText = "Height", LabelText = "Height",
Current = sizeY Current = sizeY
@ -140,23 +139,23 @@ namespace osu.Game.Overlays.Settings.Sections.Input
areaOffset.BindTo(tabletHandler.AreaOffset); areaOffset.BindTo(tabletHandler.AreaOffset);
areaOffset.BindValueChanged(val => areaOffset.BindValueChanged(val =>
{ {
offsetX.Value = val.NewValue.Width; offsetX.Value = val.NewValue.X;
offsetY.Value = val.NewValue.Height; offsetY.Value = val.NewValue.Y;
}, true); }, true);
offsetX.BindValueChanged(val => areaOffset.Value = new Size(val.NewValue, areaOffset.Value.Height)); offsetX.BindValueChanged(val => areaOffset.Value = new Vector2(val.NewValue, areaOffset.Value.Y));
offsetY.BindValueChanged(val => areaOffset.Value = new Size(areaOffset.Value.Width, val.NewValue)); offsetY.BindValueChanged(val => areaOffset.Value = new Vector2(areaOffset.Value.X, val.NewValue));
areaSize.BindTo(tabletHandler.AreaSize); areaSize.BindTo(tabletHandler.AreaSize);
areaSize.BindValueChanged(val => areaSize.BindValueChanged(val =>
{ {
sizeX.Value = val.NewValue.Width; sizeX.Value = val.NewValue.X;
sizeY.Value = val.NewValue.Height; sizeY.Value = val.NewValue.Y;
}, true); }, true);
sizeX.BindValueChanged(val => sizeX.BindValueChanged(val =>
{ {
areaSize.Value = new Size(val.NewValue, areaSize.Value.Height); areaSize.Value = new Vector2(val.NewValue, areaSize.Value.Y);
aspectRatioApplication?.Cancel(); aspectRatioApplication?.Cancel();
aspectRatioApplication = Schedule(() => applyAspectRatio(sizeX)); aspectRatioApplication = Schedule(() => applyAspectRatio(sizeX));
@ -164,7 +163,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
sizeY.BindValueChanged(val => sizeY.BindValueChanged(val =>
{ {
areaSize.Value = new Size(areaSize.Value.Width, val.NewValue); areaSize.Value = new Vector2(areaSize.Value.X, val.NewValue);
aspectRatioApplication?.Cancel(); aspectRatioApplication?.Cancel();
aspectRatioApplication = Schedule(() => applyAspectRatio(sizeY)); aspectRatioApplication = Schedule(() => applyAspectRatio(sizeY));
@ -176,10 +175,12 @@ namespace osu.Game.Overlays.Settings.Sections.Input
aspectRatioApplication = Schedule(() => forceAspectRatio(aspect.NewValue)); aspectRatioApplication = Schedule(() => forceAspectRatio(aspect.NewValue));
}); });
tabletSize.BindTo(tabletHandler.TabletSize); tablet.BindTo(tabletHandler.Tablet);
tabletSize.BindValueChanged(val => tablet.BindValueChanged(val =>
{ {
bool tabletFound = tabletSize.Value != System.Drawing.Size.Empty; var tab = val.NewValue;
bool tabletFound = tab != null;
if (!tabletFound) if (!tabletFound)
{ {
@ -192,17 +193,19 @@ namespace osu.Game.Overlays.Settings.Sections.Input
noTabletMessage.Hide(); noTabletMessage.Hide();
// todo: these should propagate from a TabletChanged event or similar. // todo: these should propagate from a TabletChanged event or similar.
offsetX.MaxValue = val.NewValue.Width; offsetX.MaxValue = tab.Size.X;
sizeX.Default = sizeX.MaxValue = val.NewValue.Width; offsetX.Default = tab.Size.X / 2;
sizeX.Default = sizeX.MaxValue = tab.Size.X;
offsetY.MaxValue = val.NewValue.Height; offsetY.MaxValue = tab.Size.Y;
sizeY.Default = sizeY.MaxValue = val.NewValue.Height; offsetY.Default = tab.Size.Y / 2;
sizeY.Default = sizeY.MaxValue = tab.Size.Y;
areaSize.Default = new Size(sizeX.Default, sizeY.Default); areaSize.Default = new Vector2(sizeX.Default, sizeY.Default);
}, true); }, true);
} }
private void applyAspectRatio(BindableNumber<int> sizeChanged) private void applyAspectRatio(BindableNumber<float> sizeChanged)
{ {
try try
{ {
@ -220,9 +223,9 @@ namespace osu.Game.Overlays.Settings.Sections.Input
// if lock is applied (or the specified values were out of range) aim to adjust the axis the user was not adjusting to conform. // if lock is applied (or the specified values were out of range) aim to adjust the axis the user was not adjusting to conform.
if (sizeChanged == sizeX) if (sizeChanged == sizeX)
sizeY.Value = (int)(areaSize.Value.Width / aspectRatio.Value); sizeY.Value = (int)(areaSize.Value.X / aspectRatio.Value);
else else
sizeX.Value = (int)(areaSize.Value.Height * aspectRatio.Value); sizeX.Value = (int)(areaSize.Value.Y * aspectRatio.Value);
} }
finally finally
{ {
@ -251,6 +254,6 @@ namespace osu.Game.Overlays.Settings.Sections.Input
private void updateAspectRatio() => aspectRatio.Value = curentAspectRatio; private void updateAspectRatio() => aspectRatio.Value = curentAspectRatio;
private float curentAspectRatio => (float)sizeX.Value / sizeY.Value; private float curentAspectRatio => sizeX.Value / sizeY.Value;
} }
} }