1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 13:22:55 +08:00
osu-lazer/osu.Game/Rulesets/Edit/HitObjectComposer.cs

355 lines
14 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;
2019-10-16 19:20:07 +08:00
using JetBrains.Annotations;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
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.Threading;
2018-04-13 17:19:50 +08:00
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
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;
using osuTK;
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-12-05 19:12:25 +08:00
2019-08-29 17:02:50 +08:00
protected readonly Ruleset Ruleset;
2018-04-13 17:19:50 +08:00
2019-10-16 19:20:07 +08:00
[Resolved]
protected IFrameBasedClock EditorClock { get; private set; }
[Resolved]
protected EditorBeatmap EditorBeatmap { get; private set; }
[Resolved]
private IAdjustableClock adjustableClock { get; set; }
[Resolved]
private BindableBeatDivisor beatDivisor { get; set; }
2019-08-29 17:02:50 +08:00
private IBeatmapProcessor beatmapProcessor;
2018-07-19 16:04:51 +08:00
private DrawableEditRulesetWrapper<TObject> drawableRulesetWrapper;
private ComposeBlueprintContainer blueprintContainer;
2019-10-16 19:20:07 +08:00
private Container distanceSnapGridContainer;
private DistanceSnapGrid distanceSnapGrid;
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
{
beatmapProcessor = Ruleset.CreateBeatmapProcessor(EditorBeatmap.PlayableBeatmap);
EditorBeatmap.HitObjectAdded += addHitObject;
EditorBeatmap.HitObjectRemoved += removeHitObject;
EditorBeatmap.StartTimeChanged += UpdateHitObject;
Config = Dependencies.Get<RulesetConfigCache>().GetConfigFor(Ruleset);
2018-04-13 17:19:50 +08:00
try
{
drawableRulesetWrapper = new DrawableEditRulesetWrapper<TObject>(CreateDrawableRuleset(Ruleset, EditorBeatmap.PlayableBeatmap))
2019-08-29 17:02:50 +08:00
{
Clock = framedClock,
ProcessCustomClock = false
2019-08-29 17:02:50 +08:00
};
2018-04-13 17:19:50 +08:00
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap sucessfully!");
return;
}
2019-10-16 19:20:07 +08:00
var layerBelowRuleset = drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer().WithChildren(new Drawable[]
{
distanceSnapGridContainer = new Container { RelativeSizeAxes = Axes.Both },
new EditorPlayfieldBorder { RelativeSizeAxes = Axes.Both }
});
2018-04-13 17:19:50 +08:00
var layerAboveRuleset = drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer().WithChild(blueprintContainer = CreateBlueprintContainer());
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,
drawableRulesetWrapper,
2018-04-13 17:19:50 +08:00
layerAboveRuleset
}
}
},
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 200),
}
};
toolboxCollection.Items =
2019-10-16 19:20:07 +08:00
CompositionTools.Select(t => new RadioButton(t.Name, () => selectTool(t)))
.Prepend(new RadioButton("Select", () => selectTool(null)))
2019-02-28 12:31:40 +08:00
.ToList();
2018-04-13 17:19:50 +08:00
toolboxCollection.Items[0].Select();
2019-10-16 19:20:07 +08:00
blueprintContainer.SelectionChanged += selectionChanged;
2018-04-13 17:19:50 +08:00
}
2019-08-29 17:02:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
private double lastGridUpdateTime;
2019-10-16 19:20:07 +08:00
protected override void Update()
{
base.Update();
if (EditorClock.CurrentTime != lastGridUpdateTime && blueprintContainer.CurrentTool != null)
2019-10-16 19:20:07 +08:00
showGridFor(Enumerable.Empty<HitObject>());
}
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
});
}
2019-10-16 19:20:07 +08: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();
}
2019-10-23 16:49:21 +08:00
lastGridUpdateTime = EditorClock.CurrentTime;
2019-10-16 19:20:07 +08:00
}
private ScheduledDelegate scheduledUpdate;
public override void UpdateHitObject(HitObject hitObject)
2019-08-29 15:06:40 +08:00
{
scheduledUpdate?.Cancel();
scheduledUpdate = Schedule(() =>
{
beatmapProcessor?.PreProcess();
hitObject?.ApplyDefaults(EditorBeatmap.ControlPointInfo, EditorBeatmap.BeatmapInfo.BaseDifficulty);
beatmapProcessor?.PostProcess();
});
2019-08-29 11:43:43 +08:00
}
private void addHitObject(HitObject hitObject) => UpdateHitObject(hitObject);
private void removeHitObject(HitObject hitObject) => UpdateHitObject(null);
public override IEnumerable<DrawableHitObject> HitObjects => drawableRulesetWrapper.Playfield.AllHitObjects;
public override bool CursorInPlacementArea => drawableRulesetWrapper.Playfield.ReceivePositionalInputAt(inputManager.CurrentState.Mouse.Position);
2019-08-29 17:02:50 +08:00
protected abstract IReadOnlyList<HitObjectCompositionTool> CompositionTools { get; }
2020-01-16 10:54:03 +08:00
protected abstract ComposeBlueprintContainer CreateBlueprintContainer();
protected abstract DrawableRuleset<TObject> CreateDrawableRuleset(Ruleset ruleset, IBeatmap beatmap, IReadOnlyList<Mod> mods = null);
2019-08-29 15:06:40 +08:00
public void BeginPlacement(HitObject hitObject)
{
2019-10-18 16:56:31 +08:00
if (distanceSnapGrid != null)
hitObject.StartTime = GetSnappedPosition(distanceSnapGrid.ToLocalSpace(inputManager.CurrentState.Mouse.Position), hitObject.StartTime).time;
2019-08-29 15:06:40 +08:00
}
2019-10-18 18:04:08 +08:00
public void EndPlacement(HitObject hitObject)
{
EditorBeatmap.Add(hitObject);
adjustableClock.Seek(hitObject.StartTime);
2019-10-18 18:04:08 +08:00
showGridFor(Enumerable.Empty<HitObject>());
}
2019-08-29 15:06:40 +08:00
2019-10-16 19:10:03 +08:00
public void Delete(HitObject hitObject) => EditorBeatmap.Remove(hitObject);
2019-08-29 15:06:40 +08:00
public override (Vector2 position, double time) GetSnappedPosition(Vector2 position, double time) => distanceSnapGrid?.GetSnappedPosition(position) ?? (position, time);
public override float GetBeatSnapDistanceAt(double referenceTime)
{
DifficultyControlPoint difficultyPoint = EditorBeatmap.ControlPointInfo.DifficultyPointAt(referenceTime);
return (float)(100 * EditorBeatmap.BeatmapInfo.BaseDifficulty.SliderMultiplier * difficultyPoint.SpeedMultiplier / beatDivisor.Value);
}
public override float DurationToDistance(double referenceTime, double duration)
{
double beatLength = EditorBeatmap.ControlPointInfo.TimingPointAt(referenceTime).BeatLength / beatDivisor.Value;
return (float)(duration / beatLength * GetBeatSnapDistanceAt(referenceTime));
}
2019-10-16 19:34:02 +08:00
public override double DistanceToDuration(double referenceTime, float distance)
{
double beatLength = EditorBeatmap.ControlPointInfo.TimingPointAt(referenceTime).BeatLength / beatDivisor.Value;
return distance / GetBeatSnapDistanceAt(referenceTime) * beatLength;
}
public override double GetSnappedDurationFromDistance(double referenceTime, float distance)
=> beatSnap(referenceTime, DistanceToDuration(referenceTime, distance));
public override float GetSnappedDistanceFromDistance(double referenceTime, float distance)
=> DurationToDistance(referenceTime, beatSnap(referenceTime, DistanceToDuration(referenceTime, distance)));
/// <summary>
/// Snaps a duration to the closest beat of a timing point applicable at the reference time.
/// </summary>
/// <param name="referenceTime">The time of the timing point which <paramref name="duration"/> resides in.</param>
/// <param name="duration">The duration to snap.</param>
/// <returns>A value that represents <paramref name="duration"/> snapped to the closest beat of the timing point.</returns>
private double beatSnap(double referenceTime, double duration)
{
double beatLength = EditorBeatmap.ControlPointInfo.TimingPointAt(referenceTime).BeatLength / beatDivisor.Value;
2019-10-16 19:34:16 +08:00
// A 1ms offset prevents rounding errors due to minute variations in duration
return (int)((duration + 1) / beatLength) * beatLength;
}
2019-08-29 15:06:40 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2019-10-16 19:10:03 +08:00
if (EditorBeatmap != null)
2019-08-29 15:06:40 +08:00
{
2019-10-16 19:10:03 +08:00
EditorBeatmap.HitObjectAdded -= addHitObject;
EditorBeatmap.HitObjectRemoved -= removeHitObject;
2019-08-29 15:06:40 +08:00
}
}
}
2019-08-29 17:02:50 +08:00
[Cached(typeof(HitObjectComposer))]
2019-10-25 15:50:21 +08:00
[Cached(typeof(IDistanceSnapProvider))]
public abstract class HitObjectComposer : CompositeDrawable, IDistanceSnapProvider
2019-08-29 17:02:50 +08:00
{
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; }
2019-10-16 19:20:07 +08: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 19:34:02 +08:00
/// <summary>
/// Updates a <see cref="HitObject"/>, invoking <see cref="HitObject.ApplyDefaults"/> and re-processing the beatmap.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to update.</param>
public abstract void UpdateHitObject([CanBeNull] HitObject hitObject);
public abstract (Vector2 position, double time) GetSnappedPosition(Vector2 position, double time);
public abstract float GetBeatSnapDistanceAt(double referenceTime);
public abstract float DurationToDistance(double referenceTime, double duration);
public abstract double DistanceToDuration(double referenceTime, float distance);
public abstract double GetSnappedDurationFromDistance(double referenceTime, float distance);
public abstract float GetSnappedDistanceFromDistance(double referenceTime, float distance);
2019-08-29 17:02:50 +08:00
}
2018-04-13 17:19:50 +08:00
}