1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 16:57:48 +08:00
osu-lazer/osu.Game.Rulesets.Catch/Edit/CatchSelectionHandler.cs

170 lines
6.8 KiB
C#
Raw Normal View History

// 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.Linq;
using osu.Framework.Allocation;
using osu.Game.Rulesets.Catch.Objects;
using osu.Game.Rulesets.Catch.UI;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.UI;
using osu.Game.Rulesets.UI.Scrolling;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
using Direction = osu.Framework.Graphics.Direction;
namespace osu.Game.Rulesets.Catch.Edit
{
2022-11-24 13:32:20 +08:00
public partial class CatchSelectionHandler : EditorSelectionHandler
{
protected ScrollingHitObjectContainer HitObjectContainer => (ScrollingHitObjectContainer)playfield.HitObjectContainer;
[Resolved]
private Playfield playfield { get; set; } = null!;
public override bool HandleMovement(MoveSelectionEvent<HitObject> moveEvent)
{
var blueprint = moveEvent.Blueprint;
Vector2 originalPosition = HitObjectContainer.ToLocalSpace(blueprint.ScreenSpaceSelectionPoint);
Vector2 targetPosition = HitObjectContainer.ToLocalSpace(blueprint.ScreenSpaceSelectionPoint + moveEvent.ScreenSpaceDelta);
float deltaX = targetPosition.X - originalPosition.X;
deltaX = limitMovement(deltaX, SelectedItems);
if (deltaX == 0)
{
2021-07-10 17:22:54 +08:00
// Even if there is no positional change, there may be a time change.
return true;
}
EditorBeatmap.PerformOnSelection(h =>
{
if (!(h is CatchHitObject catchObject)) return;
catchObject.OriginalX += deltaX;
2021-06-23 09:19:39 +08:00
// Move the nested hit objects to give an instant result before nested objects are recreated.
foreach (var nested in catchObject.NestedHitObjects.OfType<CatchHitObject>())
nested.OriginalX += deltaX;
});
return true;
}
2022-01-05 15:46:34 +08:00
public override bool HandleFlip(Direction direction, bool flipOverOrigin)
{
if (SelectedItems.Count == 0)
return false;
// This could be implemented in the future if there's a requirement for it.
if (direction == Direction.Vertical)
return false;
var selectionRange = CatchHitObjectUtils.GetPositionRange(SelectedItems);
bool changed = false;
EditorBeatmap.PerformOnSelection(h =>
{
if (h is CatchHitObject catchObject)
2022-01-05 15:46:34 +08:00
changed |= handleFlip(selectionRange, catchObject, flipOverOrigin);
});
return changed;
}
public override bool HandleReverse()
{
var hitObjects = EditorBeatmap.SelectedHitObjects
2024-03-28 17:12:27 +08:00
.OfType<CatchHitObject>()
.OrderBy(obj => obj.StartTime)
.ToList();
2024-03-25 15:19:14 +08:00
double selectionStartTime = SelectedItems.Min(h => h.StartTime);
double selectionEndTime = SelectedItems.Max(h => h.GetEndTime());
2024-03-28 17:12:27 +08:00
// the expectation is that even if the objects themselves are reversed temporally,
// the position of new combos in the selection should remain the same.
// preserve it for later before doing the reversal.
var newComboOrder = hitObjects.Select(obj => obj.NewCombo).ToList();
2024-03-25 15:19:14 +08:00
foreach (var h in hitObjects)
{
2024-03-25 15:19:14 +08:00
h.StartTime = selectionEndTime - (h.GetEndTime() - selectionStartTime);
2024-03-25 15:19:14 +08:00
if (h is JuiceStream juiceStream)
{
juiceStream.Path.Reverse(out Vector2 positionalOffset);
juiceStream.OriginalX += positionalOffset.X;
juiceStream.LegacyConvertedY += positionalOffset.Y;
EditorBeatmap.Update(juiceStream);
}
2024-03-25 15:19:14 +08:00
}
2024-03-28 17:12:27 +08:00
// re-order objects by start time again after reversing, and restore new combo flag positioning
hitObjects = hitObjects.OrderBy(obj => obj.StartTime).ToList();
2024-03-28 17:12:27 +08:00
for (int i = 0; i < hitObjects.Count; ++i)
hitObjects[i].NewCombo = newComboOrder[i];
2024-03-25 15:19:14 +08:00
return true;
}
protected override void OnSelectionChanged()
{
base.OnSelectionChanged();
var selectionRange = CatchHitObjectUtils.GetPositionRange(SelectedItems);
SelectionBox.CanFlipX = selectionRange.Length > 0 && SelectedItems.Any(h => h is CatchHitObject && !(h is BananaShower));
SelectionBox.CanReverse = SelectedItems.Count > 1 || SelectedItems.Any(h => h is JuiceStream);
}
/// <summary>
/// Limit positional movement of the objects by the constraint that moved objects should stay in bounds.
/// </summary>
/// <param name="deltaX">The positional movement.</param>
/// <param name="movingObjects">The objects to be moved.</param>
/// <returns>The positional movement with the restriction applied.</returns>
private float limitMovement(float deltaX, IEnumerable<HitObject> movingObjects)
{
var range = CatchHitObjectUtils.GetPositionRange(movingObjects);
// To make an object with position `x` stay in bounds after `deltaX` movement, `0 <= x + deltaX <= WIDTH` should be satisfied.
// Subtracting `x`, we get `-x <= deltaX <= WIDTH - x`.
// We only need to apply the inequality to extreme values of `x`.
float lowerBound = -range.Min;
float upperBound = CatchPlayfield.WIDTH - range.Max;
// The inequality may be unsatisfiable if the objects were already out of bounds.
// In that case, don't move objects at all.
2021-07-10 17:23:38 +08:00
if (lowerBound > upperBound)
return 0;
return Math.Clamp(deltaX, lowerBound, upperBound);
}
2022-01-05 15:46:34 +08:00
private bool handleFlip(PositionRange selectionRange, CatchHitObject hitObject, bool flipOverOrigin)
{
switch (hitObject)
{
2022-06-24 20:25:23 +08:00
case BananaShower:
return false;
case JuiceStream juiceStream:
2022-01-05 15:46:34 +08:00
juiceStream.OriginalX = getFlippedPosition(juiceStream.OriginalX);
foreach (var point in juiceStream.Path.ControlPoints)
point.Position *= new Vector2(-1, 1);
EditorBeatmap.Update(juiceStream);
return true;
default:
2022-01-05 15:46:34 +08:00
hitObject.OriginalX = getFlippedPosition(hitObject.OriginalX);
return true;
}
2022-01-05 15:46:34 +08:00
float getFlippedPosition(float original) => flipOverOrigin ? CatchPlayfield.WIDTH - original : selectionRange.GetFlippedPosition(original);
}
}
}