1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 11:27:24 +08:00

Merge pull request #10310 from peppy/osu-selection-flip-rotate-buttons

Add buttons to flip/rotate the current selection by known amounts
This commit is contained in:
Dan Balasescu 2020-10-05 20:02:09 +09:00 committed by GitHub
commit ef8e6b3d96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 456 additions and 335 deletions

View File

@ -27,9 +27,9 @@ namespace osu.Game.Rulesets.Osu.Edit
SelectionBox.CanScaleY = canOperate;
}
protected override void OnDragOperationEnded()
protected override void OnOperationEnded()
{
base.OnDragOperationEnded();
base.OnOperationEnded();
referenceOrigin = null;
}
@ -41,6 +41,45 @@ namespace osu.Game.Rulesets.Osu.Edit
/// </summary>
private Vector2? referenceOrigin;
public override bool HandleFlip(Direction direction)
{
var hitObjects = selectedMovableObjects;
var selectedObjectsQuad = getSurroundingQuad(hitObjects);
var centre = selectedObjectsQuad.Centre;
foreach (var h in hitObjects)
{
var pos = h.Position;
switch (direction)
{
case Direction.Horizontal:
pos.X = centre.X - (pos.X - centre.X);
break;
case Direction.Vertical:
pos.Y = centre.Y - (pos.Y - centre.Y);
break;
}
h.Position = pos;
if (h is Slider slider)
{
foreach (var point in slider.Path.ControlPoints)
{
point.Position.Value = new Vector2(
(direction == Direction.Horizontal ? -1 : 1) * point.Position.Value.X,
(direction == Direction.Vertical ? -1 : 1) * point.Position.Value.Y
);
}
}
}
return true;
}
public override bool HandleScale(Vector2 scale, Anchor reference)
{
adjustScaleFromAnchor(ref scale, reference);

View File

@ -3,7 +3,6 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
@ -15,17 +14,17 @@ namespace osu.Game.Tests.Visual.Editing
public TestSceneComposeSelectBox()
{
ComposeSelectionBox selectionBox = null;
SelectionBox selectionBox = null;
AddStep("create box", () =>
Child = selectionArea = new Container
{
Size = new Vector2(300),
Size = new Vector2(400),
Position = -new Vector2(150),
Anchor = Anchor.Centre,
Children = new Drawable[]
{
selectionBox = new ComposeSelectionBox
selectionBox = new SelectionBox
{
CanRotate = true,
CanScaleX = true,
@ -42,29 +41,29 @@ namespace osu.Game.Tests.Visual.Editing
AddToggleStep("toggle y", state => selectionBox.CanScaleY = state);
}
private void handleScale(DragEvent e, Anchor reference)
private void handleScale(Vector2 amount, Anchor reference)
{
if ((reference & Anchor.y1) == 0)
{
int directionY = (reference & Anchor.y0) > 0 ? -1 : 1;
if (directionY < 0)
selectionArea.Y += e.Delta.Y;
selectionArea.Height += directionY * e.Delta.Y;
selectionArea.Y += amount.Y;
selectionArea.Height += directionY * amount.Y;
}
if ((reference & Anchor.x1) == 0)
{
int directionX = (reference & Anchor.x0) > 0 ? -1 : 1;
if (directionX < 0)
selectionArea.X += e.Delta.X;
selectionArea.Width += directionX * e.Delta.X;
selectionArea.X += amount.X;
selectionArea.Width += directionX * amount.X;
}
}
private void handleRotation(DragEvent e)
private void handleRotation(float angle)
{
// kinda silly and wrong, but just showing that the drag handles work.
selectionArea.Rotation += e.Delta.X;
selectionArea.Rotation += angle;
}
}
}

View File

@ -1,312 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Edit.Compose.Components
{
public class ComposeSelectionBox : CompositeDrawable
{
public Action<DragEvent> OnRotation;
public Action<DragEvent, Anchor> OnScale;
public Action OperationStarted;
public Action OperationEnded;
private bool canRotate;
/// <summary>
/// Whether rotation support should be enabled.
/// </summary>
public bool CanRotate
{
get => canRotate;
set
{
canRotate = value;
recreate();
}
}
private bool canScaleX;
/// <summary>
/// Whether vertical scale support should be enabled.
/// </summary>
public bool CanScaleX
{
get => canScaleX;
set
{
canScaleX = value;
recreate();
}
}
private bool canScaleY;
/// <summary>
/// Whether horizontal scale support should be enabled.
/// </summary>
public bool CanScaleY
{
get => canScaleY;
set
{
canScaleY = value;
recreate();
}
}
public const float BORDER_RADIUS = 3;
[Resolved]
private OsuColour colours { get; set; }
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.Both;
recreate();
}
private void recreate()
{
if (LoadState < LoadState.Loading)
return;
InternalChildren = new Drawable[]
{
new Container
{
Masking = true,
BorderThickness = BORDER_RADIUS,
BorderColour = colours.YellowDark,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
AlwaysPresent = true,
Alpha = 0
},
}
},
};
if (CanRotate)
{
const float separation = 40;
AddRangeInternal(new Drawable[]
{
new Box
{
Colour = colours.YellowLight,
Blending = BlendingParameters.Additive,
Alpha = 0.3f,
Size = new Vector2(BORDER_RADIUS, separation),
Anchor = Anchor.TopCentre,
Origin = Anchor.BottomCentre,
},
new RotationDragHandle
{
Anchor = Anchor.TopCentre,
Y = -separation,
HandleDrag = e => OnRotation?.Invoke(e),
OperationStarted = operationStarted,
OperationEnded = operationEnded
}
});
}
if (CanScaleY)
{
AddRangeInternal(new[]
{
createDragHandle(Anchor.TopCentre),
createDragHandle(Anchor.BottomCentre),
});
}
if (CanScaleX)
{
AddRangeInternal(new[]
{
createDragHandle(Anchor.CentreLeft),
createDragHandle(Anchor.CentreRight),
});
}
if (CanScaleX && CanScaleY)
{
AddRangeInternal(new[]
{
createDragHandle(Anchor.TopLeft),
createDragHandle(Anchor.TopRight),
createDragHandle(Anchor.BottomLeft),
createDragHandle(Anchor.BottomRight),
});
}
ScaleDragHandle createDragHandle(Anchor anchor) =>
new ScaleDragHandle(anchor)
{
HandleDrag = e => OnScale?.Invoke(e, anchor),
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 ScaleDragHandle : DragHandle
{
public ScaleDragHandle(Anchor anchor)
{
Anchor = anchor;
}
}
private class RotationDragHandle : DragHandle
{
private SpriteIcon icon;
[BackgroundDependencyLoader]
private void load()
{
Size *= 2;
AddInternal(icon = new SpriteIcon
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.5f),
Icon = FontAwesome.Solid.Redo,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
});
}
protected override void UpdateHoverState()
{
base.UpdateHoverState();
icon.Colour = !HandlingMouse && IsHovered ? Color4.White : Color4.Black;
}
}
private class DragHandle : Container
{
public Action OperationStarted;
public Action OperationEnded;
public Action<DragEvent> HandleDrag { get; set; }
private Circle circle;
[Resolved]
private OsuColour colours { get; set; }
[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(10);
Origin = Anchor.Centre;
InternalChildren = new Drawable[]
{
circle = new Circle
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};
}
protected override void LoadComplete()
{
base.LoadComplete();
UpdateHoverState();
}
protected override bool OnHover(HoverEvent e)
{
UpdateHoverState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
UpdateHoverState();
}
protected bool HandlingMouse;
protected override bool OnMouseDown(MouseDownEvent e)
{
HandlingMouse = true;
UpdateHoverState();
return true;
}
protected override bool OnDragStart(DragStartEvent e)
{
OperationStarted?.Invoke();
return true;
}
protected override void OnDrag(DragEvent e)
{
HandleDrag?.Invoke(e);
base.OnDrag(e);
}
protected override void OnDragEnd(DragEndEvent e)
{
HandlingMouse = false;
OperationEnded?.Invoke();
UpdateHoverState();
base.OnDragEnd(e);
}
protected override void OnMouseUp(MouseUpEvent e)
{
HandlingMouse = false;
UpdateHoverState();
base.OnMouseUp(e);
}
protected virtual void UpdateHoverState()
{
circle.Colour = HandlingMouse ? colours.GrayF : (IsHovered ? colours.Red : colours.YellowDark);
this.ScaleTo(HandlingMouse || IsHovered ? 1.5f : 1, 100, Easing.OutQuint);
}
}
}
}

View File

@ -45,7 +45,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
{
Masking = true,
BorderColour = Color4.White,
BorderThickness = ComposeSelectionBox.BORDER_RADIUS,
BorderThickness = SelectionBox.BORDER_RADIUS,
Child = new Box
{
RelativeSizeAxes = Axes.Both,

View File

@ -0,0 +1,216 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osuTK;
namespace osu.Game.Screens.Edit.Compose.Components
{
public class SelectionBox : CompositeDrawable
{
public Action<float> OnRotation;
public Action<Vector2, Anchor> OnScale;
public Action<Direction> OnFlip;
public Action OperationStarted;
public Action OperationEnded;
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 bool canScaleX;
/// <summary>
/// Whether vertical scale support should be enabled.
/// </summary>
public bool CanScaleX
{
get => canScaleX;
set
{
if (canScaleX == value) return;
canScaleX = value;
recreate();
}
}
private bool canScaleY;
/// <summary>
/// Whether horizontal scale support should be enabled.
/// </summary>
public bool CanScaleY
{
get => canScaleY;
set
{
if (canScaleY == value) return;
canScaleY = value;
recreate();
}
}
private FillFlowContainer buttons;
public const float BORDER_RADIUS = 3;
[Resolved]
private OsuColour colours { get; set; }
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.Both;
recreate();
}
private void recreate()
{
if (LoadState < LoadState.Loading)
return;
InternalChildren = new Drawable[]
{
new Container
{
Masking = true,
BorderThickness = BORDER_RADIUS,
BorderColour = colours.YellowDark,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
AlwaysPresent = true,
Alpha = 0
},
}
},
buttons = new FillFlowContainer
{
Y = 20,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Anchor = Anchor.BottomCentre,
Origin = Anchor.Centre
}
};
if (CanScaleX) addXScaleComponents();
if (CanScaleX && CanScaleY) addFullScaleComponents();
if (CanScaleY) addYScaleComponents();
if (CanRotate) addRotationComponents();
}
private void addRotationComponents()
{
const float separation = 40;
addButton(FontAwesome.Solid.Undo, "Rotate 90 degrees counter-clockwise", () => OnRotation?.Invoke(-90));
addButton(FontAwesome.Solid.Redo, "Rotate 90 degrees clockwise", () => OnRotation?.Invoke(90));
AddRangeInternal(new Drawable[]
{
new Box
{
Depth = float.MaxValue,
Colour = colours.YellowLight,
Blending = BlendingParameters.Additive,
Alpha = 0.3f,
Size = new Vector2(BORDER_RADIUS, separation),
Anchor = Anchor.TopCentre,
Origin = Anchor.BottomCentre,
},
new SelectionBoxDragHandleButton(FontAwesome.Solid.Redo, "Free rotate")
{
Anchor = Anchor.TopCentre,
Y = -separation,
HandleDrag = e => OnRotation?.Invoke(e.Delta.X),
OperationStarted = operationStarted,
OperationEnded = operationEnded
}
});
}
private void addYScaleComponents()
{
addButton(FontAwesome.Solid.ArrowsAltV, "Flip vertically", () => OnFlip?.Invoke(Direction.Vertical));
addDragHandle(Anchor.TopCentre);
addDragHandle(Anchor.BottomCentre);
}
private void addFullScaleComponents()
{
addDragHandle(Anchor.TopLeft);
addDragHandle(Anchor.TopRight);
addDragHandle(Anchor.BottomLeft);
addDragHandle(Anchor.BottomRight);
}
private void addXScaleComponents()
{
addButton(FontAwesome.Solid.ArrowsAltH, "Flip horizontally", () => OnFlip?.Invoke(Direction.Horizontal));
addDragHandle(Anchor.CentreLeft);
addDragHandle(Anchor.CentreRight);
}
private void addButton(IconUsage icon, string tooltip, Action action)
{
buttons.Add(new SelectionBoxDragHandleButton(icon, tooltip)
{
OperationStarted = operationStarted,
OperationEnded = operationEnded,
Action = action
});
}
private void addDragHandle(Anchor anchor) => AddInternal(new SelectionBoxDragHandle
{
Anchor = anchor,
HandleDrag = e => OnScale?.Invoke(e.Delta, anchor),
OperationStarted = operationStarted,
OperationEnded = operationEnded
});
private int activeOperations;
private void operationEnded()
{
if (--activeOperations == 0)
OperationEnded?.Invoke();
}
private void operationStarted()
{
if (activeOperations++ == 0)
OperationStarted?.Invoke();
}
}
}

View File

@ -0,0 +1,105 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osuTK;
namespace osu.Game.Screens.Edit.Compose.Components
{
public class SelectionBoxDragHandle : Container
{
public Action OperationStarted;
public Action OperationEnded;
public Action<DragEvent> HandleDrag { get; set; }
private Circle circle;
[Resolved]
private OsuColour colours { get; set; }
[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(10);
Origin = Anchor.Centre;
InternalChildren = new Drawable[]
{
circle = new Circle
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};
}
protected override void LoadComplete()
{
base.LoadComplete();
UpdateHoverState();
}
protected override bool OnHover(HoverEvent e)
{
UpdateHoverState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
UpdateHoverState();
}
protected bool HandlingMouse;
protected override bool OnMouseDown(MouseDownEvent e)
{
HandlingMouse = true;
UpdateHoverState();
return true;
}
protected override bool OnDragStart(DragStartEvent e)
{
OperationStarted?.Invoke();
return true;
}
protected override void OnDrag(DragEvent e)
{
HandleDrag?.Invoke(e);
base.OnDrag(e);
}
protected override void OnDragEnd(DragEndEvent e)
{
HandlingMouse = false;
OperationEnded?.Invoke();
UpdateHoverState();
base.OnDragEnd(e);
}
protected override void OnMouseUp(MouseUpEvent e)
{
HandlingMouse = false;
UpdateHoverState();
base.OnMouseUp(e);
}
protected virtual void UpdateHoverState()
{
circle.Colour = HandlingMouse ? colours.GrayF : (IsHovered ? colours.Red : colours.YellowDark);
this.ScaleTo(HandlingMouse || IsHovered ? 1.5f : 1, 100, Easing.OutQuint);
}
}
}

View File

@ -0,0 +1,66 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Edit.Compose.Components
{
/// <summary>
/// A drag "handle" which shares the visual appearance but behaves more like a clickable button.
/// </summary>
public sealed class SelectionBoxDragHandleButton : SelectionBoxDragHandle, IHasTooltip
{
private SpriteIcon icon;
private readonly IconUsage iconUsage;
public Action Action;
public SelectionBoxDragHandleButton(IconUsage iconUsage, string tooltip)
{
this.iconUsage = iconUsage;
TooltipText = tooltip;
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
}
[BackgroundDependencyLoader]
private void load()
{
Size *= 2;
AddInternal(icon = new SpriteIcon
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.5f),
Icon = iconUsage,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
});
}
protected override bool OnClick(ClickEvent e)
{
OperationStarted?.Invoke();
Action?.Invoke();
OperationEnded?.Invoke();
return true;
}
protected override void UpdateHoverState()
{
base.UpdateHoverState();
icon.Colour = !HandlingMouse && IsHovered ? Color4.White : Color4.Black;
}
public string TooltipText { get; }
}
}

View File

@ -43,7 +43,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
private OsuSpriteText selectionDetailsText;
protected ComposeSelectionBox SelectionBox { get; private set; }
protected SelectionBox SelectionBox { get; private set; }
[Resolved(CanBeNull = true)]
protected EditorBeatmap EditorBeatmap { get; private set; }
@ -94,20 +94,21 @@ namespace osu.Game.Screens.Edit.Compose.Components
};
}
public ComposeSelectionBox CreateSelectionBox()
=> new ComposeSelectionBox
public SelectionBox CreateSelectionBox()
=> new SelectionBox
{
OperationStarted = OnDragOperationBegan,
OperationEnded = OnDragOperationEnded,
OperationStarted = OnOperationBegan,
OperationEnded = OnOperationEnded,
OnRotation = e => HandleRotation(e.Delta.X),
OnScale = (e, anchor) => HandleScale(e.Delta, anchor),
OnRotation = angle => HandleRotation(angle),
OnScale = (amount, anchor) => HandleScale(amount, anchor),
OnFlip = direction => HandleFlip(direction),
};
/// <summary>
/// Fired when a drag operation ends from the selection box.
/// </summary>
protected virtual void OnDragOperationBegan()
protected virtual void OnOperationBegan()
{
ChangeHandler.BeginChange();
}
@ -115,7 +116,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// <summary>
/// Fired when a drag operation begins from the selection box.
/// </summary>
protected virtual void OnDragOperationEnded()
protected virtual void OnOperationEnded()
{
ChangeHandler.EndChange();
}
@ -151,6 +152,13 @@ namespace osu.Game.Screens.Edit.Compose.Components
/// <returns>Whether any <see cref="DrawableHitObject"/>s could be moved.</returns>
public virtual bool HandleScale(Vector2 scale, Anchor anchor) => false;
/// <summary>
/// Handled the selected <see cref="DrawableHitObject"/>s being flipped.
/// </summary>
/// <param name="direction">The direction to flip</param>
/// <returns>Whether any <see cref="DrawableHitObject"/>s could be moved.</returns>
public virtual bool HandleFlip(Direction direction) => false;
public bool OnPressed(PlatformAction action)
{
switch (action.ActionMethod)