2021-06-22 09:39:32 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-07-08 17:49:32 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-06-22 10:39:04 +08:00
|
|
|
using System.Linq;
|
2021-06-22 09:39:32 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Game.Rulesets.Catch.Objects;
|
2021-07-08 17:49:32 +08:00
|
|
|
using osu.Game.Rulesets.Catch.UI;
|
2021-06-22 09:39:32 +08:00
|
|
|
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;
|
2021-07-21 14:47:16 +08:00
|
|
|
using Direction = osu.Framework.Graphics.Direction;
|
2021-06-22 09:39:32 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Catch.Edit
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class CatchSelectionHandler : EditorSelectionHandler
|
2021-06-22 09:39:32 +08:00
|
|
|
{
|
2021-06-23 09:18:44 +08:00
|
|
|
protected ScrollingHitObjectContainer HitObjectContainer => (ScrollingHitObjectContainer)playfield.HitObjectContainer;
|
|
|
|
|
2021-06-22 09:39:32 +08:00
|
|
|
[Resolved]
|
|
|
|
private Playfield playfield { get; set; }
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-07-09 11:58:08 +08:00
|
|
|
float deltaX = targetPosition.X - originalPosition.X;
|
2021-07-22 15:52:47 +08:00
|
|
|
deltaX = limitMovement(deltaX, SelectedItems);
|
2021-07-08 17:49:32 +08:00
|
|
|
|
|
|
|
if (deltaX == 0)
|
|
|
|
{
|
2021-07-10 17:22:54 +08:00
|
|
|
// Even if there is no positional change, there may be a time change.
|
2021-07-08 17:49:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:39:32 +08:00
|
|
|
EditorBeatmap.PerformOnSelection(h =>
|
|
|
|
{
|
2021-07-22 13:07:32 +08:00
|
|
|
if (!(h is CatchHitObject catchObject)) return;
|
2021-06-22 09:39:32 +08:00
|
|
|
|
2021-07-22 13:07:32 +08:00
|
|
|
catchObject.OriginalX += deltaX;
|
2021-06-22 10:39:04 +08:00
|
|
|
|
2021-06-23 09:19:39 +08:00
|
|
|
// Move the nested hit objects to give an instant result before nested objects are recreated.
|
2021-07-22 13:07:32 +08:00
|
|
|
foreach (var nested in catchObject.NestedHitObjects.OfType<CatchHitObject>())
|
2021-06-22 16:35:30 +08:00
|
|
|
nested.OriginalX += deltaX;
|
2021-06-22 09:39:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-08 17:49:32 +08:00
|
|
|
|
2022-01-05 15:46:34 +08:00
|
|
|
public override bool HandleFlip(Direction direction, bool flipOverOrigin)
|
2021-07-21 14:47:16 +08:00
|
|
|
{
|
2022-01-06 13:39:00 +08:00
|
|
|
if (SelectedItems.Count == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This could be implemented in the future if there's a requirement for it.
|
|
|
|
if (direction == Direction.Vertical)
|
2022-01-05 15:56:54 +08:00
|
|
|
return false;
|
|
|
|
|
2021-07-22 15:52:47 +08:00
|
|
|
var selectionRange = CatchHitObjectUtils.GetPositionRange(SelectedItems);
|
2021-07-21 14:47:16 +08:00
|
|
|
|
|
|
|
bool changed = false;
|
2022-01-05 15:56:54 +08:00
|
|
|
|
2021-07-21 14:47:16 +08:00
|
|
|
EditorBeatmap.PerformOnSelection(h =>
|
|
|
|
{
|
2021-07-22 13:07:32 +08:00
|
|
|
if (h is CatchHitObject catchObject)
|
2022-01-05 15:46:34 +08:00
|
|
|
changed |= handleFlip(selectionRange, catchObject, flipOverOrigin);
|
2021-07-21 14:47:16 +08:00
|
|
|
});
|
2022-01-05 15:56:54 +08:00
|
|
|
|
2021-07-21 14:47:16 +08:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2021-07-22 16:00:08 +08:00
|
|
|
public override bool HandleReverse()
|
|
|
|
{
|
|
|
|
double selectionStartTime = SelectedItems.Min(h => h.StartTime);
|
|
|
|
double selectionEndTime = SelectedItems.Max(h => h.GetEndTime());
|
|
|
|
|
|
|
|
EditorBeatmap.PerformOnSelection(hitObject =>
|
|
|
|
{
|
|
|
|
hitObject.StartTime = selectionEndTime - (hitObject.GetEndTime() - selectionStartTime);
|
|
|
|
|
|
|
|
if (hitObject is JuiceStream juiceStream)
|
|
|
|
{
|
|
|
|
juiceStream.Path.Reverse(out Vector2 positionalOffset);
|
|
|
|
juiceStream.OriginalX += positionalOffset.X;
|
|
|
|
juiceStream.LegacyConvertedY += positionalOffset.Y;
|
|
|
|
EditorBeatmap.Update(juiceStream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:47:16 +08:00
|
|
|
protected override void OnSelectionChanged()
|
|
|
|
{
|
|
|
|
base.OnSelectionChanged();
|
|
|
|
|
2021-07-22 15:52:47 +08:00
|
|
|
var selectionRange = CatchHitObjectUtils.GetPositionRange(SelectedItems);
|
|
|
|
SelectionBox.CanFlipX = selectionRange.Length > 0 && SelectedItems.Any(h => h is CatchHitObject && !(h is BananaShower));
|
2021-07-22 16:00:08 +08:00
|
|
|
SelectionBox.CanReverse = SelectedItems.Count > 1 || SelectedItems.Any(h => h is JuiceStream);
|
2021-07-21 14:47:16 +08:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:58:08 +08:00
|
|
|
/// <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)
|
|
|
|
{
|
2021-07-21 14:37:42 +08:00
|
|
|
var range = CatchHitObjectUtils.GetPositionRange(movingObjects);
|
2021-07-09 11:58:08 +08:00
|
|
|
// 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`.
|
2021-07-21 14:37:42 +08:00
|
|
|
float lowerBound = -range.Min;
|
|
|
|
float upperBound = CatchPlayfield.WIDTH - range.Max;
|
2021-07-09 11:58:08 +08:00
|
|
|
// 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)
|
2021-07-09 11:58:08 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Math.Clamp(deltaX, lowerBound, upperBound);
|
|
|
|
}
|
2021-07-21 14:47:16 +08:00
|
|
|
|
2022-01-05 15:46:34 +08:00
|
|
|
private bool handleFlip(PositionRange selectionRange, CatchHitObject hitObject, bool flipOverOrigin)
|
2021-07-21 14:47:16 +08:00
|
|
|
{
|
|
|
|
switch (hitObject)
|
|
|
|
{
|
2022-06-24 20:25:23 +08:00
|
|
|
case BananaShower:
|
2021-07-21 14:47:16 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case JuiceStream juiceStream:
|
2022-01-05 15:46:34 +08:00
|
|
|
juiceStream.OriginalX = getFlippedPosition(juiceStream.OriginalX);
|
2021-07-21 14:47:16 +08:00
|
|
|
|
|
|
|
foreach (var point in juiceStream.Path.ControlPoints)
|
2021-08-26 00:42:57 +08:00
|
|
|
point.Position *= new Vector2(-1, 1);
|
2021-07-21 14:47:16 +08:00
|
|
|
|
|
|
|
EditorBeatmap.Update(juiceStream);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
2022-01-05 15:46:34 +08:00
|
|
|
hitObject.OriginalX = getFlippedPosition(hitObject.OriginalX);
|
2021-07-21 14:47:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
2022-01-05 15:46:34 +08:00
|
|
|
|
|
|
|
float getFlippedPosition(float original) => flipOverOrigin ? CatchPlayfield.WIDTH - original : selectionRange.GetFlippedPosition(original);
|
2021-07-21 14:47:16 +08:00
|
|
|
}
|
2021-06-22 09:39:32 +08:00
|
|
|
}
|
|
|
|
}
|