2021-04-24 12:47:15 +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.
|
|
|
|
|
2022-01-09 00:19:52 +08:00
|
|
|
using System;
|
2021-04-24 12:47:15 +08:00
|
|
|
using osu.Framework.Allocation;
|
2022-01-09 00:42:58 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-04-24 12:47:15 +08:00
|
|
|
using osu.Framework.Extensions.EnumExtensions;
|
2022-01-09 00:42:58 +08:00
|
|
|
using osu.Framework.Extensions.LocalisationExtensions;
|
2021-04-24 12:47:15 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-01-09 00:42:58 +08:00
|
|
|
using osu.Framework.Graphics.Cursor;
|
2021-04-24 12:47:15 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
2022-01-09 00:19:52 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2022-01-09 00:42:58 +08:00
|
|
|
using osu.Framework.Localisation;
|
2021-04-24 12:47:15 +08:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Compose.Components
|
|
|
|
{
|
2022-01-09 00:42:58 +08:00
|
|
|
public class SelectionBoxRotationHandle : SelectionBoxDragHandle, IHasTooltip
|
2021-04-24 12:47:15 +08:00
|
|
|
{
|
2022-01-09 00:19:52 +08:00
|
|
|
public Action<float> HandleRotate { get; set; }
|
|
|
|
|
2022-01-09 00:42:58 +08:00
|
|
|
public LocalisableString TooltipText { get; private set; }
|
|
|
|
|
2021-04-24 12:47:15 +08:00
|
|
|
private SpriteIcon icon;
|
|
|
|
|
2022-01-09 00:42:58 +08:00
|
|
|
private readonly Bindable<float?> cumulativeRotation = new Bindable<float?>();
|
|
|
|
|
2022-01-09 00:19:52 +08:00
|
|
|
[Resolved]
|
|
|
|
private SelectionBox selectionBox { get; set; }
|
|
|
|
|
2021-04-24 12:47:15 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
Size = new Vector2(15f);
|
|
|
|
AddInternal(icon = new SpriteIcon
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Size = new Vector2(0.5f),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Icon = FontAwesome.Solid.Redo,
|
|
|
|
Scale = new Vector2
|
|
|
|
{
|
|
|
|
X = Anchor.HasFlagFast(Anchor.x0) ? 1f : -1f,
|
|
|
|
Y = Anchor.HasFlagFast(Anchor.y0) ? 1f : -1f
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:42:58 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
cumulativeRotation.BindValueChanged(_ => updateTooltipText(), true);
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:47:15 +08:00
|
|
|
protected override void UpdateHoverState()
|
|
|
|
{
|
|
|
|
base.UpdateHoverState();
|
2021-05-04 11:40:43 +08:00
|
|
|
icon.FadeColour(!IsHeld && IsHovered ? Color4.White : Color4.Black, TRANSFORM_DURATION, Easing.OutQuint);
|
2021-04-24 12:47:15 +08:00
|
|
|
}
|
2022-01-09 00:19:52 +08:00
|
|
|
|
2022-01-09 00:42:58 +08:00
|
|
|
protected override bool OnDragStart(DragStartEvent e)
|
|
|
|
{
|
|
|
|
bool handle = base.OnDragStart(e);
|
|
|
|
if (handle)
|
|
|
|
cumulativeRotation.Value = 0;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:19:52 +08:00
|
|
|
protected override void OnDrag(DragEvent e)
|
|
|
|
{
|
|
|
|
base.OnDrag(e);
|
2022-01-09 00:42:58 +08:00
|
|
|
|
|
|
|
float instantaneousAngle = convertDragEventToAngleOfRotation(e);
|
|
|
|
cumulativeRotation.Value += instantaneousAngle;
|
|
|
|
|
|
|
|
if (cumulativeRotation.Value < -180)
|
|
|
|
cumulativeRotation.Value += 360;
|
|
|
|
else if (cumulativeRotation.Value > 180)
|
|
|
|
cumulativeRotation.Value -= 360;
|
|
|
|
|
|
|
|
HandleRotate?.Invoke(instantaneousAngle);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDragEnd(DragEndEvent e)
|
|
|
|
{
|
|
|
|
base.OnDragEnd(e);
|
|
|
|
cumulativeRotation.Value = null;
|
2022-01-09 00:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private float convertDragEventToAngleOfRotation(DragEvent e)
|
|
|
|
{
|
|
|
|
// Adjust coordinate system to the center of SelectionBox
|
|
|
|
float startAngle = MathF.Atan2(e.LastMousePosition.Y - selectionBox.DrawHeight / 2, e.LastMousePosition.X - selectionBox.DrawWidth / 2);
|
|
|
|
float endAngle = MathF.Atan2(e.MousePosition.Y - selectionBox.DrawHeight / 2, e.MousePosition.X - selectionBox.DrawWidth / 2);
|
|
|
|
|
|
|
|
return (endAngle - startAngle) * 180 / MathF.PI;
|
|
|
|
}
|
2022-01-09 00:42:58 +08:00
|
|
|
|
|
|
|
private void updateTooltipText()
|
|
|
|
{
|
2022-05-02 20:07:53 +08:00
|
|
|
TooltipText = cumulativeRotation.Value?.ToLocalisableString("0.0°") ?? default;
|
2022-01-09 00:42:58 +08:00
|
|
|
}
|
2021-04-24 12:47:15 +08:00
|
|
|
}
|
|
|
|
}
|