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

125 lines
4.3 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;
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-12-27 18:39:30 +08:00
public class EditorBeatmap : IEditorBeatmap
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>
2019-12-27 18:39:30 +08:00
/// Invoked when the start time of a <see cref="HitObject"/> in this <see cref="EditorBeatmap"/> was changed.
/// </summary>
public event Action<HitObject> StartTimeChanged;
2019-08-29 11:43:43 +08:00
2019-12-27 18:39:30 +08:00
private readonly Dictionary<HitObject, Bindable<double>> startTimeBindables = new Dictionary<HitObject, Bindable<double>>();
private readonly IBeatmap beatmap;
2019-08-29 11:43:43 +08:00
2019-12-27 18:39:30 +08:00
public EditorBeatmap(IBeatmap beatmap)
2019-08-29 11:43:43 +08:00
{
this.beatmap = beatmap;
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-12-27 18:39:30 +08:00
public IReadOnlyList<HitObject> HitObjects => beatmap.HitObjects;
2019-08-29 11:43:43 +08:00
IReadOnlyList<HitObject> IBeatmap.HitObjects => beatmap.HitObjects;
public IEnumerable<BeatmapStatistic> GetStatistics() => beatmap.GetStatistics();
2019-12-27 18:39:30 +08:00
public IBeatmap Clone() => (EditorBeatmap)MemberwiseClone();
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
/// 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
{
trackStartTime(hitObject);
2019-08-29 11:43:43 +08:00
// Preserve existing sorting order in the beatmap
2019-12-27 18:39:30 +08:00
var insertionIndex = findInsertionIndex(beatmap.HitObjects, hitObject.StartTime);
((IList)beatmap.HitObjects).Insert(insertionIndex + 1, hitObject);
2019-08-29 11:43:43 +08:00
HitObjectAdded?.Invoke(hitObject);
}
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>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
2019-12-27 18:39:30 +08:00
public void Remove(HitObject hitObject)
2019-08-29 11:43:43 +08:00
{
2019-12-27 18:39:30 +08:00
if (!((IList)beatmap.HitObjects).Contains(hitObject))
return;
2019-12-27 18:39:30 +08:00
((IList)beatmap.HitObjects).Remove(hitObject);
var bindable = startTimeBindables[hitObject];
bindable.UnbindAll();
startTimeBindables.Remove(hitObject);
HitObjectRemoved?.Invoke(hitObject);
}
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.
2019-12-27 18:39:30 +08:00
((IList)beatmap.HitObjects).Remove(hitObject);
2019-12-27 18:39:30 +08:00
var insertionIndex = findInsertionIndex(beatmap.HitObjects, hitObject.StartTime);
((IList)beatmap.HitObjects).Insert(insertionIndex + 1, hitObject);
StartTimeChanged?.Invoke(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;
}
2019-08-29 11:43:43 +08:00
}
}