1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 09:32:54 +08:00
osu-lazer/osu.Game/Screens/Edit/EditorBeatmap.cs

126 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
{
public class EditorBeatmap : IBeatmap
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
public readonly IBeatmap PlayableBeatmap;
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
public EditorBeatmap(IBeatmap playableBeatmap)
2019-08-29 11:43:43 +08:00
{
PlayableBeatmap = 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;
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
var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime);
mutableHitObjects.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
{
if (!mutableHitObjects.Contains(hitObject))
2019-12-27 18:39:30 +08:00
return;
mutableHitObjects.Remove(hitObject);
2019-12-27 18:39:30 +08:00
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.
mutableHitObjects.Remove(hitObject);
var insertionIndex = findInsertionIndex(PlayableBeatmap.HitObjects, hitObject.StartTime);
mutableHitObjects.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
}
}