1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-31 00:02:56 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/DragBox.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
4.3 KiB
C#
Raw Normal View History

// 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
2022-06-17 15:37:17 +08:00
#nullable disable
2018-03-29 21:52:42 +08:00
using System;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2022-05-06 19:34:44 +08:00
using osu.Framework.Layout;
using osuTK;
2018-04-13 17:19:50 +08:00
2018-11-06 17:28:22 +08:00
namespace osu.Game.Screens.Edit.Compose.Components
{
/// <summary>
2018-11-06 16:24:38 +08:00
/// A box that displays the drag selection and provides selection events for users to handle.
/// </summary>
public partial class DragBox : CompositeDrawable, IStateful<Visibility>
{
public Drawable Box { get; private set; }
2018-04-13 17:19:50 +08:00
/// <summary>
2018-11-06 16:24:38 +08:00
/// Creates a new <see cref="DragBox"/>.
/// </summary>
public DragBox()
{
RelativeSizeAxes = Axes.Both;
AlwaysPresent = true;
Alpha = 0;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
InternalChild = Box = CreateBox();
}
2018-04-13 17:19:50 +08:00
2022-05-06 19:34:44 +08:00
protected virtual Drawable CreateBox() => new BoxWithBorders();
/// <summary>
/// Handle a forwarded mouse event.
/// </summary>
/// <param name="e">The mouse event.</param>
public virtual void HandleDrag(MouseButtonEvent e)
{
Box.Position = Vector2.ComponentMin(e.MouseDownPosition, e.MousePosition);
Box.Size = Vector2.ComponentMax(e.MouseDownPosition, e.MousePosition) - Box.Position;
}
private Visibility state;
public Visibility State
{
get => state;
set
{
if (value == state) return;
state = value;
this.FadeTo(state == Visibility.Hidden ? 0 : 1, 250, Easing.OutQuint);
StateChanged?.Invoke(state);
}
}
public override void Hide() => State = Visibility.Hidden;
public override void Show() => State = Visibility.Visible;
public event Action<Visibility> StateChanged;
2022-05-06 19:34:44 +08:00
public partial class BoxWithBorders : CompositeDrawable
{
private readonly LayoutValue cache = new LayoutValue(Invalidation.RequiredParentSizeToFit);
public BoxWithBorders()
{
AddLayout(cache);
}
protected override void Update()
{
base.Update();
if (!cache.IsValid)
{
createContent();
cache.Validate();
}
}
private void createContent()
{
if (DrawSize == Vector2.Zero)
{
ClearInternal();
return;
}
// Make lines the same width independent of display resolution.
float lineThickness = DrawWidth > 0
? DrawWidth / ScreenSpaceDrawQuad.Width * 2
: DrawHeight / ScreenSpaceDrawQuad.Height * 2;
Padding = new MarginPadding(-lineThickness / 2);
2022-05-06 19:34:44 +08:00
InternalChildren = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.X,
Height = lineThickness,
},
new Box
{
RelativeSizeAxes = Axes.X,
Height = lineThickness,
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
},
new Box
{
RelativeSizeAxes = Axes.Y,
Width = lineThickness,
},
new Box
{
RelativeSizeAxes = Axes.Y,
Width = lineThickness,
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
},
new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0.1f
}
};
}
}
}
}