1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 17:52:56 +08:00

Rework SelctionLayer to support click-selections

This commit is contained in:
smoogipoo 2018-02-12 16:02:42 +09:00
parent b6531d632f
commit cfb2b3f1e8
7 changed files with 139 additions and 73 deletions

View File

@ -157,6 +157,8 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
public Drawable ProxiedLayer => HeadCircle.ApproachCircle;
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => Body.ReceiveMouseInputAt(screenSpacePos);
public override Vector2 SelectionPoint => ToScreenSpace(Body.Position);
public override Quad SelectionQuad => Body.PathDrawQuad;
}

View File

@ -78,6 +78,8 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
container.Attach(RenderbufferInternalFormat.DepthComponent16);
}
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => path.ReceiveMouseInputAt(screenSpacePos);
public void SetRange(double p0, double p1)
{
if (p0 > p1)

View File

@ -19,7 +19,12 @@ namespace osu.Game.Tests.Visual
{
public class TestCaseEditorSelectionLayer : OsuTestCase
{
public override IReadOnlyList<Type> RequiredTypes => new[] { typeof(SelectionLayer) };
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(HitObjectCapturer),
typeof(HitObjectSelectionBox),
typeof(SelectionLayer)
};
[BackgroundDependencyLoader]
private void load()

View File

@ -0,0 +1,56 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics.Primitives;
using osu.Game.Rulesets.Objects.Drawables;
using OpenTK;
namespace osu.Game.Rulesets.Edit.Layers.Selection
{
public class HitObjectCapturer
{
public event Action<DrawableHitObject> HitObjectCaptured;
private readonly IEnumerable<DrawableHitObject> capturableHitObjects;
public HitObjectCapturer(IEnumerable<DrawableHitObject> capturableHitObjects)
{
this.capturableHitObjects = capturableHitObjects;
}
/// <summary>
/// Captures all hitobjects that are present within the area of a <see cref="Quad"/>.
/// </summary>
/// <param name="screenSpaceQuad">The capture <see cref="Quad"/>.</param>
/// <returns>If any <see cref="DrawableHitObject"/>s were captured.</returns>
public bool CaptureQuad(Quad screenSpaceQuad)
{
bool anyCaptured = false;
foreach (var obj in capturableHitObjects.Where(h => h.IsAlive && h.IsPresent && screenSpaceQuad.Contains(h.SelectionPoint)))
{
HitObjectCaptured?.Invoke(obj);
anyCaptured = true;
}
return anyCaptured;
}
/// <summary>
/// Captures the top-most hitobject that is present under a specific point.
/// </summary>
/// <param name="screenSpacePoint">The <see cref="Vector2"/> to capture at.</param>
/// <returns>Whether a <see cref="DrawableHitObject"/> was captured.</returns>
public bool CapturePoint(Vector2 screenSpacePoint)
{
var captured = capturableHitObjects.Reverse().Where(h => h.IsAlive && h.IsPresent).FirstOrDefault(h => h.ReceiveMouseInputAt(screenSpacePoint));
if (captured == null)
return false;
HitObjectCaptured?.Invoke(captured);
return true;
}
}
}

View File

@ -1,7 +1,6 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
@ -12,7 +11,6 @@ using osu.Game.Graphics;
using osu.Game.Rulesets.Objects.Drawables;
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Configuration;
namespace osu.Game.Rulesets.Edit.Layers.Selection
{
@ -21,29 +19,18 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
/// </summary>
public class HitObjectSelectionBox : CompositeDrawable
{
public readonly Bindable<SelectionInfo> Selection = new Bindable<SelectionInfo>();
/// <summary>
/// The <see cref="DrawableHitObject"/>s that can be selected through a drag-selection.
/// </summary>
public IEnumerable<DrawableHitObject> CapturableObjects;
private readonly Container borderMask;
private readonly Drawable background;
private readonly HandleContainer handles;
private Color4 captureFinishedColour;
private readonly Vector2 startPos;
private RectangleF dragRectangle;
/// <summary>
/// Creates a new <see cref="HitObjectSelectionBox"/>.
/// </summary>
/// <param name="startPos">The point at which the drag was initiated, in the parent's coordinates.</param>
public HitObjectSelectionBox(Vector2 startPos)
public HitObjectSelectionBox()
{
this.startPos = startPos;
InternalChildren = new Drawable[]
{
new Container
@ -70,8 +57,8 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
RelativeSizeAxes = Axes.Both,
Alpha = 0,
GetDragRectangle = () => dragRectangle,
UpdateDragRectangle = updateDragRectangle,
FinishDrag = FinishCapture
UpdateDragRectangle = SetDragRectangle,
FinishDrag = () => FinishCapture()
}
};
}
@ -82,49 +69,29 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
captureFinishedColour = colours.Yellow;
}
/// <summary>
/// The secondary corner of the drag selection box. A rectangle will be fit between the starting position and this value.
/// </summary>
public Vector2 DragEndPosition { set => updateDragRectangle(RectangleF.FromLTRB(startPos.X, startPos.Y, value.X, value.Y)); }
private RectangleF dragRectangle;
private void updateDragRectangle(RectangleF rectangle)
public void SetDragRectangle(RectangleF rectangle)
{
dragRectangle = rectangle;
Position = new Vector2(
Math.Min(rectangle.Left, rectangle.Right),
Math.Min(rectangle.Top, rectangle.Bottom));
var topLeft = Parent.ToLocalSpace(rectangle.TopLeft);
var bottomRight = Parent.ToLocalSpace(rectangle.BottomRight);
Size = new Vector2(
Math.Max(rectangle.Left, rectangle.Right) - Position.X,
Math.Max(rectangle.Top, rectangle.Bottom) - Position.Y);
Position = topLeft;
Size = bottomRight - topLeft;
}
private readonly List<DrawableHitObject> capturedHitObjects = new List<DrawableHitObject>();
/// <summary>
/// Processes hitobjects to determine which ones are captured by the drag selection.
/// Captured hitobjects will be enclosed by the drag selection upon <see cref="FinishCapture"/>.
/// </summary>
public void BeginCapture()
{
capturedHitObjects.Clear();
public bool HasCaptured => capturedHitObjects.Count > 0;
foreach (var obj in CapturableObjects)
{
if (!obj.IsAlive || !obj.IsPresent)
continue;
public void AddCaptured(DrawableHitObject hitObject) => capturedHitObjects.Add(hitObject);
if (ScreenSpaceDrawQuad.Contains(obj.SelectionPoint))
capturedHitObjects.Add(obj);
}
}
public void ClearCaptured() => capturedHitObjects.Clear();
/// <summary>
/// Encloses hitobjects captured through <see cref="BeginCapture"/> in the drag selection box.
/// </summary>
public void FinishCapture()
public void FinishCapture(bool instant = false)
{
if (capturedHitObjects.Count == 0)
{
@ -145,8 +112,8 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
topLeft -= new Vector2(5);
bottomRight += new Vector2(5);
this.MoveTo(topLeft, 200, Easing.OutQuint)
.ResizeTo(bottomRight - topLeft, 200, Easing.OutQuint);
this.MoveTo(topLeft, instant ? 0 : 100, Easing.OutQuint)
.ResizeTo(bottomRight - topLeft, instant ? 0 : 100, Easing.OutQuint);
dragRectangle = RectangleF.FromLTRB(topLeft.X, topLeft.Y, bottomRight.X, bottomRight.Y);
@ -156,12 +123,6 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
// Transform into markers to let the user modify the drag selection further.
background.Delay(50).FadeOut(200);
handles.FadeIn(200);
Selection.Value = new SelectionInfo
{
Objects = capturedHitObjects,
SelectionQuad = Parent.ToScreenSpace(dragRectangle)
};
}
private bool isActive = true;
@ -171,9 +132,7 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
public override void Hide()
{
isActive = false;
this.FadeOut(400, Easing.OutQuint).Expire();
Selection.Value = null;
this.FadeOut(400, Easing.OutQuint);
}
}
}

View File

@ -1,18 +1,18 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Configuration;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Input;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
namespace osu.Game.Rulesets.Edit.Layers.Selection
{
public class SelectionLayer : CompositeDrawable
{
public readonly Bindable<SelectionInfo> Selection = new Bindable<SelectionInfo>();
private readonly Playfield playfield;
public SelectionLayer(Playfield playfield)
@ -22,39 +22,80 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
RelativeSizeAxes = Axes.Both;
}
private HitObjectSelectionBox selectionBoxBox;
private HitObjectSelectionBox selectionBox;
private HitObjectCapturer capturer;
[BackgroundDependencyLoader]
private void load()
{
capturer = new HitObjectCapturer(playfield.HitObjects.Objects);
capturer.HitObjectCaptured += hitObjectCaptured;
}
private void hitObjectCaptured(DrawableHitObject hitObject) => selectionBox.AddCaptured(hitObject);
protected override bool OnDragStart(InputState state)
{
// Hide the previous drag box - we won't be working with it any longer
selectionBoxBox?.Hide();
selectionBox?.Hide();
selectionBox?.Expire();
AddInternal(selectionBoxBox = new HitObjectSelectionBox(ToLocalSpace(state.Mouse.NativeState.Position))
{
CapturableObjects = playfield.HitObjects.Objects,
});
Selection.BindTo(selectionBoxBox.Selection);
AddInternal(selectionBox = new HitObjectSelectionBox());
return true;
}
protected override bool OnDrag(InputState state)
{
selectionBoxBox.DragEndPosition = ToLocalSpace(state.Mouse.NativeState.Position);
selectionBoxBox.BeginCapture();
var dragPosition = state.Mouse.NativeState.Position;
var dragStartPosition = state.Mouse.NativeState.PositionMouseDown ?? dragPosition;
var screenSpaceDragQuad = new Quad(dragStartPosition.X, dragStartPosition.Y, dragPosition.X - dragStartPosition.X, dragPosition.Y - dragStartPosition.Y);
selectionBox.SetDragRectangle(screenSpaceDragQuad.AABBFloat);
capturer.CaptureQuad(screenSpaceDragQuad);
return true;
}
protected override bool OnDragEnd(InputState state)
{
selectionBoxBox.FinishCapture();
// Due to https://github.com/ppy/osu-framework/issues/1382, we may get here after OnClick has set the selectionBox to null
// In the case that the user dragged within the click distance out of an object
if (selectionBox == null)
return true;
selectionBox.FinishCapture();
// If there are no hitobjects, remove the selection box
if (!selectionBox.HasCaptured)
{
selectionBox.Expire();
selectionBox = null;
}
return true;
}
protected override bool OnClick(InputState state)
{
selectionBoxBox?.Hide();
// We could be coming here without a previous selection box
if (selectionBox == null)
AddInternal(selectionBox = new HitObjectSelectionBox { Position = ToLocalSpace(state.Mouse.NativeState.Position), Alpha = 0 });
// If we're coming here with a previous selection, unselect those hitobjects
selectionBox.ClearCaptured();
if (capturer.CapturePoint(state.Mouse.NativeState.Position))
{
selectionBox.Alpha = 1;
selectionBox.FinishCapture(true);
}
else
{
selectionBox.Hide();
selectionBox = null;
}
return true;
}
}

View File

@ -332,6 +332,7 @@
<Compile Include="Overlays\Settings\Sections\Maintenance\DeleteAllBeatmapsDialog.cs" />
<Compile Include="Rulesets\Configuration\IRulesetConfigManager.cs" />
<Compile Include="Rulesets\Configuration\RulesetConfigManager.cs" />
<Compile Include="Rulesets\Edit\Layers\Selection\HitObjectCapturer.cs" />
<Compile Include="Rulesets\Mods\IApplicableFailOverride.cs" />
<Compile Include="Rulesets\Mods\IApplicableMod.cs" />
<Compile Include="Rulesets\Mods\IApplicableToBeatmapConverter.cs" />