// 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; 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.Edit; using osu.Game.Rulesets.Objects; namespace osu.Game.Screens.Edit { public class EditorBeatmap : IBeatmap, IBeatSnapProvider { /// /// Invoked when a is added to this . /// public event Action HitObjectAdded; /// /// Invoked when a is removed from this . /// public event Action HitObjectRemoved; /// /// Invoked when the start time of a in this was changed. /// public event Action StartTimeChanged; public BindableList SelectedHitObjects { get; } = new BindableList(); public readonly IBeatmap PlayableBeatmap; private readonly BindableBeatDivisor beatDivisor; private readonly Dictionary> startTimeBindables = new Dictionary>(); public EditorBeatmap(IBeatmap playableBeatmap, BindableBeatDivisor beatDivisor = null) { PlayableBeatmap = playableBeatmap; this.beatDivisor = beatDivisor; foreach (var obj in HitObjects) trackStartTime(obj); } public BeatmapInfo BeatmapInfo { get => PlayableBeatmap.BeatmapInfo; set => PlayableBeatmap.BeatmapInfo = value; } public BeatmapMetadata Metadata => PlayableBeatmap.Metadata; public ControlPointInfo ControlPointInfo => PlayableBeatmap.ControlPointInfo; public List Breaks => PlayableBeatmap.Breaks; public double TotalBreakTime => PlayableBeatmap.TotalBreakTime; public IReadOnlyList HitObjects => PlayableBeatmap.HitObjects; public IEnumerable GetStatistics() => PlayableBeatmap.GetStatistics(); public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone(); private IList mutableHitObjects => (IList)PlayableBeatmap.HitObjects; /// /// Adds a to this . /// /// The to add. public void Add(HitObject hitObject) { trackStartTime(hitObject); // Preserve existing sorting order in the beatmap var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime); mutableHitObjects.Insert(insertionIndex + 1, hitObject); HitObjectAdded?.Invoke(hitObject); } /// /// Removes a from this . /// /// The to add. public void Remove(HitObject hitObject) { if (!mutableHitObjects.Contains(hitObject)) return; mutableHitObjects.Remove(hitObject); var bindable = startTimeBindables[hitObject]; bindable.UnbindAll(); startTimeBindables.Remove(hitObject); HitObjectRemoved?.Invoke(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. mutableHitObjects.Remove(hitObject); var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime); mutableHitObjects.Insert(insertionIndex + 1, hitObject); StartTimeChanged?.Invoke(hitObject); }; } private int findInsertionIndex(IReadOnlyList list, double startTime) { for (int i = 0; i < list.Count; i++) { if (list[i].StartTime > startTime) return i - 1; } return list.Count - 1; } public double SnapTime(double referenceTime, double duration) { double beatLength = GetBeatLengthAtTime(referenceTime); // A 1ms offset prevents rounding errors due to minute variations in duration return (int)((duration + 1) / beatLength) * beatLength; } public double GetBeatLengthAtTime(double referenceTime) => ControlPointInfo.TimingPointAt(referenceTime).BeatLength / BeatDivisor; public int BeatDivisor => beatDivisor?.Value ?? 1; } }