Move selection drag events to BlueprintContainer

This commit is contained in:
smoogipoo 2019-10-23 18:58:15 +09:00
parent cef2318cf5
commit 714c89faa4
2 changed files with 42 additions and 44 deletions

View File

@ -35,21 +35,11 @@ public abstract class SelectionBlueprint : CompositeDrawable, IStateful<Selectio
/// </summary>
public event Action<SelectionBlueprint, InputState> SelectionRequested;
/// <summary>
/// Invoked when this <see cref="SelectionBlueprint"/> has requested drag.
/// </summary>
public event Action<SelectionBlueprint, DragEvent> DragRequested;
/// <summary>
/// The <see cref="DrawableHitObject"/> which this <see cref="SelectionBlueprint"/> applies to.
/// </summary>
public readonly DrawableHitObject DrawableObject;
/// <summary>
/// The screen-space position of <see cref="DrawableObject"/> prior to handling a movement event.
/// </summary>
internal Vector2 ScreenSpaceMovementStartPosition { get; private set; }
protected override bool ShouldBeAlive => (DrawableObject.IsAlive && DrawableObject.IsPresent) || State == SelectionState.Selected;
public override bool HandlePositionalInput => ShouldBeAlive;
public override bool RemoveWhenNotAlive => false;
@ -136,18 +126,6 @@ protected override bool OnClick(ClickEvent e)
return base.OnClick(e);
}
protected override bool OnDragStart(DragStartEvent e)
{
ScreenSpaceMovementStartPosition = DrawableObject.ToScreenSpace(DrawableObject.OriginPosition);
return true;
}
protected override bool OnDrag(DragEvent e)
{
DragRequested?.Invoke(this, e);
return true;
}
/// <summary>
/// The screen-space point that causes this <see cref="SelectionBlueprint"/> to be selected.
/// </summary>

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
@ -109,7 +110,6 @@ private void removeBlueprintFor(HitObject hitObject)
blueprint.Selected -= onBlueprintSelected;
blueprint.Deselected -= onBlueprintDeselected;
blueprint.SelectionRequested -= onSelectionRequested;
blueprint.DragRequested -= onDragRequested;
selectionBlueprints.Remove(blueprint);
}
@ -125,7 +125,6 @@ private void addBlueprintFor(DrawableHitObject hitObject)
blueprint.Selected += onBlueprintSelected;
blueprint.Deselected += onBlueprintDeselected;
blueprint.SelectionRequested += onSelectionRequested;
blueprint.DragRequested += onDragRequested;
selectionBlueprints.Add(blueprint);
}
@ -227,9 +226,18 @@ private void onBlueprintDeselected(SelectionBlueprint blueprint)
private void onSelectionRequested(SelectionBlueprint blueprint, InputState state) => selectionHandler.HandleSelectionRequested(blueprint, state);
private Vector2? screenSpaceMovementStartPosition;
private SelectionBlueprint movementBlueprint;
protected override bool OnDragStart(DragStartEvent e)
{
if (!selectionHandler.SelectedBlueprints.Any(b => b.IsHovered))
if (selectionHandler.SelectedBlueprints.Any(b => b.IsHovered))
{
// The earliest hitobject is used for drag-movement/snapping
movementBlueprint = selectionHandler.SelectedBlueprints.OrderBy(b => b.DrawableObject.HitObject.StartTime).First();
screenSpaceMovementStartPosition = movementBlueprint.DrawableObject.ToScreenSpace(movementBlueprint.DrawableObject.OriginPosition);
}
else
dragBox.FadeIn(250, Easing.OutQuint);
return true;
@ -237,34 +245,46 @@ protected override bool OnDragStart(DragStartEvent e)
protected override bool OnDrag(DragEvent e)
{
dragBox.UpdateDrag(e);
if (movementBlueprint != null)
{
Debug.Assert(screenSpaceMovementStartPosition != null);
Vector2 startPosition = screenSpaceMovementStartPosition.Value;
HitObject draggedObject = movementBlueprint.DrawableObject.HitObject;
Vector2 movePosition = startPosition + e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition;
Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition));
// Move the hitobjects
selectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprint, startPosition, ToScreenSpace(snappedPosition)));
// Apply the start time at the newly snapped-to position
double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime;
foreach (HitObject obj in selectionHandler.SelectedHitObjects)
obj.StartTime += offset;
}
else
dragBox.UpdateDrag(e);
return true;
}
protected override bool OnDragEnd(DragEndEvent e)
{
dragBox.FadeOut(250, Easing.OutQuint);
selectionHandler.UpdateVisibility();
if (movementBlueprint != null)
{
screenSpaceMovementStartPosition = null;
movementBlueprint = null;
}
else
{
dragBox.FadeOut(250, Easing.OutQuint);
selectionHandler.UpdateVisibility();
}
return true;
}
private void onDragRequested(SelectionBlueprint blueprint, DragEvent dragEvent)
{
HitObject draggedObject = blueprint.DrawableObject.HitObject;
Vector2 movePosition = blueprint.ScreenSpaceMovementStartPosition + dragEvent.ScreenSpaceMousePosition - dragEvent.ScreenSpaceMouseDownPosition;
Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition));
// Move the hitobjects
selectionHandler.HandleMovement(new MoveSelectionEvent(blueprint, blueprint.ScreenSpaceMovementStartPosition, ToScreenSpace(snappedPosition)));
// Apply the start time at the newly snapped-to position
double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime;
foreach (HitObject obj in selectionHandler.SelectedHitObjects)
obj.StartTime += offset;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);