// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using osu.Framework.Bindables; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.Timing; using osu.Game.Rulesets.Objects; namespace osu.Game.Screens.Edit { public class EditorBeatmap : IEditorBeatmap where T : HitObject { public event Action HitObjectAdded; public event Action HitObjectRemoved; public event Action HitObjectChanged; private readonly Dictionary> startTimeBindables = new Dictionary>(); private readonly Beatmap beatmap; public EditorBeatmap(Beatmap beatmap) { this.beatmap = beatmap; foreach (var obj in HitObjects) trackStartTime(obj); } public BeatmapInfo BeatmapInfo { get => beatmap.BeatmapInfo; set => beatmap.BeatmapInfo = value; } public BeatmapMetadata Metadata => beatmap.Metadata; public ControlPointInfo ControlPointInfo => beatmap.ControlPointInfo; public List Breaks => beatmap.Breaks; public double TotalBreakTime => beatmap.TotalBreakTime; public IReadOnlyList HitObjects => beatmap.HitObjects; IReadOnlyList IBeatmap.HitObjects => beatmap.HitObjects; public IEnumerable GetStatistics() => beatmap.GetStatistics(); public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone(); /// /// Adds a to this . /// /// The to add. public void Add(T 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); HitObjectAdded?.Invoke(hitObject); } /// /// Removes a from this . /// /// The to add. public void Remove(T hitObject) { if (beatmap.HitObjects.Remove(hitObject)) { var bindable = startTimeBindables[hitObject]; bindable.UnbindAll(); startTimeBindables.Remove(hitObject); HitObjectRemoved?.Invoke(hitObject); } } private void trackStartTime(T 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); var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime); beatmap.HitObjects.Insert(insertionIndex + 1, hitObject); HitObjectChanged?.Invoke(hitObject); }; } /// /// Adds a to this . /// /// The to add. public void Add(HitObject hitObject) => Add((T)hitObject); /// /// Removes a from this . /// /// The to add. public void Remove(HitObject hitObject) => Remove((T)hitObject); } }