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

Merge pull request #10399 from peppy/editor-beatmap-batch-change-support

Add editor beatmap batch change support
This commit is contained in:
Dan Balasescu 2020-10-07 14:57:51 +09:00 committed by GitHub
commit 338b4c56ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 30 deletions

View File

@ -239,10 +239,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void deleteSelected()
{
ChangeHandler?.BeginChange();
foreach (var h in selectedBlueprints.ToList())
EditorBeatmap?.Remove(h.HitObject);
EditorBeatmap?.RemoveRange(selectedBlueprints.Select(b => b.HitObject));
ChangeHandler?.EndChange();
}

View File

@ -484,8 +484,7 @@ namespace osu.Game.Screens.Edit
protected void Cut()
{
Copy();
foreach (var h in editorBeatmap.SelectedHitObjects.ToArray())
editorBeatmap.Remove(h);
editorBeatmap.RemoveRange(editorBeatmap.SelectedHitObjects.ToArray());
}
protected void Copy()

View File

@ -91,14 +91,19 @@ namespace osu.Game.Screens.Edit
private readonly HashSet<HitObject> pendingUpdates = new HashSet<HitObject>();
private bool isBatchApplying;
/// <summary>
/// Adds a collection of <see cref="HitObject"/>s to this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObjects">The <see cref="HitObject"/>s to add.</param>
public void AddRange(IEnumerable<HitObject> hitObjects)
{
ApplyBatchChanges(_ =>
{
foreach (var h in hitObjects)
Add(h);
});
}
/// <summary>
@ -126,6 +131,10 @@ namespace osu.Game.Screens.Edit
mutableHitObjects.Insert(index, hitObject);
if (isBatchApplying)
batchPendingInserts.Add(hitObject);
else
{
// must be run after any change to hitobject ordering
beatmapProcessor?.PreProcess();
processHitObject(hitObject);
@ -133,6 +142,7 @@ namespace osu.Game.Screens.Edit
HitObjectAdded?.Invoke(hitObject);
}
}
/// <summary>
/// Updates a <see cref="HitObject"/>, invoking <see cref="HitObject.ApplyDefaults"/> and re-processing the beatmap.
@ -159,6 +169,19 @@ namespace osu.Game.Screens.Edit
return true;
}
/// <summary>
/// Removes a collection of <see cref="HitObject"/>s to this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObjects">The <see cref="HitObject"/>s to remove.</param>
public void RemoveRange(IEnumerable<HitObject> hitObjects)
{
ApplyBatchChanges(_ =>
{
foreach (var h in hitObjects)
Remove(h);
});
}
/// <summary>
/// Finds the index of a <see cref="HitObject"/> in this <see cref="EditorBeatmap"/>.
/// </summary>
@ -180,6 +203,10 @@ namespace osu.Game.Screens.Edit
bindable.UnbindAll();
startTimeBindables.Remove(hitObject);
if (isBatchApplying)
batchPendingDeletes.Add(hitObject);
else
{
// must be run after any change to hitobject ordering
beatmapProcessor?.PreProcess();
processHitObject(hitObject);
@ -187,15 +214,45 @@ namespace osu.Game.Screens.Edit
HitObjectRemoved?.Invoke(hitObject);
}
}
private readonly List<HitObject> batchPendingInserts = new List<HitObject>();
private readonly List<HitObject> batchPendingDeletes = new List<HitObject>();
/// <summary>
/// Apply a batch of operations in one go, without performing Pre/Postprocessing each time.
/// </summary>
/// <param name="applyFunction">The function which will apply the batch changes.</param>
public void ApplyBatchChanges(Action<EditorBeatmap> applyFunction)
{
if (isBatchApplying)
throw new InvalidOperationException("Attempting to perform a batch application from within an existing batch");
isBatchApplying = true;
applyFunction(this);
beatmapProcessor?.PreProcess();
foreach (var h in batchPendingDeletes) processHitObject(h);
foreach (var h in batchPendingInserts) processHitObject(h);
beatmapProcessor?.PostProcess();
foreach (var h in batchPendingDeletes) HitObjectRemoved?.Invoke(h);
foreach (var h in batchPendingInserts) HitObjectAdded?.Invoke(h);
batchPendingDeletes.Clear();
batchPendingInserts.Clear();
isBatchApplying = false;
}
/// <summary>
/// Clears all <see cref="HitObjects"/> from this <see cref="EditorBeatmap"/>.
/// </summary>
public void Clear()
{
foreach (var h in HitObjects.ToArray())
Remove(h);
}
public void Clear() => RemoveRange(HitObjects.ToArray());
protected override void Update()
{

View File

@ -68,16 +68,19 @@ namespace osu.Game.Screens.Edit
toRemove.Sort();
toAdd.Sort();
editorBeatmap.ApplyBatchChanges(eb =>
{
// Apply the changes.
for (int i = toRemove.Count - 1; i >= 0; i--)
editorBeatmap.RemoveAt(toRemove[i]);
eb.RemoveAt(toRemove[i]);
if (toAdd.Count > 0)
{
IBeatmap newBeatmap = readBeatmap(newState);
foreach (var i in toAdd)
editorBeatmap.Insert(i, newBeatmap.HitObjects[i]);
eb.Insert(i, newBeatmap.HitObjects[i]);
}
});
}
private string readString(byte[] state) => Encoding.UTF8.GetString(state);