1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 09:47:52 +08:00
osu-lazer/osu.Game/Rulesets/Edit/HitObjectComposer.cs

233 lines
8.7 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-11-14 17:34:13 +08:00
using osu.Framework.Input;
2018-04-13 17:19:50 +08:00
using osu.Framework.Logging;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Configuration;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Edit.Tools;
2019-04-08 17:32:05 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
2019-08-29 11:43:43 +08:00
using osu.Game.Screens.Edit;
2018-11-06 17:14:46 +08:00
using osu.Game.Screens.Edit.Components.RadioButtons;
2019-08-29 15:06:40 +08:00
using osu.Game.Screens.Edit.Compose;
2018-11-06 17:28:22 +08:00
using osu.Game.Screens.Edit.Compose.Components;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Edit
{
2019-08-29 17:02:50 +08:00
[Cached(Type = typeof(IPlacementHandler))]
public abstract class HitObjectComposer<TObject> : HitObjectComposer, IPlacementHandler
where TObject : HitObject
2018-04-13 17:19:50 +08:00
{
protected IRulesetConfigManager Config { get; private set; }
2019-08-29 17:02:50 +08:00
protected readonly Ruleset Ruleset;
2018-04-13 17:19:50 +08:00
2019-08-29 17:02:50 +08:00
private IBindable<WorkingBeatmap> workingBeatmap;
private Beatmap<TObject> playableBeatmap;
private EditorBeatmap<TObject> editorBeatmap;
private IBeatmapProcessor beatmapProcessor;
2018-07-19 16:04:51 +08:00
2019-08-29 17:12:29 +08:00
private DrawableEditRuleset<TObject> drawableRuleset;
private BlueprintContainer blueprintContainer;
2019-08-29 17:02:50 +08:00
private readonly List<Container> layerContainers = new List<Container>();
2018-11-14 17:34:13 +08:00
private InputManager inputManager;
2019-08-29 17:02:50 +08:00
protected HitObjectComposer(Ruleset ruleset)
2018-04-13 17:19:50 +08:00
{
Ruleset = ruleset;
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
2019-08-29 17:02:50 +08:00
private void load(IFrameBasedClock framedClock)
2018-04-13 17:19:50 +08:00
{
try
{
2019-08-29 17:12:29 +08:00
drawableRuleset = new DrawableEditRuleset<TObject>(CreateDrawableRuleset(Ruleset, workingBeatmap.Value, Array.Empty<Mod>()))
2019-08-29 17:02:50 +08:00
{
Clock = framedClock
};
2018-04-13 17:19:50 +08:00
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap sucessfully!");
return;
}
2019-08-29 17:12:29 +08:00
var layerBelowRuleset = drawableRuleset.CreatePlayfieldAdjustmentContainer();
2018-11-06 17:10:46 +08:00
layerBelowRuleset.Child = new EditorPlayfieldBorder { RelativeSizeAxes = Axes.Both };
2018-04-13 17:19:50 +08:00
2019-08-29 17:12:29 +08:00
var layerAboveRuleset = drawableRuleset.CreatePlayfieldAdjustmentContainer();
2018-11-09 10:36:06 +08:00
layerAboveRuleset.Child = blueprintContainer = new BlueprintContainer();
2018-04-13 17:19:50 +08:00
layerContainers.Add(layerBelowRuleset);
layerContainers.Add(layerAboveRuleset);
RadioButtonCollection toolboxCollection;
InternalChild = new GridContainer
{
RelativeSizeAxes = Axes.Both,
Content = new[]
{
new Drawable[]
{
new FillFlowContainer
{
Name = "Sidebar",
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = 10 },
Children = new Drawable[]
{
new ToolboxGroup { Child = toolboxCollection = new RadioButtonCollection { RelativeSizeAxes = Axes.X } }
}
},
new Container
{
Name = "Content",
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
layerBelowRuleset,
2019-08-29 17:12:29 +08:00
drawableRuleset,
2018-04-13 17:19:50 +08:00
layerAboveRuleset
}
}
},
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 200),
}
};
toolboxCollection.Items =
CompositionTools.Select(t => new RadioButton(t.Name, () => blueprintContainer.CurrentTool = t))
2019-02-28 12:31:40 +08:00
.Prepend(new RadioButton("Select", () => blueprintContainer.CurrentTool = null))
.ToList();
2018-04-13 17:19:50 +08:00
toolboxCollection.Items[0].Select();
}
2019-08-29 17:02:50 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2018-11-14 17:34:13 +08:00
{
2019-08-29 17:02:50 +08:00
workingBeatmap = parent.Get<IBindable<WorkingBeatmap>>().GetBoundCopy();
playableBeatmap = (Beatmap<TObject>)workingBeatmap.Value.GetPlayableBeatmap(Ruleset.RulesetInfo, Array.Empty<Mod>());
2018-11-14 17:34:13 +08:00
2019-08-29 17:02:50 +08:00
beatmapProcessor = Ruleset.CreateBeatmapProcessor(playableBeatmap);
2018-11-14 17:34:13 +08:00
2019-08-29 17:02:50 +08:00
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);
Config = dependencies.Get<RulesetConfigCache>().GetConfigFor(Ruleset);
2019-08-29 17:02:50 +08:00
return base.CreateChildDependencies(dependencies);
}
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
2018-04-13 17:19:50 +08:00
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
layerContainers.ForEach(l =>
{
2019-08-29 17:12:29 +08:00
l.Anchor = drawableRuleset.Playfield.Anchor;
l.Origin = drawableRuleset.Playfield.Origin;
l.Position = drawableRuleset.Playfield.Position;
l.Size = drawableRuleset.Playfield.Size;
2018-04-13 17:19:50 +08:00
});
}
2019-08-29 15:06:40 +08:00
private void addHitObject(HitObject hitObject)
{
2019-08-29 16:21:52 +08:00
beatmapProcessor?.PreProcess();
2019-08-29 15:06:40 +08:00
hitObject.ApplyDefaults(playableBeatmap.ControlPointInfo, playableBeatmap.BeatmapInfo.BaseDifficulty);
2019-08-29 16:21:52 +08:00
beatmapProcessor?.PostProcess();
2019-08-29 15:06:40 +08:00
}
private void removeHitObject(HitObject hitObject)
{
2019-08-29 16:21:52 +08:00
beatmapProcessor?.PreProcess();
beatmapProcessor?.PostProcess();
2019-08-29 11:43:43 +08:00
}
2019-08-29 17:12:29 +08:00
public override IEnumerable<DrawableHitObject> HitObjects => drawableRuleset.Playfield.AllHitObjects;
public override bool CursorInPlacementArea => drawableRuleset.Playfield.ReceivePositionalInputAt(inputManager.CurrentState.Mouse.Position);
2019-08-29 17:02:50 +08:00
protected abstract IReadOnlyList<HitObjectCompositionTool> CompositionTools { get; }
protected abstract DrawableRuleset<TObject> CreateDrawableRuleset(Ruleset ruleset, WorkingBeatmap beatmap, IReadOnlyList<Mod> mods);
2019-08-29 15:06:40 +08:00
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;
}
}
}
2019-08-29 17:02:50 +08:00
[Cached(typeof(HitObjectComposer))]
public abstract class HitObjectComposer : CompositeDrawable
{
internal HitObjectComposer()
{
RelativeSizeAxes = Axes.Both;
}
/// <summary>
/// All the <see cref="DrawableHitObject"/>s.
/// </summary>
public abstract IEnumerable<DrawableHitObject> HitObjects { get; }
/// <summary>
/// Whether the user's cursor is currently in an area of the <see cref="HitObjectComposer"/> that is valid for placement.
/// </summary>
public abstract bool CursorInPlacementArea { get; }
/// <summary>
/// Creates a <see cref="SelectionBlueprint"/> for a specific <see cref="DrawableHitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to create the overlay for.</param>
public virtual SelectionBlueprint CreateBlueprintFor(DrawableHitObject hitObject) => null;
/// <summary>
/// Creates a <see cref="SelectionHandler"/> which outlines <see cref="DrawableHitObject"/>s and handles movement of selections.
/// </summary>
public virtual SelectionHandler CreateSelectionHandler() => new SelectionHandler();
}
2018-04-13 17:19:50 +08:00
}