1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 09:32:55 +08:00

Migrate SelectionHandler to use SelectionRotationHandler

This commit is contained in:
Bartłomiej Dach 2023-07-23 20:01:30 +02:00
parent 21df0e2d60
commit aec3ca250c
No known key found for this signature in database
5 changed files with 37 additions and 36 deletions

View File

@ -40,7 +40,6 @@ namespace osu.Game.Rulesets.Osu.Edit
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);

View File

@ -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;
@ -22,7 +23,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
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;
@ -51,22 +52,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
}
}
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;
@ -161,7 +147,14 @@ namespace osu.Game.Screens.Edit.Compose.Components
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)
{
@ -174,10 +167,10 @@ namespace osu.Game.Screens.Edit.Compose.Components
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);
@ -254,14 +247,14 @@ namespace osu.Game.Screens.Edit.Compose.Components
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);
@ -331,7 +324,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
var handle = new SelectionBoxRotationHandle
{
Anchor = anchor,
HandleRotate = angle => OnRotation?.Invoke(angle)
RotationHandler = RotationHandler
};
handle.OperationStarted += operationStarted;

View File

@ -4,6 +4,7 @@
#nullable disable
using System;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.EnumExtensions;
@ -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; }
@ -63,10 +65,10 @@ namespace osu.Game.Screens.Edit.Compose.Components
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)
@ -99,7 +101,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
protected override void OnDragEnd(DragEndEvent e)
{
base.OnDragEnd(e);
RotationHandler?.Commit();
UpdateHoverState();
cumulativeRotation.Value = null;
rawCumulativeRotation = 0;
TooltipText = default;
@ -116,14 +120,12 @@ namespace osu.Game.Screens.Edit.Compose.Components
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);
}

View File

@ -84,7 +84,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
OperationStarted = OnOperationBegan,
OperationEnded = OnOperationEnded,
OnRotation = HandleRotation,
RotationHandler = RotationHandler,
OnScale = HandleScale,
OnFlip = HandleFlip,
OnReverse = HandleReverse,

View File

@ -16,11 +16,18 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// </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)
{
}