osu/osu.Game/Rulesets/Edit/HitObjectComposer.cs

321 lines
12 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 09:19:50 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-10-16 11:20:07 +00:00
using JetBrains.Annotations;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-11-14 09:34:13 +00:00
using osu.Framework.Input;
2018-04-13 09:19:50 +00:00
using osu.Framework.Logging;
using osu.Framework.Threading;
2018-04-13 09:19:50 +00:00
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Configuration;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Edit.Tools;
2019-04-08 09:32:05 +00:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
2019-08-29 03:43:43 +00:00
using osu.Game.Screens.Edit;
2018-11-06 09:14:46 +00:00
using osu.Game.Screens.Edit.Components.RadioButtons;
2019-08-29 07:06:40 +00:00
using osu.Game.Screens.Edit.Compose;
2018-11-06 09:28:22 +00:00
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Edit
{
2019-08-29 09:02:50 +00:00
[Cached(Type = typeof(IPlacementHandler))]
public abstract class HitObjectComposer<TObject> : HitObjectComposer, IPlacementHandler
where TObject : HitObject
2018-04-13 09:19:50 +00:00
{
protected IRulesetConfigManager Config { get; private set; }
2019-10-16 11:10:03 +00:00
protected EditorBeatmap<TObject> EditorBeatmap { get; private set; }
2019-08-29 09:02:50 +00:00
protected readonly Ruleset Ruleset;
2018-04-13 09:19:50 +00:00
2019-10-16 11:20:07 +00:00
[Resolved]
protected IFrameBasedClock EditorClock { get; private set; }
2019-08-29 10:38:44 +00:00
private IWorkingBeatmap workingBeatmap;
2019-08-29 09:02:50 +00:00
private Beatmap<TObject> playableBeatmap;
private IBeatmapProcessor beatmapProcessor;
2018-07-19 08:04:51 +00:00
private DrawableEditRulesetWrapper<TObject> drawableRulesetWrapper;
private BlueprintContainer blueprintContainer;
2019-10-16 11:20:07 +00:00
private Container distanceSnapGridContainer;
private DistanceSnapGrid distanceSnapGrid;
2019-08-29 09:02:50 +00:00
private readonly List<Container> layerContainers = new List<Container>();
2018-11-14 09:34:13 +00:00
private InputManager inputManager;
2019-08-29 09:02:50 +00:00
protected HitObjectComposer(Ruleset ruleset)
2018-04-13 09:19:50 +00:00
{
Ruleset = ruleset;
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
2019-08-29 09:02:50 +00:00
private void load(IFrameBasedClock framedClock)
2018-04-13 09:19:50 +00:00
{
try
{
2019-08-29 10:38:44 +00:00
drawableRulesetWrapper = new DrawableEditRulesetWrapper<TObject>(CreateDrawableRuleset(Ruleset, workingBeatmap, Array.Empty<Mod>()))
2019-08-29 09:02:50 +00:00
{
Clock = framedClock,
ProcessCustomClock = false
2019-08-29 09:02:50 +00:00
};
2018-04-13 09:19:50 +00:00
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap sucessfully!");
return;
}
2019-10-16 11:20:07 +00:00
var layerBelowRuleset = drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer().WithChildren(new Drawable[]
{
distanceSnapGridContainer = new Container { RelativeSizeAxes = Axes.Both },
new EditorPlayfieldBorder { RelativeSizeAxes = Axes.Both }
});
2018-04-13 09:19:50 +00:00
2019-10-16 11:20:07 +00:00
var layerAboveRuleset = drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer().WithChild(blueprintContainer = new BlueprintContainer());
2018-04-13 09:19:50 +00: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,
drawableRulesetWrapper,
2018-04-13 09:19:50 +00:00
layerAboveRuleset
}
}
},
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 200),
}
};
toolboxCollection.Items =
2019-10-16 11:20:07 +00:00
CompositionTools.Select(t => new RadioButton(t.Name, () => selectTool(t)))
.Prepend(new RadioButton("Select", () => selectTool(null)))
2019-02-28 04:31:40 +00:00
.ToList();
2018-04-13 09:19:50 +00:00
toolboxCollection.Items[0].Select();
2019-10-16 11:20:07 +00:00
blueprintContainer.SelectionChanged += selectionChanged;
2018-04-13 09:19:50 +00:00
}
2019-08-29 09:02:50 +00:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2018-11-14 09:34:13 +00:00
{
2019-08-29 10:38:44 +00:00
var parentWorkingBeatmap = parent.Get<IBindable<WorkingBeatmap>>().Value;
playableBeatmap = (Beatmap<TObject>)parentWorkingBeatmap.GetPlayableBeatmap(Ruleset.RulesetInfo, Array.Empty<Mod>());
workingBeatmap = new EditorWorkingBeatmap<TObject>(playableBeatmap, parentWorkingBeatmap);
2018-11-14 09:34:13 +00:00
2019-08-29 09:02:50 +00:00
beatmapProcessor = Ruleset.CreateBeatmapProcessor(playableBeatmap);
2018-11-14 09:34:13 +00:00
2019-10-16 11:10:03 +00:00
EditorBeatmap = new EditorBeatmap<TObject>(playableBeatmap);
EditorBeatmap.HitObjectAdded += addHitObject;
EditorBeatmap.HitObjectRemoved += removeHitObject;
EditorBeatmap.StartTimeChanged += updateHitObject;
2019-08-29 09:02:50 +00:00
var dependencies = new DependencyContainer(parent);
2019-10-16 11:10:03 +00:00
dependencies.CacheAs<IEditorBeatmap>(EditorBeatmap);
dependencies.CacheAs<IEditorBeatmap<TObject>>(EditorBeatmap);
Config = dependencies.Get<RulesetConfigCache>().GetConfigFor(Ruleset);
2019-08-29 09:02:50 +00:00
return base.CreateChildDependencies(dependencies);
}
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
private double lastGridUpdateTime;
2019-10-16 11:20:07 +00:00
protected override void Update()
{
base.Update();
if (EditorClock.CurrentTime != lastGridUpdateTime && blueprintContainer.CurrentTool != null)
{
2019-10-16 11:20:07 +00:00
showGridFor(Enumerable.Empty<HitObject>());
lastGridUpdateTime = EditorClock.CurrentTime;
}
2019-10-16 11:20:07 +00:00
}
2018-04-13 09:19:50 +00:00
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
layerContainers.ForEach(l =>
{
l.Anchor = drawableRulesetWrapper.Playfield.Anchor;
l.Origin = drawableRulesetWrapper.Playfield.Origin;
l.Position = drawableRulesetWrapper.Playfield.Position;
l.Size = drawableRulesetWrapper.Playfield.Size;
2018-04-13 09:19:50 +00:00
});
}
2019-10-16 11:20:07 +00:00
private void selectionChanged(IEnumerable<HitObject> selectedHitObjects)
{
var hitObjects = selectedHitObjects.ToArray();
if (!hitObjects.Any())
distanceSnapGridContainer.Hide();
else
showGridFor(hitObjects);
}
private void selectTool(HitObjectCompositionTool tool)
{
blueprintContainer.CurrentTool = tool;
if (tool == null)
distanceSnapGridContainer.Hide();
else
showGridFor(Enumerable.Empty<HitObject>());
}
private void showGridFor(IEnumerable<HitObject> selectedHitObjects)
{
distanceSnapGridContainer.Clear();
distanceSnapGrid = CreateDistanceSnapGrid(selectedHitObjects);
if (distanceSnapGrid != null)
{
distanceSnapGridContainer.Child = distanceSnapGrid;
distanceSnapGridContainer.Show();
lastGridUpdateTime = EditorClock.CurrentTime;
2019-10-16 11:20:07 +00:00
}
}
private ScheduledDelegate scheduledUpdate;
2019-10-03 05:40:00 +00:00
private void addHitObject(HitObject hitObject) => updateHitObject(hitObject);
private void removeHitObject(HitObject hitObject) => updateHitObject(null);
2019-08-29 07:06:40 +00:00
private void updateHitObject([CanBeNull] HitObject hitObject)
2019-08-29 07:06:40 +00:00
{
scheduledUpdate?.Cancel();
scheduledUpdate = Schedule(() =>
{
beatmapProcessor?.PreProcess();
hitObject?.ApplyDefaults(playableBeatmap.ControlPointInfo, playableBeatmap.BeatmapInfo.BaseDifficulty);
beatmapProcessor?.PostProcess();
});
2019-08-29 03:43:43 +00:00
}
public override IEnumerable<DrawableHitObject> HitObjects => drawableRulesetWrapper.Playfield.AllHitObjects;
public override bool CursorInPlacementArea => drawableRulesetWrapper.Playfield.ReceivePositionalInputAt(inputManager.CurrentState.Mouse.Position);
2019-08-29 09:02:50 +00:00
protected abstract IReadOnlyList<HitObjectCompositionTool> CompositionTools { get; }
2019-08-29 10:38:44 +00:00
protected abstract DrawableRuleset<TObject> CreateDrawableRuleset(Ruleset ruleset, IWorkingBeatmap beatmap, IReadOnlyList<Mod> mods);
2019-08-29 07:06:40 +00:00
public void BeginPlacement(HitObject hitObject)
{
2019-10-18 08:56:31 +00:00
if (distanceSnapGrid != null)
hitObject.StartTime = GetSnappedTime(hitObject.StartTime, distanceSnapGrid.ToLocalSpace(inputManager.CurrentState.Mouse.Position));
2019-08-29 07:06:40 +00:00
}
2019-10-18 10:04:08 +00:00
public void EndPlacement(HitObject hitObject)
{
EditorBeatmap.Add(hitObject);
showGridFor(Enumerable.Empty<HitObject>());
}
2019-08-29 07:06:40 +00:00
2019-10-16 11:10:03 +00:00
public void Delete(HitObject hitObject) => EditorBeatmap.Remove(hitObject);
2019-08-29 07:06:40 +00:00
2019-10-18 04:18:16 +00:00
public override Vector2 GetSnappedPosition(Vector2 position) => distanceSnapGrid?.GetSnapPosition(position) ?? position;
2019-10-16 11:34:02 +00:00
2019-10-18 04:18:16 +00:00
public override double GetSnappedTime(double startTime, Vector2 position) => distanceSnapGrid?.GetSnapTime(position) ?? startTime;
2019-10-16 11:34:16 +00:00
2019-08-29 07:06:40 +00:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2019-10-16 11:10:03 +00:00
if (EditorBeatmap != null)
2019-08-29 07:06:40 +00:00
{
2019-10-16 11:10:03 +00:00
EditorBeatmap.HitObjectAdded -= addHitObject;
EditorBeatmap.HitObjectRemoved -= removeHitObject;
2019-08-29 07:06:40 +00:00
}
}
}
2019-08-29 09:02:50 +00: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();
2019-10-16 11:20:07 +00:00
/// <summary>
/// Creates the <see cref="DistanceSnapGrid"/> applicable for a <see cref="HitObject"/> selection.
/// </summary>
/// <param name="selectedHitObjects">The <see cref="HitObject"/> selection.</param>
/// <returns>The <see cref="DistanceSnapGrid"/> for <paramref name="selectedHitObjects"/>.</returns>
[CanBeNull]
protected virtual DistanceSnapGrid CreateDistanceSnapGrid([NotNull] IEnumerable<HitObject> selectedHitObjects) => null;
2019-10-16 11:34:02 +00:00
public abstract Vector2 GetSnappedPosition(Vector2 position);
2019-10-16 11:34:16 +00:00
public abstract double GetSnappedTime(double startTime, Vector2 screenSpacePosition);
2019-08-29 09:02:50 +00:00
}
2018-04-13 09:19:50 +00:00
}