From 98505d0bba18fe4b08c06bf44d77298040c1c5e8 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Sun, 31 Dec 2023 18:58:29 +0100 Subject: [PATCH] improve grid from points tool code --- .../Edit/GridFromPointsTool.cs | 72 +++++++++++++++++++ .../Edit/OsuBlueprintContainer.cs | 63 ---------------- .../Edit/OsuHitObjectComposer.cs | 18 +++-- osu.Game/Rulesets/Edit/HitObjectComposer.cs | 5 +- 4 files changed, 90 insertions(+), 68 deletions(-) create mode 100644 osu.Game.Rulesets.Osu/Edit/GridFromPointsTool.cs diff --git a/osu.Game.Rulesets.Osu/Edit/GridFromPointsTool.cs b/osu.Game.Rulesets.Osu/Edit/GridFromPointsTool.cs new file mode 100644 index 0000000000..722d9d1303 --- /dev/null +++ b/osu.Game.Rulesets.Osu/Edit/GridFromPointsTool.cs @@ -0,0 +1,72 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Input.Events; +using osu.Game.Rulesets.Edit; +using osuTK; +using osuTK.Input; + +namespace osu.Game.Rulesets.Osu.Edit +{ + public partial class GridFromPointsTool : Drawable + { + [Resolved] + private OsuGridToolboxGroup gridToolboxGroup { get; set; } = null!; + + [Resolved(canBeNull: true)] + private IPositionSnapProvider? snapProvider { get; set; } + + public bool IsPlacing { get; private set; } + + private Vector2? startPosition; + + public void BeginPlacement() + { + IsPlacing = true; + startPosition = null; + } + + protected override bool OnMouseDown(MouseDownEvent e) + { + if (!IsPlacing) + return base.OnMouseDown(e); + + var pos = snappedLocalPosition(e); + + if (!startPosition.HasValue) + startPosition = pos; + else + { + gridToolboxGroup.SetGridFromPoints(startPosition.Value, pos); + IsPlacing = false; + } + + if (e.Button == MouseButton.Right) + IsPlacing = false; + + return true; + } + + protected override bool OnMouseMove(MouseMoveEvent e) + { + if (!IsPlacing) + return base.OnMouseMove(e); + + var pos = snappedLocalPosition(e); + + if (!startPosition.HasValue) + gridToolboxGroup.StartPosition.Value = pos; + else + gridToolboxGroup.SetGridFromPoints(startPosition.Value, pos); + + return true; + } + + private Vector2 snappedLocalPosition(UIEvent e) + { + return ToLocalSpace(snapProvider?.FindSnappedPositionAndTime(e.ScreenSpaceMousePosition, ~SnapType.GlobalGrids).ScreenSpacePosition ?? e.ScreenSpaceMousePosition); + } + } +} diff --git a/osu.Game.Rulesets.Osu/Edit/OsuBlueprintContainer.cs b/osu.Game.Rulesets.Osu/Edit/OsuBlueprintContainer.cs index 45cf754c57..54c54fca17 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuBlueprintContainer.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuBlueprintContainer.cs @@ -1,8 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using osu.Framework.Allocation; -using osu.Framework.Input.Events; using osu.Game.Rulesets.Edit; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles; @@ -10,27 +8,16 @@ using osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders; using osu.Game.Rulesets.Osu.Edit.Blueprints.Spinners; using osu.Game.Rulesets.Osu.Objects; using osu.Game.Screens.Edit.Compose.Components; -using osuTK; -using osuTK.Input; namespace osu.Game.Rulesets.Osu.Edit { public partial class OsuBlueprintContainer : ComposeBlueprintContainer { - private OsuGridToolboxGroup gridToolbox = null!; - public OsuBlueprintContainer(HitObjectComposer composer) : base(composer) { } - [BackgroundDependencyLoader] - private void load(OsuGridToolboxGroup gridToolbox) - { - this.gridToolbox = gridToolbox; - gridToolbox.GridFromPointsClicked += OnGridFromPointsClicked; - } - protected override SelectionHandler CreateSelectionHandler() => new OsuSelectionHandler(); public override HitObjectSelectionBlueprint? CreateHitObjectBlueprintFor(HitObject hitObject) @@ -49,55 +36,5 @@ namespace osu.Game.Rulesets.Osu.Edit return base.CreateHitObjectBlueprintFor(hitObject); } - - private bool isPlacingGridFromPoints; - private Vector2? gridFromPointsStart; - - private void OnGridFromPointsClicked() - { - isPlacingGridFromPoints = true; - gridFromPointsStart = null; - - // Deselect all objects because we cant snap to objects which are selected. - DeselectAll(); - } - - protected override bool OnMouseDown(MouseDownEvent e) - { - if (!isPlacingGridFromPoints) - return base.OnMouseDown(e); - - var pos = snappedLocalPosition(e); - - if (!gridFromPointsStart.HasValue) - gridFromPointsStart = pos; - else - { - gridToolbox.SetGridFromPoints(gridFromPointsStart.Value, pos); - isPlacingGridFromPoints = false; - } - - if (e.Button == MouseButton.Right) - isPlacingGridFromPoints = false; - - return true; - } - - protected override bool OnMouseMove(MouseMoveEvent e) - { - if (!isPlacingGridFromPoints) - return base.OnMouseMove(e); - - var pos = snappedLocalPosition(e); - - if (!gridFromPointsStart.HasValue) - gridToolbox.StartPosition.Value = pos; - else - gridToolbox.SetGridFromPoints(gridFromPointsStart.Value, pos); - - return true; - } - - private Vector2 snappedLocalPosition(UIEvent e) => ToLocalSpace(Composer.FindSnappedPositionAndTime(e.ScreenSpaceMousePosition, ~SnapType.GlobalGrids).ScreenSpacePosition); } } diff --git a/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs b/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs index 02e98d75a7..e035317194 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs @@ -81,13 +81,12 @@ namespace osu.Game.Rulesets.Osu.Edit // Give a bit of breathing room around the playfield content. PlayfieldContentContainer.Padding = new MarginPadding(10); - LayerBelowRuleset.AddRange(new Drawable[] - { + LayerBelowRuleset.Add( distanceSnapGridContainer = new Container { RelativeSizeAxes = Axes.Both } - }); + ); selectedHitObjects = EditorBeatmap.SelectedHitObjects.GetBoundCopy(); selectedHitObjects.CollectionChanged += (_, _) => updateDistanceSnapGrid(); @@ -100,6 +99,15 @@ namespace osu.Game.Rulesets.Osu.Edit updateDistanceSnapGrid(); OsuGridToolboxGroup.GridType.BindValueChanged(updatePositionSnapGrid, true); + OsuGridToolboxGroup.GridFromPointsClicked += () => gridFromPointsTool.BeginPlacement(); + + LayerAboveRuleset.Add( + // Place it above the playfield and blueprints, so it takes priority when handling input. + gridFromPointsTool = new GridFromPointsTool + { + RelativeSizeAxes = Axes.Both, + } + ); RightToolbox.AddRange(new EditorToolboxGroup[] { @@ -110,6 +118,8 @@ namespace osu.Game.Rulesets.Osu.Edit ); } + private GridFromPointsTool gridFromPointsTool; + private void updatePositionSnapGrid(ValueChangedEvent obj) { if (positionSnapGrid != null) @@ -278,7 +288,7 @@ namespace osu.Game.Rulesets.Osu.Edit foreach (var b in blueprints) { - if (b.IsSelected) + if (b.IsSelected && !gridFromPointsTool.IsPlacing) continue; var snapPositions = b.ScreenSpaceSnapPoints; diff --git a/osu.Game/Rulesets/Edit/HitObjectComposer.cs b/osu.Game/Rulesets/Edit/HitObjectComposer.cs index 50e6393895..4f8329cc82 100644 --- a/osu.Game/Rulesets/Edit/HitObjectComposer.cs +++ b/osu.Game/Rulesets/Edit/HitObjectComposer.cs @@ -76,6 +76,8 @@ namespace osu.Game.Rulesets.Edit protected readonly Container LayerBelowRuleset = new Container { RelativeSizeAxes = Axes.Both }; + protected readonly Container LayerAboveRuleset = new Container { RelativeSizeAxes = Axes.Both }; + protected InputManager InputManager { get; private set; } private EditorRadioButtonCollection toolboxCollection; @@ -137,7 +139,8 @@ namespace osu.Game.Rulesets.Edit drawableRulesetWrapper, // layers above playfield drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer() - .WithChild(BlueprintContainer = CreateBlueprintContainer()) + .WithChild(BlueprintContainer = CreateBlueprintContainer()), + drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer().WithChild(LayerAboveRuleset), } }, new Container