1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 22:22:54 +08:00
osu-lazer/osu.Game/Rulesets/Edit/EditRulesetContainer.cs
2019-03-19 23:47:31 +09:00

107 lines
3.7 KiB
C#

// 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.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
namespace osu.Game.Rulesets.Edit
{
public abstract class EditDrawableRuleset : CompositeDrawable
{
/// <summary>
/// The <see cref="Playfield"/> contained by this <see cref="EditDrawableRuleset"/>.
/// </summary>
public abstract Playfield Playfield { get; }
internal EditDrawableRuleset()
{
RelativeSizeAxes = Axes.Both;
}
/// <summary>
/// Adds a <see cref="HitObject"/> to the <see cref="Beatmap"/> and displays a visual representation of it.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
/// <returns>The visual representation of <paramref name="hitObject"/>.</returns>
internal abstract DrawableHitObject Add(HitObject hitObject);
/// <summary>
/// Removes a <see cref="HitObject"/> from the <see cref="Beatmap"/> and the display.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to remove.</param>
/// <returns>The visual representation of the removed <paramref name="hitObject"/>.</returns>
internal abstract DrawableHitObject Remove(HitObject hitObject);
}
public class EditDrawableRuleset<TObject> : EditDrawableRuleset
where TObject : HitObject
{
public override Playfield Playfield => rrawableRuleset.Playfield;
private Ruleset ruleset => rrawableRuleset.Ruleset;
private Beatmap<TObject> beatmap => rrawableRuleset.Beatmap;
private readonly DrawableRuleset<TObject> rrawableRuleset;
public EditDrawableRuleset(DrawableRuleset<TObject> rrawableRuleset)
{
this.rrawableRuleset = rrawableRuleset;
InternalChild = rrawableRuleset;
Playfield.DisplayJudgements.Value = false;
}
internal override DrawableHitObject Add(HitObject hitObject)
{
var tObject = (TObject)hitObject;
// Add to beatmap, preserving sorting order
var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime);
beatmap.HitObjects.Insert(insertionIndex + 1, tObject);
// Process object
var processor = ruleset.CreateBeatmapProcessor(beatmap);
processor?.PreProcess();
tObject.ApplyDefaults(beatmap.ControlPointInfo, beatmap.BeatmapInfo.BaseDifficulty);
processor?.PostProcess();
// Add visual representation
var drawableObject = rrawableRuleset.GetVisualRepresentation(tObject);
rrawableRuleset.Playfield.Add(drawableObject);
rrawableRuleset.Playfield.PostProcess();
return drawableObject;
}
internal override DrawableHitObject Remove(HitObject hitObject)
{
var tObject = (TObject)hitObject;
// Remove from beatmap
beatmap.HitObjects.Remove(tObject);
// Process the beatmap
var processor = ruleset.CreateBeatmapProcessor(beatmap);
processor?.PreProcess();
processor?.PostProcess();
// Remove visual representation
var drawableObject = Playfield.AllHitObjects.Single(d => d.HitObject == hitObject);
rrawableRuleset.Playfield.Remove(drawableObject);
rrawableRuleset.Playfield.PostProcess();
return drawableObject;
}
}
}