1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 03:52:54 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/OsuGridToolboxGroup.cs

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

321 lines
12 KiB
C#
Raw Normal View History

2023-12-29 05:36:30 +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.
2023-12-31 02:16:05 +08:00
using System;
using System.Linq;
2023-12-29 05:36:30 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2023-12-29 06:10:06 +08:00
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
2023-12-31 10:53:42 +08:00
using osu.Game.Graphics.Containers;
2023-12-29 05:36:30 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
2023-12-29 06:10:06 +08:00
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Osu.UI;
2023-12-29 05:36:30 +08:00
using osu.Game.Screens.Edit;
using osu.Game.Screens.Edit.Components.RadioButtons;
using osuTK;
using osuTK.Graphics;
2023-12-29 05:36:30 +08:00
2023-12-29 06:10:06 +08:00
namespace osu.Game.Rulesets.Osu.Edit
2023-12-29 05:36:30 +08:00
{
2023-12-29 06:10:06 +08:00
public partial class OsuGridToolboxGroup : EditorToolboxGroup, IKeyBindingHandler<GlobalAction>
2023-12-29 05:36:30 +08:00
{
[Resolved]
private EditorBeatmap editorBeatmap { get; set; } = null!;
2023-12-31 10:53:42 +08:00
[Resolved]
private IExpandingContainer? expandingContainer { get; set; }
/// <summary>
/// X position of the grid's origin.
/// </summary>
2023-12-29 06:10:06 +08:00
public BindableFloat StartPositionX { get; } = new BindableFloat(OsuPlayfield.BASE_SIZE.X / 2)
2023-12-29 05:36:30 +08:00
{
MinValue = 0f,
2023-12-29 06:10:06 +08:00
MaxValue = OsuPlayfield.BASE_SIZE.X,
2023-12-29 05:36:30 +08:00
};
/// <summary>
/// Y position of the grid's origin.
/// </summary>
2023-12-29 06:10:06 +08:00
public BindableFloat StartPositionY { get; } = new BindableFloat(OsuPlayfield.BASE_SIZE.Y / 2)
2023-12-29 05:36:30 +08:00
{
MinValue = 0f,
2023-12-29 06:10:06 +08:00
MaxValue = OsuPlayfield.BASE_SIZE.Y,
2023-12-29 05:36:30 +08:00
};
/// <summary>
/// The spacing between grid lines.
/// </summary>
2023-12-29 05:36:30 +08:00
public BindableFloat Spacing { get; } = new BindableFloat(4f)
{
MinValue = 4f,
MaxValue = 128f,
};
/// <summary>
/// Rotation of the grid lines in degrees.
/// </summary>
2023-12-29 05:36:30 +08:00
public BindableFloat GridLinesRotation { get; } = new BindableFloat(0f)
{
MinValue = -180f,
MaxValue = 180f,
2023-12-29 05:36:30 +08:00
};
/// <summary>
/// Read-only bindable representing the grid's origin.
/// Equivalent to <code>new Vector2(StartPositionX, StartPositionY)</code>
/// </summary>
public Bindable<Vector2> StartPosition { get; } = new Bindable<Vector2>();
/// <summary>
/// Read-only bindable representing the grid's spacing in both the X and Y dimension.
/// Equivalent to <code>new Vector2(Spacing)</code>
/// </summary>
public Bindable<Vector2> SpacingVector { get; } = new Bindable<Vector2>();
public Bindable<PositionSnapGridType> GridType { get; } = new Bindable<PositionSnapGridType>();
2023-12-29 05:36:30 +08:00
private ExpandableSlider<float> startPositionXSlider = null!;
private ExpandableSlider<float> startPositionYSlider = null!;
private ExpandableSlider<float> spacingSlider = null!;
private ExpandableSlider<float> gridLinesRotationSlider = null!;
private RoundedButton gridFromPointsButton = null!;
private EditorRadioButtonCollection gridTypeButtons = null!;
2023-12-29 05:36:30 +08:00
public event Action? GridFromPointsClicked;
2023-12-29 06:10:06 +08:00
public OsuGridToolboxGroup()
: base("grid")
{
}
private const float max_automatic_spacing = 64;
2023-12-31 02:16:05 +08:00
public void SetGridFromPoints(Vector2 point1, Vector2 point2)
{
StartPositionX.Value = point1.X;
StartPositionY.Value = point1.Y;
2023-12-31 03:22:14 +08:00
// Get the angle between the two points and normalize to the valid range.
if (!GridLinesRotation.Disabled)
{
float period = GridLinesRotation.MaxValue - GridLinesRotation.MinValue;
GridLinesRotation.Value = normalizeRotation(MathHelper.RadiansToDegrees(MathF.Atan2(point2.Y - point1.Y, point2.X - point1.X)), period);
}
2023-12-31 03:22:14 +08:00
// Divide the distance so that there is a good density of grid lines.
// This matches the maximum grid size of the grid size cycling hotkey.
2023-12-31 02:16:05 +08:00
float dist = Vector2.Distance(point1, point2);
2024-01-01 23:14:20 +08:00
while (dist >= max_automatic_spacing)
2023-12-31 02:16:05 +08:00
dist /= 2;
Spacing.Value = dist;
}
2023-12-29 05:36:30 +08:00
[BackgroundDependencyLoader]
private void load()
{
Children = new Drawable[]
{
startPositionXSlider = new ExpandableSlider<float>
{
2023-12-31 23:41:22 +08:00
Current = StartPositionX,
KeyboardStep = 1,
2023-12-29 05:36:30 +08:00
},
startPositionYSlider = new ExpandableSlider<float>
{
2023-12-31 23:41:22 +08:00
Current = StartPositionY,
KeyboardStep = 1,
2023-12-29 05:36:30 +08:00
},
spacingSlider = new ExpandableSlider<float>
{
2023-12-31 23:41:22 +08:00
Current = Spacing,
KeyboardStep = 1,
2023-12-29 05:36:30 +08:00
},
gridLinesRotationSlider = new ExpandableSlider<float>
{
2023-12-31 23:41:22 +08:00
Current = GridLinesRotation,
KeyboardStep = 1,
2023-12-29 23:59:12 +08:00
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0f, 10f),
Children = new Drawable[]
{
gridFromPointsButton = new RoundedButton
{
Action = () => GridFromPointsClicked?.Invoke(),
RelativeSizeAxes = Axes.X,
Text = "Grid from points",
TooltipText = """
Left click to set the origin.
2024-10-01 21:38:21 +08:00
Left click again to set the spacing and rotation.
Right click to only set the origin.
Click and drag to set the origin, spacing and rotation.
"""
},
gridTypeButtons = new EditorRadioButtonCollection
{
RelativeSizeAxes = Axes.X,
Items = new[]
{
new RadioButton("Square",
() => GridType.Value = PositionSnapGridType.Square,
() => new SpriteIcon { Icon = FontAwesome.Regular.Square }),
new RadioButton("Triangle",
() => GridType.Value = PositionSnapGridType.Triangle,
() => new OutlineTriangle(true, 20)),
new RadioButton("Circle",
() => GridType.Value = PositionSnapGridType.Circle,
() => new SpriteIcon { Icon = FontAwesome.Regular.Circle }),
}
},
}
},
2023-12-29 05:36:30 +08:00
};
2023-12-29 06:10:06 +08:00
2023-12-30 21:32:20 +08:00
Spacing.Value = editorBeatmap.BeatmapInfo.GridSize;
2023-12-29 05:36:30 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
gridTypeButtons.Items.First().Select();
2023-12-29 05:36:30 +08:00
StartPositionX.BindValueChanged(x =>
{
2023-12-31 03:22:14 +08:00
startPositionXSlider.ContractedLabelText = $"X: {x.NewValue:#,0.##}";
startPositionXSlider.ExpandedLabelText = $"X Offset: {x.NewValue:#,0.##}";
StartPosition.Value = new Vector2(x.NewValue, StartPosition.Value.Y);
2023-12-29 05:36:30 +08:00
}, true);
StartPositionY.BindValueChanged(y =>
{
2023-12-31 03:22:14 +08:00
startPositionYSlider.ContractedLabelText = $"Y: {y.NewValue:#,0.##}";
startPositionYSlider.ExpandedLabelText = $"Y Offset: {y.NewValue:#,0.##}";
StartPosition.Value = new Vector2(StartPosition.Value.X, y.NewValue);
2023-12-29 05:36:30 +08:00
}, true);
2023-12-31 03:22:14 +08:00
StartPosition.BindValueChanged(pos =>
{
StartPositionX.Value = pos.NewValue.X;
StartPositionY.Value = pos.NewValue.Y;
});
2023-12-29 05:36:30 +08:00
Spacing.BindValueChanged(spacing =>
{
2023-12-31 03:22:14 +08:00
spacingSlider.ContractedLabelText = $"S: {spacing.NewValue:#,0.##}";
spacingSlider.ExpandedLabelText = $"Spacing: {spacing.NewValue:#,0.##}";
SpacingVector.Value = new Vector2(spacing.NewValue);
2023-12-30 21:32:20 +08:00
editorBeatmap.BeatmapInfo.GridSize = (int)spacing.NewValue;
2023-12-29 05:36:30 +08:00
}, true);
GridLinesRotation.BindValueChanged(rotation =>
{
gridLinesRotationSlider.ContractedLabelText = $"R: {rotation.NewValue:#,0.##}";
gridLinesRotationSlider.ExpandedLabelText = $"Rotation: {rotation.NewValue:#,0.##}";
}, true);
GridType.BindValueChanged(v =>
{
GridLinesRotation.Disabled = v.NewValue == PositionSnapGridType.Circle;
switch (v.NewValue)
{
case PositionSnapGridType.Square:
GridLinesRotation.Value = normalizeRotation(GridLinesRotation.Value, 90);
GridLinesRotation.MinValue = -45;
GridLinesRotation.MaxValue = 45;
break;
case PositionSnapGridType.Triangle:
GridLinesRotation.Value = normalizeRotation(GridLinesRotation.Value, 60);
GridLinesRotation.MinValue = -30;
GridLinesRotation.MaxValue = 30;
break;
}
}, true);
expandingContainer?.Expanded.BindValueChanged(v =>
{
gridFromPointsButton.FadeTo(v.NewValue ? 1f : 0f, 500, Easing.OutQuint);
gridFromPointsButton.BypassAutoSizeAxes = !v.NewValue ? Axes.Y : Axes.None;
gridTypeButtons.FadeTo(v.NewValue ? 1f : 0f, 500, Easing.OutQuint);
gridTypeButtons.BypassAutoSizeAxes = !v.NewValue ? Axes.Y : Axes.None;
}, true);
}
private float normalizeRotation(float rotation, float period)
{
return ((rotation + 360 + period * 0.5f) % period) - period * 0.5f;
}
private void nextGridSize()
2023-12-29 06:10:06 +08:00
{
Spacing.Value = Spacing.Value * 2 >= max_automatic_spacing ? Spacing.Value / 8 : Spacing.Value * 2;
2023-12-29 06:10:06 +08:00
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.EditorCycleGridDisplayMode:
nextGridSize();
2023-12-29 06:10:06 +08:00
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
public partial class OutlineTriangle : BufferedContainer
{
public OutlineTriangle(bool outlineOnly, float size)
: base(cachedFrameBuffer: true)
{
Size = new Vector2(size);
InternalChildren = new Drawable[]
{
new EquilateralTriangle { RelativeSizeAxes = Axes.Both },
};
if (outlineOnly)
{
AddInternal(new EquilateralTriangle
{
Anchor = Anchor.TopCentre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Y,
Y = 0.48f,
Colour = Color4.Black,
Size = new Vector2(size - 7),
Blending = BlendingParameters.None,
});
}
Blending = BlendingParameters.Additive;
}
}
}
public enum PositionSnapGridType
{
Square,
Triangle,
Circle,
2023-12-29 23:59:12 +08:00
}
2023-12-29 05:36:30 +08:00
}