2020-10-01 15:34:34 +08:00
|
|
|
// 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;
|
2020-11-03 20:10:31 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2020-10-01 15:34:34 +08:00
|
|
|
using osu.Game.Graphics;
|
2021-05-03 16:34:34 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2020-10-01 15:34:34 +08:00
|
|
|
using osuTK;
|
2020-11-03 20:10:31 +08:00
|
|
|
using osuTK.Input;
|
2020-10-01 15:34:34 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Compose.Components
|
|
|
|
{
|
|
|
|
public class SelectionBox : CompositeDrawable
|
|
|
|
{
|
2021-05-03 16:34:34 +08:00
|
|
|
public const float BORDER_RADIUS = 3;
|
|
|
|
|
2020-11-03 20:10:31 +08:00
|
|
|
public Func<float, bool> OnRotation;
|
|
|
|
public Func<Vector2, Anchor, bool> OnScale;
|
|
|
|
public Func<Direction, bool> OnFlip;
|
|
|
|
public Func<bool> OnReverse;
|
2020-10-01 15:34:34 +08:00
|
|
|
|
|
|
|
public Action OperationStarted;
|
|
|
|
public Action OperationEnded;
|
|
|
|
|
2020-10-09 05:32:23 +08:00
|
|
|
private bool canReverse;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether pattern reversing support should be enabled.
|
|
|
|
/// </summary>
|
|
|
|
public bool CanReverse
|
|
|
|
{
|
|
|
|
get => canReverse;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (canReverse == value) return;
|
|
|
|
|
|
|
|
canReverse = value;
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:34:34 +08:00
|
|
|
private bool canRotate;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether rotation support should be enabled.
|
|
|
|
/// </summary>
|
|
|
|
public bool CanRotate
|
|
|
|
{
|
|
|
|
get => canRotate;
|
|
|
|
set
|
|
|
|
{
|
2020-10-01 15:36:03 +08:00
|
|
|
if (canRotate == value) return;
|
|
|
|
|
2020-10-01 15:34:34 +08:00
|
|
|
canRotate = value;
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool canScaleX;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether vertical scale support should be enabled.
|
|
|
|
/// </summary>
|
|
|
|
public bool CanScaleX
|
|
|
|
{
|
|
|
|
get => canScaleX;
|
|
|
|
set
|
|
|
|
{
|
2020-10-01 15:36:03 +08:00
|
|
|
if (canScaleX == value) return;
|
|
|
|
|
2020-10-01 15:34:34 +08:00
|
|
|
canScaleX = value;
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool canScaleY;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether horizontal scale support should be enabled.
|
|
|
|
/// </summary>
|
|
|
|
public bool CanScaleY
|
|
|
|
{
|
|
|
|
get => canScaleY;
|
|
|
|
set
|
|
|
|
{
|
2020-10-01 15:36:03 +08:00
|
|
|
if (canScaleY == value) return;
|
|
|
|
|
2020-10-01 15:34:34 +08:00
|
|
|
canScaleY = value;
|
|
|
|
recreate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 16:34:34 +08:00
|
|
|
private string text;
|
|
|
|
|
|
|
|
public string Text
|
|
|
|
{
|
|
|
|
get => text;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == text)
|
|
|
|
return;
|
|
|
|
|
|
|
|
text = value;
|
|
|
|
if (selectionDetailsText != null)
|
|
|
|
selectionDetailsText.Text = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 06:45:42 +08:00
|
|
|
private Container dragHandles;
|
2020-10-01 15:34:34 +08:00
|
|
|
private FillFlowContainer buttons;
|
|
|
|
|
2021-05-03 16:34:34 +08:00
|
|
|
private OsuSpriteText selectionDetailsText;
|
2020-10-01 15:34:34 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2021-05-03 16:34:34 +08:00
|
|
|
private void load() => recreate();
|
2020-10-01 15:34:34 +08:00
|
|
|
|
2020-11-03 20:10:31 +08:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
if (e.Repeat || !e.ControlPressed)
|
|
|
|
return false;
|
|
|
|
|
2021-03-18 04:50:45 +08:00
|
|
|
bool runOperationFromHotkey(Func<bool> operation)
|
|
|
|
{
|
|
|
|
operationStarted();
|
|
|
|
bool result = operation?.Invoke() ?? false;
|
|
|
|
operationEnded();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-03 20:10:31 +08:00
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.G:
|
2021-03-18 04:50:45 +08:00
|
|
|
return CanReverse && runOperationFromHotkey(OnReverse);
|
2020-11-03 20:10:31 +08:00
|
|
|
|
|
|
|
case Key.H:
|
2021-03-18 04:50:45 +08:00
|
|
|
return CanScaleX && runOperationFromHotkey(() => OnFlip?.Invoke(Direction.Horizontal) ?? false);
|
2020-11-03 20:10:31 +08:00
|
|
|
|
|
|
|
case Key.J:
|
2021-03-18 04:50:45 +08:00
|
|
|
return CanScaleY && runOperationFromHotkey(() => OnFlip?.Invoke(Direction.Vertical) ?? false);
|
2020-11-03 20:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return base.OnKeyDown(e);
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:34:34 +08:00
|
|
|
private void recreate()
|
|
|
|
{
|
|
|
|
if (LoadState < LoadState.Loading)
|
|
|
|
return;
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
2021-05-03 16:34:34 +08:00
|
|
|
new Container
|
|
|
|
{
|
|
|
|
Name = "info text",
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
Colour = colours.YellowDark,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
selectionDetailsText = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Padding = new MarginPadding(2),
|
|
|
|
Colour = colours.Gray0,
|
|
|
|
Font = OsuFont.Default.With(size: 11),
|
|
|
|
Text = text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-10-01 15:34:34 +08:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2020-12-19 06:45:42 +08:00
|
|
|
dragHandles = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
// ensures that the centres of all drag handles line up with the middle of the selection box border.
|
|
|
|
Padding = new MarginPadding(BORDER_RADIUS / 2)
|
|
|
|
},
|
2020-10-01 15:34:34 +08:00
|
|
|
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();
|
2020-11-03 20:10:31 +08:00
|
|
|
if (CanReverse) addButton(FontAwesome.Solid.Backward, "Reverse pattern (Ctrl-G)", () => OnReverse?.Invoke());
|
2020-10-01 15:34:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-12-13 20:58:58 +08:00
|
|
|
HandleDrag = e => OnRotation?.Invoke(convertDragEventToAngleOfRotation(e)),
|
2020-10-01 15:34:34 +08:00
|
|
|
OperationStarted = operationStarted,
|
|
|
|
OperationEnded = operationEnded
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addYScaleComponents()
|
|
|
|
{
|
2020-11-03 20:10:31 +08:00
|
|
|
addButton(FontAwesome.Solid.ArrowsAltV, "Flip vertically (Ctrl-J)", () => OnFlip?.Invoke(Direction.Vertical));
|
2020-10-01 15:34:34 +08:00
|
|
|
|
|
|
|
addDragHandle(Anchor.TopCentre);
|
|
|
|
addDragHandle(Anchor.BottomCentre);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addFullScaleComponents()
|
|
|
|
{
|
|
|
|
addDragHandle(Anchor.TopLeft);
|
|
|
|
addDragHandle(Anchor.TopRight);
|
|
|
|
addDragHandle(Anchor.BottomLeft);
|
|
|
|
addDragHandle(Anchor.BottomRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addXScaleComponents()
|
|
|
|
{
|
2020-11-03 20:10:31 +08:00
|
|
|
addButton(FontAwesome.Solid.ArrowsAltH, "Flip horizontally (Ctrl-H)", () => OnFlip?.Invoke(Direction.Horizontal));
|
2020-10-01 15:34:34 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-19 06:45:42 +08:00
|
|
|
private void addDragHandle(Anchor anchor) => dragHandles.Add(new SelectionBoxDragHandle
|
2020-10-01 15:34:34 +08:00
|
|
|
{
|
|
|
|
Anchor = anchor,
|
|
|
|
HandleDrag = e => OnScale?.Invoke(e.Delta, anchor),
|
|
|
|
OperationStarted = operationStarted,
|
|
|
|
OperationEnded = operationEnded
|
|
|
|
});
|
|
|
|
|
|
|
|
private int activeOperations;
|
|
|
|
|
2020-12-13 20:58:58 +08:00
|
|
|
private float convertDragEventToAngleOfRotation(DragEvent e)
|
|
|
|
{
|
|
|
|
// Adjust coordinate system to the center of SelectionBox
|
|
|
|
float startAngle = MathF.Atan2(e.LastMousePosition.Y - DrawHeight / 2, e.LastMousePosition.X - DrawWidth / 2);
|
|
|
|
float endAngle = MathF.Atan2(e.MousePosition.Y - DrawHeight / 2, e.MousePosition.X - DrawWidth / 2);
|
|
|
|
|
|
|
|
return (endAngle - startAngle) * 180 / MathF.PI;
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:34:34 +08:00
|
|
|
private void operationEnded()
|
|
|
|
{
|
|
|
|
if (--activeOperations == 0)
|
|
|
|
OperationEnded?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void operationStarted()
|
|
|
|
{
|
|
|
|
if (activeOperations++ == 0)
|
|
|
|
OperationStarted?.Invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|