// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable 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.Legacy; 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 partial class EditorBeatmap : TransactionalCommitComponent, IBeatmap, IBeatSnapProvider { /// /// Will become true when a new update is queued, and false when all updates have been applied. /// /// /// This is intended to be used to avoid performing operations (like playback of samples) /// while mutating hitobjects. /// public IBindable UpdateInProgress => updateInProgress; private readonly BindableBool updateInProgress = new BindableBool(); /// /// 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; /// /// Invoked after any state changes occurred which triggered a beatmap reprocess via an . /// /// /// Beatmap processing may change the order of hitobjects. This event gives external components a chance to handle any changes /// not covered by the / / events. /// public event Action BeatmapReprocessed; /// /// 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(); private readonly BeatmapInfo beatmapInfo; public readonly IBeatmap PlayableBeatmap; /// /// Whether at least one timing control point is present and providing timing information. /// public IBindable HasTiming => hasTiming; private readonly Bindable hasTiming = new Bindable(); [CanBeNull] public readonly EditorBeatmapSkin BeatmapSkin; [Resolved] private BindableBeatDivisor beatDivisor { get; set; } [Resolved] private EditorClock editorClock { get; set; } public BindableInt PreviewTime { get; } private readonly IBeatmapProcessor beatmapProcessor; private readonly Dictionary> startTimeBindables = new Dictionary>(); public EditorBeatmap(IBeatmap playableBeatmap, ISkin beatmapSkin = null, BeatmapInfo beatmapInfo = null) { PlayableBeatmap = playableBeatmap; PlayableBeatmap.ControlPointInfo = ConvertControlPoints(PlayableBeatmap.ControlPointInfo); this.beatmapInfo = beatmapInfo ?? playableBeatmap.BeatmapInfo; if (beatmapSkin is Skin skin) { BeatmapSkin = new EditorBeatmapSkin(skin); BeatmapSkin.BeatmapSkinChanged += SaveState; } beatmapProcessor = playableBeatmap.BeatmapInfo.Ruleset.CreateInstance().CreateBeatmapProcessor(this); foreach (var obj in HitObjects) trackStartTime(obj); PreviewTime = new BindableInt(BeatmapInfo.Metadata.PreviewTime); PreviewTime.BindValueChanged(s => { BeginChange(); BeatmapInfo.Metadata.PreviewTime = s.NewValue; EndChange(); }); } /// /// Converts a such that the resultant is non-legacy. /// /// The to convert. /// The non-legacy . is returned if already non-legacy. public static ControlPointInfo ConvertControlPoints(ControlPointInfo incoming) { // ensure we are not working with legacy control points. // if we leave the legacy points around they will be applied over any local changes on // ApplyDefaults calls. this should eventually be removed once the default logic is moved to the decoder/converter. if (!(incoming is LegacyControlPointInfo)) return incoming; var newControlPoints = new ControlPointInfo(); foreach (var controlPoint in incoming.AllControlPoints) { switch (controlPoint) { case DifficultyControlPoint: case SampleControlPoint: // skip legacy types. continue; default: newControlPoints.Add(controlPoint.Time, controlPoint); break; } } return newControlPoints; } public BeatmapInfo BeatmapInfo { get => beatmapInfo; set => throw new InvalidOperationException($"Can't set {nameof(BeatmapInfo)} on {nameof(EditorBeatmap)}"); } public BeatmapMetadata Metadata => beatmapInfo.Metadata; public BeatmapDifficulty Difficulty { get => PlayableBeatmap.Difficulty; set => PlayableBeatmap.Difficulty = value; } public ControlPointInfo ControlPointInfo { get => PlayableBeatmap.ControlPointInfo; set => PlayableBeatmap.ControlPointInfo = value; } public List Breaks => PlayableBeatmap.Breaks; public List UnhandledEventLines => PlayableBeatmap.UnhandledEventLines; 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. /// /// /// Note that this incurs a full state save, and as such requires the entire beatmap to be encoded, etc. /// Very frequent use of this method (e.g. once a frame) is most discouraged. /// If there is need to do so, use local precondition checks to eliminate changes that are known to be no-ops. /// /// 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 int 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); updateInProgress.Value = true; } /// /// Update all hit objects with potentially changed difficulty or control point data. /// public void UpdateAllHitObjects() { foreach (var h in HitObjects) batchPendingUpdates.Add(h); updateInProgress.Value = true; } /// /// 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) { HitObject 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(); hasTiming.Value = !ReferenceEquals(ControlPointInfo.TimingPointAt(editorClock.CurrentTime), TimingControlPoint.DEFAULT); } 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(); BeatmapReprocessed?.Invoke(); // 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) SelectedHitObjects.Remove(h); foreach (var h in deletes) HitObjectRemoved?.Invoke(h); foreach (var h in inserts) HitObjectAdded?.Invoke(h); foreach (var h in updates) HitObjectUpdated?.Invoke(h); updateInProgress.Value = false; } /// /// Clears all from this . /// public void Clear() => RemoveRange(HitObjects.ToArray()); private void processHitObject(HitObject hitObject) => hitObject.ApplyDefaults(ControlPointInfo, PlayableBeatmap.Difficulty); 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); int 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; } }