1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 15:02:55 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

352 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
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Collections.Generic;
2019-10-16 19:20:07 +08:00
using System.Linq;
using osu.Framework.Allocation;
2020-09-09 18:14:28 +08:00
using osu.Framework.Bindables;
using osu.Framework.Caching;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2017-11-30 18:19:34 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
2017-11-30 17:48:00 +08:00
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Tools;
2019-04-08 17:32:05 +08:00
using osu.Game.Rulesets.Mods;
2019-10-16 19:20:07 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Edit.Components.TernaryButtons;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
2018-04-13 17:19:50 +08:00
2017-11-29 15:22:11 +08:00
namespace osu.Game.Rulesets.Osu.Edit
{
public partial class OsuHitObjectComposer : DistancedHitObjectComposer<OsuHitObject>
2017-11-29 15:22:11 +08:00
{
public OsuHitObjectComposer(Ruleset ruleset)
: base(ruleset)
{
}
2018-04-13 17:19:50 +08:00
protected override DrawableRuleset<OsuHitObject> CreateDrawableRuleset(Ruleset ruleset, IBeatmap beatmap, IReadOnlyList<Mod> mods = null)
=> new DrawableOsuEditorRuleset(ruleset, beatmap, mods);
2018-04-13 17:19:50 +08:00
protected override IReadOnlyList<HitObjectCompositionTool> CompositionTools => new HitObjectCompositionTool[]
{
new HitCircleCompositionTool(),
new SliderCompositionTool(),
2018-10-29 17:35:46 +08:00
new SpinnerCompositionTool()
};
2018-04-13 17:19:50 +08:00
private readonly Bindable<TernaryState> rectangularGridSnapToggle = new Bindable<TernaryState>();
2020-09-09 18:14:28 +08:00
2020-09-25 16:45:19 +08:00
protected override IEnumerable<TernaryButton> CreateTernaryButtons() => base.CreateTernaryButtons().Concat(new[]
2020-09-09 18:14:28 +08:00
{
new TernaryButton(rectangularGridSnapToggle, "Grid Snap", () => new SpriteIcon { Icon = FontAwesome.Solid.Th })
});
2020-09-09 18:14:28 +08:00
private BindableList<HitObject> selectedHitObjects;
private Bindable<HitObject> placementObject;
[BackgroundDependencyLoader]
private void load()
{
// Give a bit of breathing room around the playfield content.
PlayfieldContentContainer.Padding = new MarginPadding
{
Vertical = 10,
Left = TOOLBOX_CONTRACTED_SIZE_LEFT + 10,
Right = TOOLBOX_CONTRACTED_SIZE_RIGHT + 10,
};
2020-10-20 12:59:03 +08:00
LayerBelowRuleset.AddRange(new Drawable[]
{
2020-10-20 12:59:03 +08:00
distanceSnapGridContainer = new Container
{
RelativeSizeAxes = Axes.Both
},
rectangularPositionSnapGrid = new OsuRectangularPositionSnapGrid
2020-10-20 12:59:03 +08:00
{
RelativeSizeAxes = Axes.Both
}
});
selectedHitObjects = EditorBeatmap.SelectedHitObjects.GetBoundCopy();
2022-06-24 20:25:23 +08:00
selectedHitObjects.CollectionChanged += (_, _) => updateDistanceSnapGrid();
placementObject = EditorBeatmap.PlacementObject.GetBoundCopy();
placementObject.ValueChanged += _ => updateDistanceSnapGrid();
DistanceSnapToggle.ValueChanged += _ => updateDistanceSnapGrid();
// we may be entering the screen with a selection already active
updateDistanceSnapGrid();
}
protected override ComposeBlueprintContainer CreateBlueprintContainer()
=> new OsuBlueprintContainer(this);
2019-10-16 19:20:07 +08:00
2021-03-29 17:29:05 +08:00
public override string ConvertSelectionToString()
=> string.Join(',', selectedHitObjects.Cast<OsuHitObject>().OrderBy(h => h.StartTime).Select(h => (h.IndexInCurrentCombo + 1).ToString()));
private DistanceSnapGrid distanceSnapGrid;
private Container distanceSnapGridContainer;
private readonly Cached distanceSnapGridCache = new Cached();
private double? lastDistanceSnapGridTime;
private RectangularPositionSnapGrid rectangularPositionSnapGrid;
protected override double ReadCurrentDistanceSnap(HitObject before, HitObject after)
{
float expectedDistance = DurationToDistance(before, after.StartTime - before.GetEndTime());
float actualDistance = Vector2.Distance(((OsuHitObject)before).EndPosition, ((OsuHitObject)after).Position);
return actualDistance / expectedDistance;
}
protected override void Update()
{
base.Update();
if (!(BlueprintContainer.CurrentTool is SelectTool))
{
if (EditorClock.CurrentTime != lastDistanceSnapGridTime)
{
distanceSnapGridCache.Invalidate();
lastDistanceSnapGridTime = EditorClock.CurrentTime;
}
if (!distanceSnapGridCache.IsValid)
updateDistanceSnapGrid();
}
}
public override SnapResult FindSnappedPositionAndTime(Vector2 screenSpacePosition, SnapType snapType = SnapType.All)
{
if (snapType.HasFlagFast(SnapType.NearbyObjects) && snapToVisibleBlueprints(screenSpacePosition, out var snapResult))
{
// In the case of snapping to nearby objects, a time value is not provided.
// This matches the stable editor (which also uses current time), but with the introduction of time-snapping distance snap
2022-10-27 05:30:14 +08:00
// this could result in unexpected behaviour when distance snapping is turned on and a user attempts to place an object that is
// BOTH on a valid distance snap ring, and also at the same position as a previous object.
//
// We want to ensure that in this particular case, the time-snapping component of distance snap is still applied.
// The easiest way to ensure this is to attempt application of distance snap after a nearby object is found, and copy over
// the time value if the proposed positions are roughly the same.
if (snapType.HasFlagFast(SnapType.RelativeGrids) && DistanceSnapToggle.Value == TernaryState.True && distanceSnapGrid != null)
{
(Vector2 distanceSnappedPosition, double distanceSnappedTime) = distanceSnapGrid.GetSnappedPosition(distanceSnapGrid.ToLocalSpace(snapResult.ScreenSpacePosition));
if (Precision.AlmostEquals(distanceSnapGrid.ToScreenSpace(distanceSnappedPosition), snapResult.ScreenSpacePosition, 1))
snapResult.Time = distanceSnappedTime;
}
return snapResult;
}
SnapResult result = base.FindSnappedPositionAndTime(screenSpacePosition, snapType);
if (snapType.HasFlagFast(SnapType.RelativeGrids))
{
if (DistanceSnapToggle.Value == TernaryState.True && distanceSnapGrid != null)
{
(Vector2 pos, double time) = distanceSnapGrid.GetSnappedPosition(distanceSnapGrid.ToLocalSpace(screenSpacePosition));
result.ScreenSpacePosition = distanceSnapGrid.ToScreenSpace(pos);
result.Time = time;
}
}
if (snapType.HasFlagFast(SnapType.GlobalGrids))
{
if (rectangularGridSnapToggle.Value == TernaryState.True)
{
Vector2 pos = rectangularPositionSnapGrid.GetSnappedPosition(rectangularPositionSnapGrid.ToLocalSpace(result.ScreenSpacePosition));
result.ScreenSpacePosition = rectangularPositionSnapGrid.ToScreenSpace(pos);
}
}
return result;
}
private bool snapToVisibleBlueprints(Vector2 screenSpacePosition, out SnapResult snapResult)
{
// check other on-screen objects for snapping/stacking
var blueprints = BlueprintContainer.SelectionBlueprints.AliveChildren;
var playfield = PlayfieldAtScreenSpacePosition(screenSpacePosition);
float snapRadius =
playfield.GamefieldToScreenSpace(new Vector2(OsuHitObject.OBJECT_RADIUS * 0.10f)).X -
playfield.GamefieldToScreenSpace(Vector2.Zero).X;
foreach (var b in blueprints)
{
if (b.IsSelected)
continue;
2023-02-02 21:22:30 +08:00
var snapPositions = b.ScreenSpaceSnapPoints;
2023-02-03 23:05:16 +08:00
if (!snapPositions.Any())
continue;
2023-02-03 23:13:37 +08:00
2023-02-03 23:05:16 +08:00
var closestSnapPosition = snapPositions.MinBy(p => Vector2.Distance(p, screenSpacePosition));
2023-02-03 23:05:16 +08:00
if (Vector2.Distance(closestSnapPosition, screenSpacePosition) < snapRadius)
2020-09-24 13:34:41 +08:00
{
// only return distance portion, since time is not really valid
2023-02-04 21:36:30 +08:00
snapResult = new SnapResult(closestSnapPosition, null, playfield);
2020-09-24 13:34:41 +08:00
return true;
}
}
snapResult = null;
return false;
}
private void updateDistanceSnapGrid()
{
distanceSnapGridContainer.Clear();
distanceSnapGridCache.Invalidate();
2020-09-09 18:14:28 +08:00
distanceSnapGrid = null;
if (DistanceSnapToggle.Value != TernaryState.True)
2020-09-09 18:14:28 +08:00
return;
switch (BlueprintContainer.CurrentTool)
{
2022-06-24 20:25:23 +08:00
case SelectTool:
if (!EditorBeatmap.SelectedHitObjects.Any())
return;
distanceSnapGrid = createDistanceSnapGrid(EditorBeatmap.SelectedHitObjects);
break;
default:
if (!CursorInPlacementArea)
return;
distanceSnapGrid = createDistanceSnapGrid(Enumerable.Empty<HitObject>());
break;
}
if (distanceSnapGrid != null)
{
distanceSnapGridContainer.Add(distanceSnapGrid);
distanceSnapGridCache.Validate();
}
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Repeat)
return false;
handleToggleViaKey(e);
return base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyUpEvent e)
{
handleToggleViaKey(e);
base.OnKeyUp(e);
}
protected override bool AdjustDistanceSpacing(GlobalAction action, float amount)
{
// To allow better visualisation, ensure that the spacing grid is visible before adjusting.
DistanceSnapToggle.Value = TernaryState.True;
return base.AdjustDistanceSpacing(action, amount);
}
private bool gridSnapMomentary;
private void handleToggleViaKey(KeyboardEvent key)
{
bool shiftPressed = key.ShiftPressed;
if (shiftPressed != gridSnapMomentary)
{
gridSnapMomentary = shiftPressed;
rectangularGridSnapToggle.Value = rectangularGridSnapToggle.Value == TernaryState.False ? TernaryState.True : TernaryState.False;
}
}
private DistanceSnapGrid createDistanceSnapGrid(IEnumerable<HitObject> selectedHitObjects)
2019-10-16 19:20:07 +08:00
{
if (BlueprintContainer.CurrentTool is SpinnerCompositionTool)
return null;
2019-10-16 19:20:07 +08:00
var objects = selectedHitObjects.ToList();
if (objects.Count == 0)
// use accurate time value to give more instantaneous feedback to the user.
return createGrid(h => h.StartTime <= EditorClock.CurrentTimeAccurate);
double minTime = objects.Min(h => h.StartTime);
2019-11-06 15:04:20 +08:00
return createGrid(h => h.StartTime < minTime, objects.Count + 1);
}
2019-11-06 15:04:20 +08:00
/// <summary>
/// Creates a grid from the last <see cref="HitObject"/> matching a predicate to a target <see cref="HitObject"/>.
/// </summary>
/// <param name="sourceSelector">A predicate that matches <see cref="HitObject"/>s where the grid can start from.
/// Only the last <see cref="HitObject"/> matching the predicate is used.</param>
/// <param name="targetOffset">An offset from the <see cref="HitObject"/> selected via <paramref name="sourceSelector"/> at which the grid should stop.</param>
/// <returns>The <see cref="OsuDistanceSnapGrid"/> from a selected <see cref="HitObject"/> to a target <see cref="HitObject"/>.</returns>
private OsuDistanceSnapGrid createGrid(Func<HitObject, bool> sourceSelector, int targetOffset = 1)
{
2019-11-06 15:04:20 +08:00
if (targetOffset < 1) throw new ArgumentOutOfRangeException(nameof(targetOffset));
int sourceIndex = -1;
for (int i = 0; i < EditorBeatmap.HitObjects.Count; i++)
2019-10-16 19:20:07 +08:00
{
2019-11-06 15:04:20 +08:00
if (!sourceSelector(EditorBeatmap.HitObjects[i]))
break;
2019-10-16 19:20:07 +08:00
2019-11-06 15:04:20 +08:00
sourceIndex = i;
2019-10-16 19:20:07 +08:00
}
2019-11-06 15:04:20 +08:00
if (sourceIndex == -1)
return null;
2019-10-16 19:20:07 +08:00
2019-12-27 18:39:30 +08:00
HitObject sourceObject = EditorBeatmap.HitObjects[sourceIndex];
int targetIndex = sourceIndex + targetOffset;
2019-12-27 18:39:30 +08:00
HitObject targetObject = null;
// Keep advancing the target object while its start time falls before the end time of the source object
while (true)
{
if (targetIndex >= EditorBeatmap.HitObjects.Count)
break;
if (EditorBeatmap.HitObjects[targetIndex].StartTime >= sourceObject.GetEndTime())
{
targetObject = EditorBeatmap.HitObjects[targetIndex];
break;
}
targetIndex++;
}
2019-10-16 19:20:07 +08:00
if (sourceObject is Spinner)
return null;
2019-12-27 18:39:30 +08:00
return new OsuDistanceSnapGrid((OsuHitObject)sourceObject, (OsuHitObject)targetObject);
2019-10-16 19:20:07 +08:00
}
2017-11-29 15:22:11 +08:00
}
}