1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 16:07:25 +08:00
osu-lazer/osu.Game/Screens/Edit/EditorBeatmap.cs

319 lines
12 KiB
C#
Raw Normal View History

2019-08-29 11:43:43 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
2019-12-27 18:39:30 +08:00
using System.Collections;
2019-08-29 11:43:43 +08:00
using System.Collections.Generic;
2020-05-08 17:46:37 +08:00
using System.Linq;
2020-04-09 18:54:58 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2019-08-29 11:43:43 +08:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Beatmaps.Timing;
using osu.Game.Rulesets.Edit;
2019-08-29 11:43:43 +08:00
using osu.Game.Rulesets.Objects;
2020-08-31 03:12:45 +08:00
using osu.Game.Skinning;
2019-08-29 11:43:43 +08:00
namespace osu.Game.Screens.Edit
{
public class EditorBeatmap : Component, IBeatmap, IBeatSnapProvider
2019-08-29 11:43:43 +08:00
{
/// <summary>
2019-12-27 18:39:30 +08:00
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="EditorBeatmap"/>.
/// </summary>
public event Action<HitObject> HitObjectAdded;
/// <summary>
2019-12-27 18:39:30 +08:00
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="EditorBeatmap"/>.
/// </summary>
public event Action<HitObject> HitObjectRemoved;
/// <summary>
2020-04-09 18:54:58 +08:00
/// Invoked when a <see cref="HitObject"/> is updated.
/// </summary>
2020-04-09 18:54:58 +08:00
public event Action<HitObject> HitObjectUpdated;
2019-08-29 11:43:43 +08:00
/// <summary>
/// All currently selected <see cref="HitObject"/>s.
/// </summary>
public readonly BindableList<HitObject> SelectedHitObjects = new BindableList<HitObject>();
/// <summary>
2020-02-08 10:35:27 +08:00
/// The current placement. Null if there's no active placement.
/// </summary>
public readonly Bindable<HitObject> PlacementObject = new Bindable<HitObject>();
public readonly IBeatmap PlayableBeatmap;
2020-09-01 23:58:06 +08:00
public readonly ISkin BeatmapSkin;
2020-08-31 03:12:45 +08:00
[Resolved]
private BindableBeatDivisor beatDivisor { get; set; }
private readonly IBeatmapProcessor beatmapProcessor;
2019-12-27 18:39:30 +08:00
private readonly Dictionary<HitObject, Bindable<double>> startTimeBindables = new Dictionary<HitObject, Bindable<double>>();
2019-08-29 11:43:43 +08:00
2020-09-01 23:58:06 +08:00
public EditorBeatmap(IBeatmap playableBeatmap, ISkin beatmapSkin = null)
2019-08-29 11:43:43 +08:00
{
PlayableBeatmap = playableBeatmap;
2020-08-31 03:12:45 +08:00
BeatmapSkin = beatmapSkin;
beatmapProcessor = playableBeatmap.BeatmapInfo.Ruleset?.CreateInstance().CreateBeatmapProcessor(PlayableBeatmap);
foreach (var obj in HitObjects)
trackStartTime(obj);
2019-08-29 11:43:43 +08:00
}
public BeatmapInfo BeatmapInfo
{
get => PlayableBeatmap.BeatmapInfo;
set => PlayableBeatmap.BeatmapInfo = value;
2019-08-29 11:43:43 +08:00
}
public BeatmapMetadata Metadata => PlayableBeatmap.Metadata;
2019-08-29 11:43:43 +08:00
public ControlPointInfo ControlPointInfo => PlayableBeatmap.ControlPointInfo;
2019-08-29 11:43:43 +08:00
public List<BreakPeriod> Breaks => PlayableBeatmap.Breaks;
2019-08-29 11:43:43 +08:00
public double TotalBreakTime => PlayableBeatmap.TotalBreakTime;
2019-08-29 11:43:43 +08:00
public IReadOnlyList<HitObject> HitObjects => PlayableBeatmap.HitObjects;
2019-08-29 11:43:43 +08:00
public IEnumerable<BeatmapStatistic> GetStatistics() => PlayableBeatmap.GetStatistics();
2019-08-29 11:43:43 +08:00
2019-12-27 18:39:30 +08:00
public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone();
2019-08-29 11:43:43 +08:00
private IList mutableHitObjects => (IList)PlayableBeatmap.HitObjects;
private bool isBatchApplying;
2020-04-09 19:48:59 +08:00
/// <summary>
/// Adds a collection of <see cref="HitObject"/>s to this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObjects">The <see cref="HitObject"/>s to add.</param>
public void AddRange(IEnumerable<HitObject> hitObjects)
{
ApplyBatchChanges(_ =>
{
foreach (var h in hitObjects)
Add(h);
});
2020-04-09 19:48:59 +08:00
}
2019-08-29 15:31:43 +08:00
/// <summary>
2019-12-27 18:39:30 +08:00
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap"/>.
2019-08-29 15:31:43 +08:00
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
2019-12-27 18:39:30 +08:00
public void Add(HitObject hitObject)
2019-08-29 11:43:43 +08:00
{
// Preserve existing sorting order in the beatmap
var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime);
Insert(insertionIndex + 1, hitObject);
}
2019-08-29 11:43:43 +08:00
/// <summary>
/// Inserts a <see cref="HitObject"/> into this <see cref="EditorBeatmap"/>.
/// </summary>
/// <remarks>
/// It is the invoker's responsibility to make sure that <see cref="HitObject"/> sorting order is maintained.
/// </remarks>
/// <param name="index">The index to insert the <see cref="HitObject"/> at.</param>
/// <param name="hitObject">The <see cref="HitObject"/> to insert.</param>
public void Insert(int index, HitObject hitObject)
{
trackStartTime(hitObject);
2020-04-09 18:54:58 +08:00
2020-09-12 20:20:37 +08:00
mutableHitObjects.Insert(index, hitObject);
2020-09-14 13:45:49 +08:00
if (isBatchApplying)
batchPendingInserts.Add(hitObject);
else
{
// must be run after any change to hitobject ordering
beatmapProcessor?.PreProcess();
processHitObject(hitObject);
beatmapProcessor?.PostProcess();
2020-09-14 13:45:49 +08:00
HitObjectAdded?.Invoke(hitObject);
}
2020-09-14 13:45:49 +08:00
}
/// <summary>
/// Updates a <see cref="HitObject"/>, invoking <see cref="HitObject.ApplyDefaults"/> and re-processing the beatmap.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to update.</param>
public void UpdateHitObject([NotNull] HitObject hitObject)
{
if (isBatchApplying)
batchPendingUpdates.Add(hitObject);
else
{
beatmapProcessor?.PreProcess();
processHitObject(hitObject);
beatmapProcessor?.PostProcess();
HitObjectUpdated?.Invoke(hitObject);
}
2019-08-29 11:43:43 +08:00
}
2019-08-29 15:31:43 +08:00
/// <summary>
2019-12-27 18:39:30 +08:00
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap"/>.
2019-08-29 15:31:43 +08:00
/// </summary>
2020-09-14 16:55:41 +08:00
/// <param name="hitObject">The <see cref="HitObject"/> to remove.</param>
2020-04-09 19:48:59 +08:00
/// <returns>True if the <see cref="HitObject"/> has been removed, false otherwise.</returns>
2020-09-14 16:55:41 +08:00
public bool Remove(HitObject hitObject)
2020-04-09 19:48:59 +08:00
{
2020-09-14 16:55:41 +08:00
int index = FindIndex(hitObject);
2020-04-09 19:48:59 +08:00
2020-09-14 16:55:41 +08:00
if (index == -1)
return false;
2020-04-09 19:48:59 +08:00
2020-09-14 16:55:41 +08:00
RemoveAt(index);
return true;
2020-04-09 19:48:59 +08:00
}
/// <summary>
/// Removes a collection of <see cref="HitObject"/>s to this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObjects">The <see cref="HitObject"/>s to remove.</param>
public void RemoveRange(IEnumerable<HitObject> hitObjects)
{
ApplyBatchChanges(_ =>
{
foreach (var h in hitObjects)
Remove(h);
});
}
2020-04-09 19:48:59 +08:00
/// <summary>
/// Finds the index of a <see cref="HitObject"/> in this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to search for.</param>
/// <returns>The index of <paramref name="hitObject"/>.</returns>
public int FindIndex(HitObject hitObject) => mutableHitObjects.IndexOf(hitObject);
/// <summary>
/// Removes a <see cref="HitObject"/> at an index in this <see cref="EditorBeatmap"/>.
/// </summary>
/// <param name="index">The index of the <see cref="HitObject"/> to remove.</param>
public void RemoveAt(int index)
2019-08-29 11:43:43 +08:00
{
2020-04-09 19:48:59 +08:00
var hitObject = (HitObject)mutableHitObjects[index];
2020-04-09 19:48:59 +08:00
mutableHitObjects.RemoveAt(index);
2019-12-27 18:39:30 +08:00
var bindable = startTimeBindables[hitObject];
bindable.UnbindAll();
startTimeBindables.Remove(hitObject);
2020-04-09 18:54:58 +08:00
if (isBatchApplying)
batchPendingDeletes.Add(hitObject);
else
{
// must be run after any change to hitobject ordering
beatmapProcessor?.PreProcess();
processHitObject(hitObject);
beatmapProcessor?.PostProcess();
HitObjectRemoved?.Invoke(hitObject);
}
}
private readonly List<HitObject> batchPendingInserts = new List<HitObject>();
private readonly List<HitObject> batchPendingDeletes = new List<HitObject>();
private readonly HashSet<HitObject> batchPendingUpdates = new HashSet<HitObject>();
/// <summary>
/// Apply a batch of operations in one go, without performing Pre/Postprocessing each time.
/// </summary>
/// <param name="applyFunction">The function which will apply the batch changes.</param>
public void ApplyBatchChanges(Action<EditorBeatmap> applyFunction)
{
if (isBatchApplying)
throw new InvalidOperationException("Attempting to perform a batch application from within an existing batch");
isBatchApplying = true;
applyFunction(this);
2020-09-14 13:45:49 +08:00
beatmapProcessor?.PreProcess();
2020-10-07 10:09:45 +08:00
foreach (var h in batchPendingDeletes) processHitObject(h);
foreach (var h in batchPendingInserts) processHitObject(h);
foreach (var h in batchPendingUpdates) processHitObject(h);
2020-10-07 10:09:45 +08:00
beatmapProcessor?.PostProcess();
2020-10-07 10:09:45 +08:00
foreach (var h in batchPendingDeletes) HitObjectRemoved?.Invoke(h);
foreach (var h in batchPendingInserts) HitObjectAdded?.Invoke(h);
foreach (var h in batchPendingUpdates) HitObjectUpdated?.Invoke(h);
2020-10-07 10:09:45 +08:00
batchPendingDeletes.Clear();
batchPendingInserts.Clear();
batchPendingUpdates.Clear();
2020-10-07 10:09:45 +08:00
isBatchApplying = false;
}
2020-05-08 17:46:37 +08:00
/// <summary>
/// Clears all <see cref="HitObjects"/> from this <see cref="EditorBeatmap"/>.
/// </summary>
public void Clear() => RemoveRange(HitObjects.ToArray());
2020-05-08 17:46:37 +08:00
2020-09-14 13:45:49 +08:00
private void processHitObject(HitObject hitObject) => hitObject.ApplyDefaults(ControlPointInfo, BeatmapInfo.BaseDifficulty);
2019-12-27 18:39:30 +08:00
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);
2020-04-09 18:54:58 +08:00
UpdateHitObject(hitObject);
};
2019-08-29 11:43:43 +08:00
}
2019-12-27 18:39:30 +08:00
private int findInsertionIndex(IReadOnlyList<HitObject> list, double startTime)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].StartTime > startTime)
return i - 1;
}
2019-08-29 11:43:43 +08:00
2019-12-27 18:39:30 +08:00
return list.Count - 1;
}
public double SnapTime(double time, double? referenceTime)
{
var timingPoint = ControlPointInfo.TimingPointAt(referenceTime ?? time);
var beatLength = timingPoint.BeatLength / BeatDivisor;
2020-01-28 12:42:22 +08:00
return timingPoint.Time + (int)Math.Round((time - timingPoint.Time) / beatLength, MidpointRounding.AwayFromZero) * beatLength;
}
2020-01-23 14:31:56 +08:00
public double GetBeatLengthAtTime(double referenceTime) => ControlPointInfo.TimingPointAt(referenceTime).BeatLength / BeatDivisor;
public int BeatDivisor => beatDivisor?.Value ?? 1;
/// <summary>
/// Update all hit objects with potentially changed difficulty or control point data.
/// </summary>
public void UpdateBeatmap()
{
foreach (var h in HitObjects)
batchPendingUpdates.Add(h);
}
2019-08-29 11:43:43 +08:00
}
}