Extract drag events into multiple methods

This commit is contained in:
smoogipoo 2019-10-24 15:11:54 +09:00
parent f128e99fb2
commit a07e5a269b

View File

@ -160,6 +160,33 @@ namespace osu.Game.Screens.Edit.Compose.Components
return base.OnMouseMove(e); return base.OnMouseMove(e);
} }
protected override bool OnDragStart(DragStartEvent e)
{
if (!beginSelectionMovement())
dragBox.FadeIn(250, Easing.OutQuint);
return true;
}
protected override bool OnDrag(DragEvent e)
{
if (!moveCurrentSelection(e))
dragBox.UpdateDrag(e);
return true;
}
protected override bool OnDragEnd(DragEndEvent e)
{
if (!finishSelectionMovement())
{
dragBox.FadeOut(250, Easing.OutQuint);
selectionHandler.UpdateVisibility();
}
return true;
}
protected override void Update() protected override void Update()
{ {
base.Update(); base.Update();
@ -239,58 +266,66 @@ namespace osu.Game.Screens.Edit.Compose.Components
private Vector2? screenSpaceMovementStartPosition; private Vector2? screenSpaceMovementStartPosition;
private SelectionBlueprint movementBlueprint; private SelectionBlueprint movementBlueprint;
protected override bool OnDragStart(DragStartEvent e) /// <summary>
/// Attempts to begin the movement of any selected blueprints.
/// </summary>
/// <returns>Whether movement began.</returns>
private bool beginSelectionMovement()
{ {
if (selectionHandler.SelectedBlueprints.Any(b => b.IsHovered)) Debug.Assert(movementBlueprint == null);
{
// The earliest hitobject is used for drag-movement/snapping // Any selected blueprints can begin the movement of the group, however only the earliest hitobject is used for movement
movementBlueprint = selectionHandler.SelectedBlueprints.OrderBy(b => b.DrawableObject.HitObject.StartTime).First(); if (!selectionHandler.SelectedBlueprints.Any(b => b.IsHovered))
screenSpaceMovementStartPosition = movementBlueprint.DrawableObject.ToScreenSpace(movementBlueprint.DrawableObject.OriginPosition); return false;
}
else // Movement is tracked from the blueprint of the earliest hitobject, since it only makes sense to distance snap from that hitobject
dragBox.FadeIn(250, Easing.OutQuint); movementBlueprint = selectionHandler.SelectedBlueprints.OrderBy(b => b.DrawableObject.HitObject.StartTime).First();
screenSpaceMovementStartPosition = movementBlueprint.DrawableObject.ToScreenSpace(movementBlueprint.DrawableObject.OriginPosition);
return true; return true;
} }
protected override bool OnDrag(DragEvent e) /// <summary>
/// Moves the current selected blueprints.
/// </summary>
/// <param name="e">The <see cref="DragEvent"/> defining the movement event.</param>
/// <returns>Whether a movement was active.</returns>
private bool moveCurrentSelection(DragEvent e)
{ {
if (movementBlueprint != null) if (movementBlueprint == null)
{ return false;
Debug.Assert(screenSpaceMovementStartPosition != null);
Vector2 startPosition = screenSpaceMovementStartPosition.Value; Debug.Assert(screenSpaceMovementStartPosition != null);
HitObject draggedObject = movementBlueprint.DrawableObject.HitObject;
Vector2 movePosition = startPosition + e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition; Vector2 startPosition = screenSpaceMovementStartPosition.Value;
Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition)); HitObject draggedObject = movementBlueprint.DrawableObject.HitObject;
// Move the hitobjects // The final movement position, relative to screenSpaceMovementStartPosition
selectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprint, startPosition, ToScreenSpace(snappedPosition))); Vector2 movePosition = startPosition + e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition;
Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition));
// Apply the start time at the newly snapped-to position // Move the hitobjects
double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime; selectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprint, startPosition, ToScreenSpace(snappedPosition)));
foreach (HitObject obj in selectionHandler.SelectedHitObjects)
obj.StartTime += offset; // Apply the start time at the newly snapped-to position
} double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime;
else foreach (HitObject obj in selectionHandler.SelectedHitObjects)
dragBox.UpdateDrag(e); obj.StartTime += offset;
return true; return true;
} }
protected override bool OnDragEnd(DragEndEvent e) /// <summary>
/// Finishes the current movement of selected blueprints.
/// </summary>
/// <returns>Whether a movement was active.</returns>
private bool finishSelectionMovement()
{ {
if (movementBlueprint != null) if (movementBlueprint == null)
{ return false;
screenSpaceMovementStartPosition = null;
movementBlueprint = null; screenSpaceMovementStartPosition = null;
} movementBlueprint = null;
else
{
dragBox.FadeOut(250, Easing.OutQuint);
selectionHandler.UpdateVisibility();
}
return true; return true;
} }