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;
|
|
|
|
using System.Collections.Generic;
|
2019-10-03 13:23:48 +08:00
|
|
|
using osu.Framework.Bindables;
|
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.Objects;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit
|
|
|
|
{
|
2019-08-29 15:26:39 +08:00
|
|
|
public class EditorBeatmap<T> : IEditorBeatmap<T>
|
2019-08-29 11:43:43 +08:00
|
|
|
where T : HitObject
|
|
|
|
{
|
2019-10-03 13:37:16 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="EditorBeatmap{T}"/>.
|
|
|
|
/// </summary>
|
2019-08-29 15:05:44 +08:00
|
|
|
public event Action<HitObject> HitObjectAdded;
|
2019-10-03 13:37:16 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="EditorBeatmap{T}"/>.
|
|
|
|
/// </summary>
|
2019-08-29 15:05:44 +08:00
|
|
|
public event Action<HitObject> HitObjectRemoved;
|
2019-10-03 13:37:16 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Invoked when the start time of a <see cref="HitObject"/> in this <see cref="EditorBeatmap{T}"/> was changed.
|
|
|
|
/// </summary>
|
|
|
|
public event Action<HitObject> StartTimeChanged;
|
2019-08-29 11:43:43 +08:00
|
|
|
|
2019-10-03 13:23:48 +08:00
|
|
|
private readonly Dictionary<T, Bindable<double>> startTimeBindables = new Dictionary<T, Bindable<double>>();
|
2019-08-29 11:43:43 +08:00
|
|
|
private readonly Beatmap<T> beatmap;
|
|
|
|
|
|
|
|
public EditorBeatmap(Beatmap<T> beatmap)
|
|
|
|
{
|
|
|
|
this.beatmap = beatmap;
|
2019-10-03 13:23:48 +08:00
|
|
|
|
|
|
|
foreach (var obj in HitObjects)
|
|
|
|
trackStartTime(obj);
|
2019-08-29 11:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public BeatmapInfo BeatmapInfo
|
|
|
|
{
|
|
|
|
get => beatmap.BeatmapInfo;
|
|
|
|
set => beatmap.BeatmapInfo = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BeatmapMetadata Metadata => beatmap.Metadata;
|
|
|
|
|
|
|
|
public ControlPointInfo ControlPointInfo => beatmap.ControlPointInfo;
|
|
|
|
|
|
|
|
public List<BreakPeriod> Breaks => beatmap.Breaks;
|
|
|
|
|
|
|
|
public double TotalBreakTime => beatmap.TotalBreakTime;
|
|
|
|
|
2019-10-03 13:23:48 +08:00
|
|
|
public IReadOnlyList<T> HitObjects => beatmap.HitObjects;
|
2019-08-29 11:43:43 +08:00
|
|
|
|
|
|
|
IReadOnlyList<HitObject> IBeatmap.HitObjects => beatmap.HitObjects;
|
|
|
|
|
|
|
|
public IEnumerable<BeatmapStatistic> GetStatistics() => beatmap.GetStatistics();
|
|
|
|
|
|
|
|
public IBeatmap Clone() => (EditorBeatmap<T>)MemberwiseClone();
|
|
|
|
|
2019-08-29 15:31:43 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap{T}"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
|
2019-08-29 11:43:43 +08:00
|
|
|
public void Add(T hitObject)
|
|
|
|
{
|
2019-10-03 13:23:48 +08:00
|
|
|
trackStartTime(hitObject);
|
|
|
|
|
2019-08-29 11:43:43 +08:00
|
|
|
// Preserve existing sorting order in the beatmap
|
|
|
|
var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime);
|
|
|
|
beatmap.HitObjects.Insert(insertionIndex + 1, hitObject);
|
|
|
|
|
|
|
|
HitObjectAdded?.Invoke(hitObject);
|
|
|
|
}
|
|
|
|
|
2019-08-29 15:31:43 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap{T}"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
|
2019-08-29 11:43:43 +08:00
|
|
|
public void Remove(T hitObject)
|
|
|
|
{
|
|
|
|
if (beatmap.HitObjects.Remove(hitObject))
|
2019-10-03 13:23:48 +08:00
|
|
|
{
|
|
|
|
var bindable = startTimeBindables[hitObject];
|
|
|
|
bindable.UnbindAll();
|
|
|
|
|
|
|
|
startTimeBindables.Remove(hitObject);
|
2019-08-29 11:43:43 +08:00
|
|
|
HitObjectRemoved?.Invoke(hitObject);
|
2019-10-03 13:23:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void trackStartTime(T 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.
|
|
|
|
beatmap.HitObjects.Remove(hitObject);
|
|
|
|
|
|
|
|
var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime);
|
|
|
|
beatmap.HitObjects.Insert(insertionIndex + 1, hitObject);
|
|
|
|
|
2019-10-03 13:37:16 +08:00
|
|
|
StartTimeChanged?.Invoke(hitObject);
|
2019-10-03 13:23:48 +08:00
|
|
|
};
|
2019-08-29 11:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 15:31:43 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap{T}"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
|
2019-08-29 11:43:43 +08:00
|
|
|
public void Add(HitObject hitObject) => Add((T)hitObject);
|
|
|
|
|
2019-08-29 15:31:43 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap{T}"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
|
2019-08-29 11:43:43 +08:00
|
|
|
public void Remove(HitObject hitObject) => Remove((T)hitObject);
|
|
|
|
}
|
|
|
|
}
|