Skip to content

Commit

Permalink
Migrate SelectionHandler to use SelectionRotationHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
bdach committed Jul 23, 2023
1 parent b951f19 commit 1399cce
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
1 change: 0 additions & 1 deletion osu.Game.Rulesets.Osu/Edit/OsuSelectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ protected override void OnSelectionChanged()

Quad quad = selectedMovableObjects.Length > 0 ? GeometryUtils.GetSurroundingQuad(selectedMovableObjects) : new Quad();

SelectionBox.CanRotate = quad.Width > 0 || quad.Height > 0;
SelectionBox.CanFlipX = SelectionBox.CanScaleX = quad.Width > 0;
SelectionBox.CanFlipY = SelectionBox.CanScaleY = quad.Height > 0;
SelectionBox.CanReverse = EditorBeatmap.SelectedHitObjects.Count > 1 || EditorBeatmap.SelectedHitObjects.Any(s => s is Slider);
Expand Down
41 changes: 17 additions & 24 deletions osu.Game/Screens/Edit/Compose/Components/SelectionBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
Expand All @@ -22,7 +23,7 @@ public partial class SelectionBox : CompositeDrawable

private const float button_padding = 5;

public Func<float, bool>? OnRotation;
public SelectionRotationHandler? RotationHandler { get; init; }
public Func<Vector2, Anchor, bool>? OnScale;
public Func<Direction, bool, bool>? OnFlip;
public Func<bool>? OnReverse;
Expand Down Expand Up @@ -51,22 +52,7 @@ public bool CanReverse
}
}

private bool canRotate;

/// <summary>
/// Whether rotation support should be enabled.
/// </summary>
public bool CanRotate
{
get => canRotate;
set
{
if (canRotate == value) return;

canRotate = value;
recreate();
}
}
private IBindable<bool> canRotate = new BindableBool();

private bool canScaleX;

Expand Down Expand Up @@ -161,7 +147,14 @@ public string Text
private OsuColour colours { get; set; } = null!;

[BackgroundDependencyLoader]
private void load() => recreate();
private void load()
{
if (RotationHandler != null)
canRotate.BindTo(RotationHandler.CanRotate);

canRotate.BindValueChanged(_ => recreate());
recreate();
}

protected override bool OnKeyDown(KeyDownEvent e)
{
Expand All @@ -174,10 +167,10 @@ protected override bool OnKeyDown(KeyDownEvent e)
return CanReverse && reverseButton?.TriggerClick() == true;

case Key.Comma:
return CanRotate && rotateCounterClockwiseButton?.TriggerClick() == true;
return canRotate.Value && rotateCounterClockwiseButton?.TriggerClick() == true;

case Key.Period:
return CanRotate && rotateClockwiseButton?.TriggerClick() == true;
return canRotate.Value && rotateClockwiseButton?.TriggerClick() == true;
}

return base.OnKeyDown(e);
Expand Down Expand Up @@ -254,14 +247,14 @@ private void recreate()
if (CanScaleY) addYScaleComponents();
if (CanFlipX) addXFlipComponents();
if (CanFlipY) addYFlipComponents();
if (CanRotate) addRotationComponents();
if (canRotate.Value) addRotationComponents();
if (CanReverse) reverseButton = addButton(FontAwesome.Solid.Backward, "Reverse pattern (Ctrl-G)", () => OnReverse?.Invoke());
}

private void addRotationComponents()
{
rotateCounterClockwiseButton = addButton(FontAwesome.Solid.Undo, "Rotate 90 degrees counter-clockwise (Ctrl-<)", () => OnRotation?.Invoke(-90));
rotateClockwiseButton = addButton(FontAwesome.Solid.Redo, "Rotate 90 degrees clockwise (Ctrl->)", () => OnRotation?.Invoke(90));
rotateCounterClockwiseButton = addButton(FontAwesome.Solid.Undo, "Rotate 90 degrees counter-clockwise (Ctrl-<)", () => RotationHandler?.Rotate(-90));
rotateClockwiseButton = addButton(FontAwesome.Solid.Redo, "Rotate 90 degrees clockwise (Ctrl->)", () => RotationHandler?.Rotate(90));

addRotateHandle(Anchor.TopLeft);
addRotateHandle(Anchor.TopRight);
Expand Down Expand Up @@ -331,7 +324,7 @@ private void addRotateHandle(Anchor anchor)
var handle = new SelectionBoxRotationHandle
{
Anchor = anchor,
HandleRotate = angle => OnRotation?.Invoke(angle)
RotationHandler = RotationHandler
};

handle.OperationStarted += operationStarted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.EnumExtensions;
Expand All @@ -21,7 +22,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
{
public partial class SelectionBoxRotationHandle : SelectionBoxDragHandle, IHasTooltip
{
public Action<float> HandleRotate { get; set; }
[CanBeNull]
public SelectionRotationHandler RotationHandler { get; init; }

public LocalisableString TooltipText { get; private set; }

Expand Down Expand Up @@ -63,10 +65,10 @@ protected override void UpdateHoverState()

protected override bool OnDragStart(DragStartEvent e)
{
bool handle = base.OnDragStart(e);
if (handle)
cumulativeRotation.Value = 0;
return handle;
if (RotationHandler == null) return false;

RotationHandler.Begin();
return true;
}

protected override void OnDrag(DragEvent e)
Expand Down Expand Up @@ -99,7 +101,9 @@ protected override void OnKeyUp(KeyUpEvent e)

protected override void OnDragEnd(DragEndEvent e)
{
base.OnDragEnd(e);
RotationHandler?.Commit();
UpdateHoverState();

cumulativeRotation.Value = null;
rawCumulativeRotation = 0;
TooltipText = default;
Expand All @@ -116,14 +120,12 @@ private float convertDragEventToAngleOfRotation(DragEvent e)

private void applyRotation(bool shouldSnap)
{
float oldRotation = cumulativeRotation.Value ?? 0;

float newRotation = shouldSnap ? snap(rawCumulativeRotation, snap_step) : MathF.Round(rawCumulativeRotation);
newRotation = (newRotation - 180) % 360 + 180;

cumulativeRotation.Value = newRotation;

HandleRotate?.Invoke(newRotation - oldRotation);
RotationHandler?.Update(newRotation);
TooltipText = shouldSnap ? EditorStrings.RotationSnapped(newRotation) : EditorStrings.RotationUnsnapped(newRotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public SelectionBox CreateSelectionBox()
OperationStarted = OnOperationBegan,
OperationEnded = OnOperationEnded,

OnRotation = HandleRotation,
RotationHandler = RotationHandler,
OnScale = HandleScale,
OnFlip = HandleFlip,
OnReverse = HandleReverse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ public class SelectionRotationHandler : ISelectionOperationHandler
/// </summary>
public Bindable<bool> CanRotate { get; private set; } = new BindableBool();

public void Rotate(float rotation, Vector2? origin = null)
{
Begin();
Update(rotation, origin);
Commit();
}

public virtual void Begin()
{
}

public virtual void Update(float rotation, Vector2 origin)
public virtual void Update(float rotation, Vector2? origin = null)
{
}

Expand Down

0 comments on commit 1399cce

Please sign in to comment.