1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:33:05 +08:00

Merge remote-tracking branch 'refs/remotes/ppy/master' into comments-vote-pill

This commit is contained in:
Andrei Zavatski 2019-10-24 17:38:50 +03:00
commit 4f79ac8095
4 changed files with 201 additions and 122 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Rulesets.Edit; using osu.Game.Rulesets.Edit;
@ -28,6 +29,7 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
private readonly List<Segment> segments = new List<Segment>(); private readonly List<Segment> segments = new List<Segment>();
private Vector2 cursor; private Vector2 cursor;
private InputManager inputManager;
private PlacementState state; private PlacementState state;
@ -52,6 +54,12 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
setState(PlacementState.Initial); setState(PlacementState.Initial);
} }
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
public override void UpdatePosition(Vector2 screenSpacePosition) public override void UpdatePosition(Vector2 screenSpacePosition)
{ {
switch (state) switch (state)
@ -61,7 +69,9 @@ namespace osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders
break; break;
case PlacementState.Body: case PlacementState.Body:
cursor = ToLocalSpace(screenSpacePosition) - HitObject.Position; // The given screen-space position may have been externally snapped, but the unsnapped position from the input manager
// is used instead since snapping control points doesn't make much sense
cursor = ToLocalSpace(inputManager.CurrentState.Mouse.Position) - HitObject.Position;
break; break;
} }
} }

View File

@ -6,8 +6,6 @@ using osu.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Primitives;
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osuTK; using osuTK;
@ -29,27 +27,11 @@ namespace osu.Game.Rulesets.Edit
/// </summary> /// </summary>
public event Action<SelectionBlueprint> Deselected; public event Action<SelectionBlueprint> Deselected;
/// <summary>
/// Invoked when this <see cref="SelectionBlueprint"/> has requested selection.
/// Will fire even if already selected. Does not actually perform selection.
/// </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> /// <summary>
/// The <see cref="DrawableHitObject"/> which this <see cref="SelectionBlueprint"/> applies to. /// The <see cref="DrawableHitObject"/> which this <see cref="SelectionBlueprint"/> applies to.
/// </summary> /// </summary>
public readonly DrawableHitObject DrawableObject; 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; protected override bool ShouldBeAlive => (DrawableObject.IsAlive && DrawableObject.IsPresent) || State == SelectionState.Selected;
public override bool HandlePositionalInput => ShouldBeAlive; public override bool HandlePositionalInput => ShouldBeAlive;
public override bool RemoveWhenNotAlive => false; public override bool RemoveWhenNotAlive => false;
@ -109,45 +91,6 @@ namespace osu.Game.Rulesets.Edit
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => DrawableObject.ReceivePositionalInputAt(screenSpacePos); public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => DrawableObject.ReceivePositionalInputAt(screenSpacePos);
private bool selectionRequested;
protected override bool OnMouseDown(MouseDownEvent e)
{
selectionRequested = false;
if (State == SelectionState.NotSelected)
{
SelectionRequested?.Invoke(this, e.CurrentState);
selectionRequested = true;
}
return IsSelected;
}
protected override bool OnClick(ClickEvent e)
{
if (State == SelectionState.Selected && !selectionRequested)
{
selectionRequested = true;
SelectionRequested?.Invoke(this, e.CurrentState);
return true;
}
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> /// <summary>
/// The screen-space point that causes this <see cref="SelectionBlueprint"/> to be selected. /// The screen-space point that causes this <see cref="SelectionBlueprint"/> to be selected.
/// </summary> /// </summary>

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -10,7 +11,6 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Primitives;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Rulesets.Edit; using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Tools; using osu.Game.Rulesets.Edit.Tools;
using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects;
@ -23,6 +23,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
public event Action<IEnumerable<HitObject>> SelectionChanged; public event Action<IEnumerable<HitObject>> SelectionChanged;
private DragBox dragBox;
private SelectionBlueprintContainer selectionBlueprints; private SelectionBlueprintContainer selectionBlueprints;
private Container<PlacementBlueprint> placementBlueprintContainer; private Container<PlacementBlueprint> placementBlueprintContainer;
private PlacementBlueprint currentPlacement; private PlacementBlueprint currentPlacement;
@ -46,12 +47,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
selectionHandler = composer.CreateSelectionHandler(); selectionHandler = composer.CreateSelectionHandler();
selectionHandler.DeselectAll = deselectAll; selectionHandler.DeselectAll = deselectAll;
var dragBox = new DragBox(select);
dragBox.DragEnd += () => selectionHandler.UpdateVisibility();
InternalChildren = new[] InternalChildren = new[]
{ {
dragBox, dragBox = new DragBox(select),
selectionHandler, selectionHandler,
selectionBlueprints = new SelectionBlueprintContainer { RelativeSizeAxes = Axes.Both }, selectionBlueprints = new SelectionBlueprintContainer { RelativeSizeAxes = Axes.Both },
placementBlueprintContainer = new Container<PlacementBlueprint> { RelativeSizeAxes = Axes.Both }, placementBlueprintContainer = new Container<PlacementBlueprint> { RelativeSizeAxes = Axes.Both },
@ -91,6 +89,86 @@ namespace osu.Game.Screens.Edit.Compose.Components
} }
} }
protected override bool OnMouseDown(MouseDownEvent e)
{
beginClickSelection(e);
return true;
}
protected override bool OnClick(ClickEvent e)
{
// Deselection should only occur if no selected blueprints are hovered
// A special case for when a blueprint was selected via this click is added since OnClick() may occur outside the hitobject and should not trigger deselection
if (endClickSelection() || selectionHandler.SelectedBlueprints.Any(b => b.IsHovered))
return true;
deselectAll();
return true;
}
protected override bool OnMouseUp(MouseUpEvent e)
{
// Special case for when a drag happened instead of a click
Schedule(() => endClickSelection());
return true;
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
if (currentPlacement != null)
{
updatePlacementPosition(e.ScreenSpaceMousePosition);
return true;
}
return base.OnMouseMove(e);
}
protected override bool OnDragStart(DragStartEvent e)
{
if (!beginSelectionMovement())
{
dragBox.UpdateDrag(e);
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()
{
base.Update();
if (currentPlacement != null)
{
if (composer.CursorInPlacementArea)
currentPlacement.State = PlacementState.Shown;
else if (currentPlacement?.PlacementBegun == false)
currentPlacement.State = PlacementState.Hidden;
}
}
#region Blueprint Addition/Removal
private void addBlueprintFor(HitObject hitObject) private void addBlueprintFor(HitObject hitObject)
{ {
var drawable = composer.HitObjects.FirstOrDefault(d => d.HitObject == hitObject); var drawable = composer.HitObjects.FirstOrDefault(d => d.HitObject == hitObject);
@ -110,8 +188,6 @@ namespace osu.Game.Screens.Edit.Compose.Components
blueprint.Selected -= onBlueprintSelected; blueprint.Selected -= onBlueprintSelected;
blueprint.Deselected -= onBlueprintDeselected; blueprint.Deselected -= onBlueprintDeselected;
blueprint.SelectionRequested -= onSelectionRequested;
blueprint.DragRequested -= onDragRequested;
selectionBlueprints.Remove(blueprint); selectionBlueprints.Remove(blueprint);
} }
@ -126,43 +202,13 @@ namespace osu.Game.Screens.Edit.Compose.Components
blueprint.Selected += onBlueprintSelected; blueprint.Selected += onBlueprintSelected;
blueprint.Deselected += onBlueprintDeselected; blueprint.Deselected += onBlueprintDeselected;
blueprint.SelectionRequested += onSelectionRequested;
blueprint.DragRequested += onDragRequested;
selectionBlueprints.Add(blueprint); selectionBlueprints.Add(blueprint);
} }
private void removeBlueprintFor(DrawableHitObject hitObject) => removeBlueprintFor(hitObject.HitObject); #endregion
protected override bool OnClick(ClickEvent e) #region Placement
{
deselectAll();
return true;
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
if (currentPlacement != null)
{
updatePlacementPosition(e.ScreenSpaceMousePosition);
return true;
}
return base.OnMouseMove(e);
}
protected override void Update()
{
base.Update();
if (currentPlacement != null)
{
if (composer.CursorInPlacementArea)
currentPlacement.State = PlacementState.Shown;
else if (currentPlacement?.PlacementBegun == false)
currentPlacement.State = PlacementState.Hidden;
}
}
/// <summary> /// <summary>
/// Refreshes the current placement tool. /// Refreshes the current placement tool.
@ -191,6 +237,47 @@ namespace osu.Game.Screens.Edit.Compose.Components
currentPlacement.UpdatePosition(snappedScreenSpacePosition); currentPlacement.UpdatePosition(snappedScreenSpacePosition);
} }
#endregion
#region Selection
/// <summary>
/// Whether a blueprint was selected by a previous click event.
/// </summary>
private bool clickSelectionBegan;
/// <summary>
/// Attempts to select any hovered blueprints.
/// </summary>
/// <param name="e">The input event that triggered this selection.</param>
private void beginClickSelection(UIEvent e)
{
Debug.Assert(!clickSelectionBegan);
foreach (SelectionBlueprint blueprint in selectionBlueprints.AliveBlueprints)
{
if (blueprint.IsHovered)
{
selectionHandler.HandleSelectionRequested(blueprint, e.CurrentState);
clickSelectionBegan = true;
break;
}
}
}
/// <summary>
/// Finishes the current blueprint selection.
/// </summary>
/// <returns>Whether a click selection was active.</returns>
private bool endClickSelection()
{
if (!clickSelectionBegan)
return false;
clickSelectionBegan = false;
return true;
}
/// <summary> /// <summary>
/// Select all masks in a given rectangle selection area. /// Select all masks in a given rectangle selection area.
/// </summary> /// </summary>
@ -227,24 +314,80 @@ namespace osu.Game.Screens.Edit.Compose.Components
SelectionChanged?.Invoke(selectionHandler.SelectedHitObjects); SelectionChanged?.Invoke(selectionHandler.SelectedHitObjects);
} }
private void onSelectionRequested(SelectionBlueprint blueprint, InputState state) => selectionHandler.HandleSelectionRequested(blueprint, state); #endregion
private void onDragRequested(SelectionBlueprint blueprint, DragEvent dragEvent) #region Selection Movement
private Vector2? screenSpaceMovementStartPosition;
private SelectionBlueprint movementBlueprint;
/// <summary>
/// Attempts to begin the movement of any selected blueprints.
/// </summary>
/// <returns>Whether movement began.</returns>
private bool beginSelectionMovement()
{ {
HitObject draggedObject = blueprint.DrawableObject.HitObject; Debug.Assert(movementBlueprint == null);
Vector2 movePosition = blueprint.ScreenSpaceMovementStartPosition + dragEvent.ScreenSpaceMousePosition - dragEvent.ScreenSpaceMouseDownPosition; // Any selected blueprint that is hovered can begin the movement of the group, however only the earliest hitobject is used for movement
// A special case is added for when a click selection occurred before the drag
if (!clickSelectionBegan && !selectionHandler.SelectedBlueprints.Any(b => b.IsHovered))
return false;
// Movement is tracked from the blueprint of the earliest hitobject, since it only makes sense to distance snap from that hitobject
movementBlueprint = selectionHandler.SelectedBlueprints.OrderBy(b => b.DrawableObject.HitObject.StartTime).First();
screenSpaceMovementStartPosition = movementBlueprint.DrawableObject.ToScreenSpace(movementBlueprint.DrawableObject.OriginPosition);
return true;
}
/// <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)
return false;
Debug.Assert(screenSpaceMovementStartPosition != null);
Vector2 startPosition = screenSpaceMovementStartPosition.Value;
HitObject draggedObject = movementBlueprint.DrawableObject.HitObject;
// The final movement position, relative to screenSpaceMovementStartPosition
Vector2 movePosition = startPosition + e.ScreenSpaceMousePosition - e.ScreenSpaceMouseDownPosition;
Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition)); Vector2 snappedPosition = composer.GetSnappedPosition(ToLocalSpace(movePosition));
// Move the hitobjects // Move the hitobjects
selectionHandler.HandleMovement(new MoveSelectionEvent(blueprint, blueprint.ScreenSpaceMovementStartPosition, ToScreenSpace(snappedPosition))); selectionHandler.HandleMovement(new MoveSelectionEvent(movementBlueprint, startPosition, ToScreenSpace(snappedPosition)));
// Apply the start time at the newly snapped-to position // Apply the start time at the newly snapped-to position
double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime; double offset = composer.GetSnappedTime(draggedObject.StartTime, snappedPosition) - draggedObject.StartTime;
foreach (HitObject obj in selectionHandler.SelectedHitObjects) foreach (HitObject obj in selectionHandler.SelectedHitObjects)
obj.StartTime += offset; obj.StartTime += offset;
return true;
} }
/// <summary>
/// Finishes the current movement of selected blueprints.
/// </summary>
/// <returns>Whether a movement was active.</returns>
private bool finishSelectionMovement()
{
if (movementBlueprint == null)
return false;
screenSpaceMovementStartPosition = null;
movementBlueprint = null;
return true;
}
#endregion
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
base.Dispose(isDisposing); base.Dispose(isDisposing);
@ -258,6 +401,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
private class SelectionBlueprintContainer : Container<SelectionBlueprint> private class SelectionBlueprintContainer : Container<SelectionBlueprint>
{ {
public IEnumerable<SelectionBlueprint> AliveBlueprints => AliveInternalChildren.Cast<SelectionBlueprint>();
protected override int Compare(Drawable x, Drawable y) protected override int Compare(Drawable x, Drawable y)
{ {
if (!(x is SelectionBlueprint xBlueprint) || !(y is SelectionBlueprint yBlueprint)) if (!(x is SelectionBlueprint xBlueprint) || !(y is SelectionBlueprint yBlueprint))

View File

@ -19,11 +19,6 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
private readonly Action<RectangleF> performSelection; private readonly Action<RectangleF> performSelection;
/// <summary>
/// Invoked when the drag selection has finished.
/// </summary>
public event Action DragEnd;
private Drawable box; private Drawable box;
/// <summary> /// <summary>
@ -55,13 +50,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
}; };
} }
protected override bool OnDragStart(DragStartEvent e) public void UpdateDrag(MouseButtonEvent e)
{
this.FadeIn(250, Easing.OutQuint);
return true;
}
protected override bool OnDrag(DragEvent e)
{ {
var dragPosition = e.ScreenSpaceMousePosition; var dragPosition = e.ScreenSpaceMousePosition;
var dragStartPosition = e.ScreenSpaceMouseDownPosition; var dragStartPosition = e.ScreenSpaceMouseDownPosition;
@ -78,14 +67,6 @@ namespace osu.Game.Screens.Edit.Compose.Components
box.Size = bottomRight - topLeft; box.Size = bottomRight - topLeft;
performSelection?.Invoke(dragRectangle); performSelection?.Invoke(dragRectangle);
return true;
}
protected override bool OnDragEnd(DragEndEvent e)
{
this.FadeOut(250, Easing.OutQuint);
DragEnd?.Invoke();
return true;
} }
} }
} }