mirror of
https://github.com/ppy/osu.git
synced 2024-11-08 06:19:38 +08:00
Merge pull request #10231 from peppy/editor-object-object-snapping
Add editor object to object snapping
This commit is contained in:
commit
c273b9dcb3
@ -0,0 +1,90 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
using System.Linq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using osu.Framework.Testing;
|
||||||
|
using osu.Framework.Utils;
|
||||||
|
using osu.Game.Beatmaps;
|
||||||
|
using osu.Game.Rulesets.Osu.Objects;
|
||||||
|
using osu.Game.Rulesets.Osu.UI;
|
||||||
|
using osu.Game.Tests.Beatmaps;
|
||||||
|
using osuTK;
|
||||||
|
using osuTK.Input;
|
||||||
|
|
||||||
|
namespace osu.Game.Rulesets.Osu.Tests.Editor
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class TestSceneObjectObjectSnap : TestSceneOsuEditor
|
||||||
|
{
|
||||||
|
private OsuPlayfield playfield;
|
||||||
|
|
||||||
|
protected override IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(Ruleset.Value, false);
|
||||||
|
|
||||||
|
public override void SetUpSteps()
|
||||||
|
{
|
||||||
|
base.SetUpSteps();
|
||||||
|
AddStep("get playfield", () => playfield = Editor.ChildrenOfType<OsuPlayfield>().First());
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(true)]
|
||||||
|
[TestCase(false)]
|
||||||
|
public void TestHitCircleSnapsToOtherHitCircle(bool distanceSnapEnabled)
|
||||||
|
{
|
||||||
|
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(playfield.ScreenSpaceDrawQuad.Centre));
|
||||||
|
|
||||||
|
if (!distanceSnapEnabled)
|
||||||
|
AddStep("disable distance snap", () => InputManager.Key(Key.Q));
|
||||||
|
|
||||||
|
AddStep("enter placement mode", () => InputManager.Key(Key.Number2));
|
||||||
|
|
||||||
|
AddStep("place first object", () => InputManager.Click(MouseButton.Left));
|
||||||
|
|
||||||
|
AddStep("move mouse slightly", () => InputManager.MoveMouseTo(playfield.ScreenSpaceDrawQuad.Centre + new Vector2(playfield.ScreenSpaceDrawQuad.Width * 0.02f, 0)));
|
||||||
|
|
||||||
|
AddStep("place second object", () => InputManager.Click(MouseButton.Left));
|
||||||
|
|
||||||
|
AddAssert("both objects at same location", () =>
|
||||||
|
{
|
||||||
|
var objects = EditorBeatmap.HitObjects;
|
||||||
|
|
||||||
|
var first = (OsuHitObject)objects.First();
|
||||||
|
var second = (OsuHitObject)objects.Last();
|
||||||
|
|
||||||
|
return first.Position == second.Position;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestHitCircleSnapsToSliderEnd()
|
||||||
|
{
|
||||||
|
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(playfield.ScreenSpaceDrawQuad.Centre));
|
||||||
|
|
||||||
|
AddStep("disable distance snap", () => InputManager.Key(Key.Q));
|
||||||
|
|
||||||
|
AddStep("enter slider placement mode", () => InputManager.Key(Key.Number3));
|
||||||
|
|
||||||
|
AddStep("start slider placement", () => InputManager.Click(MouseButton.Left));
|
||||||
|
|
||||||
|
AddStep("move to place end", () => InputManager.MoveMouseTo(playfield.ScreenSpaceDrawQuad.Centre + new Vector2(playfield.ScreenSpaceDrawQuad.Width * 0.185f, 0)));
|
||||||
|
|
||||||
|
AddStep("end slider placement", () => InputManager.Click(MouseButton.Right));
|
||||||
|
|
||||||
|
AddStep("enter circle placement mode", () => InputManager.Key(Key.Number2));
|
||||||
|
|
||||||
|
AddStep("move mouse slightly", () => InputManager.MoveMouseTo(playfield.ScreenSpaceDrawQuad.Centre + new Vector2(playfield.ScreenSpaceDrawQuad.Width * 0.20f, 0)));
|
||||||
|
|
||||||
|
AddStep("place second object", () => InputManager.Click(MouseButton.Left));
|
||||||
|
|
||||||
|
AddAssert("circle is at slider's end", () =>
|
||||||
|
{
|
||||||
|
var objects = EditorBeatmap.HitObjects;
|
||||||
|
|
||||||
|
var first = (Slider)objects.First();
|
||||||
|
var second = (OsuHitObject)objects.Last();
|
||||||
|
|
||||||
|
return Precision.AlmostEquals(first.EndPosition, second.Position);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,10 +4,10 @@
|
|||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using osu.Game.Tests.Visual;
|
using osu.Game.Tests.Visual;
|
||||||
|
|
||||||
namespace osu.Game.Rulesets.Osu.Tests
|
namespace osu.Game.Rulesets.Osu.Tests.Editor
|
||||||
{
|
{
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class TestSceneEditor : EditorTestScene
|
public class TestSceneOsuEditor : EditorTestScene
|
||||||
{
|
{
|
||||||
protected override Ruleset CreateEditorRuleset() => new OsuRuleset();
|
protected override Ruleset CreateEditorRuleset() => new OsuRuleset();
|
||||||
}
|
}
|
@ -94,6 +94,10 @@ namespace osu.Game.Rulesets.Osu.Edit
|
|||||||
|
|
||||||
public override SnapResult SnapScreenSpacePositionToValidTime(Vector2 screenSpacePosition)
|
public override SnapResult SnapScreenSpacePositionToValidTime(Vector2 screenSpacePosition)
|
||||||
{
|
{
|
||||||
|
if (snapToVisibleBlueprints(screenSpacePosition, out var snapResult))
|
||||||
|
return snapResult;
|
||||||
|
|
||||||
|
// will be null if distance snap is disabled or not feasible for the current time value.
|
||||||
if (distanceSnapGrid == null)
|
if (distanceSnapGrid == null)
|
||||||
return base.SnapScreenSpacePositionToValidTime(screenSpacePosition);
|
return base.SnapScreenSpacePositionToValidTime(screenSpacePosition);
|
||||||
|
|
||||||
@ -102,6 +106,50 @@ namespace osu.Game.Rulesets.Osu.Edit
|
|||||||
return new SnapResult(distanceSnapGrid.ToScreenSpace(pos), time, PlayfieldAtScreenSpacePosition(screenSpacePosition));
|
return new SnapResult(distanceSnapGrid.ToScreenSpace(pos), time, PlayfieldAtScreenSpacePosition(screenSpacePosition));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool snapToVisibleBlueprints(Vector2 screenSpacePosition, out SnapResult snapResult)
|
||||||
|
{
|
||||||
|
// check other on-screen objects for snapping/stacking
|
||||||
|
var blueprints = BlueprintContainer.SelectionBlueprints.AliveChildren;
|
||||||
|
|
||||||
|
var playfield = PlayfieldAtScreenSpacePosition(screenSpacePosition);
|
||||||
|
|
||||||
|
float snapRadius =
|
||||||
|
playfield.GamefieldToScreenSpace(new Vector2(OsuHitObject.OBJECT_RADIUS / 5)).X -
|
||||||
|
playfield.GamefieldToScreenSpace(Vector2.Zero).X;
|
||||||
|
|
||||||
|
foreach (var b in blueprints)
|
||||||
|
{
|
||||||
|
if (b.IsSelected)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var hitObject = (OsuHitObject)b.HitObject;
|
||||||
|
|
||||||
|
Vector2? snap = checkSnap(hitObject.Position);
|
||||||
|
if (snap == null && hitObject.Position != hitObject.EndPosition)
|
||||||
|
snap = checkSnap(hitObject.EndPosition);
|
||||||
|
|
||||||
|
if (snap != null)
|
||||||
|
{
|
||||||
|
// only return distance portion, since time is not really valid
|
||||||
|
snapResult = new SnapResult(snap.Value, null, playfield);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2? checkSnap(Vector2 checkPos)
|
||||||
|
{
|
||||||
|
Vector2 checkScreenPos = playfield.GamefieldToScreenSpace(checkPos);
|
||||||
|
|
||||||
|
if (Vector2.Distance(checkScreenPos, screenSpacePosition) < snapRadius)
|
||||||
|
return checkScreenPos;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
snapResult = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void updateDistanceSnapGrid()
|
private void updateDistanceSnapGrid()
|
||||||
{
|
{
|
||||||
distanceSnapGridContainer.Clear();
|
distanceSnapGridContainer.Clear();
|
||||||
|
@ -30,7 +30,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
|||||||
{
|
{
|
||||||
protected DragBox DragBox { get; private set; }
|
protected DragBox DragBox { get; private set; }
|
||||||
|
|
||||||
protected Container<SelectionBlueprint> SelectionBlueprints { get; private set; }
|
public Container<SelectionBlueprint> SelectionBlueprints { get; private set; }
|
||||||
|
|
||||||
private SelectionHandler selectionHandler;
|
private SelectionHandler selectionHandler;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user