From 262f25dce826574cbfd466fbecf52e151dabe642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sun, 30 Jul 2023 19:39:30 +0200 Subject: [PATCH] Make `SelectionRotationHandler` a `Component` --- .../Edit/OsuSelectionHandler.cs | 5 +--- .../Edit/OsuSelectionRotationHandler.cs | 23 +++++++++----- .../Editing/TestSceneComposeSelectBox.cs | 2 +- .../SkinEditor/SkinSelectionHandler.cs | 3 +- .../SkinSelectionRotationHandler.cs | 30 ++++++++++++------- .../Compose/Components/SelectionHandler.cs | 8 +++-- .../Components/SelectionRotationHandler.cs | 3 +- 7 files changed, 46 insertions(+), 28 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Edit/OsuSelectionHandler.cs b/osu.Game.Rulesets.Osu/Edit/OsuSelectionHandler.cs index 1dfbf4179b..e81941d254 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuSelectionHandler.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuSelectionHandler.cs @@ -163,10 +163,7 @@ namespace osu.Game.Rulesets.Osu.Edit if ((reference & Anchor.y0) > 0) scale.Y = -scale.Y; } - public override SelectionRotationHandler CreateRotationHandler() => new OsuSelectionRotationHandler(ChangeHandler) - { - SelectedItems = { BindTarget = SelectedItems } - }; + public override SelectionRotationHandler CreateRotationHandler() => new OsuSelectionRotationHandler(); private void scaleSlider(Slider slider, Vector2 scale) { diff --git a/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs b/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs index 0eb7637786..21fb8a67de 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuSelectionRotationHandler.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects.Types; @@ -16,17 +17,25 @@ using osuTK; namespace osu.Game.Rulesets.Osu.Edit { - public class OsuSelectionRotationHandler : SelectionRotationHandler + public partial class OsuSelectionRotationHandler : SelectionRotationHandler { - private readonly IEditorChangeHandler? changeHandler; + [Resolved] + private IEditorChangeHandler? changeHandler { get; set; } - public BindableList SelectedItems { get; } = new BindableList(); + private BindableList selectedItems { get; } = new BindableList(); - public OsuSelectionRotationHandler(IEditorChangeHandler? changeHandler) + [BackgroundDependencyLoader] + private void load(EditorBeatmap editorBeatmap) { - this.changeHandler = changeHandler; + selectedItems.BindTo(editorBeatmap.SelectedHitObjects); + } - SelectedItems.CollectionChanged += (_, __) => updateState(); + protected override void LoadComplete() + { + base.LoadComplete(); + + selectedItems.CollectionChanged += (_, __) => updateState(); + updateState(); } private void updateState() @@ -92,7 +101,7 @@ namespace osu.Game.Rulesets.Osu.Edit defaultOrigin = null; } - private IEnumerable selectedMovableObjects => SelectedItems.Cast() + private IEnumerable selectedMovableObjects => selectedItems.Cast() .Where(h => h is not Spinner); } } diff --git a/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs b/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs index 147488812e..9901118ce8 100644 --- a/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs +++ b/osu.Game.Tests/Visual/Editing/TestSceneComposeSelectBox.cs @@ -72,7 +72,7 @@ namespace osu.Game.Tests.Visual.Editing return true; } - private class TestSelectionRotationHandler : SelectionRotationHandler + private partial class TestSelectionRotationHandler : SelectionRotationHandler { private readonly Func getTargetContainer; diff --git a/osu.Game/Overlays/SkinEditor/SkinSelectionHandler.cs b/osu.Game/Overlays/SkinEditor/SkinSelectionHandler.cs index bee973bea0..72216f040e 100644 --- a/osu.Game/Overlays/SkinEditor/SkinSelectionHandler.cs +++ b/osu.Game/Overlays/SkinEditor/SkinSelectionHandler.cs @@ -26,9 +26,8 @@ namespace osu.Game.Overlays.SkinEditor [Resolved] private SkinEditor skinEditor { get; set; } = null!; - public override SelectionRotationHandler CreateRotationHandler() => new SkinSelectionRotationHandler(ChangeHandler) + public override SelectionRotationHandler CreateRotationHandler() => new SkinSelectionRotationHandler { - SelectedItems = { BindTarget = SelectedItems }, UpdatePosition = updateDrawablePosition }; diff --git a/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs b/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs index e60e2b1e12..60f69000a2 100644 --- a/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs +++ b/osu.Game/Overlays/SkinEditor/SkinSelectionRotationHandler.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Game.Screens.Edit; @@ -15,23 +16,32 @@ using osuTK; namespace osu.Game.Overlays.SkinEditor { - public class SkinSelectionRotationHandler : SelectionRotationHandler + public partial class SkinSelectionRotationHandler : SelectionRotationHandler { - private readonly IEditorChangeHandler? changeHandler; - - public BindableList SelectedItems { get; } = new BindableList(); public Action UpdatePosition { get; init; } = null!; - public SkinSelectionRotationHandler(IEditorChangeHandler? changeHandler) - { - this.changeHandler = changeHandler; + [Resolved] + private IEditorChangeHandler? changeHandler { get; set; } - SelectedItems.CollectionChanged += (_, __) => updateState(); + private BindableList selectedItems { get; } = new BindableList(); + + [BackgroundDependencyLoader] + private void load(SkinEditor skinEditor) + { + selectedItems.BindTo(skinEditor.SelectedComponents); + } + + protected override void LoadComplete() + { + base.LoadComplete(); + + selectedItems.CollectionChanged += (_, __) => updateState(); + updateState(); } private void updateState() { - CanRotate.Value = SelectedItems.Count > 0; + CanRotate.Value = selectedItems.Count > 0; } private Drawable[]? objectsInRotation; @@ -47,7 +57,7 @@ namespace osu.Game.Overlays.SkinEditor changeHandler?.BeginChange(); - objectsInRotation = SelectedItems.Cast().ToArray(); + objectsInRotation = selectedItems.Cast().ToArray(); originalRotations = objectsInRotation.ToDictionary(d => d, d => d.Rotation); originalPositions = objectsInRotation.ToDictionary(d => d, d => d.ToScreenSpace(d.OriginPosition)); defaultOrigin = GeometryUtils.GetSurroundingQuad(objectsInRotation.SelectMany(d => d.ScreenSpaceDrawQuad.GetVertices().ToArray())).Centre; diff --git a/osu.Game/Screens/Edit/Compose/Components/SelectionHandler.cs b/osu.Game/Screens/Edit/Compose/Components/SelectionHandler.cs index 31ad8fa3d7..57f9513cc1 100644 --- a/osu.Game/Screens/Edit/Compose/Components/SelectionHandler.cs +++ b/osu.Game/Screens/Edit/Compose/Components/SelectionHandler.cs @@ -68,9 +68,11 @@ namespace osu.Game.Screens.Edit.Compose.Components [BackgroundDependencyLoader] private void load() { - RotationHandler = CreateRotationHandler(); - - InternalChild = SelectionBox = CreateSelectionBox(); + AddRangeInternal(new Drawable[] + { + RotationHandler = CreateRotationHandler(), + SelectionBox = CreateSelectionBox(), + }); SelectedItems.CollectionChanged += (_, _) => { diff --git a/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs b/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs index 6524f7fa35..5faa4a108d 100644 --- a/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs +++ b/osu.Game/Screens/Edit/Compose/Components/SelectionRotationHandler.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Bindables; +using osu.Framework.Graphics; using osuTK; namespace osu.Game.Screens.Edit.Compose.Components @@ -9,7 +10,7 @@ namespace osu.Game.Screens.Edit.Compose.Components /// /// Base handler for editor rotation operations. /// - public class SelectionRotationHandler + public partial class SelectionRotationHandler : Component { /// /// Whether the rotation can currently be performed.