// 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 System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; 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; using osu.Game.Skinning; namespace osu.Game.Screens.Edit { public class EditorBeatmap : TransactionalCommitComponent, 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 a is updated. /// public event Action HitObjectUpdated; /// /// All currently selected s. /// public readonly BindableList SelectedHitObjects = new BindableList(); /// /// The current placement. Null if there's no active placement. /// public readonly Bindable PlacementObject = new Bindable(); public readonly IBeatmap PlayableBeatmap; [CanBeNull] public readonly ISkin BeatmapSkin; [Resolved] private BindableBeatDivisor beatDivisor { get; set; } private readonly IBeatmapProcessor beatmapProcessor; private readonly Dictionary> startTimeBindables = new Dictionary>(); public EditorBeatmap(IBeatmap playableBeatmap, ISkin beatmapSkin = null) { PlayableBeatmap = playableBeatmap; BeatmapSkin = beatmapSkin; beatmapProcessor = playableBeatmap.BeatmapInfo.Ruleset?.CreateInstance().CreateBeatmapProcessor(PlayableBeatmap); 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 { get => PlayableBeatmap.ControlPointInfo; set => PlayableBeatmap.ControlPointInfo = value; } public List Breaks => PlayableBeatmap.Breaks; public double TotalBreakTime => PlayableBeatmap.TotalBreakTime; public IReadOnlyList HitObjects => PlayableBeatmap.HitObjects; public IEnumerable GetStatistics() => PlayableBeatmap.GetStatistics(); public double GetMostCommonBeatLength() => PlayableBeatmap.GetMostCommonBeatLength(); public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone(); private IList mutableHitObjects => (IList)PlayableBeatmap.HitObjects; private readonly List batchPendingInserts = new List(); private readonly List batchPendingDeletes = new List(); private readonly HashSet batchPendingUpdates = new HashSet(); /// /// Perform the provided action on every selected hitobject. /// Changes will be grouped as one history action. /// /// The action to perform. public void PerformOnSelection(Action action) { if (SelectedHitObjects.Count == 0) return; BeginChange(); foreach (var h in SelectedHitObjects) action(h); EndChange(); } /// /// Adds a collection of s to this . /// /// The s to add. public void AddRange(IEnumerable hitObjects) { BeginChange(); foreach (var h in hitObjects) Add(h); EndChange(); } /// /// Adds a to this . /// /// The to add. public void Add(HitObject hitObject) { // Preserve existing sorting order in the beatmap var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime); Insert(insertionIndex + 1, hitObject); } /// /// Inserts a into this . /// /// /// It is the invoker's responsibility to make sure that sorting order is maintained. /// /// The index to insert the at. /// The to insert. public void Insert(int index, HitObject hitObject) { trackStartTime(hitObject); mutableHitObjects.Insert(index, hitObject); BeginChange(); batchPendingInserts.Add(hitObject); EndChange(); } /// /// Updates a , invoking and re-processing the beatmap. /// /// The to update. public void Update([NotNull] HitObject hitObject) { // updates are debounced regardless of whether a batch is active. batchPendingUpdates.Add(hitObject); } /// /// Update all hit objects with potentially changed difficulty or control point data. /// public void UpdateAllHitObjects() { foreach (var h in HitObjects) batchPendingUpdates.Add(h); } /// /// Removes a from this . /// /// The to remove. /// True if the has been removed, false otherwise. public bool Remove(HitObject hitObject) { int index = FindIndex(hitObject); if (index == -1) return false; RemoveAt(index); return true; } /// /// Removes a collection of s to this . /// /// The s to remove. public void RemoveRange(IEnumerable hitObjects) { BeginChange(); foreach (var h in hitObjects) Remove(h); EndChange(); } /// /// Finds the index of a in this . /// /// The to search for. /// The index of . public int FindIndex(HitObject hitObject) => mutableHitObjects.IndexOf(hitObject); /// /// Removes a at an index in this . /// /// The index of the to remove. public void RemoveAt(int index) { var hitObject = (HitObject)mutableHitObjects[index]; mutableHitObjects.RemoveAt(index); var bindable = startTimeBindables[hitObject]; bindable.UnbindAll(); startTimeBindables.Remove(hitObject); BeginChange(); batchPendingDeletes.Add(hitObject); EndChange(); } protected override void Update() { base.Update(); if (batchPendingUpdates.Count > 0) UpdateState(); } protected override void UpdateState() { if (batchPendingUpdates.Count == 0 && batchPendingDeletes.Count == 0 && batchPendingInserts.Count == 0) return; beatmapProcessor?.PreProcess(); foreach (var h in batchPendingDeletes) processHitObject(h); foreach (var h in batchPendingInserts) processHitObject(h); foreach (var h in batchPendingUpdates) processHitObject(h); beatmapProcessor?.PostProcess(); // callbacks may modify the lists so let's be safe about it var deletes = batchPendingDeletes.ToArray(); batchPendingDeletes.Clear(); var inserts = batchPendingInserts.ToArray(); batchPendingInserts.Clear(); var updates = batchPendingUpdates.ToArray(); batchPendingUpdates.Clear(); foreach (var h in deletes) HitObjectRemoved?.Invoke(h); foreach (var h in inserts) HitObjectAdded?.Invoke(h); foreach (var h in updates) HitObjectUpdated?.Invoke(h); } /// /// Clears all from this . /// public void Clear() => RemoveRange(HitObjects.ToArray()); private void processHitObject(HitObject hitObject) => hitObject.ApplyDefaults(ControlPointInfo, BeatmapInfo.BaseDifficulty); 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); Update(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 time, double? referenceTime) => ControlPointInfo.GetClosestSnappedTime(time, BeatDivisor, referenceTime); public double GetBeatLengthAtTime(double referenceTime) => ControlPointInfo.TimingPointAt(referenceTime).BeatLength / BeatDivisor; public int BeatDivisor => beatDivisor?.Value ?? 1; } }