Skip to content

Commit

Permalink
Merge pull request #28589 from bdach/scale-handling-crash
Browse files Browse the repository at this point in the history
Fix crashes when opening scale/rotation popovers during selection box operations
  • Loading branch information
peppy authored Jun 25, 2024
2 parents 3d22f70 + d722be1 commit aadb104
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 10 deletions.
12 changes: 8 additions & 4 deletions osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ private void updateState()

public override void Begin()
{
if (objectsInRotation != null)
if (OperationInProgress.Value)
throw new InvalidOperationException($"Cannot {nameof(Begin)} a rotate operation while another is in progress!");

base.Begin();

changeHandler?.BeginChange();

objectsInRotation = selectedMovableObjects.ToArray();
Expand All @@ -68,10 +70,10 @@ public override void Begin()

public override void Update(float rotation, Vector2? origin = null)
{
if (objectsInRotation == null)
if (!OperationInProgress.Value)
throw new InvalidOperationException($"Cannot {nameof(Update)} a rotate operation without calling {nameof(Begin)} first!");

Debug.Assert(originalPositions != null && originalPathControlPointPositions != null && defaultOrigin != null);
Debug.Assert(objectsInRotation != null && originalPositions != null && originalPathControlPointPositions != null && defaultOrigin != null);

Vector2 actualOrigin = origin ?? defaultOrigin.Value;

Expand All @@ -91,11 +93,13 @@ public override void Update(float rotation, Vector2? origin = null)

public override void Commit()
{
if (objectsInRotation == null)
if (!OperationInProgress.Value)
throw new InvalidOperationException($"Cannot {nameof(Commit)} a rotate operation without calling {nameof(Begin)} first!");

changeHandler?.EndChange();

base.Commit();

objectsInRotation = null;
originalPositions = null;
originalPathControlPointPositions = null;
Expand Down
12 changes: 8 additions & 4 deletions osu.Game.Rulesets.Osu/Edit/OsuSelectionScaleHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ private void updateState()

public override void Begin()
{
if (objectsInScale != null)
if (OperationInProgress.Value)
throw new InvalidOperationException($"Cannot {nameof(Begin)} a scale operation while another is in progress!");

base.Begin();

changeHandler?.BeginChange();

objectsInScale = selectedMovableObjects.ToDictionary(ho => ho, ho => new OriginalHitObjectState(ho));
Expand All @@ -86,10 +88,10 @@ public override void Begin()

public override void Update(Vector2 scale, Vector2? origin = null, Axes adjustAxis = Axes.Both)
{
if (objectsInScale == null)
if (!OperationInProgress.Value)
throw new InvalidOperationException($"Cannot {nameof(Update)} a scale operation without calling {nameof(Begin)} first!");

Debug.Assert(defaultOrigin != null && OriginalSurroundingQuad != null);
Debug.Assert(objectsInScale != null && defaultOrigin != null && OriginalSurroundingQuad != null);

Vector2 actualOrigin = origin ?? defaultOrigin.Value;

Expand Down Expand Up @@ -117,11 +119,13 @@ public override void Update(Vector2 scale, Vector2? origin = null, Axes adjustAx

public override void Commit()
{
if (objectsInScale == null)
if (!OperationInProgress.Value)
throw new InvalidOperationException($"Cannot {nameof(Commit)} a rotate operation without calling {nameof(Begin)} first!");

changeHandler?.EndChange();

base.Commit();

objectsInScale = null;
OriginalSurroundingQuad = null;
defaultOrigin = null;
Expand Down
6 changes: 4 additions & 2 deletions osu.Game.Rulesets.Osu/Edit/TransformToolboxGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
case GlobalAction.EditorToggleRotateControl:
{
rotateButton.TriggerClick();
if (!RotationHandler.OperationInProgress.Value || rotateButton.Selected.Value)
rotateButton.TriggerClick();
return true;
}

case GlobalAction.EditorToggleScaleControl:
{
scaleButton.TriggerClick();
if (!ScaleHandler.OperationInProgress.Value || scaleButton.Selected.Value)
scaleButton.TriggerClick();
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public override void Begin()

targetContainer = getTargetContainer();
initialRotation = targetContainer!.Rotation;

base.Begin();
}

public override void Update(float rotation, Vector2? origin = null)
Expand All @@ -102,6 +104,8 @@ public override void Commit()

targetContainer = null;
initialRotation = null;

base.Commit();
}
}

Expand Down
4 changes: 4 additions & 0 deletions osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public override void Begin()
originalRotations = objectsInRotation.ToDictionary(d => d, d => d.Rotation);
originalPositions = objectsInRotation.ToDictionary(d => d, d => d.ToScreenSpace(d.OriginPosition));
defaultOrigin = GeometryUtils.GetSurroundingQuad(objectsInRotation.SelectMany(d => d.ScreenSpaceDrawQuad.GetVertices().ToArray())).Centre;

base.Begin();
}

public override void Update(float rotation, Vector2? origin = null)
Expand Down Expand Up @@ -99,6 +101,8 @@ public override void Commit()
originalPositions = null;
originalRotations = null;
defaultOrigin = null;

base.Commit();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ protected override bool OnDragStart(DragStartEvent e)

if (rotationHandler == null) return false;

if (rotationHandler.OperationInProgress.Value)
return false;

rotationHandler.Begin();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ protected override bool OnDragStart(DragStartEvent e)

if (scaleHandler == null) return false;

if (scaleHandler.OperationInProgress.Value)
return false;

originalAnchor = Anchor;

scaleHandler.Begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </summary>
public partial class SelectionRotationHandler : Component
{
/// <summary>
/// Whether there is any ongoing scale operation right now.
/// </summary>
public Bindable<bool> OperationInProgress { get; private set; } = new BindableBool();

/// <summary>
/// Whether rotation anchored by the selection origin can currently be performed.
/// </summary>
Expand Down Expand Up @@ -50,6 +55,7 @@ public void Rotate(float rotation, Vector2? origin = null)
/// </remarks>
public virtual void Begin()
{
OperationInProgress.Value = true;
}

/// <summary>
Expand Down Expand Up @@ -85,6 +91,7 @@ public virtual void Update(float rotation, Vector2? origin = null)
/// </remarks>
public virtual void Commit()
{
OperationInProgress.Value = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </summary>
public partial class SelectionScaleHandler : Component
{
/// <summary>
/// Whether there is any ongoing scale operation right now.
/// </summary>
public Bindable<bool> OperationInProgress { get; private set; } = new BindableBool();

/// <summary>
/// Whether horizontal scaling (from the left or right edge) support should be enabled.
/// </summary>
Expand Down Expand Up @@ -63,6 +68,7 @@ public void ScaleSelection(Vector2 scale, Vector2? origin = null, Axes adjustAxi
/// </remarks>
public virtual void Begin()
{
OperationInProgress.Value = true;
}

/// <summary>
Expand Down Expand Up @@ -99,6 +105,7 @@ public virtual void Update(Vector2 scale, Vector2? origin = null, Axes adjustAxi
/// </remarks>
public virtual void Commit()
{
OperationInProgress.Value = false;
}
}
}

0 comments on commit aadb104

Please sign in to comment.