mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 04:12:57 +08:00
Merge pull request #23156 from Micha-ohne-el/editor-selection-rotation-snapping
Add rotation snapping to editor selection
This commit is contained in:
commit
8757276c37
@ -99,6 +99,16 @@ namespace osu.Game.Localisation
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static LocalisableString TimelineTicks => new TranslatableString(getKey(@"timeline_ticks"), @"Ticks");
|
public static LocalisableString TimelineTicks => new TranslatableString(getKey(@"timeline_ticks"), @"Ticks");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// "{0:0.0}°"
|
||||||
|
/// </summary>
|
||||||
|
public static LocalisableString RotationUnsnapped(float newRotation) => new TranslatableString(getKey(@"rotation_unsnapped"), @"{0:0.0}°", newRotation);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// "{0:0.0}° (snapped)"
|
||||||
|
/// </summary>
|
||||||
|
public static LocalisableString RotationSnapped(float newRotation) => new TranslatableString(getKey(@"rotation_snapped"), @"{0:0.0}° (snapped)", newRotation);
|
||||||
|
|
||||||
private static string getKey(string key) => $@"{prefix}:{key}";
|
private static string getKey(string key) => $@"{prefix}:{key}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,15 @@ using System;
|
|||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
using osu.Framework.Extensions.EnumExtensions;
|
using osu.Framework.Extensions.EnumExtensions;
|
||||||
using osu.Framework.Extensions.LocalisationExtensions;
|
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Cursor;
|
using osu.Framework.Graphics.Cursor;
|
||||||
using osu.Framework.Graphics.Sprites;
|
using osu.Framework.Graphics.Sprites;
|
||||||
using osu.Framework.Input.Events;
|
using osu.Framework.Input.Events;
|
||||||
using osu.Framework.Localisation;
|
using osu.Framework.Localisation;
|
||||||
|
using osu.Game.Localisation;
|
||||||
using osuTK;
|
using osuTK;
|
||||||
using osuTK.Graphics;
|
using osuTK.Graphics;
|
||||||
|
using Key = osuTK.Input.Key;
|
||||||
|
|
||||||
namespace osu.Game.Screens.Edit.Compose.Components
|
namespace osu.Game.Screens.Edit.Compose.Components
|
||||||
{
|
{
|
||||||
@ -26,6 +27,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
|||||||
|
|
||||||
private SpriteIcon icon;
|
private SpriteIcon icon;
|
||||||
|
|
||||||
|
private const float snap_step = 15;
|
||||||
|
|
||||||
private readonly Bindable<float?> cumulativeRotation = new Bindable<float?>();
|
private readonly Bindable<float?> cumulativeRotation = new Bindable<float?>();
|
||||||
|
|
||||||
[Resolved]
|
[Resolved]
|
||||||
@ -50,18 +53,14 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void LoadComplete()
|
|
||||||
{
|
|
||||||
base.LoadComplete();
|
|
||||||
cumulativeRotation.BindValueChanged(_ => updateTooltipText(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void UpdateHoverState()
|
protected override void UpdateHoverState()
|
||||||
{
|
{
|
||||||
base.UpdateHoverState();
|
base.UpdateHoverState();
|
||||||
icon.FadeColour(!IsHeld && IsHovered ? Color4.White : Color4.Black, TRANSFORM_DURATION, Easing.OutQuint);
|
icon.FadeColour(!IsHeld && IsHovered ? Color4.White : Color4.Black, TRANSFORM_DURATION, Easing.OutQuint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private float rawCumulativeRotation;
|
||||||
|
|
||||||
protected override bool OnDragStart(DragStartEvent e)
|
protected override bool OnDragStart(DragStartEvent e)
|
||||||
{
|
{
|
||||||
bool handle = base.OnDragStart(e);
|
bool handle = base.OnDragStart(e);
|
||||||
@ -74,21 +73,36 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
|||||||
{
|
{
|
||||||
base.OnDrag(e);
|
base.OnDrag(e);
|
||||||
|
|
||||||
float instantaneousAngle = convertDragEventToAngleOfRotation(e);
|
rawCumulativeRotation += convertDragEventToAngleOfRotation(e);
|
||||||
cumulativeRotation.Value += instantaneousAngle;
|
|
||||||
|
|
||||||
if (cumulativeRotation.Value < -180)
|
applyRotation(shouldSnap: e.ShiftPressed);
|
||||||
cumulativeRotation.Value += 360;
|
}
|
||||||
else if (cumulativeRotation.Value > 180)
|
|
||||||
cumulativeRotation.Value -= 360;
|
|
||||||
|
|
||||||
HandleRotate?.Invoke(instantaneousAngle);
|
protected override bool OnKeyDown(KeyDownEvent e)
|
||||||
|
{
|
||||||
|
if (IsDragged && (e.Key == Key.ShiftLeft || e.Key == Key.ShiftRight))
|
||||||
|
{
|
||||||
|
applyRotation(shouldSnap: true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return base.OnKeyDown(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnKeyUp(KeyUpEvent e)
|
||||||
|
{
|
||||||
|
base.OnKeyUp(e);
|
||||||
|
|
||||||
|
if (IsDragged && (e.Key == Key.ShiftLeft || e.Key == Key.ShiftRight))
|
||||||
|
applyRotation(shouldSnap: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnDragEnd(DragEndEvent e)
|
protected override void OnDragEnd(DragEndEvent e)
|
||||||
{
|
{
|
||||||
base.OnDragEnd(e);
|
base.OnDragEnd(e);
|
||||||
cumulativeRotation.Value = null;
|
cumulativeRotation.Value = null;
|
||||||
|
rawCumulativeRotation = 0;
|
||||||
|
TooltipText = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
private float convertDragEventToAngleOfRotation(DragEvent e)
|
private float convertDragEventToAngleOfRotation(DragEvent e)
|
||||||
@ -100,9 +114,19 @@ namespace osu.Game.Screens.Edit.Compose.Components
|
|||||||
return (endAngle - startAngle) * 180 / MathF.PI;
|
return (endAngle - startAngle) * 180 / MathF.PI;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateTooltipText()
|
private void applyRotation(bool shouldSnap)
|
||||||
{
|
{
|
||||||
TooltipText = cumulativeRotation.Value?.ToLocalisableString("0.0°") ?? default;
|
float oldRotation = cumulativeRotation.Value ?? 0;
|
||||||
|
|
||||||
|
float newRotation = shouldSnap ? snap(rawCumulativeRotation, snap_step) : rawCumulativeRotation;
|
||||||
|
newRotation = (newRotation - 180) % 360 + 180;
|
||||||
|
|
||||||
|
cumulativeRotation.Value = newRotation;
|
||||||
|
|
||||||
|
HandleRotate?.Invoke(newRotation - oldRotation);
|
||||||
|
TooltipText = shouldSnap ? EditorStrings.RotationSnapped(newRotation) : EditorStrings.RotationUnsnapped(newRotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private float snap(float value, float step) => MathF.Round(value / step) * step;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user