mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 20:07:29 +08:00
Handle all selection events within SelectionBox (incl. single-mask)
This commit is contained in:
parent
082e5e4949
commit
1dca1663c3
@ -253,8 +253,8 @@ namespace osu.Game.Rulesets.Edit
|
||||
/// Creates a <see cref="SelectionBox"/> which outlines <see cref="DrawableHitObject"/>s
|
||||
/// and handles hitobject pattern adjustments.
|
||||
/// </summary>
|
||||
/// <param name="overlays">The <see cref="DrawableHitObject"/> overlays.</param>
|
||||
public virtual SelectionBox CreateSelectionBox() => new SelectionBox();
|
||||
/// <param name="maskContainer">The <see cref="HitObjectMask"/> container.</param>
|
||||
public virtual SelectionBox CreateSelectionBox(MaskContainer maskContainer) => new SelectionBox(maskContainer);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="ScalableContainer"/> which provides a layer above or below the <see cref="Playfield"/>.
|
||||
|
@ -4,8 +4,6 @@
|
||||
using System;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Primitives;
|
||||
using osu.Framework.Input;
|
||||
using osu.Game.Rulesets.Edit.Types;
|
||||
using osu.Game.Rulesets.Objects.Drawables;
|
||||
using OpenTK;
|
||||
|
||||
@ -26,12 +24,6 @@ namespace osu.Game.Rulesets.Edit
|
||||
/// </summary>
|
||||
public event Action<HitObjectMask> Deselected;
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when this <see cref="HitObjectMask"/> is requesting to be the single selection.
|
||||
/// This <see cref="HitObjectMask"/> has not been selected at this point, but will be selected immediately afterwards.
|
||||
/// </summary>
|
||||
public event Action<HitObjectMask> SingleSelectionRequested;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DrawableHitObject"/> which this <see cref="HitObjectMask"/> applies to.
|
||||
/// </summary>
|
||||
@ -77,29 +69,6 @@ namespace osu.Game.Rulesets.Edit
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
||||
{
|
||||
SingleSelectionRequested?.Invoke(this);
|
||||
Select();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override bool OnDragStart(InputState state) => true;
|
||||
|
||||
protected override bool OnDrag(InputState state)
|
||||
{
|
||||
// Todo: Various forms of snapping
|
||||
switch (HitObject.HitObject)
|
||||
{
|
||||
case IHasEditablePosition editablePosition:
|
||||
editablePosition.OffsetPosition(state.Mouse.Delta);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override bool OnDragEnd(InputState state) => true;
|
||||
|
||||
protected override void PopIn() => Alpha = 1;
|
||||
protected override void PopOut() => Alpha = 0;
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
// 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;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
@ -24,17 +23,17 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
/// </summary>
|
||||
public event Action DragEnd;
|
||||
|
||||
private readonly IEnumerable<HitObjectMask> selectableMasks;
|
||||
private readonly MaskContainer maskContainer;
|
||||
|
||||
private Drawable box;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="DragBox"/>.
|
||||
/// </summary>
|
||||
/// <param name="selectableMasks">The selectable <see cref="HitObjectMask"/>s.</param>
|
||||
public DragBox(IEnumerable<HitObjectMask> selectableMasks)
|
||||
/// <param name="maskContainer">The selectable <see cref="HitObjectMask"/>s.</param>
|
||||
public DragBox(MaskContainer maskContainer)
|
||||
{
|
||||
this.selectableMasks = selectableMasks;
|
||||
this.maskContainer = maskContainer;
|
||||
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
AlwaysPresent = true;
|
||||
@ -79,7 +78,7 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
box.Position = topLeft;
|
||||
box.Size = bottomRight - topLeft;
|
||||
|
||||
foreach (var mask in selectableMasks)
|
||||
foreach (var mask in maskContainer.AliveMasks)
|
||||
{
|
||||
if (mask.IsPresent && dragRectangle.Contains(mask.SelectionPoint))
|
||||
mask.Select();
|
||||
|
@ -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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
@ -21,8 +20,6 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
private MaskContainer maskContainer;
|
||||
private SelectionBox selectionBox;
|
||||
|
||||
private readonly HashSet<HitObjectMask> selectedMasks = new HashSet<HitObjectMask>();
|
||||
|
||||
public HitObjectMaskLayer(Playfield playfield, HitObjectComposer composer)
|
||||
{
|
||||
this.playfield = playfield;
|
||||
@ -36,9 +33,9 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
{
|
||||
maskContainer = new MaskContainer();
|
||||
|
||||
selectionBox = composer.CreateSelectionBox();
|
||||
selectionBox = composer.CreateSelectionBox(maskContainer);
|
||||
|
||||
var dragBox = new DragBox(maskContainer.AliveChildren);
|
||||
var dragBox = new DragBox(maskContainer);
|
||||
dragBox.DragEnd += () => selectionBox.UpdateVisibility();
|
||||
|
||||
InternalChildren = new Drawable[]
|
||||
@ -63,12 +60,7 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
if (mask == null)
|
||||
return;
|
||||
|
||||
mask.Selected += onSelected;
|
||||
mask.Deselected += onDeselected;
|
||||
mask.SingleSelectionRequested += onSingleSelectionRequested;
|
||||
|
||||
maskContainer.Add(mask);
|
||||
selectionBox.AddMask(mask);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -81,34 +73,13 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
if (mask == null)
|
||||
return;
|
||||
|
||||
mask.Selected -= onSelected;
|
||||
mask.Deselected -= onDeselected;
|
||||
mask.SingleSelectionRequested -= onSingleSelectionRequested;
|
||||
|
||||
maskContainer.Remove(mask);
|
||||
selectionBox.RemoveMask(mask);
|
||||
}
|
||||
|
||||
private void onSelected(HitObjectMask mask) => selectedMasks.Add(mask);
|
||||
|
||||
private void onDeselected(HitObjectMask mask) => selectedMasks.Remove(mask);
|
||||
|
||||
private void onSingleSelectionRequested(HitObjectMask mask) => DeselectAll();
|
||||
|
||||
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
||||
{
|
||||
DeselectAll();
|
||||
selectionBox.DeselectAll();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deselects all selected <see cref="DrawableHitObject"/>s.
|
||||
/// </summary>
|
||||
public void DeselectAll() => selectedMasks.ToList().ForEach(m => m.Deselect());
|
||||
|
||||
private class MaskContainer : Container<HitObjectMask>
|
||||
{
|
||||
public new IEnumerable<HitObjectMask> AliveChildren => AliveInternalChildren.Cast<HitObjectMask>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Rulesets.Edit;
|
||||
|
||||
namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
{
|
||||
public class MaskContainer : Container<HitObjectMask>
|
||||
{
|
||||
/// <summary>
|
||||
/// Invoked when any <see cref="HitObjectMask"/> is selected.
|
||||
/// </summary>
|
||||
public event Action<HitObjectMask> MaskSelected;
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when any <see cref="HitObjectMask"/> is deselected.
|
||||
/// </summary>
|
||||
public event Action<HitObjectMask> MaskDeselected;
|
||||
|
||||
/// <summary>
|
||||
/// All the <see cref="HitObjectMask"/>s with <see cref="IsAlive"/> == true.
|
||||
/// </summary>
|
||||
public IEnumerable<HitObjectMask> AliveMasks => AliveInternalChildren.Cast<HitObjectMask>();
|
||||
|
||||
public override void Add(HitObjectMask drawable)
|
||||
{
|
||||
base.Add(drawable);
|
||||
|
||||
drawable.Selected += onMaskSelected;
|
||||
drawable.Deselected += onMaskDeselected;
|
||||
}
|
||||
|
||||
public override bool Remove(HitObjectMask drawable)
|
||||
{
|
||||
var result = base.Remove(drawable);
|
||||
|
||||
if (result)
|
||||
{
|
||||
drawable.Selected -= onMaskSelected;
|
||||
drawable.Deselected += onMaskDeselected;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void onMaskSelected(HitObjectMask mask) => MaskSelected?.Invoke(mask);
|
||||
private void onMaskDeselected(HitObjectMask mask) => MaskDeselected?.Invoke(mask);
|
||||
}
|
||||
}
|
@ -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 System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
@ -23,15 +22,23 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
{
|
||||
public const float BORDER_RADIUS = 2;
|
||||
|
||||
private readonly HashSet<HitObjectMask> selectedMasks = new HashSet<HitObjectMask>();
|
||||
private readonly MaskContainer maskContainer;
|
||||
|
||||
private readonly List<HitObjectMask> selectedMasks = new List<HitObjectMask>();
|
||||
private IEnumerable<HitObjectMask> selectableMasks => maskContainer.AliveMasks;
|
||||
|
||||
private Drawable box;
|
||||
|
||||
public SelectionBox()
|
||||
public SelectionBox(MaskContainer maskContainer)
|
||||
{
|
||||
this.maskContainer = maskContainer;
|
||||
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
AlwaysPresent = true;
|
||||
Alpha = 0;
|
||||
|
||||
maskContainer.MaskSelected += onSelected;
|
||||
maskContainer.MaskDeselected += onDeselected;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
@ -51,58 +58,34 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tracks a selectable <see cref="HitObjectMask"/>.
|
||||
/// </summary>
|
||||
/// <param name="mask">The <see cref="HitObjectMask"/> to track.</param>
|
||||
public void AddMask(HitObjectMask mask)
|
||||
#region User Input Handling
|
||||
|
||||
// Only handle input on selectable or selected masks
|
||||
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => selectableMasks.Concat(selectedMasks).Any(m => m.ReceiveMouseInputAt(screenSpacePos));
|
||||
|
||||
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
||||
{
|
||||
mask.Selected += onSelected;
|
||||
mask.Deselected += onDeselected;
|
||||
mask.SingleSelectionRequested += onSingleSelectionRequested;
|
||||
}
|
||||
if (selectedMasks.Any(m => m.ReceiveMouseInputAt(state.Mouse.NativeState.Position)))
|
||||
return true;
|
||||
|
||||
/// <summary>
|
||||
/// Stops tracking a <see cref="HitObjectMask"/>.
|
||||
/// </summary>
|
||||
/// <param name="mask">The <see cref="HitObjectMask"/> to stop tracking.</param>
|
||||
public void RemoveMask(HitObjectMask mask)
|
||||
{
|
||||
mask.Selected -= onSelected;
|
||||
mask.Deselected -= onDeselected;
|
||||
mask.SingleSelectionRequested -= onSingleSelectionRequested;
|
||||
}
|
||||
DeselectAll();
|
||||
selectableMasks.First(m => m.ReceiveMouseInputAt(state.Mouse.NativeState.Position)).Select();
|
||||
|
||||
private void onSelected(HitObjectMask mask) => selectedMasks.Add(mask);
|
||||
|
||||
private void onDeselected(HitObjectMask mask)
|
||||
{
|
||||
selectedMasks.Remove(mask);
|
||||
|
||||
// We don't want to update visibility if > 0, since we may be deselecting masks during drag-selection
|
||||
if (selectedMasks.Count == 0)
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
private void onSingleSelectionRequested(HitObjectMask mask)
|
||||
{
|
||||
selectedMasks.Add(mask);
|
||||
UpdateVisibility();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only handle input on the selected masks
|
||||
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => selectedMasks.Any(m => m.ReceiveMouseInputAt(screenSpacePos));
|
||||
|
||||
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) => true;
|
||||
|
||||
protected override bool OnClick(InputState state)
|
||||
{
|
||||
if (state.Mouse.NativeState.PositionMouseDown == null)
|
||||
throw new InvalidOperationException("Click event received without a mouse down position.");
|
||||
if (selectedMasks.Count == 1)
|
||||
return true;
|
||||
|
||||
// We handled mousedown, but if the mouse has been clicked and not dragged, select the mask which would've handled the mouse down
|
||||
// A mousedown event is triggered such that a single selection is requested
|
||||
selectedMasks.First(m => m.ReceiveMouseInputAt(state.Mouse.NativeState.PositionMouseDown.Value)).TriggerOnMouseDown(state);
|
||||
var toSelect = selectedMasks.First(m => m.ReceiveMouseInputAt(state.Mouse.NativeState.Position));
|
||||
|
||||
DeselectAll();
|
||||
toSelect.Select();
|
||||
|
||||
UpdateVisibility();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -127,10 +110,32 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
|
||||
protected override bool OnDragEnd(InputState state) => true;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Selection Handling
|
||||
|
||||
private void onSelected(HitObjectMask mask) => selectedMasks.Add(mask);
|
||||
|
||||
private void onDeselected(HitObjectMask mask)
|
||||
{
|
||||
selectedMasks.Remove(mask);
|
||||
|
||||
// We don't want to update visibility if > 0, since we may be deselecting masks during drag-selection
|
||||
if (selectedMasks.Count == 0)
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deselects all selected <see cref="HitObjectMask"/>s.
|
||||
/// </summary>
|
||||
public void DeselectAll() => selectedMasks.ToList().ForEach(m => m.Deselect());
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Updates whether this <see cref="SelectionBox"/> is visible.
|
||||
/// </summary>
|
||||
public void UpdateVisibility()
|
||||
internal void UpdateVisibility()
|
||||
{
|
||||
if (selectedMasks.Count > 0)
|
||||
Show();
|
||||
@ -145,8 +150,6 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
if (selectedMasks.Count == 0)
|
||||
return;
|
||||
|
||||
// Todo: We might need to optimise this
|
||||
|
||||
// Move the rectangle to cover the hitobjects
|
||||
var topLeft = new Vector2(float.MaxValue, float.MaxValue);
|
||||
var bottomRight = new Vector2(float.MinValue, float.MinValue);
|
||||
@ -165,5 +168,13 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
|
||||
box.Size = bottomRight - topLeft;
|
||||
box.Position = topLeft;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
base.Dispose(isDisposing);
|
||||
|
||||
maskContainer.MaskSelected -= onSelected;
|
||||
maskContainer.MaskDeselected -= onDeselected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user