From 458cde832da5a75b742fb808637e559de62fad3a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 6 Sep 2021 14:09:40 +0900 Subject: [PATCH] Avoid using SSDQ for validity computation --- .../Settings/TestSceneTabletSettings.cs | 14 +----- .../Sections/Input/TabletAreaSelection.cs | 44 ++++++++++++------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/osu.Game.Tests/Visual/Settings/TestSceneTabletSettings.cs b/osu.Game.Tests/Visual/Settings/TestSceneTabletSettings.cs index 49d6b7033e..2486abdd7a 100644 --- a/osu.Game.Tests/Visual/Settings/TestSceneTabletSettings.cs +++ b/osu.Game.Tests/Visual/Settings/TestSceneTabletSettings.cs @@ -1,11 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Linq; using NUnit.Framework; using osu.Framework.Bindables; using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; using osu.Framework.Input.Handlers.Tablet; using osu.Framework.Testing; using osu.Framework.Utils; @@ -93,17 +91,9 @@ public void TestOffsetValidity() ensureValid(); } - private void ensureValid() - { - AddUntilStep("wait for transforms", () => settings.AreaSelection.ChildrenOfType().All(c => !c.Transforms.Any())); - AddAssert("area valid", () => settings.AreaSelection.IsWithinBounds); - } + private void ensureValid() => AddAssert("area valid", () => settings.AreaSelection.IsWithinBounds); - private void ensureInvalid() - { - AddUntilStep("wait for transforms", () => settings.AreaSelection.ChildrenOfType().All(c => !c.Transforms.Any())); - AddAssert("area invalid", () => !settings.AreaSelection.IsWithinBounds); - } + private void ensureInvalid() => AddAssert("area invalid", () => !settings.AreaSelection.IsWithinBounds); public class TestTabletHandler : ITabletHandler { diff --git a/osu.Game/Overlays/Settings/Sections/Input/TabletAreaSelection.cs b/osu.Game/Overlays/Settings/Sections/Input/TabletAreaSelection.cs index d12052b24d..58abfab29c 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/TabletAreaSelection.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/TabletAreaSelection.cs @@ -114,29 +114,30 @@ protected override void LoadComplete() areaOffset.BindTo(handler.AreaOffset); areaOffset.BindValueChanged(val => { - usableAreaContainer.MoveTo(val.NewValue, 100, Easing.OutQuint) - .OnComplete(_ => checkBounds()); // required as we are using SSDQ. + usableAreaContainer.MoveTo(val.NewValue, 100, Easing.OutQuint); + checkBounds(); }, true); areaSize.BindTo(handler.AreaSize); areaSize.BindValueChanged(val => { - usableAreaContainer.ResizeTo(val.NewValue, 100, Easing.OutQuint) - .OnComplete(_ => checkBounds()); // required as we are using SSDQ. + usableAreaContainer.ResizeTo(val.NewValue, 100, Easing.OutQuint); int x = (int)val.NewValue.X; int y = (int)val.NewValue.Y; int commonDivider = greatestCommonDivider(x, y); usableAreaText.Text = $"{(float)x / commonDivider}:{(float)y / commonDivider}"; + checkBounds(); }, true); rotation.BindTo(handler.Rotation); rotation.BindValueChanged(val => { - usableAreaContainer.RotateTo(val.NewValue, 100, Easing.OutQuint) - .OnComplete(_ => checkBounds()); // required as we are using SSDQ. + usableAreaContainer.RotateTo(val.NewValue, 100, Easing.OutQuint); tabletContainer.RotateTo(-val.NewValue, 800, Easing.OutQuint); + + checkBounds(); }, true); tablet.BindTo(handler.Tablet); @@ -174,22 +175,33 @@ private void checkBounds() if (tablet.Value == null) return; - // All of this manual logic is just to get around floating point issues when doing a contains check on the screen quads. - // This is best effort, as it's only used for display purposes. If we need for anything more, manual math on the raw values should be preferred. - var containerQuad = tabletContainer.ScreenSpaceDrawQuad.AABBFloat.Inflate(1); - var usableAreaQuad = Quad.FromRectangle(usableAreaContainer.ScreenSpaceDrawQuad.AABBFloat); + // allow for some degree of floating point error, as we don't care about being perfect here. + const float lenience = 0.5f; + + var tabletArea = new Quad(-lenience, -lenience, tablet.Value.Size.X + lenience * 2, tablet.Value.Size.Y + lenience * 2); + + var halfUsableArea = areaSize.Value / 2; + var offset = areaOffset.Value; + + var usableAreaQuad = new Quad( + new Vector2(-halfUsableArea.X, -halfUsableArea.Y), + new Vector2(halfUsableArea.X, -halfUsableArea.Y), + new Vector2(-halfUsableArea.X, halfUsableArea.Y), + new Vector2(halfUsableArea.X, halfUsableArea.Y) + ); var matrix = Matrix3.Identity; - MatrixExtensions.TranslateFromLeft(ref matrix, usableAreaQuad.Centre); + + MatrixExtensions.TranslateFromLeft(ref matrix, offset); MatrixExtensions.RotateFromLeft(ref matrix, MathUtils.DegreesToRadians(rotation.Value)); - MatrixExtensions.TranslateFromLeft(ref matrix, -usableAreaQuad.Centre); + usableAreaQuad *= matrix; IsWithinBounds = - containerQuad.Contains(usableAreaQuad.TopLeft) && - containerQuad.Contains(usableAreaQuad.TopRight) && - containerQuad.Contains(usableAreaQuad.BottomLeft) && - containerQuad.Contains(usableAreaQuad.BottomRight); + tabletArea.Contains(usableAreaQuad.TopLeft) && + tabletArea.Contains(usableAreaQuad.TopRight) && + tabletArea.Contains(usableAreaQuad.BottomLeft) && + tabletArea.Contains(usableAreaQuad.BottomRight); usableFill.FadeColour(IsWithinBounds ? colour.Blue : colour.RedLight, 100); }