From 286754f6f73c4f0fbf87d0250ec964a088aa41bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Mon, 8 Nov 2021 14:24:39 +0100 Subject: [PATCH] Move clipboard operation implementation down to current screen --- .../Screens/Edit/Compose/ComposeScreen.cs | 55 ++++++++++++++++++- osu.Game/Screens/Edit/Editor.cs | 51 +++-------------- osu.Game/Screens/Edit/EditorScreen.cs | 25 +++++++++ 3 files changed, 86 insertions(+), 45 deletions(-) diff --git a/osu.Game/Screens/Edit/Compose/ComposeScreen.cs b/osu.Game/Screens/Edit/Compose/ComposeScreen.cs index 926a2ad4e0..2214c517db 100644 --- a/osu.Game/Screens/Edit/Compose/ComposeScreen.cs +++ b/osu.Game/Screens/Edit/Compose/ComposeScreen.cs @@ -13,6 +13,7 @@ using osu.Framework.Input.Events; using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.Extensions; +using osu.Game.IO.Serialization; using osu.Game.Rulesets; using osu.Game.Rulesets.Edit; using osu.Game.Screens.Edit.Compose.Components.Timeline; @@ -21,15 +22,15 @@ namespace osu.Game.Screens.Edit.Compose { public class ComposeScreen : EditorScreenWithTimeline, IKeyBindingHandler { - [Resolved] - private IBindable beatmap { get; set; } - [Resolved] private GameHost host { get; set; } [Resolved] private EditorClock clock { get; set; } + [Resolved(Name = nameof(Editor.Clipboard))] + private Bindable clipboard { get; set; } + private HitObjectComposer composer; public ComposeScreen() @@ -104,5 +105,53 @@ namespace osu.Game.Screens.Edit.Compose } #endregion + + #region Clipboard operations + + public override void Cut() + { + base.Cut(); + + Copy(); + EditorBeatmap.RemoveRange(EditorBeatmap.SelectedHitObjects.ToArray()); + } + + public override void Copy() + { + base.Copy(); + + if (EditorBeatmap.SelectedHitObjects.Count == 0) + return; + + clipboard.Value = new ClipboardContent(EditorBeatmap).Serialize(); + } + + public override void Paste() + { + base.Paste(); + + if (string.IsNullOrEmpty(clipboard.Value)) + return; + + var objects = clipboard.Value.Deserialize().HitObjects; + + Debug.Assert(objects.Any()); + + double timeOffset = clock.CurrentTime - objects.Min(o => o.StartTime); + + foreach (var h in objects) + h.StartTime += timeOffset; + + EditorBeatmap.BeginChange(); + + EditorBeatmap.SelectedHitObjects.Clear(); + + EditorBeatmap.AddRange(objects); + EditorBeatmap.SelectedHitObjects.AddRange(objects); + + EditorBeatmap.EndChange(); + } + + #endregion } } diff --git a/osu.Game/Screens/Edit/Editor.cs b/osu.Game/Screens/Edit/Editor.cs index 81b2847443..f51b527117 100644 --- a/osu.Game/Screens/Edit/Editor.cs +++ b/osu.Game/Screens/Edit/Editor.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using JetBrains.Annotations; using osu.Framework; @@ -24,7 +23,6 @@ using osu.Game.Graphics; using osu.Game.Graphics.Cursor; using osu.Game.Graphics.UserInterface; using osu.Game.Input.Bindings; -using osu.Game.IO.Serialization; using osu.Game.Online.API; using osu.Game.Overlays; using osu.Game.Rulesets.Edit; @@ -105,6 +103,9 @@ namespace osu.Game.Screens.Edit [Resolved] private MusicController music { get; set; } + [Cached(Name = nameof(Clipboard))] + public readonly Bindable Clipboard = new Bindable(); + public Editor(EditorLoader loader = null) { this.loader = loader; @@ -307,7 +308,7 @@ namespace osu.Game.Screens.Edit copyMenuItem.Action.Disabled = !hasObjects; }, true); - clipboard.BindValueChanged(content => pasteMenuItem.Action.Disabled = string.IsNullOrEmpty(content.NewValue)); + Clipboard.BindValueChanged(content => pasteMenuItem.Action.Disabled = string.IsNullOrEmpty(content.NewValue)); menuBar.Mode.ValueChanged += onModeChanged; } @@ -324,7 +325,7 @@ namespace osu.Game.Screens.Edit public void RestoreState([NotNull] EditorState state) => Schedule(() => { clock.Seek(state.Time); - clipboard.Value = state.ClipboardContent; + Clipboard.Value = state.ClipboardContent; }); protected void Save() @@ -561,45 +562,11 @@ namespace osu.Game.Screens.Edit this.Exit(); } - private readonly Bindable clipboard = new Bindable(); + protected void Cut() => currentScreen?.Cut(); - protected void Cut() - { - Copy(); - editorBeatmap.RemoveRange(editorBeatmap.SelectedHitObjects.ToArray()); - } + protected void Copy() => currentScreen?.Copy(); - protected void Copy() - { - if (editorBeatmap.SelectedHitObjects.Count == 0) - return; - - clipboard.Value = new ClipboardContent(editorBeatmap).Serialize(); - } - - protected void Paste() - { - if (string.IsNullOrEmpty(clipboard.Value)) - return; - - var objects = clipboard.Value.Deserialize().HitObjects; - - Debug.Assert(objects.Any()); - - double timeOffset = clock.CurrentTime - objects.Min(o => o.StartTime); - - foreach (var h in objects) - h.StartTime += timeOffset; - - editorBeatmap.BeginChange(); - - editorBeatmap.SelectedHitObjects.Clear(); - - editorBeatmap.AddRange(objects); - editorBeatmap.SelectedHitObjects.AddRange(objects); - - editorBeatmap.EndChange(); - } + protected void Paste() => currentScreen?.Paste(); protected void Undo() => changeHandler.RestoreState(-1); @@ -757,7 +724,7 @@ namespace osu.Game.Screens.Edit protected void SwitchToDifficulty(BeatmapInfo nextBeatmap) => loader?.ScheduleDifficultySwitch(nextBeatmap, new EditorState { Time = clock.CurrentTimeAccurate, - ClipboardContent = editorBeatmap.BeatmapInfo.RulesetID == nextBeatmap.RulesetID ? clipboard.Value : string.Empty + ClipboardContent = editorBeatmap.BeatmapInfo.RulesetID == nextBeatmap.RulesetID ? Clipboard.Value : string.Empty }); private void cancelExit() diff --git a/osu.Game/Screens/Edit/EditorScreen.cs b/osu.Game/Screens/Edit/EditorScreen.cs index 2810f78835..a406aeb743 100644 --- a/osu.Game/Screens/Edit/EditorScreen.cs +++ b/osu.Game/Screens/Edit/EditorScreen.cs @@ -49,5 +49,30 @@ namespace osu.Game.Screens.Edit this.ScaleTo(0.98f, 200, Easing.OutQuint) .FadeOut(200, Easing.OutQuint); } + + #region Clipboard operations + + /// + /// Performs a "cut to clipboard" operation appropriate for the given screen. + /// + public virtual void Cut() + { + } + + /// + /// Performs a "copy to clipboard" operation appropriate for the given screen. + /// + public virtual void Copy() + { + } + + /// + /// Performs a "paste from clipboard" operation appropriate for the given screen. + /// + public virtual void Paste() + { + } + + #endregion } }