2023-07-23 17:18:38 +00: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;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2023-07-30 17:39:30 +00:00
|
|
|
using osu.Framework.Allocation;
|
2023-07-23 17:18:38 +00:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
using osu.Game.Rulesets.Osu.Objects;
|
|
|
|
using osu.Game.Screens.Edit;
|
|
|
|
using osu.Game.Screens.Edit.Compose.Components;
|
|
|
|
using osu.Game.Utils;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.Edit
|
|
|
|
{
|
2023-07-30 17:39:30 +00:00
|
|
|
public partial class OsuSelectionRotationHandler : SelectionRotationHandler
|
2023-07-23 17:18:38 +00:00
|
|
|
{
|
2023-07-30 17:39:30 +00:00
|
|
|
[Resolved]
|
|
|
|
private IEditorChangeHandler? changeHandler { get; set; }
|
2023-07-23 17:18:38 +00:00
|
|
|
|
2023-07-30 17:39:30 +00:00
|
|
|
private BindableList<HitObject> selectedItems { get; } = new BindableList<HitObject>();
|
2023-07-23 17:18:38 +00:00
|
|
|
|
2023-07-30 17:39:30 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(EditorBeatmap editorBeatmap)
|
2023-07-23 17:18:38 +00:00
|
|
|
{
|
2023-07-30 17:39:30 +00:00
|
|
|
selectedItems.BindTo(editorBeatmap.SelectedHitObjects);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2023-07-23 17:18:38 +00:00
|
|
|
|
2023-07-30 17:39:30 +00:00
|
|
|
selectedItems.CollectionChanged += (_, __) => updateState();
|
|
|
|
updateState();
|
2023-07-23 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateState()
|
|
|
|
{
|
|
|
|
var quad = GeometryUtils.GetSurroundingQuad(selectedMovableObjects);
|
2024-05-29 07:40:29 +00:00
|
|
|
CanRotateAroundSelectionOrigin.Value = quad.Width > 0 || quad.Height > 0;
|
|
|
|
CanRotateAroundPlayfieldOrigin.Value = selectedMovableObjects.Any();
|
2023-07-23 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private OsuHitObject[]? objectsInRotation;
|
|
|
|
|
|
|
|
private Dictionary<OsuHitObject, Vector2>? originalPositions;
|
|
|
|
private Dictionary<IHasPath, Vector2[]>? originalPathControlPointPositions;
|
|
|
|
|
|
|
|
public override void Begin()
|
|
|
|
{
|
2024-06-25 13:01:43 +00:00
|
|
|
if (OperationInProgress.Value)
|
2023-07-23 17:18:38 +00:00
|
|
|
throw new InvalidOperationException($"Cannot {nameof(Begin)} a rotate operation while another is in progress!");
|
|
|
|
|
2024-06-25 13:01:43 +00:00
|
|
|
base.Begin();
|
|
|
|
|
2023-07-23 17:18:38 +00:00
|
|
|
changeHandler?.BeginChange();
|
|
|
|
|
|
|
|
objectsInRotation = selectedMovableObjects.ToArray();
|
2024-09-19 23:07:47 +00:00
|
|
|
DefaultOrigin = GeometryUtils.MinimumEnclosingCircle(objectsInRotation).Item1;
|
2023-07-23 17:18:38 +00:00
|
|
|
originalPositions = objectsInRotation.ToDictionary(obj => obj, obj => obj.Position);
|
|
|
|
originalPathControlPointPositions = objectsInRotation.OfType<IHasPath>().ToDictionary(
|
|
|
|
obj => obj,
|
|
|
|
obj => obj.Path.ControlPoints.Select(point => point.Position).ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Update(float rotation, Vector2? origin = null)
|
|
|
|
{
|
2024-06-25 13:01:43 +00:00
|
|
|
if (!OperationInProgress.Value)
|
2023-07-23 17:18:38 +00:00
|
|
|
throw new InvalidOperationException($"Cannot {nameof(Update)} a rotate operation without calling {nameof(Begin)} first!");
|
|
|
|
|
2024-09-19 23:07:47 +00:00
|
|
|
Debug.Assert(objectsInRotation != null && originalPositions != null && originalPathControlPointPositions != null && DefaultOrigin != null);
|
2023-07-23 17:18:38 +00:00
|
|
|
|
2024-09-19 23:07:47 +00:00
|
|
|
Vector2 actualOrigin = origin ?? DefaultOrigin.Value;
|
2023-07-23 17:18:38 +00:00
|
|
|
|
|
|
|
foreach (var ho in objectsInRotation)
|
|
|
|
{
|
|
|
|
ho.Position = GeometryUtils.RotatePointAroundOrigin(originalPositions[ho], actualOrigin, rotation);
|
|
|
|
|
|
|
|
if (ho is IHasPath withPath)
|
|
|
|
{
|
|
|
|
var originalPath = originalPathControlPointPositions[withPath];
|
|
|
|
|
|
|
|
for (int i = 0; i < withPath.Path.ControlPoints.Count; ++i)
|
|
|
|
withPath.Path.ControlPoints[i].Position = GeometryUtils.RotatePointAroundOrigin(originalPath[i], Vector2.Zero, rotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Commit()
|
|
|
|
{
|
2024-06-25 13:01:43 +00:00
|
|
|
if (!OperationInProgress.Value)
|
2023-07-23 17:18:38 +00:00
|
|
|
throw new InvalidOperationException($"Cannot {nameof(Commit)} a rotate operation without calling {nameof(Begin)} first!");
|
|
|
|
|
|
|
|
changeHandler?.EndChange();
|
|
|
|
|
2024-06-25 13:01:43 +00:00
|
|
|
base.Commit();
|
|
|
|
|
2023-07-23 17:18:38 +00:00
|
|
|
objectsInRotation = null;
|
|
|
|
originalPositions = null;
|
|
|
|
originalPathControlPointPositions = null;
|
2024-09-19 23:07:47 +00:00
|
|
|
DefaultOrigin = null;
|
2023-07-23 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 17:39:30 +00:00
|
|
|
private IEnumerable<OsuHitObject> selectedMovableObjects => selectedItems.Cast<OsuHitObject>()
|
2023-07-23 17:18:38 +00:00
|
|
|
.Where(h => h is not Spinner);
|
|
|
|
}
|
|
|
|
}
|