1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 21:03:08 +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); return new EditorSkinProvidingContainer(EditorBeatmap).WithChild(content);
} }
protected override void LoadComplete()
{
base.LoadComplete();
EditorBeatmap.SelectedHitObjects.BindCollectionChanged((_, __) => updateClipboardActionAvailability());
clipboard.BindValueChanged(_ => updateClipboardActionAvailability(), true);
}
#region Input Handling #region Input Handling
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e) public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
@ -108,30 +115,24 @@ namespace osu.Game.Screens.Edit.Compose
#region Clipboard operations #region Clipboard operations
public override void Cut() protected override void PerformCut()
{ {
base.Cut(); base.PerformCut();
Copy(); Copy();
EditorBeatmap.RemoveRange(EditorBeatmap.SelectedHitObjects.ToArray()); EditorBeatmap.RemoveRange(EditorBeatmap.SelectedHitObjects.ToArray());
} }
public override void Copy() protected override void PerformCopy()
{ {
base.Copy(); base.PerformCopy();
if (EditorBeatmap.SelectedHitObjects.Count == 0)
return;
clipboard.Value = new ClipboardContent(EditorBeatmap).Serialize(); clipboard.Value = new ClipboardContent(EditorBeatmap).Serialize();
} }
public override void Paste() protected override void PerformPaste()
{ {
base.Paste(); base.PerformPaste();
if (string.IsNullOrEmpty(clipboard.Value))
return;
var objects = clipboard.Value.Deserialize<ClipboardContent>().HitObjects; var objects = clipboard.Value.Deserialize<ClipboardContent>().HitObjects;
@ -152,6 +153,12 @@ namespace osu.Game.Screens.Edit.Compose
EditorBeatmap.EndChange(); EditorBeatmap.EndChange();
} }
private void updateClipboardActionAvailability()
{
CanCut.Value = CanCopy.Value = EditorBeatmap.SelectedHitObjects.Any();
CanPaste.Value = !string.IsNullOrEmpty(clipboard.Value);
}
#endregion #endregion
} }
} }

View File

@ -181,10 +181,6 @@ namespace osu.Game.Screens.Edit
OsuMenuItem undoMenuItem; OsuMenuItem undoMenuItem;
OsuMenuItem redoMenuItem; OsuMenuItem redoMenuItem;
EditorMenuItem cutMenuItem;
EditorMenuItem copyMenuItem;
EditorMenuItem pasteMenuItem;
AddInternal(new OsuContextMenuContainer AddInternal(new OsuContextMenuContainer
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
@ -300,19 +296,15 @@ namespace osu.Game.Screens.Edit
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true); changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.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; menuBar.Mode.ValueChanged += onModeChanged;
} }
protected override void LoadComplete()
{
base.LoadComplete();
setUpClipboardActionAvailability();
}
/// <summary> /// <summary>
/// If the beatmap's track has changed, this method must be called to keep the editor in a valid state. /// If the beatmap's track has changed, this method must be called to keep the editor in a valid state.
/// </summary> /// </summary>
@ -562,12 +554,38 @@ namespace osu.Game.Screens.Edit
this.Exit(); 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 Cut() => currentScreen?.Cut();
protected void Copy() => currentScreen?.Copy(); protected void Copy() => currentScreen?.Copy();
protected void Paste() => currentScreen?.Paste(); protected void Paste() => currentScreen?.Paste();
#endregion
protected void Undo() => changeHandler.RestoreState(-1); protected void Undo() => changeHandler.RestoreState(-1);
protected void Redo() => changeHandler.RestoreState(1); protected void Redo() => changeHandler.RestoreState(1);
@ -644,6 +662,7 @@ namespace osu.Game.Screens.Edit
finally finally
{ {
updateSampleDisabledState(); updateSampleDisabledState();
rebindClipboardBindables();
} }
} }

View File

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