1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 11:47:24 +08:00
osu-lazer/osu.Game.Rulesets.Catch/Edit/Blueprints/Components/SelectionEditablePath.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
4.3 KiB
C#
Raw Normal View History

2021-07-19 21:15:35 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
2021-07-19 21:15:35 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
2021-07-19 21:15:35 +08:00
using osu.Framework.Input.Events;
using osu.Game.Graphics.UserInterface;
2021-07-19 21:15:35 +08:00
using osu.Game.Screens.Edit;
using osuTK;
using osuTK.Input;
namespace osu.Game.Rulesets.Catch.Edit.Blueprints.Components
{
public partial class SelectionEditablePath : EditablePath, IHasContextMenu
2021-07-19 21:15:35 +08:00
{
public MenuItem[] ContextMenuItems => getContextMenuItems().ToArray();
2021-07-19 21:15:35 +08:00
// To handle when the editor is scrolled while dragging.
private Vector2 dragStartPosition;
2023-01-15 22:11:59 +08:00
[Resolved]
private IEditorChangeHandler? changeHandler { get; set; }
2021-07-19 21:15:35 +08:00
public SelectionEditablePath(Func<float, double> positionToTime)
: base(positionToTime)
2021-07-19 21:15:35 +08:00
{
}
public void AddVertex(Vector2 relativePosition)
{
double time = Math.Max(0, PositionToTime(relativePosition.Y));
int index = AddVertex(time, relativePosition.X);
selectOnly(index);
}
2021-07-19 21:15:35 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => InternalChildren.Any(d => d.ReceivePositionalInputAt(screenSpacePos));
protected override bool OnMouseDown(MouseDownEvent e)
{
int index = getMouseTargetVertex(e.ScreenSpaceMouseDownPosition);
if (index == -1 || VertexStates[index].IsFixed)
2021-07-19 21:15:35 +08:00
return false;
if (e.Button == MouseButton.Left && e.ShiftPressed)
{
RemoveVertex(index);
return true;
}
2021-07-19 21:15:35 +08:00
if (e.ControlPressed)
VertexStates[index].IsSelected = !VertexStates[index].IsSelected;
else if (!VertexStates[index].IsSelected)
selectOnly(index);
2021-07-21 11:53:48 +08:00
// Don't inhibit right click, to show the context menu
2021-07-19 21:15:35 +08:00
return e.Button != MouseButton.Right;
}
protected override bool OnDragStart(DragStartEvent e)
{
int index = getMouseTargetVertex(e.ScreenSpaceMouseDownPosition);
if (index == -1 || VertexStates[index].IsFixed)
return false;
if (e.Button != MouseButton.Left)
return false;
2021-07-19 21:15:35 +08:00
dragStartPosition = ToRelativePosition(e.ScreenSpaceMouseDownPosition);
for (int i = 0; i < VertexCount; i++)
VertexStates[i].VertexBeforeChange = Vertices[i];
changeHandler?.BeginChange();
return true;
}
protected override void OnDrag(DragEvent e)
{
2021-07-21 11:59:42 +08:00
Vector2 mousePosition = ToRelativePosition(e.ScreenSpaceMousePosition);
double timeDelta = PositionToTime(mousePosition.Y) - PositionToTime(dragStartPosition.Y);
2021-07-19 21:15:35 +08:00
float xDelta = mousePosition.X - dragStartPosition.X;
MoveSelectedVertices(timeDelta, xDelta);
2021-07-19 21:15:35 +08:00
}
protected override void OnDragEnd(DragEndEvent e)
{
changeHandler?.EndChange();
}
private int getMouseTargetVertex(Vector2 screenSpacePosition)
{
for (int i = InternalChildren.Count - 1; i >= 0; i--)
{
if (i < VertexCount && InternalChildren[i].ReceivePositionalInputAt(screenSpacePosition))
return i;
}
return -1;
}
private IEnumerable<MenuItem> getContextMenuItems()
{
int selectedCount = VertexStates.Count(state => state.IsSelected);
if (selectedCount != 0)
yield return new OsuMenuItem($"Delete selected {(selectedCount == 1 ? "vertex" : $"{selectedCount} vertices")}", MenuItemType.Destructive, deleteSelectedVertices);
}
2021-07-19 21:15:35 +08:00
private void selectOnly(int index)
{
for (int i = 0; i < VertexCount; i++)
VertexStates[i].IsSelected = i == index;
}
private void deleteSelectedVertices()
{
for (int i = VertexCount - 1; i >= 0; i--)
{
if (VertexStates[i].IsSelected)
RemoveVertex(i);
}
}
2021-07-19 21:15:35 +08:00
}
}