Add proper change handler support

This commit is contained in:
Dean Herbert 2020-09-29 20:08:28 +09:00
parent 934db14e03
commit a2e2cca396
2 changed files with 68 additions and 10 deletions

View File

@ -22,11 +22,25 @@ namespace osu.Game.Rulesets.Osu.Edit
CanScaleX = true, CanScaleX = true,
CanScaleY = true, CanScaleY = true,
OperationStarted = onStart,
OperationEnded = onEnd,
OnRotation = handleRotation, OnRotation = handleRotation,
OnScaleX = handleScaleX, OnScaleX = handleScaleX,
OnScaleY = handleScaleY, OnScaleY = handleScaleY,
}; };
private void onEnd()
{
ChangeHandler.EndChange();
centre = null;
}
private void onStart()
{
ChangeHandler.BeginChange();
}
private void handleScaleY(DragEvent e, Anchor reference) private void handleScaleY(DragEvent e, Anchor reference)
{ {
int direction = (reference & Anchor.y0) > 0 ? -1 : 1; int direction = (reference & Anchor.y0) > 0 ? -1 : 1;

View File

@ -20,6 +20,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
public Action<DragEvent, Anchor> OnScaleX; public Action<DragEvent, Anchor> OnScaleX;
public Action<DragEvent, Anchor> OnScaleY; public Action<DragEvent, Anchor> OnScaleY;
public Action OperationStarted;
public Action OperationEnded;
private bool canRotate; private bool canRotate;
public bool CanRotate public bool CanRotate
@ -114,7 +117,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Y = -separation, Y = -separation,
HandleDrag = e => OnRotation?.Invoke(e) HandleDrag = e => OnRotation?.Invoke(e),
OperationStarted = operationStarted,
OperationEnded = operationEnded
} }
}); });
} }
@ -126,12 +131,16 @@ namespace osu.Game.Screens.Edit.Compose.Components
new DragHandle new DragHandle
{ {
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
HandleDrag = e => OnScaleY?.Invoke(e, Anchor.TopCentre) HandleDrag = e => OnScaleY?.Invoke(e, Anchor.TopCentre),
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
new DragHandle new DragHandle
{ {
Anchor = Anchor.BottomCentre, Anchor = Anchor.BottomCentre,
HandleDrag = e => OnScaleY?.Invoke(e, Anchor.BottomCentre) HandleDrag = e => OnScaleY?.Invoke(e, Anchor.BottomCentre),
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
}); });
} }
@ -143,12 +152,16 @@ namespace osu.Game.Screens.Edit.Compose.Components
new DragHandle new DragHandle
{ {
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
HandleDrag = e => OnScaleX?.Invoke(e, Anchor.CentreLeft) HandleDrag = e => OnScaleX?.Invoke(e, Anchor.CentreLeft),
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
new DragHandle new DragHandle
{ {
Anchor = Anchor.CentreRight, Anchor = Anchor.CentreRight,
HandleDrag = e => OnScaleX?.Invoke(e, Anchor.CentreRight) HandleDrag = e => OnScaleX?.Invoke(e, Anchor.CentreRight),
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
}); });
} }
@ -164,7 +177,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
OnScaleX?.Invoke(e, Anchor.TopLeft); OnScaleX?.Invoke(e, Anchor.TopLeft);
OnScaleY?.Invoke(e, Anchor.TopLeft); OnScaleY?.Invoke(e, Anchor.TopLeft);
} },
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
new DragHandle new DragHandle
{ {
@ -173,7 +188,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
OnScaleX?.Invoke(e, Anchor.TopRight); OnScaleX?.Invoke(e, Anchor.TopRight);
OnScaleY?.Invoke(e, Anchor.TopRight); OnScaleY?.Invoke(e, Anchor.TopRight);
} },
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
new DragHandle new DragHandle
{ {
@ -182,7 +199,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
OnScaleX?.Invoke(e, Anchor.BottomLeft); OnScaleX?.Invoke(e, Anchor.BottomLeft);
OnScaleY?.Invoke(e, Anchor.BottomLeft); OnScaleY?.Invoke(e, Anchor.BottomLeft);
} },
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
new DragHandle new DragHandle
{ {
@ -191,12 +210,28 @@ namespace osu.Game.Screens.Edit.Compose.Components
{ {
OnScaleX?.Invoke(e, Anchor.BottomRight); OnScaleX?.Invoke(e, Anchor.BottomRight);
OnScaleY?.Invoke(e, Anchor.BottomRight); OnScaleY?.Invoke(e, Anchor.BottomRight);
} },
OperationStarted = operationStarted,
OperationEnded = operationEnded
}, },
}); });
} }
} }
private int activeOperations;
private void operationEnded()
{
if (--activeOperations == 0)
OperationEnded?.Invoke();
}
private void operationStarted()
{
if (activeOperations++ == 0)
OperationStarted?.Invoke();
}
private class RotationDragHandle : DragHandle private class RotationDragHandle : DragHandle
{ {
private SpriteIcon icon; private SpriteIcon icon;
@ -225,6 +260,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
private class DragHandle : Container private class DragHandle : Container
{ {
public Action OperationStarted;
public Action OperationEnded;
public Action<DragEvent> HandleDrag { get; set; } public Action<DragEvent> HandleDrag { get; set; }
private Circle circle; private Circle circle;
@ -276,7 +314,11 @@ namespace osu.Game.Screens.Edit.Compose.Components
return true; return true;
} }
protected override bool OnDragStart(DragStartEvent e) => true; protected override bool OnDragStart(DragStartEvent e)
{
OperationStarted?.Invoke();
return true;
}
protected override void OnDrag(DragEvent e) protected override void OnDrag(DragEvent e)
{ {
@ -287,6 +329,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
protected override void OnDragEnd(DragEndEvent e) protected override void OnDragEnd(DragEndEvent e)
{ {
HandlingMouse = false; HandlingMouse = false;
OperationEnded?.Invoke();
UpdateHoverState(); UpdateHoverState();
base.OnDragEnd(e); base.OnDragEnd(e);
} }