Skip to content

Commit

Permalink
Add global flip hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Jan 5, 2022
1 parent 13cce50 commit 866ae34
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 24 deletions.
12 changes: 7 additions & 5 deletions osu.Game.Rulesets.Catch/Edit/CatchSelectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public override bool HandleMovement(MoveSelectionEvent<HitObject> moveEvent)
return true;
}

public override bool HandleFlip(Direction direction)
public override bool HandleFlip(Direction direction, bool flipOverOrigin)
{
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;
}
Expand Down Expand Up @@ -116,15 +116,15 @@ 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)
{
case BananaShower _:
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);
Expand All @@ -133,9 +133,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);
}
}
}
7 changes: 4 additions & 3 deletions osu.Game.Rulesets.Osu/Edit/OsuSelectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -84,15 +85,15 @@ 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);

foreach (var h in hitObjects)
{
h.Position = GetFlippedPosition(direction, selectedObjectsQuad, h.Position);
h.Position = GetFlippedPosition(direction, flipQuad, h.Position);

if (h is Slider slider)
{
Expand Down
10 changes: 9 additions & 1 deletion osu.Game/Input/Bindings/GlobalActionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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,
}
}
10 changes: 10 additions & 0 deletions osu.Game/Localisation/GlobalActionKeyBindingStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 3 additions & 3 deletions osu.Game/Screens/Edit/Compose/Components/SelectionBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -281,12 +281,12 @@ private void addXScaleComponents()

private void addXFlipComponents()
{
addButton(FontAwesome.Solid.ArrowsAltH, "Flip horizontally", () => OnFlip?.Invoke(Direction.Horizontal));
addButton(FontAwesome.Solid.ArrowsAltH, "Flip horizontally", () => OnFlip?.Invoke(Direction.Horizontal, false));
}

private void addYFlipComponents()
{
addButton(FontAwesome.Solid.ArrowsAltV, "Flip vertically", () => OnFlip?.Invoke(Direction.Vertical));
addButton(FontAwesome.Solid.ArrowsAltV, "Flip vertically", () => OnFlip?.Invoke(Direction.Vertical, false));
}

private void addButton(IconUsage icon, string tooltip, Action action)
Expand Down
31 changes: 28 additions & 3 deletions osu.Game/Screens/Edit/Compose/Components/SelectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -127,16 +128,40 @@ 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.
/// </summary>
/// <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:
HandleFlip(Direction.Horizontal, true);
return true;

case GlobalAction.EditorFlipVertically:
HandleFlip(Direction.Vertical, true);
return true;
}

return false;
}

public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}

public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
{
switch (e.Action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }
Expand All @@ -27,8 +26,11 @@ 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)
{
// Importantly, we block the base call here.
// Other key operations will be handled by the composer view's SelectionHandler instead.

switch (e.Action)
{
case GlobalAction.EditorNudgeLeft:
Expand All @@ -43,10 +45,6 @@ public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
return false;
}

public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}

/// <summary>
/// Nudge the current selection by the specified multiple of beat divisor lengths,
/// based on the timing at the first object in the selection.
Expand Down
4 changes: 2 additions & 2 deletions osu.Game/Skinning/Editor/SkinSelectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ 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();
var selectionQuad = flipOverOrigin ? ScreenSpaceDrawQuad : getSelectionQuad();
Vector2 scaleFactor = direction == Direction.Horizontal ? new Vector2(-1, 1) : new Vector2(1, -1);

foreach (var b in SelectedBlueprints)
Expand Down

0 comments on commit 866ae34

Please sign in to comment.