1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 05:02:55 +08:00

Move clipboard action availability logic down to editor screens

This commit is contained in:
Bartłomiej Dach 2021-11-08 14:54:31 +01:00
parent 286754f6f7
commit 042b05a250
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
3 changed files with 79 additions and 28 deletions

View File

@ -77,6 +77,13 @@ namespace osu.Game.Screens.Edit.Compose
return new EditorSkinProvidingContainer(EditorBeatmap).WithChild(content);
}
protected override void LoadComplete()
{
base.LoadComplete();
EditorBeatmap.SelectedHitObjects.BindCollectionChanged((_, __) => updateClipboardActionAvailability());
clipboard.BindValueChanged(_ => updateClipboardActionAvailability(), true);
}
#region Input Handling
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
@ -108,30 +115,24 @@ namespace osu.Game.Screens.Edit.Compose
#region Clipboard operations
public override void Cut()
protected override void PerformCut()
{
base.Cut();
base.PerformCut();
Copy();
EditorBeatmap.RemoveRange(EditorBeatmap.SelectedHitObjects.ToArray());
}
public override void Copy()
protected override void PerformCopy()
{
base.Copy();
if (EditorBeatmap.SelectedHitObjects.Count == 0)
return;
base.PerformCopy();
clipboard.Value = new ClipboardContent(EditorBeatmap).Serialize();
}
public override void Paste()
protected override void PerformPaste()
{
base.Paste();
if (string.IsNullOrEmpty(clipboard.Value))
return;
base.PerformPaste();
var objects = clipboard.Value.Deserialize<ClipboardContent>().HitObjects;
@ -152,6 +153,12 @@ namespace osu.Game.Screens.Edit.Compose
EditorBeatmap.EndChange();
}
private void updateClipboardActionAvailability()
{
CanCut.Value = CanCopy.Value = EditorBeatmap.SelectedHitObjects.Any();
CanPaste.Value = !string.IsNullOrEmpty(clipboard.Value);
}
#endregion
}
}

View File

@ -181,10 +181,6 @@ namespace osu.Game.Screens.Edit
OsuMenuItem undoMenuItem;
OsuMenuItem redoMenuItem;
EditorMenuItem cutMenuItem;
EditorMenuItem copyMenuItem;
EditorMenuItem pasteMenuItem;
AddInternal(new OsuContextMenuContainer
{
RelativeSizeAxes = Axes.Both,
@ -300,19 +296,15 @@ namespace osu.Game.Screens.Edit
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
editorBeatmap.SelectedHitObjects.BindCollectionChanged((_, __) =>
{
bool hasObjects = editorBeatmap.SelectedHitObjects.Count > 0;
cutMenuItem.Action.Disabled = !hasObjects;
copyMenuItem.Action.Disabled = !hasObjects;
}, true);
Clipboard.BindValueChanged(content => pasteMenuItem.Action.Disabled = string.IsNullOrEmpty(content.NewValue));
menuBar.Mode.ValueChanged += onModeChanged;
}
protected override void LoadComplete()
{
base.LoadComplete();
setUpClipboardActionAvailability();
}
/// <summary>
/// If the beatmap's track has changed, this method must be called to keep the editor in a valid state.
/// </summary>
@ -562,12 +554,38 @@ namespace osu.Game.Screens.Edit
this.Exit();
}
#region Clipboard support
private EditorMenuItem cutMenuItem;
private EditorMenuItem copyMenuItem;
private EditorMenuItem pasteMenuItem;
private readonly BindableWithCurrent<bool> canCut = new BindableWithCurrent<bool>();
private readonly BindableWithCurrent<bool> canCopy = new BindableWithCurrent<bool>();
private readonly BindableWithCurrent<bool> canPaste = new BindableWithCurrent<bool>();
private void setUpClipboardActionAvailability()
{
canCut.Current.BindValueChanged(cut => cutMenuItem.Action.Disabled = !cut.NewValue, true);
canCopy.Current.BindValueChanged(copy => copyMenuItem.Action.Disabled = !copy.NewValue, true);
canPaste.Current.BindValueChanged(paste => pasteMenuItem.Action.Disabled = !paste.NewValue, true);
}
private void rebindClipboardBindables()
{
canCut.Current = currentScreen.CanCut;
canCopy.Current = currentScreen.CanCopy;
canPaste.Current = currentScreen.CanPaste;
}
protected void Cut() => currentScreen?.Cut();
protected void Copy() => currentScreen?.Copy();
protected void Paste() => currentScreen?.Paste();
#endregion
protected void Undo() => changeHandler.RestoreState(-1);
protected void Redo() => changeHandler.RestoreState(1);
@ -644,6 +662,7 @@ namespace osu.Game.Screens.Edit
finally
{
updateSampleDisabledState();
rebindClipboardBindables();
}
}

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
@ -52,25 +53,49 @@ namespace osu.Game.Screens.Edit
#region Clipboard operations
public BindableBool CanCut { get; } = new BindableBool();
/// <summary>
/// Performs a "cut to clipboard" operation appropriate for the given screen.
/// </summary>
public virtual void Cut()
protected virtual void PerformCut()
{
}
public void Cut()
{
if (CanCut.Value)
PerformCut();
}
public BindableBool CanCopy { get; } = new BindableBool();
/// <summary>
/// Performs a "copy to clipboard" operation appropriate for the given screen.
/// </summary>
public virtual void Copy()
protected virtual void PerformCopy()
{
}
public virtual void Copy()
{
if (CanCopy.Value)
PerformCopy();
}
public BindableBool CanPaste { get; } = new BindableBool();
/// <summary>
/// Performs a "paste from clipboard" operation appropriate for the given screen.
/// </summary>
protected virtual void PerformPaste()
{
}
public virtual void Paste()
{
if (CanPaste.Value)
PerformPaste();
}
#endregion