1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 15:43:21 +08:00

Encapsulate editor hitobject additions/removals (#5878)

Encapsulate editor hitobject additions/removals
This commit is contained in:
Dean Herbert 2019-08-29 18:20:24 +09:00 committed by GitHub
commit 98626018fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 267 additions and 116 deletions

View File

@ -16,15 +16,13 @@ using osu.Game.Rulesets.Osu.Edit;
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles;
using osu.Game.Rulesets.Osu.Edit.Blueprints.HitCircles.Components;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Screens.Edit.Compose;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
namespace osu.Game.Tests.Visual.Editor
{
[TestFixture]
[Cached(Type = typeof(IPlacementHandler))]
public class TestSceneHitObjectComposer : OsuTestScene, IPlacementHandler
public class TestSceneHitObjectComposer : OsuTestScene
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
@ -39,8 +37,6 @@ namespace osu.Game.Tests.Visual.Editor
typeof(HitCirclePlacementBlueprint),
};
private HitObjectComposer composer;
[BackgroundDependencyLoader]
private void load()
{
@ -67,15 +63,7 @@ namespace osu.Game.Tests.Visual.Editor
Dependencies.CacheAs<IAdjustableClock>(clock);
Dependencies.CacheAs<IFrameBasedClock>(clock);
Child = composer = new OsuHitObjectComposer(new OsuRuleset());
Child = new OsuHitObjectComposer(new OsuRuleset());
}
public void BeginPlacement(HitObject hitObject)
{
}
public void EndPlacement(HitObject hitObject) => composer.Add(hitObject);
public void Delete(HitObject hitObject) => composer.Remove(hitObject);
}
}

View File

@ -14,7 +14,7 @@ namespace osu.Game.Beatmaps
/// <summary>
/// A Beatmap containing converted HitObjects.
/// </summary>
public class Beatmap<T> : IBeatmap
public class Beatmap<T> : IBeatmap<T>
where T : HitObject
{
public BeatmapInfo BeatmapInfo { get; set; } = new BeatmapInfo
@ -36,17 +36,13 @@ namespace osu.Game.Beatmaps
public List<BreakPeriod> Breaks { get; set; } = new List<BreakPeriod>();
/// <summary>
/// Total amount of break time in the beatmap.
/// </summary>
[JsonIgnore]
public double TotalBreakTime => Breaks.Sum(b => b.Duration);
/// <summary>
/// The HitObjects this Beatmap contains.
/// </summary>
[JsonConverter(typeof(TypedListConverter<HitObject>))]
public List<T> HitObjects = new List<T>();
public List<T> HitObjects { get; set; } = new List<T>();
IReadOnlyList<T> IBeatmap<T>.HitObjects => HitObjects;
IReadOnlyList<HitObject> IBeatmap.HitObjects => HitObjects;

View File

@ -53,4 +53,13 @@ namespace osu.Game.Beatmaps
/// <returns>The shallow-cloned beatmap.</returns>
IBeatmap Clone();
}
public interface IBeatmap<out T> : IBeatmap
where T : HitObject
{
/// <summary>
/// The hitobjects contained by this beatmap.
/// </summary>
new IReadOnlyList<T> HitObjects { get; }
}
}

View File

@ -5,10 +5,9 @@ using System.Linq;
using osu.Framework.Allocation;
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;
using osu.Game.Screens.Edit;
namespace osu.Game.Rulesets.Edit
{
@ -25,20 +24,6 @@ namespace osu.Game.Rulesets.Edit
{
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 DrawableEditRuleset<TObject> : DrawableEditRuleset
@ -48,11 +33,11 @@ namespace osu.Game.Rulesets.Edit
public override PlayfieldAdjustmentContainer CreatePlayfieldAdjustmentContainer() => drawableRuleset.CreatePlayfieldAdjustmentContainer();
private Ruleset ruleset => drawableRuleset.Ruleset;
private Beatmap<TObject> beatmap => drawableRuleset.Beatmap;
private readonly DrawableRuleset<TObject> drawableRuleset;
[Resolved]
private IEditorBeatmap<TObject> beatmap { get; set; }
public DrawableEditRuleset(DrawableRuleset<TObject> drawableRuleset)
{
this.drawableRuleset = drawableRuleset;
@ -67,50 +52,39 @@ namespace osu.Game.Rulesets.Edit
Playfield.DisplayJudgements.Value = false;
}
internal override DrawableHitObject Add(HitObject hitObject)
protected override void LoadComplete()
{
var tObject = (TObject)hitObject;
base.LoadComplete();
// Add to beatmap, preserving sorting order
var insertionIndex = beatmap.HitObjects.FindLastIndex(h => h.StartTime <= hitObject.StartTime);
beatmap.HitObjects.Insert(insertionIndex + 1, tObject);
beatmap.HitObjectAdded += addHitObject;
beatmap.HitObjectRemoved += removeHitObject;
}
// Process object
var processor = ruleset.CreateBeatmapProcessor(beatmap);
processor?.PreProcess();
tObject.ApplyDefaults(beatmap.ControlPointInfo, beatmap.BeatmapInfo.BaseDifficulty);
processor?.PostProcess();
// Add visual representation
var drawableObject = drawableRuleset.CreateDrawableRepresentation(tObject);
private void addHitObject(HitObject hitObject)
{
var drawableObject = drawableRuleset.CreateDrawableRepresentation((TObject)hitObject);
drawableRuleset.Playfield.Add(drawableObject);
drawableRuleset.Playfield.PostProcess();
return drawableObject;
}
internal override DrawableHitObject Remove(HitObject hitObject)
private void removeHitObject(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);
drawableRuleset.Playfield.Remove(drawableObject);
drawableRuleset.Playfield.PostProcess();
}
return drawableObject;
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (beatmap != null)
{
beatmap.HitObjectAdded -= addHitObject;
beatmap.HitObjectRemoved -= removeHitObject;
}
}
}
}

View File

@ -18,7 +18,9 @@ using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Edit;
using osu.Game.Screens.Edit.Components.RadioButtons;
using osu.Game.Screens.Edit.Compose;
using osu.Game.Screens.Edit.Compose.Components;
namespace osu.Game.Rulesets.Edit
@ -153,14 +155,6 @@ namespace osu.Game.Rulesets.Edit
/// </summary>
public virtual bool CursorInPlacementArea => DrawableRuleset.Playfield.ReceivePositionalInputAt(inputManager.CurrentState.Mouse.Position);
/// <summary>
/// Adds a <see cref="HitObject"/> to the <see cref="Beatmaps.Beatmap"/> and visualises it.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Add(HitObject hitObject) => blueprintContainer.AddBlueprintFor(DrawableRuleset.Add(hitObject));
public void Remove(HitObject hitObject) => blueprintContainer.RemoveBlueprintFor(DrawableRuleset.Remove(hitObject));
internal abstract DrawableEditRuleset CreateDrawableRuleset();
protected abstract IReadOnlyList<HitObjectCompositionTool> CompositionTools { get; }
@ -177,17 +171,72 @@ namespace osu.Game.Rulesets.Edit
public virtual SelectionHandler CreateSelectionHandler() => new SelectionHandler();
}
public abstract class HitObjectComposer<TObject> : HitObjectComposer
[Cached(Type = typeof(IPlacementHandler))]
public abstract class HitObjectComposer<TObject> : HitObjectComposer, IPlacementHandler
where TObject : HitObject
{
private Beatmap<TObject> playableBeatmap;
private EditorBeatmap<TObject> editorBeatmap;
private IBeatmapProcessor beatmapProcessor;
protected HitObjectComposer(Ruleset ruleset)
: base(ruleset)
{
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var workingBeatmap = parent.Get<IBindable<WorkingBeatmap>>();
playableBeatmap = (Beatmap<TObject>)workingBeatmap.Value.GetPlayableBeatmap(Ruleset.RulesetInfo, Array.Empty<Mod>());
beatmapProcessor = Ruleset.CreateBeatmapProcessor(playableBeatmap);
editorBeatmap = new EditorBeatmap<TObject>(playableBeatmap);
editorBeatmap.HitObjectAdded += addHitObject;
editorBeatmap.HitObjectRemoved += removeHitObject;
var dependencies = new DependencyContainer(parent);
dependencies.CacheAs<IEditorBeatmap>(editorBeatmap);
dependencies.CacheAs<IEditorBeatmap<TObject>>(editorBeatmap);
return base.CreateChildDependencies(dependencies);
}
private void addHitObject(HitObject hitObject)
{
beatmapProcessor?.PreProcess();
hitObject.ApplyDefaults(playableBeatmap.ControlPointInfo, playableBeatmap.BeatmapInfo.BaseDifficulty);
beatmapProcessor?.PostProcess();
}
private void removeHitObject(HitObject hitObject)
{
beatmapProcessor?.PreProcess();
beatmapProcessor?.PostProcess();
}
internal override DrawableEditRuleset CreateDrawableRuleset()
=> new DrawableEditRuleset<TObject>(CreateDrawableRuleset(Ruleset, Beatmap.Value, Array.Empty<Mod>()));
protected abstract DrawableRuleset<TObject> CreateDrawableRuleset(Ruleset ruleset, WorkingBeatmap beatmap, IReadOnlyList<Mod> mods);
public void BeginPlacement(HitObject hitObject)
{
}
public void EndPlacement(HitObject hitObject) => editorBeatmap.Add(hitObject);
public void Delete(HitObject hitObject) => editorBeatmap.Remove(hitObject);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (editorBeatmap != null)
{
editorBeatmap.HitObjectAdded -= addHitObject;
editorBeatmap.HitObjectRemoved -= removeHitObject;
}
}
}
}

View File

@ -11,6 +11,7 @@ using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Tools;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
namespace osu.Game.Screens.Edit.Compose.Components
@ -29,6 +30,9 @@ namespace osu.Game.Screens.Edit.Compose.Components
[Resolved]
private HitObjectComposer composer { get; set; }
[Resolved]
private IEditorBeatmap beatmap { get; set; }
public BlueprintContainer()
{
RelativeSizeAxes = Axes.Both;
@ -53,7 +57,15 @@ namespace osu.Game.Screens.Edit.Compose.Components
};
foreach (var obj in composer.HitObjects)
AddBlueprintFor(obj);
addBlueprintFor(obj);
}
protected override void LoadComplete()
{
base.LoadComplete();
beatmap.HitObjectAdded += addBlueprintFor;
beatmap.HitObjectRemoved += removeBlueprintFor;
}
private HitObjectCompositionTool currentTool;
@ -75,11 +87,32 @@ namespace osu.Game.Screens.Edit.Compose.Components
}
}
/// <summary>
/// Adds a blueprint for a <see cref="DrawableHitObject"/> which adds movement support.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to create a blueprint for.</param>
public void AddBlueprintFor(DrawableHitObject hitObject)
private void addBlueprintFor(HitObject hitObject)
{
var drawable = composer.HitObjects.FirstOrDefault(d => d.HitObject == hitObject);
if (drawable == null)
return;
addBlueprintFor(drawable);
}
private void removeBlueprintFor(HitObject hitObject)
{
var blueprint = selectionBlueprints.Single(m => m.HitObject.HitObject == hitObject);
if (blueprint == null)
return;
blueprint.Deselect();
blueprint.Selected -= onBlueprintSelected;
blueprint.Deselected -= onBlueprintDeselected;
blueprint.SelectionRequested -= onSelectionRequested;
blueprint.DragRequested -= onDragRequested;
selectionBlueprints.Remove(blueprint);
}
private void addBlueprintFor(DrawableHitObject hitObject)
{
refreshTool();
@ -95,25 +128,7 @@ namespace osu.Game.Screens.Edit.Compose.Components
selectionBlueprints.Add(blueprint);
}
/// <summary>
/// Removes a blueprint for a <see cref="DrawableHitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> for which to remove the blueprint.</param>
public void RemoveBlueprintFor(DrawableHitObject hitObject)
{
var blueprint = selectionBlueprints.Single(m => m.HitObject == hitObject);
if (blueprint == null)
return;
blueprint.Deselect();
blueprint.Selected -= onBlueprintSelected;
blueprint.Deselected -= onBlueprintDeselected;
blueprint.SelectionRequested -= onSelectionRequested;
blueprint.DragRequested -= onDragRequested;
selectionBlueprints.Remove(blueprint);
}
private void removeBlueprintFor(DrawableHitObject hitObject) => removeBlueprintFor(hitObject.HitObject);
protected override bool OnClick(ClickEvent e)
{
@ -183,6 +198,17 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void onDragRequested(SelectionBlueprint blueprint, DragEvent dragEvent) => selectionHandler.HandleDrag(blueprint, dragEvent);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (beatmap != null)
{
beatmap.HitObjectAdded -= addBlueprintFor;
beatmap.HitObjectRemoved -= removeBlueprintFor;
}
}
private class SelectionBlueprintContainer : Container<SelectionBlueprint>
{
protected override int Compare(Drawable x, Drawable y)

View File

@ -9,15 +9,13 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Logging;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osu.Game.Screens.Edit.Compose.Components;
using osu.Game.Screens.Edit.Compose.Components.Timeline;
using osuTK.Graphics;
namespace osu.Game.Screens.Edit.Compose
{
[Cached(Type = typeof(IPlacementHandler))]
public class ComposeScreen : EditorScreen, IPlacementHandler
public class ComposeScreen : EditorScreen
{
private const float vertical_margins = 10;
private const float horizontal_margins = 20;
@ -119,13 +117,5 @@ namespace osu.Game.Screens.Edit.Compose
composerContainer.Child = composer;
}
public void BeginPlacement(HitObject hitObject)
{
}
public void EndPlacement(HitObject hitObject) => composer.Add(hitObject);
public void Delete(HitObject hitObject) => composer.Remove(hitObject);
}
}

View File

@ -0,0 +1,83 @@
// 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;
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<T> : IEditorBeatmap<T>
where T : HitObject
{
public event Action<HitObject> HitObjectAdded;
public event Action<HitObject> HitObjectRemoved;
private readonly Beatmap<T> beatmap;
public EditorBeatmap(Beatmap<T> beatmap)
{
this.beatmap = beatmap;
}
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;
IReadOnlyList<T> IBeatmap<T>.HitObjects => beatmap.HitObjects;
IReadOnlyList<HitObject> IBeatmap.HitObjects => beatmap.HitObjects;
public IEnumerable<BeatmapStatistic> GetStatistics() => beatmap.GetStatistics();
public IBeatmap Clone() => (EditorBeatmap<T>)MemberwiseClone();
/// <summary>
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap{T}"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Add(T hitObject)
{
// 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);
}
/// <summary>
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap{T}"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Remove(T hitObject)
{
if (beatmap.HitObjects.Remove(hitObject))
HitObjectRemoved?.Invoke(hitObject);
}
/// <summary>
/// Adds a <see cref="HitObject"/> to this <see cref="EditorBeatmap{T}"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Add(HitObject hitObject) => Add((T)hitObject);
/// <summary>
/// Removes a <see cref="HitObject"/> from this <see cref="EditorBeatmap{T}"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void Remove(HitObject hitObject) => Remove((T)hitObject);
}
}

View File

@ -0,0 +1,36 @@
// 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 osu.Game.Beatmaps;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Screens.Edit
{
/// <summary>
/// Interface for the <see cref="IBeatmap"/> contained by the see <see cref="HitObjectComposer"/>.
/// Children of <see cref="HitObjectComposer"/> may resolve the beatmap via <see cref="IEditorBeatmap"/> or <see cref="IEditorBeatmap{T}"/>.
/// </summary>
public interface IEditorBeatmap : IBeatmap
{
/// <summary>
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="IEditorBeatmap"/>.
/// </summary>
event Action<HitObject> HitObjectAdded;
/// <summary>
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="IEditorBeatmap"/>.
/// </summary>
event Action<HitObject> HitObjectRemoved;
}
/// <summary>
/// Interface for the <see cref="IBeatmap"/> contained by the see <see cref="HitObjectComposer"/>.
/// Children of <see cref="HitObjectComposer"/> may resolve the beatmap via <see cref="IEditorBeatmap"/> or <see cref="IEditorBeatmap{T}"/>.
/// </summary>
public interface IEditorBeatmap<out T> : IEditorBeatmap, IBeatmap<T>
where T : HitObject
{
}
}