mirror of
https://github.com/ppy/osu.git
synced 2025-02-13 16:02:58 +08:00
Merge pull request #14851 from bdach/blueprint-container-sorting
Rewrite blueprint sorting logic to be more robust
This commit is contained in:
commit
7f45bd27f2
132
osu.Game.Tests/Visual/Editing/TestSceneBlueprintOrdering.cs
Normal file
132
osu.Game.Tests/Visual/Editing/TestSceneBlueprintOrdering.cs
Normal file
@ -0,0 +1,132 @@
|
||||
// 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.Game.Beatmaps;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
using osu.Game.Rulesets.Objects.Types;
|
||||
using osu.Game.Rulesets.Osu;
|
||||
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles;
|
||||
using osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components;
|
||||
using osu.Game.Rulesets.Osu.Objects;
|
||||
using osu.Game.Rulesets.Osu.UI;
|
||||
using osu.Game.Rulesets.UI;
|
||||
using osu.Game.Screens.Edit.Compose.Components;
|
||||
using osu.Game.Screens.Edit.Compose.Components.Timeline;
|
||||
using osu.Game.Tests.Beatmaps;
|
||||
using osuTK;
|
||||
using osuTK.Input;
|
||||
|
||||
namespace osu.Game.Tests.Visual.Editing
|
||||
{
|
||||
public class TestSceneBlueprintOrdering : EditorTestScene
|
||||
{
|
||||
protected override Ruleset CreateEditorRuleset() => new OsuRuleset();
|
||||
|
||||
protected override IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(ruleset, false);
|
||||
|
||||
private EditorBlueprintContainer blueprintContainer
|
||||
=> Editor.ChildrenOfType<EditorBlueprintContainer>().First();
|
||||
|
||||
[Test]
|
||||
public void TestSelectedObjectHasPriorityWhenOverlapping()
|
||||
{
|
||||
var firstSlider = new Slider
|
||||
{
|
||||
Path = new SliderPath(new[]
|
||||
{
|
||||
new PathControlPoint(new Vector2()),
|
||||
new PathControlPoint(new Vector2(150, -50)),
|
||||
new PathControlPoint(new Vector2(300, 0))
|
||||
}),
|
||||
Position = new Vector2(0, 100)
|
||||
};
|
||||
var secondSlider = new Slider
|
||||
{
|
||||
Path = new SliderPath(new[]
|
||||
{
|
||||
new PathControlPoint(new Vector2()),
|
||||
new PathControlPoint(new Vector2(-50, 50)),
|
||||
new PathControlPoint(new Vector2(-100, 100))
|
||||
}),
|
||||
Position = new Vector2(200, 0)
|
||||
};
|
||||
|
||||
AddStep("add overlapping sliders", () =>
|
||||
{
|
||||
EditorBeatmap.Add(firstSlider);
|
||||
EditorBeatmap.Add(secondSlider);
|
||||
});
|
||||
AddStep("select first slider", () => EditorBeatmap.SelectedHitObjects.Add(firstSlider));
|
||||
|
||||
AddStep("move mouse to common point", () =>
|
||||
{
|
||||
var pos = blueprintContainer.ChildrenOfType<PathControlPointPiece>().ElementAt(1).ScreenSpaceDrawQuad.Centre;
|
||||
InputManager.MoveMouseTo(pos);
|
||||
});
|
||||
AddStep("right click", () => InputManager.Click(MouseButton.Right));
|
||||
|
||||
AddAssert("selection is unchanged", () => EditorBeatmap.SelectedHitObjects.Single() == firstSlider);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestOverlappingObjectsWithSameStartTime()
|
||||
{
|
||||
AddStep("add overlapping circles", () =>
|
||||
{
|
||||
EditorBeatmap.Add(createHitCircle(50, OsuPlayfield.BASE_SIZE / 2));
|
||||
EditorBeatmap.Add(createHitCircle(50, OsuPlayfield.BASE_SIZE / 2 + new Vector2(-10, -20)));
|
||||
EditorBeatmap.Add(createHitCircle(50, OsuPlayfield.BASE_SIZE / 2 + new Vector2(10, -20)));
|
||||
});
|
||||
|
||||
AddStep("click at centre of playfield", () =>
|
||||
{
|
||||
var hitObjectContainer = Editor.ChildrenOfType<HitObjectContainer>().Single();
|
||||
var centre = hitObjectContainer.ToScreenSpace(OsuPlayfield.BASE_SIZE / 2);
|
||||
InputManager.MoveMouseTo(centre);
|
||||
InputManager.Click(MouseButton.Left);
|
||||
});
|
||||
|
||||
AddAssert("frontmost object selected", () =>
|
||||
{
|
||||
var hasCombo = Editor.ChildrenOfType<HitCircleSelectionBlueprint>().Single(b => b.IsSelected).Item as IHasComboInformation;
|
||||
return hasCombo?.IndexInCurrentCombo == 0;
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPlacementOfConcurrentObjectWithDuration()
|
||||
{
|
||||
AddStep("seek to timing point", () => EditorClock.Seek(2170));
|
||||
AddStep("add hit circle", () => EditorBeatmap.Add(createHitCircle(2170, Vector2.Zero)));
|
||||
|
||||
AddStep("choose spinner placement tool", () =>
|
||||
{
|
||||
InputManager.Key(Key.Number4);
|
||||
var hitObjectContainer = Editor.ChildrenOfType<HitObjectContainer>().Single();
|
||||
InputManager.MoveMouseTo(hitObjectContainer.ScreenSpaceDrawQuad.Centre);
|
||||
});
|
||||
|
||||
AddStep("begin placing spinner", () =>
|
||||
{
|
||||
InputManager.Click(MouseButton.Left);
|
||||
});
|
||||
AddStep("end placing spinner", () =>
|
||||
{
|
||||
EditorClock.Seek(2500);
|
||||
InputManager.Click(MouseButton.Right);
|
||||
});
|
||||
|
||||
AddAssert("two timeline blueprints present", () => Editor.ChildrenOfType<TimelineHitObjectBlueprint>().Count() == 2);
|
||||
}
|
||||
|
||||
private HitCircle createHitCircle(double startTime, Vector2 position) => new HitCircle
|
||||
{
|
||||
StartTime = startTime,
|
||||
Position = position,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
// 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.Game.Beatmaps;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
using osu.Game.Rulesets.Osu;
|
||||
using osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components;
|
||||
using osu.Game.Rulesets.Osu.Objects;
|
||||
using osu.Game.Screens.Edit.Compose.Components;
|
||||
using osu.Game.Tests.Beatmaps;
|
||||
using osuTK;
|
||||
using osuTK.Input;
|
||||
|
||||
namespace osu.Game.Tests.Visual.Editing
|
||||
{
|
||||
public class TestSceneBlueprintSelection : EditorTestScene
|
||||
{
|
||||
protected override Ruleset CreateEditorRuleset() => new OsuRuleset();
|
||||
|
||||
protected override IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(ruleset, false);
|
||||
|
||||
private EditorBlueprintContainer blueprintContainer
|
||||
=> Editor.ChildrenOfType<EditorBlueprintContainer>().First();
|
||||
|
||||
[Test]
|
||||
public void TestSelectedObjectHasPriorityWhenOverlapping()
|
||||
{
|
||||
var firstSlider = new Slider
|
||||
{
|
||||
Path = new SliderPath(new[]
|
||||
{
|
||||
new PathControlPoint(new Vector2()),
|
||||
new PathControlPoint(new Vector2(150, -50)),
|
||||
new PathControlPoint(new Vector2(300, 0))
|
||||
}),
|
||||
Position = new Vector2(0, 100)
|
||||
};
|
||||
var secondSlider = new Slider
|
||||
{
|
||||
Path = new SliderPath(new[]
|
||||
{
|
||||
new PathControlPoint(new Vector2()),
|
||||
new PathControlPoint(new Vector2(-50, 50)),
|
||||
new PathControlPoint(new Vector2(-100, 100))
|
||||
}),
|
||||
Position = new Vector2(200, 0)
|
||||
};
|
||||
|
||||
AddStep("add overlapping sliders", () =>
|
||||
{
|
||||
EditorBeatmap.Add(firstSlider);
|
||||
EditorBeatmap.Add(secondSlider);
|
||||
});
|
||||
AddStep("select first slider", () => EditorBeatmap.SelectedHitObjects.Add(firstSlider));
|
||||
|
||||
AddStep("move mouse to common point", () =>
|
||||
{
|
||||
var pos = blueprintContainer.ChildrenOfType<PathControlPointPiece>().ElementAt(1).ScreenSpaceDrawQuad.Centre;
|
||||
InputManager.MoveMouseTo(pos);
|
||||
});
|
||||
AddStep("right click", () => InputManager.Click(MouseButton.Right));
|
||||
|
||||
AddAssert("selection is unchanged", () => EditorBeatmap.SelectedHitObjects.Single() == firstSlider);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
// 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.Collections.Generic;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Rulesets.Edit;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
using osu.Game.Rulesets.Objects.Types;
|
||||
|
||||
namespace osu.Game.Screens.Edit.Compose.Components
|
||||
{
|
||||
@ -15,69 +15,62 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
||||
/// </summary>
|
||||
public sealed class HitObjectOrderedSelectionContainer : Container<SelectionBlueprint<HitObject>>
|
||||
{
|
||||
[Resolved]
|
||||
private EditorBeatmap editorBeatmap { get; set; }
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
base.LoadComplete();
|
||||
|
||||
editorBeatmap.HitObjectUpdated += hitObjectUpdated;
|
||||
}
|
||||
|
||||
private void hitObjectUpdated(HitObject _) => SortInternal();
|
||||
|
||||
public override void Add(SelectionBlueprint<HitObject> drawable)
|
||||
{
|
||||
SortInternal();
|
||||
base.Add(drawable);
|
||||
bindStartTime(drawable);
|
||||
}
|
||||
|
||||
public override bool Remove(SelectionBlueprint<HitObject> drawable)
|
||||
{
|
||||
if (!base.Remove(drawable))
|
||||
return false;
|
||||
|
||||
unbindStartTime(drawable);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Clear(bool disposeChildren)
|
||||
{
|
||||
base.Clear(disposeChildren);
|
||||
unbindAllStartTimes();
|
||||
}
|
||||
|
||||
private readonly Dictionary<SelectionBlueprint<HitObject>, IBindable> startTimeMap = new Dictionary<SelectionBlueprint<HitObject>, IBindable>();
|
||||
|
||||
private void bindStartTime(SelectionBlueprint<HitObject> blueprint)
|
||||
{
|
||||
var bindable = blueprint.Item.StartTimeBindable.GetBoundCopy();
|
||||
|
||||
bindable.BindValueChanged(_ =>
|
||||
{
|
||||
if (LoadState >= LoadState.Ready)
|
||||
SortInternal();
|
||||
});
|
||||
|
||||
startTimeMap[blueprint] = bindable;
|
||||
}
|
||||
|
||||
private void unbindStartTime(SelectionBlueprint<HitObject> blueprint)
|
||||
{
|
||||
startTimeMap[blueprint].UnbindAll();
|
||||
startTimeMap.Remove(blueprint);
|
||||
}
|
||||
|
||||
private void unbindAllStartTimes()
|
||||
{
|
||||
foreach (var kvp in startTimeMap)
|
||||
kvp.Value.UnbindAll();
|
||||
startTimeMap.Clear();
|
||||
SortInternal();
|
||||
return base.Remove(drawable);
|
||||
}
|
||||
|
||||
protected override int Compare(Drawable x, Drawable y)
|
||||
{
|
||||
var xObj = (SelectionBlueprint<HitObject>)x;
|
||||
var yObj = (SelectionBlueprint<HitObject>)y;
|
||||
var xObj = ((SelectionBlueprint<HitObject>)x).Item;
|
||||
var yObj = ((SelectionBlueprint<HitObject>)y).Item;
|
||||
|
||||
// Put earlier blueprints towards the end of the list, so they handle input first
|
||||
int i = yObj.Item.StartTime.CompareTo(xObj.Item.StartTime);
|
||||
|
||||
if (i != 0) return i;
|
||||
int result = yObj.StartTime.CompareTo(xObj.StartTime);
|
||||
if (result != 0) return result;
|
||||
|
||||
// Fall back to end time if the start time is equal.
|
||||
i = yObj.Item.GetEndTime().CompareTo(xObj.Item.GetEndTime());
|
||||
result = yObj.GetEndTime().CompareTo(xObj.GetEndTime());
|
||||
if (result != 0) return result;
|
||||
|
||||
return i == 0 ? CompareReverseChildID(y, x) : i;
|
||||
// As a final fallback, use combo information if available.
|
||||
if (xObj is IHasComboInformation xHasCombo && yObj is IHasComboInformation yHasCombo)
|
||||
{
|
||||
result = yHasCombo.ComboIndex.CompareTo(xHasCombo.ComboIndex);
|
||||
if (result != 0) return result;
|
||||
|
||||
result = yHasCombo.IndexInCurrentCombo.CompareTo(xHasCombo.IndexInCurrentCombo);
|
||||
if (result != 0) return result;
|
||||
}
|
||||
|
||||
return CompareReverseChildID(y, x);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
base.Dispose(isDisposing);
|
||||
|
||||
if (editorBeatmap != null)
|
||||
editorBeatmap.HitObjectUpdated -= hitObjectUpdated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user