1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 13:37:25 +08:00

Merge pull request #14853 from bdach/timeline-shift-click-selection

Add support for range selection to editor timeline
This commit is contained in:
Dean Herbert 2021-09-27 14:11:44 +09:00 committed by GitHub
commit 91c5328fc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 1 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Testing;
@ -159,5 +160,72 @@ namespace osu.Game.Tests.Visual.Editing
AddAssert("no hitobjects in beatmap", () => EditorBeatmap.HitObjects.Count == 0);
}
[Test]
public void TestRangeSelect()
{
var addedObjects = new[]
{
new HitCircle { StartTime = 100 },
new HitCircle { StartTime = 200, Position = new Vector2(100) },
new HitCircle { StartTime = 300, Position = new Vector2(200) },
new HitCircle { StartTime = 400, Position = new Vector2(300) },
new HitCircle { StartTime = 500, Position = new Vector2(400) },
};
AddStep("add hitobjects", () => EditorBeatmap.AddRange(addedObjects));
moveMouseToObject(() => addedObjects[1]);
AddStep("click second", () => InputManager.Click(MouseButton.Left));
AddAssert("hitobject selected", () => EditorBeatmap.SelectedHitObjects.Single() == addedObjects[1]);
AddStep("hold shift", () => InputManager.PressKey(Key.ShiftLeft));
moveMouseToObject(() => addedObjects[3]);
AddStep("click fourth", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Skip(1).Take(3));
moveMouseToObject(() => addedObjects[0]);
AddStep("click first", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Take(2));
AddStep("clear selection", () => EditorBeatmap.SelectedHitObjects.Clear());
AddStep("release shift", () => InputManager.ReleaseKey(Key.ShiftLeft));
moveMouseToObject(() => addedObjects[0]);
AddStep("click first", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Take(1));
AddStep("hold ctrl", () => InputManager.PressKey(Key.ControlLeft));
moveMouseToObject(() => addedObjects[2]);
AddStep("click third", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(new[] { addedObjects[0], addedObjects[2] });
AddStep("hold shift", () => InputManager.PressKey(Key.ShiftLeft));
moveMouseToObject(() => addedObjects[4]);
AddStep("click fifth", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Except(new[] { addedObjects[1] }));
moveMouseToObject(() => addedObjects[0]);
AddStep("click first", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects);
AddStep("clear selection", () => EditorBeatmap.SelectedHitObjects.Clear());
moveMouseToObject(() => addedObjects[0]);
AddStep("click first", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Take(1));
moveMouseToObject(() => addedObjects[1]);
AddStep("click first", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Take(2));
moveMouseToObject(() => addedObjects[2]);
AddStep("click first", () => InputManager.Click(MouseButton.Left));
assertSelectionIs(addedObjects.Take(3));
}
private void assertSelectionIs(IEnumerable<HitObject> hitObjects)
=> AddAssert("correct hitobjects selected", () => EditorBeatmap.SelectedHitObjects.OrderBy(h => h.StartTime).SequenceEqual(hitObjects));
}
}

View File

@ -191,7 +191,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// <param name="blueprint">The blueprint.</param>
/// <param name="e">The mouse event responsible for selection.</param>
/// <returns>Whether a selection was performed.</returns>
internal bool MouseDownSelectionRequested(SelectionBlueprint<T> blueprint, MouseButtonEvent e)
internal virtual bool MouseDownSelectionRequested(SelectionBlueprint<T> blueprint, MouseButtonEvent e)
{
if (e.ShiftPressed && e.Button == MouseButton.Right)
{

View File

@ -1,13 +1,19 @@
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osuTK;
using osuTK.Input;
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
{
@ -62,5 +68,62 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
EditorBeatmap.Update(h);
});
}
/// <summary>
/// The "pivot" object, used in range selection mode.
/// When in range selection, the range to select is determined by the pivot object
/// (last existing object interacted with prior to holding down Shift)
/// and by the object clicked last when Shift was pressed.
/// </summary>
[CanBeNull]
private HitObject pivot;
internal override bool MouseDownSelectionRequested(SelectionBlueprint<HitObject> blueprint, MouseButtonEvent e)
{
if (e.ShiftPressed && e.Button == MouseButton.Left && SelectedItems.Any())
{
handleRangeSelection(blueprint, e.ControlPressed);
return true;
}
bool result = base.MouseDownSelectionRequested(blueprint, e);
// ensure that the object wasn't removed by the base implementation before making it the new pivot.
if (EditorBeatmap.HitObjects.Contains(blueprint.Item))
pivot = blueprint.Item;
return result;
}
/// <summary>
/// Handles a request for range selection (triggered when Shift is held down).
/// </summary>
/// <param name="blueprint">The blueprint which was clicked in range selection mode.</param>
/// <param name="cumulative">
/// Whether the selection should be cumulative.
/// In cumulative mode, consecutive range selections will shift the pivot (which usually stays fixed for the duration of a range selection)
/// and will never deselect an object that was previously selected.
/// </param>
private void handleRangeSelection(SelectionBlueprint<HitObject> blueprint, bool cumulative)
{
var clickedObject = blueprint.Item;
Debug.Assert(pivot != null);
double rangeStart = Math.Min(clickedObject.StartTime, pivot.StartTime);
double rangeEnd = Math.Max(clickedObject.GetEndTime(), pivot.GetEndTime());
var newSelection = new HashSet<HitObject>(EditorBeatmap.HitObjects.Where(obj => isInRange(obj, rangeStart, rangeEnd)));
if (cumulative)
{
pivot = clickedObject;
newSelection.UnionWith(EditorBeatmap.SelectedHitObjects);
}
EditorBeatmap.SelectedHitObjects.Clear();
EditorBeatmap.SelectedHitObjects.AddRange(newSelection);
bool isInRange(HitObject hitObject, double start, double end)
=> hitObject.StartTime >= start && hitObject.GetEndTime() <= end;
}
}
}