1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:17:26 +08:00

Move clipboard operation implementation down to current screen

This commit is contained in:
Bartłomiej Dach 2021-11-08 14:24:39 +01:00
parent baa5285b59
commit 286754f6f7
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
3 changed files with 86 additions and 45 deletions

View File

@ -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<PlatformAction>
{
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
[Resolved]
private GameHost host { get; set; }
[Resolved]
private EditorClock clock { get; set; }
[Resolved(Name = nameof(Editor.Clipboard))]
private Bindable<string> 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<ClipboardContent>().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
}
}

View File

@ -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<string> Clipboard = new Bindable<string>();
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<string> clipboard = new Bindable<string>();
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<ClipboardContent>().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()

View File

@ -49,5 +49,30 @@ namespace osu.Game.Screens.Edit
this.ScaleTo(0.98f, 200, Easing.OutQuint)
.FadeOut(200, Easing.OutQuint);
}
#region Clipboard operations
/// <summary>
/// Performs a "cut to clipboard" operation appropriate for the given screen.
/// </summary>
public virtual void Cut()
{
}
/// <summary>
/// Performs a "copy to clipboard" operation appropriate for the given screen.
/// </summary>
public virtual void Copy()
{
}
/// <summary>
/// Performs a "paste from clipboard" operation appropriate for the given screen.
/// </summary>
public virtual void Paste()
{
}
#endregion
}
}