Remove generic editor beatmap

This commit is contained in:
smoogipoo
2019-12-27 19:39:30 +09:00
parent df7714a3e8
commit 6069d98ced
10 changed files with 59 additions and 70 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections;
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
@ -11,28 +12,27 @@ using osu.Game.Rulesets.Objects;
namespace osu.Game.Screens.Edit
{
public class EditorBeatmap<T> : IEditorBeatmap<T>
where T : HitObject
public class EditorBeatmap : IEditorBeatmap
{
/// <summary>
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="EditorBeatmap{T}"/>.
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="EditorBeatmap"/>.
/// </summary>
public event Action<HitObject> HitObjectAdded;
/// <summary>
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="EditorBeatmap{T}"/>.
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="EditorBeatmap"/>.
/// </summary>
public event Action<HitObject> HitObjectRemoved;
/// <summary>
/// Invoked when the start time of a <see cref="HitObject"/> in this <see cref="EditorBeatmap{T}"/> was changed.
/// Invoked when the start time of a <see cref="HitObject"/> in this <see cref="EditorBeatmap"/> was changed.
/// </summary>
public event Action<HitObject> StartTimeChanged;
private readonly Dictionary<T, Bindable<double>> startTimeBindables = new Dictionary<T, Bindable<double>>();
private readonly Beatmap<T> beatmap;
private readonly Dictionary<HitObject, Bindable<double>> startTimeBindables = new Dictionary<HitObject, Bindable<double>>();
private readonly IBeatmap beatmap;
public EditorBeatmap(Beatmap<T> beatmap)
public EditorBeatmap(IBeatmap beatmap)
{
this.beatmap = beatmap;
@ -54,70 +54,71 @@ namespace osu.Game.Screens.Edit
public double TotalBreakTime => beatmap.TotalBreakTime;
public IReadOnlyList<T> HitObjects => beatmap.HitObjects;
public IReadOnlyList<HitObject> HitObjects => beatmap.HitObjects;
IReadOnlyList<HitObject> IBeatmap.HitObjects => beatmap.HitObjects;
public IEnumerable<BeatmapStatistic> GetStatistics() => beatmap.GetStatistics();
public IBeatmap Clone() => (EditorBeatmap<T>)MemberwiseClone();
public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone();
/// <summary>
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap{T}"/>.
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Add(T hitObject)
public void Add(HitObject hitObject)
{
trackStartTime(hitObject);
// Preserve existing sorting order in the beatmap
var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime);
beatmap.HitObjects.Insert(insertionIndex + 1, hitObject);
var insertionIndex = findInsertionIndex(beatmap.HitObjects, hitObject.StartTime);
((IList)beatmap.HitObjects).Insert(insertionIndex + 1, hitObject);
HitObjectAdded?.Invoke(hitObject);
}
/// <summary>
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap{T}"/>.
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Remove(T hitObject)
public void Remove(HitObject hitObject)
{
if (beatmap.HitObjects.Remove(hitObject))
{
var bindable = startTimeBindables[hitObject];
bindable.UnbindAll();
if (!((IList)beatmap.HitObjects).Contains(hitObject))
return;
startTimeBindables.Remove(hitObject);
HitObjectRemoved?.Invoke(hitObject);
}
((IList)beatmap.HitObjects).Remove(hitObject);
var bindable = startTimeBindables[hitObject];
bindable.UnbindAll();
startTimeBindables.Remove(hitObject);
HitObjectRemoved?.Invoke(hitObject);
}
private void trackStartTime(T hitObject)
private void trackStartTime(HitObject hitObject)
{
startTimeBindables[hitObject] = hitObject.StartTimeBindable.GetBoundCopy();
startTimeBindables[hitObject].ValueChanged += _ =>
{
// For now we'll remove and re-add the hitobject. This is not optimal and can be improved if required.
beatmap.HitObjects.Remove(hitObject);
((IList)beatmap.HitObjects).Remove(hitObject);
var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime);
beatmap.HitObjects.Insert(insertionIndex + 1, hitObject);
var insertionIndex = findInsertionIndex(beatmap.HitObjects, hitObject.StartTime);
((IList)beatmap.HitObjects).Insert(insertionIndex + 1, hitObject);
StartTimeChanged?.Invoke(hitObject);
};
}
/// <summary>
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap{T}"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Add(HitObject hitObject) => Add((T)hitObject);
private int findInsertionIndex(IReadOnlyList<HitObject> list, double startTime)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].StartTime > startTime)
return i - 1;
}
/// <summary>
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap{T}"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Remove(HitObject hitObject) => Remove((T)hitObject);
return list.Count - 1;
}
}
}