2019-08-29 03:43:43 +00: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 10:39:30 +00:00
|
|
|
using System.Collections;
|
2019-08-29 03:43:43 +00:00
|
|
|
using System.Collections.Generic;
|
2020-05-08 09:46:37 +00:00
|
|
|
using System.Linq;
|
2020-04-09 10:54:58 +00:00
|
|
|
using JetBrains.Annotations;
|
2020-02-05 08:32:33 +00:00
|
|
|
using osu.Framework.Allocation;
|
2019-10-03 05:23:48 +00:00
|
|
|
using osu.Framework.Bindables;
|
2020-02-05 08:16:15 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Threading;
|
2019-08-29 03:43:43 +00:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
|
|
|
using osu.Game.Beatmaps.Timing;
|
2020-01-23 04:33:55 +00:00
|
|
|
using osu.Game.Rulesets.Edit;
|
2019-08-29 03:43:43 +00:00
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit
|
|
|
|
{
|
2020-02-05 08:16:15 +00:00
|
|
|
public class EditorBeatmap : Component, IBeatmap, IBeatSnapProvider
|
2019-08-29 03:43:43 +00:00
|
|
|
{
|
2019-10-03 05:37:16 +00:00
|
|
|
/// <summary>
|
2019-12-27 10:39:30 +00:00
|
|
|
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="EditorBeatmap"/>.
|
2019-10-03 05:37:16 +00:00
|
|
|
/// </summary>
|
2019-08-29 07:05:44 +00:00
|
|
|
public event Action<HitObject> HitObjectAdded;
|
2019-10-03 05:37:16 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2019-12-27 10:39:30 +00:00
|
|
|
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="EditorBeatmap"/>.
|
2019-10-03 05:37:16 +00:00
|
|
|
/// </summary>
|
2019-08-29 07:05:44 +00:00
|
|
|
public event Action<HitObject> HitObjectRemoved;
|
2019-10-03 05:37:16 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-04-09 10:54:58 +00:00
|
|
|
/// Invoked when a <see cref="HitObject"/> is updated.
|
2019-10-03 05:37:16 +00:00
|
|
|
/// </summary>
|
2020-04-09 10:54:58 +00:00
|
|
|
public event Action<HitObject> HitObjectUpdated;
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2020-02-07 09:03:14 +00:00
|
|
|
/// <summary>
|
|
|
|
/// All currently selected <see cref="HitObject"/>s.
|
|
|
|
/// </summary>
|
|
|
|
public readonly BindableList<HitObject> SelectedHitObjects = new BindableList<HitObject>();
|
|
|
|
|
|
|
|
/// <summary>
|
2020-02-08 02:35:27 +00:00
|
|
|
/// The current placement. Null if there's no active placement.
|
2020-02-07 09:03:14 +00:00
|
|
|
/// </summary>
|
|
|
|
public readonly Bindable<HitObject> PlacementObject = new Bindable<HitObject>();
|
2020-01-21 11:46:39 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public readonly IBeatmap PlayableBeatmap;
|
|
|
|
|
2020-02-05 08:32:33 +00:00
|
|
|
[Resolved]
|
|
|
|
private BindableBeatDivisor beatDivisor { get; set; }
|
2020-01-23 04:33:55 +00:00
|
|
|
|
2020-02-05 08:16:15 +00:00
|
|
|
private readonly IBeatmapProcessor beatmapProcessor;
|
|
|
|
|
2019-12-27 10:39:30 +00:00
|
|
|
private readonly Dictionary<HitObject, Bindable<double>> startTimeBindables = new Dictionary<HitObject, Bindable<double>>();
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2020-02-05 08:32:33 +00:00
|
|
|
public EditorBeatmap(IBeatmap playableBeatmap)
|
2019-08-29 03:43:43 +00:00
|
|
|
{
|
2019-12-27 10:46:33 +00:00
|
|
|
PlayableBeatmap = playableBeatmap;
|
2020-02-05 08:16:15 +00:00
|
|
|
|
|
|
|
beatmapProcessor = playableBeatmap.BeatmapInfo.Ruleset?.CreateInstance().CreateBeatmapProcessor(PlayableBeatmap);
|
|
|
|
|
2019-10-03 05:23:48 +00:00
|
|
|
foreach (var obj in HitObjects)
|
|
|
|
trackStartTime(obj);
|
2019-08-29 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 04:29:46 +00:00
|
|
|
private readonly HashSet<HitObject> pendingUpdates = new HashSet<HitObject>();
|
2020-02-05 08:16:15 +00:00
|
|
|
private ScheduledDelegate scheduledUpdate;
|
|
|
|
|
|
|
|
/// <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>
|
2020-04-09 10:54:58 +00:00
|
|
|
public void UpdateHitObject([NotNull] HitObject hitObject) => updateHitObject(hitObject, false);
|
|
|
|
|
|
|
|
private void updateHitObject([CanBeNull] HitObject hitObject, bool silent)
|
2020-02-05 08:16:15 +00:00
|
|
|
{
|
|
|
|
scheduledUpdate?.Cancel();
|
2020-04-10 04:29:46 +00:00
|
|
|
|
|
|
|
if (hitObject != null)
|
|
|
|
pendingUpdates.Add(hitObject);
|
|
|
|
|
|
|
|
scheduledUpdate = Schedule(() =>
|
2020-02-05 08:16:15 +00:00
|
|
|
{
|
|
|
|
beatmapProcessor?.PreProcess();
|
2020-04-10 04:29:46 +00:00
|
|
|
|
|
|
|
foreach (var obj in pendingUpdates)
|
|
|
|
obj.ApplyDefaults(ControlPointInfo, BeatmapInfo.BaseDifficulty);
|
|
|
|
|
2020-02-05 08:16:15 +00:00
|
|
|
beatmapProcessor?.PostProcess();
|
2020-04-09 10:54:58 +00:00
|
|
|
|
|
|
|
if (!silent)
|
2020-04-10 04:29:46 +00:00
|
|
|
{
|
|
|
|
foreach (var obj in pendingUpdates)
|
|
|
|
HitObjectUpdated?.Invoke(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingUpdates.Clear();
|
|
|
|
});
|
2020-02-05 08:16:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 03:43:43 +00:00
|
|
|
public BeatmapInfo BeatmapInfo
|
|
|
|
{
|
2019-12-27 10:46:33 +00:00
|
|
|
get => PlayableBeatmap.BeatmapInfo;
|
|
|
|
set => PlayableBeatmap.BeatmapInfo = value;
|
2019-08-29 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public BeatmapMetadata Metadata => PlayableBeatmap.Metadata;
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public ControlPointInfo ControlPointInfo => PlayableBeatmap.ControlPointInfo;
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public List<BreakPeriod> Breaks => PlayableBeatmap.Breaks;
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public double TotalBreakTime => PlayableBeatmap.TotalBreakTime;
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public IReadOnlyList<HitObject> HitObjects => PlayableBeatmap.HitObjects;
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
public IEnumerable<BeatmapStatistic> GetStatistics() => PlayableBeatmap.GetStatistics();
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2019-12-27 10:39:30 +00:00
|
|
|
public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone();
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2020-01-01 14:26:45 +00:00
|
|
|
private IList mutableHitObjects => (IList)PlayableBeatmap.HitObjects;
|
2020-01-01 12:24:00 +00:00
|
|
|
|
2020-04-09 11:48:59 +00: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)
|
|
|
|
{
|
|
|
|
foreach (var h in hitObjects)
|
|
|
|
Add(h);
|
|
|
|
}
|
|
|
|
|
2019-08-29 07:31:43 +00:00
|
|
|
/// <summary>
|
2019-12-27 10:39:30 +00:00
|
|
|
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap"/>.
|
2019-08-29 07:31:43 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
|
2019-12-27 10:39:30 +00:00
|
|
|
public void Add(HitObject hitObject)
|
2019-08-29 03:43:43 +00:00
|
|
|
{
|
|
|
|
// Preserve existing sorting order in the beatmap
|
2019-12-27 10:46:33 +00:00
|
|
|
var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime);
|
2020-04-30 11:39:41 +00:00
|
|
|
Insert(insertionIndex + 1, hitObject);
|
|
|
|
}
|
2019-08-29 03:43:43 +00:00
|
|
|
|
2020-04-30 11:39:41 +00: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 10:54:58 +00:00
|
|
|
|
2020-04-30 11:39:41 +00:00
|
|
|
mutableHitObjects.Insert(index, hitObject);
|
|
|
|
|
|
|
|
HitObjectAdded?.Invoke(hitObject);
|
2020-04-09 10:54:58 +00:00
|
|
|
updateHitObject(hitObject, true);
|
2019-08-29 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 07:31:43 +00:00
|
|
|
/// <summary>
|
2019-12-27 10:39:30 +00:00
|
|
|
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap"/>.
|
2019-08-29 07:31:43 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
|
2020-04-09 11:48:59 +00:00
|
|
|
/// <returns>True if the <see cref="HitObject"/> has been removed, false otherwise.</returns>
|
|
|
|
public bool Remove(HitObject hitObject)
|
|
|
|
{
|
|
|
|
int index = FindIndex(hitObject);
|
|
|
|
|
|
|
|
if (index == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RemoveAt(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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 03:43:43 +00:00
|
|
|
{
|
2020-04-09 11:48:59 +00:00
|
|
|
var hitObject = (HitObject)mutableHitObjects[index];
|
2019-10-03 05:23:48 +00:00
|
|
|
|
2020-04-09 11:48:59 +00:00
|
|
|
mutableHitObjects.RemoveAt(index);
|
2019-12-27 10:39:30 +00:00
|
|
|
|
|
|
|
var bindable = startTimeBindables[hitObject];
|
|
|
|
bindable.UnbindAll();
|
|
|
|
|
|
|
|
startTimeBindables.Remove(hitObject);
|
|
|
|
HitObjectRemoved?.Invoke(hitObject);
|
2020-04-09 10:54:58 +00:00
|
|
|
|
|
|
|
updateHitObject(null, true);
|
2019-10-03 05:23:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 09:46:37 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Clears all <see cref="HitObjects"/> from this <see cref="EditorBeatmap"/>.
|
|
|
|
/// </summary>
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
var removed = HitObjects.ToList();
|
|
|
|
|
|
|
|
mutableHitObjects.Clear();
|
|
|
|
|
|
|
|
foreach (var b in startTimeBindables)
|
|
|
|
b.Value.UnbindAll();
|
|
|
|
startTimeBindables.Clear();
|
|
|
|
|
|
|
|
foreach (var h in removed)
|
|
|
|
HitObjectRemoved?.Invoke(h);
|
|
|
|
|
|
|
|
updateHitObject(null, true);
|
|
|
|
}
|
|
|
|
|
2019-12-27 10:39:30 +00:00
|
|
|
private void trackStartTime(HitObject hitObject)
|
2019-10-03 05:23:48 +00:00
|
|
|
{
|
|
|
|
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.
|
2020-01-01 12:24:00 +00:00
|
|
|
mutableHitObjects.Remove(hitObject);
|
2019-10-03 05:23:48 +00:00
|
|
|
|
2019-12-27 10:46:33 +00:00
|
|
|
var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime);
|
2020-01-01 12:24:00 +00:00
|
|
|
mutableHitObjects.Insert(insertionIndex + 1, hitObject);
|
2019-10-03 05:23:48 +00:00
|
|
|
|
2020-04-09 10:54:58 +00:00
|
|
|
UpdateHitObject(hitObject);
|
2019-10-03 05:23:48 +00:00
|
|
|
};
|
2019-08-29 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 10:39:30 +00: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 03:43:43 +00:00
|
|
|
|
2019-12-27 10:39:30 +00:00
|
|
|
return list.Count - 1;
|
|
|
|
}
|
2020-01-23 04:33:55 +00:00
|
|
|
|
2020-01-28 03:48:24 +00:00
|
|
|
public double SnapTime(double time, double? referenceTime)
|
2020-01-23 04:33:55 +00:00
|
|
|
{
|
2020-01-28 03:48:24 +00:00
|
|
|
var timingPoint = ControlPointInfo.TimingPointAt(referenceTime ?? time);
|
|
|
|
var beatLength = timingPoint.BeatLength / BeatDivisor;
|
2020-01-23 04:33:55 +00:00
|
|
|
|
2020-01-28 04:42:22 +00:00
|
|
|
return timingPoint.Time + (int)Math.Round((time - timingPoint.Time) / beatLength, MidpointRounding.AwayFromZero) * beatLength;
|
2020-01-23 04:33:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 06:31:56 +00:00
|
|
|
public double GetBeatLengthAtTime(double referenceTime) => ControlPointInfo.TimingPointAt(referenceTime).BeatLength / BeatDivisor;
|
2020-01-23 04:33:55 +00:00
|
|
|
|
|
|
|
public int BeatDivisor => beatDivisor?.Value ?? 1;
|
2019-08-29 03:43:43 +00:00
|
|
|
}
|
|
|
|
}
|