1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 09:17:48 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/SelectionBoxRotationHandle.cs

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

137 lines
4.3 KiB
C#
Raw Normal View History

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.
using System;
2021-04-24 12:47:15 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2021-04-24 12:47:15 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
2021-04-24 12:47:15 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Localisation;
2021-04-24 12:47:15 +08:00
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
2021-04-24 12:47:15 +08:00
namespace osu.Game.Screens.Edit.Compose.Components
{
public partial class SelectionBoxRotationHandle : SelectionBoxDragHandle, IHasTooltip
2021-04-24 12:47:15 +08:00
{
public LocalisableString TooltipText { get; private set; }
private SpriteIcon icon = null!;
2021-04-24 12:47:15 +08:00
private const float snap_step = 15;
private readonly Bindable<float?> cumulativeRotation = new Bindable<float?>();
[Resolved]
private SelectionBox selectionBox { get; set; } = null!;
[Resolved]
private SelectionRotationHandler? rotationHandler { 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
{
2024-07-02 23:19:04 +08:00
X = Anchor.HasFlag(Anchor.x0) ? 1f : -1f,
Y = Anchor.HasFlag(Anchor.y0) ? 1f : -1f
2021-04-24 12:47:15 +08:00
}
});
}
protected override void UpdateHoverState()
{
base.UpdateHoverState();
icon.FadeColour(!IsHeld && IsHovered ? Color4.White : Color4.Black, TRANSFORM_DURATION, Easing.OutQuint);
2021-04-24 12:47:15 +08:00
}
private float rawCumulativeRotation;
protected override bool OnDragStart(DragStartEvent e)
{
if (e.Button != MouseButton.Left)
return false;
if (rotationHandler == null) return false;
if (rotationHandler.OperationInProgress.Value)
return false;
rotationHandler.Begin();
return true;
}
protected override void OnDrag(DragEvent e)
{
base.OnDrag(e);
rawCumulativeRotation += convertDragEventToAngleOfRotation(e);
applyRotation(shouldSnap: e.ShiftPressed);
}
protected override bool OnKeyDown(KeyDownEvent e)
{
2023-04-08 21:18:29 +08:00
if (IsDragged && (e.Key == Key.ShiftLeft || e.Key == Key.ShiftRight))
{
applyRotation(shouldSnap: true);
2023-04-08 21:18:29 +08:00
return true;
}
2023-04-08 21:18:29 +08:00
return base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyUpEvent e)
{
base.OnKeyUp(e);
2023-04-08 21:18:29 +08:00
if (IsDragged && (e.Key == Key.ShiftLeft || e.Key == Key.ShiftRight))
applyRotation(shouldSnap: false);
}
protected override void OnDragEnd(DragEndEvent e)
{
rotationHandler?.Commit();
UpdateHoverState();
cumulativeRotation.Value = null;
rawCumulativeRotation = 0;
TooltipText = default;
}
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;
}
private void applyRotation(bool shouldSnap)
{
float newRotation = shouldSnap ? snap(rawCumulativeRotation, snap_step) : MathF.Round(rawCumulativeRotation);
newRotation = (newRotation - 180) % 360 + 180;
cumulativeRotation.Value = newRotation;
rotationHandler?.Update(newRotation);
TooltipText = shouldSnap ? EditorStrings.RotationSnapped(newRotation) : EditorStrings.RotationUnsnapped(newRotation);
}
2023-04-10 20:55:30 +08:00
private float snap(float value, float step) => MathF.Round(value / step) * step;
2021-04-24 12:47:15 +08:00
}
}