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

349 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.Collections.Specialized;
2018-04-13 17:19:50 +08:00
using System.Linq;
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;
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.Logging;
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;
2020-05-25 18:21:53 +08:00
using osu.Game.Rulesets.UI.Scrolling;
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;
using Key = osuTK.Input.Key;
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 EditorClock EditorClock { get; private set; }
2019-10-16 19:20:07 +08:00
[Resolved]
protected EditorBeatmap EditorBeatmap { get; private set; }
[Resolved]
2020-05-20 17:40:55 +08:00
protected IBeatSnapProvider BeatSnapProvider { get; private set; }
protected ComposeBlueprintContainer BlueprintContainer { get; private set; }
private DrawableEditRulesetWrapper<TObject> drawableRulesetWrapper;
protected readonly Container LayerBelowRuleset = new Container { RelativeSizeAxes = Axes.Both };
2018-11-14 17:34:13 +08:00
private InputManager inputManager;
private RadioButtonCollection toolboxCollection;
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
}
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
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 = EditorClock,
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;
}
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,
Masking = true,
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
// layers below playfield
drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer().WithChildren(new Drawable[]
{
LayerBelowRuleset,
new EditorPlayfieldBorder { RelativeSizeAxes = Axes.Both }
}),
drawableRulesetWrapper,
// layers above playfield
drawableRulesetWrapper.CreatePlayfieldAdjustmentContainer()
.WithChild(BlueprintContainer = CreateBlueprintContainer(HitObjects))
2018-04-13 17:19:50 +08:00
}
}
},
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, 200),
}
};
toolboxCollection.Items = CompositionTools
.Prepend(new SelectTool())
.Select(t => new RadioButton(t.Name, () => toolSelected(t)))
.ToList();
2018-04-13 17:19:50 +08:00
setSelectTool();
2019-10-16 19:20:07 +08:00
EditorBeatmap.SelectedHitObjects.CollectionChanged += selectionChanged;
2018-04-13 17:19:50 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
public override Playfield Playfield => drawableRulesetWrapper.Playfield;
public override IEnumerable<DrawableHitObject> HitObjects => drawableRulesetWrapper.Playfield.AllHitObjects;
public override bool CursorInPlacementArea => drawableRulesetWrapper.Playfield.ReceivePositionalInputAt(inputManager.CurrentState.Mouse.Position);
/// <summary>
/// Defines all available composition tools, listed on the left side of the editor screen as button controls.
/// This should usually define one tool for each <see cref="HitObject"/> type used in the target ruleset.
/// </summary>
/// <remarks>
/// A "select" tool is automatically added as the first tool.
/// </remarks>
protected abstract IReadOnlyList<HitObjectCompositionTool> CompositionTools { get; }
/// <summary>
/// Construct a relevant blueprint container. This will manage hitobject selection/placement input handling and display logic.
/// </summary>
/// <param name="hitObjects">A live collection of all <see cref="DrawableHitObject"/>s in the editor beatmap.</param>
protected virtual ComposeBlueprintContainer CreateBlueprintContainer(IEnumerable<DrawableHitObject> hitObjects)
=> new ComposeBlueprintContainer(hitObjects);
/// <summary>
/// Construct a drawable ruleset for the provided ruleset.
/// </summary>
/// <remarks>
/// Can be overridden to add editor-specific logical changes to a <see cref="Ruleset"/>'s standard <see cref="DrawableRuleset{TObject}"/>.
/// For example, hit animations or judgement logic may be changed to give a better editor user experience.
/// </remarks>
/// <param name="ruleset">The ruleset used to construct its drawable counterpart.</param>
/// <param name="beatmap">The loaded beatmap.</param>
/// <param name="mods">The mods to be applied.</param>
/// <returns>An editor-relevant <see cref="DrawableRuleset{TObject}"/>.</returns>
protected virtual DrawableRuleset<TObject> CreateDrawableRuleset(Ruleset ruleset, IBeatmap beatmap, IReadOnlyList<Mod> mods = null)
=> (DrawableRuleset<TObject>)ruleset.CreateDrawableRulesetWith(beatmap, mods);
#region Tool selection logic
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Key >= Key.Number1 && e.Key <= Key.Number9)
{
var item = toolboxCollection.Items.ElementAtOrDefault(e.Key - Key.Number1);
if (item != null)
{
item.Select();
return true;
}
}
return base.OnKeyDown(e);
}
private void selectionChanged(object sender, NotifyCollectionChangedEventArgs changedArgs)
2019-10-16 19:20:07 +08:00
{
if (EditorBeatmap.SelectedHitObjects.Any())
{
// ensure in selection mode if a selection is made.
setSelectTool();
}
2019-10-16 19:20:07 +08:00
}
private void setSelectTool() => toolboxCollection.Items.First().Select();
private void toolSelected(HitObjectCompositionTool tool)
2019-10-16 19:20:07 +08:00
{
BlueprintContainer.CurrentTool = tool;
2019-10-16 19:20:07 +08:00
if (!(tool is SelectTool))
EditorBeatmap.SelectedHitObjects.Clear();
2019-10-16 19:20:07 +08:00
}
#endregion
2019-08-29 17:02:50 +08:00
#region IPlacementHandler
2019-08-29 15:06:40 +08:00
public void BeginPlacement(HitObject hitObject)
{
EditorBeatmap.PlacementObject.Value = hitObject;
2019-08-29 15:06:40 +08:00
}
public void EndPlacement(HitObject hitObject, bool commit)
2019-10-18 18:04:08 +08:00
{
EditorBeatmap.PlacementObject.Value = null;
if (commit)
{
EditorBeatmap.Add(hitObject);
if (EditorClock.CurrentTime < hitObject.StartTime)
EditorClock.SeekTo(hitObject.StartTime);
}
2019-10-18 18:04:08 +08:00
}
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
#endregion
#region IPositionSnapProvider
/// <summary>
/// Retrieve the relevant <see cref="Playfield"/> at a specified screen-space position.
/// In cases where a ruleset doesn't require custom logic (due to nested playfields, for example)
/// this will return the ruleset's main playfield.
/// </summary>
/// <param name="screenSpacePosition">The screen-space position to query.</param>
/// <returns>The most relevant <see cref="Playfield"/>.</returns>
2020-05-25 18:21:53 +08:00
protected virtual Playfield PlayfieldAtScreenSpacePosition(Vector2 screenSpacePosition) => drawableRulesetWrapper.Playfield;
public override SnapResult SnapScreenSpacePositionToValidTime(Vector2 screenSpacePosition)
{
var playfield = PlayfieldAtScreenSpacePosition(screenSpacePosition);
double? targetTime = null;
if (playfield is ScrollingPlayfield scrollingPlayfield)
{
targetTime = scrollingPlayfield.TimeAtScreenSpacePosition(screenSpacePosition);
// apply beat snapping
targetTime = BeatSnapProvider.SnapTime(targetTime.Value);
// convert back to screen space
screenSpacePosition = scrollingPlayfield.ScreenSpacePositionAtTime(targetTime.Value);
}
return new SnapResult(screenSpacePosition, targetTime, playfield);
}
public override float GetBeatSnapDistanceAt(double referenceTime)
{
DifficultyControlPoint difficultyPoint = EditorBeatmap.ControlPointInfo.DifficultyPointAt(referenceTime);
2020-05-20 17:40:55 +08:00
return (float)(100 * EditorBeatmap.BeatmapInfo.BaseDifficulty.SliderMultiplier * difficultyPoint.SpeedMultiplier / BeatSnapProvider.BeatDivisor);
}
public override float DurationToDistance(double referenceTime, double duration)
{
2020-05-20 17:40:55 +08:00
double beatLength = BeatSnapProvider.GetBeatLengthAtTime(referenceTime);
return (float)(duration / beatLength * GetBeatSnapDistanceAt(referenceTime));
}
2019-10-16 19:34:02 +08:00
public override double DistanceToDuration(double referenceTime, float distance)
{
2020-05-20 17:40:55 +08:00
double beatLength = BeatSnapProvider.GetBeatLengthAtTime(referenceTime);
return distance / GetBeatSnapDistanceAt(referenceTime) * beatLength;
}
public override double GetSnappedDurationFromDistance(double referenceTime, float distance)
2020-05-20 17:40:55 +08:00
=> BeatSnapProvider.SnapTime(referenceTime + DistanceToDuration(referenceTime, distance), referenceTime) - referenceTime;
public override float GetSnappedDistanceFromDistance(double referenceTime, float distance)
{
2020-05-20 17:40:55 +08:00
var snappedEndTime = BeatSnapProvider.SnapTime(referenceTime + DistanceToDuration(referenceTime, distance), referenceTime);
return DurationToDistance(referenceTime, snappedEndTime - referenceTime);
}
#endregion
}
2019-08-29 17:02:50 +08:00
/// <summary>
/// A non-generic definition of a HitObject composer class.
/// Generally used to access certain methods without requiring a generic type for <see cref="HitObjectComposer{T}" />.
/// </summary>
2019-08-29 17:02:50 +08:00
[Cached(typeof(HitObjectComposer))]
[Cached(typeof(IPositionSnapProvider))]
public abstract class HitObjectComposer : CompositeDrawable, IPositionSnapProvider
2019-08-29 17:02:50 +08:00
{
protected HitObjectComposer()
2019-08-29 17:02:50 +08:00
{
RelativeSizeAxes = Axes.Both;
}
/// <summary>
/// The target ruleset's playfield.
/// </summary>
public abstract Playfield Playfield { get; }
2019-08-29 17:02:50 +08:00
/// <summary>
/// All <see cref="DrawableHitObject"/>s in currently loaded beatmap.
2019-08-29 17:02:50 +08:00
/// </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; }
#region IPositionSnapProvider
public abstract SnapResult SnapScreenSpacePositionToValidTime(Vector2 screenSpacePosition);
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);
#endregion
2019-08-29 17:02:50 +08:00
}
2018-04-13 17:19:50 +08:00
}