2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-11-06 17:28:22 +08:00
|
|
|
|
namespace osu.Game.Screens.Edit.Compose.Components
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2018-11-06 16:24:38 +08:00
|
|
|
|
/// A box that displays the drag selection and provides selection events for users to handle.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// </summary>
|
2018-11-06 16:24:38 +08:00
|
|
|
|
public class DragBox : CompositeDrawable
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly Action<RectangleF> performSelection;
|
|
|
|
|
|
|
|
|
|
private Drawable box;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-06 16:24:38 +08:00
|
|
|
|
/// Creates a new <see cref="DragBox"/>.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// </summary>
|
2018-11-06 16:24:38 +08:00
|
|
|
|
/// <param name="performSelection">A delegate that performs drag selection.</param>
|
|
|
|
|
public DragBox(Action<RectangleF> performSelection)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
this.performSelection = performSelection;
|
|
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
AlwaysPresent = true;
|
|
|
|
|
Alpha = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
InternalChild = box = new Container
|
|
|
|
|
{
|
|
|
|
|
Masking = true,
|
|
|
|
|
BorderColour = Color4.White,
|
2018-11-19 15:58:11 +08:00
|
|
|
|
BorderThickness = SelectionHandler.BORDER_RADIUS,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Child = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Alpha = 0.1f
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 16:22:14 +08:00
|
|
|
|
public void UpdateDrag(MouseButtonEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-09-19 19:52:57 +08:00
|
|
|
|
var dragPosition = e.ScreenSpaceMousePosition;
|
|
|
|
|
var dragStartPosition = e.ScreenSpaceMouseDownPosition;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
var dragQuad = new Quad(dragStartPosition.X, dragStartPosition.Y, dragPosition.X - dragStartPosition.X, dragPosition.Y - dragStartPosition.Y);
|
|
|
|
|
|
|
|
|
|
// We use AABBFloat instead of RectangleF since it handles negative sizes for us
|
|
|
|
|
var dragRectangle = dragQuad.AABBFloat;
|
|
|
|
|
|
|
|
|
|
var topLeft = ToLocalSpace(dragRectangle.TopLeft);
|
|
|
|
|
var bottomRight = ToLocalSpace(dragRectangle.BottomRight);
|
|
|
|
|
|
|
|
|
|
box.Position = topLeft;
|
|
|
|
|
box.Size = bottomRight - topLeft;
|
|
|
|
|
|
|
|
|
|
performSelection?.Invoke(dragRectangle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|