Merge pull request #16328 from peppy/editor-flip-over-origin

Change editor flip hotkeys to perform flips around origin
This commit is contained in:
Bartłomiej Dach 2022-01-06 10:26:14 +01:00 committed by GitHub
commit a9e4a0fd61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 84 additions and 32 deletions

View File

@ -52,16 +52,25 @@ public override bool HandleMovement(MoveSelectionEvent<HitObject> moveEvent)
return true;
}
public override bool HandleFlip(Direction direction)
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)
changed |= handleFlip(selectionRange, catchObject);
changed |= handleFlip(selectionRange, catchObject, flipOverOrigin);
});
return changed;
}
@ -116,7 +125,7 @@ private float limitMovement(float deltaX, IEnumerable<HitObject> movingObjects)
return Math.Clamp(deltaX, lowerBound, upperBound);
}
private bool handleFlip(PositionRange selectionRange, CatchHitObject hitObject)
private bool handleFlip(PositionRange selectionRange, CatchHitObject hitObject, bool flipOverOrigin)
{
switch (hitObject)
{
@ -124,7 +133,7 @@ private bool handleFlip(PositionRange selectionRange, CatchHitObject hitObject)
return false;
case JuiceStream juiceStream:
juiceStream.OriginalX = selectionRange.GetFlippedPosition(juiceStream.OriginalX);
juiceStream.OriginalX = getFlippedPosition(juiceStream.OriginalX);
foreach (var point in juiceStream.Path.ControlPoints)
point.Position *= new Vector2(-1, 1);
@ -133,9 +142,11 @@ private bool handleFlip(PositionRange selectionRange, CatchHitObject hitObject)
return true;
default:
hitObject.OriginalX = selectionRange.GetFlippedPosition(hitObject.OriginalX);
hitObject.OriginalX = getFlippedPosition(hitObject.OriginalX);
return true;
}
float getFlippedPosition(float original) => flipOverOrigin ? CatchPlayfield.WIDTH - original : selectionRange.GetFlippedPosition(original);
}
}
}

View File

@ -10,6 +10,7 @@
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
@ -84,18 +85,28 @@ public override bool HandleReverse()
return true;
}
public override bool HandleFlip(Direction direction)
public override bool HandleFlip(Direction direction, bool flipOverOrigin)
{
var hitObjects = selectedMovableObjects;
var selectedObjectsQuad = getSurroundingQuad(hitObjects);
var flipQuad = flipOverOrigin ? new Quad(0, 0, OsuPlayfield.BASE_SIZE.X, OsuPlayfield.BASE_SIZE.Y) : getSurroundingQuad(hitObjects);
bool didFlip = false;
foreach (var h in hitObjects)
{
h.Position = GetFlippedPosition(direction, selectedObjectsQuad, h.Position);
var flippedPosition = GetFlippedPosition(direction, flipQuad, h.Position);
if (!Precision.AlmostEquals(flippedPosition, h.Position))
{
h.Position = flippedPosition;
didFlip = true;
}
if (h is Slider slider)
{
didFlip = true;
foreach (var point in slider.Path.ControlPoints)
{
point.Position = new Vector2(
@ -106,7 +117,7 @@ public override bool HandleFlip(Direction direction)
}
}
return true;
return didFlip;
}
public override bool HandleScale(Vector2 scale, Anchor reference)

View File

@ -77,6 +77,8 @@ protected override void LoadComplete()
new KeyBinding(new[] { InputKey.K }, GlobalAction.EditorNudgeRight),
new KeyBinding(new[] { InputKey.G }, GlobalAction.EditorCycleGridDisplayMode),
new KeyBinding(new[] { InputKey.F5 }, GlobalAction.EditorTestGameplay),
new KeyBinding(new[] { InputKey.Control, InputKey.H }, GlobalAction.EditorFlipHorizontally),
new KeyBinding(new[] { InputKey.Control, InputKey.J }, GlobalAction.EditorFlipVertically),
};
public IEnumerable<KeyBinding> InGameKeyBindings => new[]
@ -292,6 +294,12 @@ public enum GlobalAction
EditorCycleGridDisplayMode,
[LocalisableDescription(typeof(GlobalActionKeyBindingStrings), nameof(GlobalActionKeyBindingStrings.EditorTestGameplay))]
EditorTestGameplay
EditorTestGameplay,
[LocalisableDescription(typeof(GlobalActionKeyBindingStrings), nameof(GlobalActionKeyBindingStrings.EditorFlipHorizontally))]
EditorFlipHorizontally,
[LocalisableDescription(typeof(GlobalActionKeyBindingStrings), nameof(GlobalActionKeyBindingStrings.EditorFlipVertically))]
EditorFlipVertically,
}
}

View File

@ -229,6 +229,16 @@ public static class GlobalActionKeyBindingStrings
/// </summary>
public static LocalisableString EditorNudgeRight => new TranslatableString(getKey(@"editor_nudge_right"), @"Nudge selection right");
/// <summary>
/// "Flip selection horizontally"
/// </summary>
public static LocalisableString EditorFlipHorizontally => new TranslatableString(getKey(@"editor_flip_horizontally"), @"Flip selection horizontally");
/// <summary>
/// "Flip selection vertically"
/// </summary>
public static LocalisableString EditorFlipVertically => new TranslatableString(getKey(@"editor_flip_vertically"), @"Flip selection vertically");
/// <summary>
/// "Toggle skin editor"
/// </summary>

View File

@ -21,7 +21,7 @@ public class SelectionBox : CompositeDrawable
public Func<float, bool> OnRotation;
public Func<Vector2, Anchor, bool> OnScale;
public Func<Direction, bool> OnFlip;
public Func<Direction, bool, bool> OnFlip;
public Func<bool> OnReverse;
public Action OperationStarted;
@ -174,12 +174,6 @@ bool runOperationFromHotkey(Func<bool> operation)
{
case Key.G:
return CanReverse && runOperationFromHotkey(OnReverse);
case Key.H:
return CanFlipX && runOperationFromHotkey(() => OnFlip?.Invoke(Direction.Horizontal) ?? false);
case Key.J:
return CanFlipY && runOperationFromHotkey(() => OnFlip?.Invoke(Direction.Vertical) ?? false);
}
return base.OnKeyDown(e);
@ -287,12 +281,12 @@ private void addXScaleComponents()
private void addXFlipComponents()
{
addButton(FontAwesome.Solid.ArrowsAltH, "Flip horizontally (Ctrl-H)", () => OnFlip?.Invoke(Direction.Horizontal));
addButton(FontAwesome.Solid.ArrowsAltH, "Flip horizontally", () => OnFlip?.Invoke(Direction.Horizontal, false));
}
private void addYFlipComponents()
{
addButton(FontAwesome.Solid.ArrowsAltV, "Flip vertically (Ctrl-J)", () => OnFlip?.Invoke(Direction.Vertical));
addButton(FontAwesome.Solid.ArrowsAltV, "Flip vertically", () => OnFlip?.Invoke(Direction.Vertical, false));
}
private void addButton(IconUsage icon, string tooltip, Action action)

View File

@ -17,6 +17,7 @@
using osu.Framework.Utils;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osuTK;
using osuTK.Input;
@ -26,7 +27,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// <summary>
/// A component which outlines items and handles movement of selections.
/// </summary>
public abstract class SelectionHandler<T> : CompositeDrawable, IKeyBindingHandler<PlatformAction>, IHasContextMenu
public abstract class SelectionHandler<T> : CompositeDrawable, IKeyBindingHandler<PlatformAction>, IKeyBindingHandler<GlobalAction>, IHasContextMenu
{
/// <summary>
/// The currently selected blueprints.
@ -127,9 +128,10 @@ protected virtual void OnOperationEnded()
/// <summary>
/// Handles the selected items being flipped.
/// </summary>
/// <param name="direction">The direction to flip</param>
/// <param name="direction">The direction to flip.</param>
/// <param name="flipOverOrigin">Whether the flip operation should be global to the playfield's origin or local to the selected pattern.</param>
/// <returns>Whether any items could be flipped.</returns>
public virtual bool HandleFlip(Direction direction) => false;
public virtual bool HandleFlip(Direction direction, bool flipOverOrigin) => false;
/// <summary>
/// Handles the selected items being reversed pattern-wise.
@ -137,6 +139,27 @@ protected virtual void OnOperationEnded()
/// <returns>Whether any items could be reversed.</returns>
public virtual bool HandleReverse() => false;
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat)
return false;
switch (e.Action)
{
case GlobalAction.EditorFlipHorizontally:
return HandleFlip(Direction.Horizontal, true);
case GlobalAction.EditorFlipVertically:
return HandleFlip(Direction.Vertical, true);
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
{
switch (e.Action)

View File

@ -7,7 +7,6 @@
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
@ -17,7 +16,7 @@
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
{
internal class TimelineSelectionHandler : EditorSelectionHandler, IKeyBindingHandler<GlobalAction>
internal class TimelineSelectionHandler : EditorSelectionHandler
{
[Resolved]
private Timeline timeline { get; set; }
@ -27,7 +26,7 @@ internal class TimelineSelectionHandler : EditorSelectionHandler, IKeyBindingHan
// for now we always allow movement. snapping is provided by the Timeline's "distance" snap implementation
public override bool HandleMovement(MoveSelectionEvent<HitObject> moveEvent) => true;
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
@ -40,11 +39,7 @@ public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
return base.OnPressed(e);
}
/// <summary>

View File

@ -126,7 +126,7 @@ public override bool HandleScale(Vector2 scale, Anchor anchor)
return true;
}
public override bool HandleFlip(Direction direction)
public override bool HandleFlip(Direction direction, bool flipOverOrigin)
{
var selectionQuad = getSelectionQuad();
Vector2 scaleFactor = direction == Direction.Horizontal ? new Vector2(-1, 1) : new Vector2(1, -1);
@ -135,7 +135,7 @@ public override bool HandleFlip(Direction direction)
{
var drawableItem = (Drawable)b.Item;
var flippedPosition = GetFlippedPosition(direction, selectionQuad, b.ScreenSpaceSelectionPoint);
var flippedPosition = GetFlippedPosition(direction, flipOverOrigin ? drawableItem.Parent.ScreenSpaceDrawQuad : selectionQuad, b.ScreenSpaceSelectionPoint);
updateDrawablePosition(drawableItem, flippedPosition);