2022-04-28 10:49:37 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2022-10-26 12:27:23 +08:00
|
|
|
using System.Collections.Generic;
|
2022-10-21 21:58:36 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2022-04-28 10:49:37 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2022-05-04 16:41:33 +08:00
|
|
|
using osu.Framework.Extensions;
|
2022-05-03 15:30:32 +08:00
|
|
|
using osu.Framework.Extensions.LocalisationExtensions;
|
2022-04-28 10:49:37 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-10-13 15:33:01 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2022-10-26 12:27:23 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
2022-05-03 09:44:55 +08:00
|
|
|
using osu.Framework.Input.Bindings;
|
2022-04-28 10:49:37 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2022-05-03 15:30:32 +08:00
|
|
|
using osu.Framework.Localisation;
|
2022-10-21 21:58:36 +08:00
|
|
|
using osu.Framework.Utils;
|
2022-05-04 16:41:33 +08:00
|
|
|
using osu.Game.Configuration;
|
2022-04-28 10:49:37 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2022-05-03 09:44:55 +08:00
|
|
|
using osu.Game.Input.Bindings;
|
2022-05-03 15:30:32 +08:00
|
|
|
using osu.Game.Overlays;
|
|
|
|
using osu.Game.Overlays.OSD;
|
2022-04-28 10:49:37 +08:00
|
|
|
using osu.Game.Overlays.Settings.Sections;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2022-10-26 12:27:23 +08:00
|
|
|
using osu.Game.Screens.Edit.Components.TernaryButtons;
|
2022-04-28 10:49:37 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Edit
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a <see cref="HitObjectComposer{TObject}"/> for rulesets with the concept of distances between objects.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TObject">The base type of supported objects.</typeparam>
|
2022-05-04 14:00:54 +08:00
|
|
|
public abstract partial class DistancedHitObjectComposer<TObject> : HitObjectComposer<TObject>, IDistanceSnapProvider, IScrollBindingHandler<GlobalAction>
|
2022-04-28 10:49:37 +08:00
|
|
|
where TObject : HitObject
|
|
|
|
{
|
2022-05-24 02:22:27 +08:00
|
|
|
private const float adjust_step = 0.1f;
|
|
|
|
|
2022-10-21 21:58:36 +08:00
|
|
|
public BindableDouble DistanceSpacingMultiplier { get; } = new BindableDouble(1.0)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-04-29 13:02:07 +08:00
|
|
|
MinValue = 0.1,
|
|
|
|
MaxValue = 6.0,
|
|
|
|
Precision = 0.01,
|
2022-04-28 10:49:37 +08:00
|
|
|
};
|
|
|
|
|
2022-04-29 13:02:07 +08:00
|
|
|
IBindable<double> IDistanceSnapProvider.DistanceSpacingMultiplier => DistanceSpacingMultiplier;
|
2022-04-28 10:49:37 +08:00
|
|
|
|
|
|
|
protected ExpandingToolboxContainer RightSideToolboxContainer { get; private set; }
|
|
|
|
|
2022-04-29 13:02:07 +08:00
|
|
|
private ExpandableSlider<double, SizeSlider<double>> distanceSpacingSlider;
|
2022-10-21 22:21:07 +08:00
|
|
|
private ExpandableButton currentDistanceSpacingButton;
|
2022-04-28 10:49:37 +08:00
|
|
|
|
2022-05-03 16:15:28 +08:00
|
|
|
[Resolved(canBeNull: true)]
|
2022-05-03 15:30:32 +08:00
|
|
|
private OnScreenDisplay onScreenDisplay { get; set; }
|
|
|
|
|
2022-10-26 12:27:23 +08:00
|
|
|
protected readonly Bindable<TernaryState> DistanceSnapToggle = new Bindable<TernaryState>();
|
|
|
|
|
|
|
|
private bool distanceSnapMomentary;
|
|
|
|
|
2022-04-28 10:49:37 +08:00
|
|
|
protected DistancedHitObjectComposer(Ruleset ruleset)
|
|
|
|
: base(ruleset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-10-13 15:33:01 +08:00
|
|
|
private void load(OverlayColourProvider colourProvider)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-10-13 15:33:01 +08:00
|
|
|
AddInternal(new Container
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
2022-10-13 15:33:01 +08:00
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
AutoSizeAxes = Axes.X,
|
|
|
|
Children = new Drawable[]
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-10-13 15:33:01 +08:00
|
|
|
new Box
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-10-13 15:33:01 +08:00
|
|
|
Colour = colourProvider.Background5,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
RightSideToolboxContainer = new ExpandingToolboxContainer(130, 250)
|
|
|
|
{
|
|
|
|
Alpha = DistanceSpacingMultiplier.Disabled ? 0 : 1,
|
|
|
|
Child = new EditorToolboxGroup("snapping")
|
|
|
|
{
|
2022-10-21 21:58:36 +08:00
|
|
|
Children = new Drawable[]
|
2022-10-13 15:33:01 +08:00
|
|
|
{
|
2022-10-21 21:58:36 +08:00
|
|
|
distanceSpacingSlider = new ExpandableSlider<double, SizeSlider<double>>
|
|
|
|
{
|
|
|
|
KeyboardStep = adjust_step,
|
2022-10-26 12:37:01 +08:00
|
|
|
// Manual binding in LoadComplete to handle one-way event flow.
|
|
|
|
Current = DistanceSpacingMultiplier.GetUnboundCopy(),
|
2022-10-21 21:58:36 +08:00
|
|
|
},
|
2022-10-21 22:21:07 +08:00
|
|
|
currentDistanceSpacingButton = new ExpandableButton
|
2022-10-21 21:58:36 +08:00
|
|
|
{
|
|
|
|
Action = () =>
|
|
|
|
{
|
|
|
|
(HitObject before, HitObject after)? objects = getObjectsOnEitherSideOfCurrentTime();
|
|
|
|
|
|
|
|
Debug.Assert(objects != null);
|
|
|
|
|
|
|
|
DistanceSpacingMultiplier.Value = ReadCurrentDistanceSnap(objects.Value.before, objects.Value.after);
|
2022-10-26 12:37:01 +08:00
|
|
|
DistanceSnapToggle.Value = TernaryState.True;
|
2022-10-21 21:58:36 +08:00
|
|
|
},
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
}
|
2022-10-13 15:33:01 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:58:36 +08:00
|
|
|
private (HitObject before, HitObject after)? getObjectsOnEitherSideOfCurrentTime()
|
|
|
|
{
|
2022-10-21 22:35:47 +08:00
|
|
|
HitObject lastBefore = Playfield.HitObjectContainer.AliveObjects.LastOrDefault(h => h.HitObject.StartTime <= EditorClock.CurrentTime)?.HitObject;
|
2022-10-21 21:58:36 +08:00
|
|
|
|
|
|
|
if (lastBefore == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
HitObject firstAfter = Playfield.HitObjectContainer.AliveObjects.FirstOrDefault(h => h.HitObject.StartTime >= EditorClock.CurrentTime)?.HitObject;
|
|
|
|
|
|
|
|
if (firstAfter == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (lastBefore == firstAfter)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return (lastBefore, firstAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract double ReadCurrentDistanceSnap(HitObject before, HitObject after);
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
(HitObject before, HitObject after)? objects = getObjectsOnEitherSideOfCurrentTime();
|
|
|
|
|
2022-10-21 22:35:53 +08:00
|
|
|
double currentSnap = objects == null
|
|
|
|
? 0
|
|
|
|
: ReadCurrentDistanceSnap(objects.Value.before, objects.Value.after);
|
2022-10-21 21:58:36 +08:00
|
|
|
|
2022-10-21 22:35:53 +08:00
|
|
|
if (currentSnap > DistanceSpacingMultiplier.MinValue)
|
|
|
|
{
|
2022-10-21 22:21:07 +08:00
|
|
|
currentDistanceSpacingButton.Enabled.Value = currentDistanceSpacingButton.Expanded.Value
|
|
|
|
&& !Precision.AlmostEquals(currentSnap, DistanceSpacingMultiplier.Value, DistanceSpacingMultiplier.Precision / 2);
|
2022-10-21 22:35:53 +08:00
|
|
|
currentDistanceSpacingButton.ContractedLabelText = $"current {currentSnap:N2}x";
|
2022-10-21 22:21:07 +08:00
|
|
|
currentDistanceSpacingButton.ExpandedLabelText = $"Use current ({currentSnap:N2}x)";
|
2022-10-21 21:58:36 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-21 22:21:07 +08:00
|
|
|
currentDistanceSpacingButton.Enabled.Value = false;
|
2022-10-21 22:35:53 +08:00
|
|
|
currentDistanceSpacingButton.ContractedLabelText = string.Empty;
|
|
|
|
currentDistanceSpacingButton.ExpandedLabelText = "Use current (unavailable)";
|
2022-10-21 21:58:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:49:37 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-04-28 10:50:55 +08:00
|
|
|
if (!DistanceSpacingMultiplier.Disabled)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-04-28 10:50:55 +08:00
|
|
|
DistanceSpacingMultiplier.Value = EditorBeatmap.BeatmapInfo.DistanceSpacing;
|
2022-05-04 17:13:30 +08:00
|
|
|
DistanceSpacingMultiplier.BindValueChanged(multiplier =>
|
2022-04-28 10:50:55 +08:00
|
|
|
{
|
2022-05-04 17:13:30 +08:00
|
|
|
distanceSpacingSlider.ContractedLabelText = $"D. S. ({multiplier.NewValue:0.##x})";
|
|
|
|
distanceSpacingSlider.ExpandedLabelText = $"Distance Spacing ({multiplier.NewValue:0.##x})";
|
2022-05-03 15:30:32 +08:00
|
|
|
|
2022-05-04 17:13:30 +08:00
|
|
|
if (multiplier.NewValue != multiplier.OldValue)
|
|
|
|
onScreenDisplay?.Display(new DistanceSpacingToast(multiplier.NewValue.ToLocalisableString(@"0.##x"), multiplier));
|
2022-05-03 15:30:32 +08:00
|
|
|
|
2022-05-04 17:13:30 +08:00
|
|
|
EditorBeatmap.BeatmapInfo.DistanceSpacing = multiplier.NewValue;
|
2022-04-28 10:50:55 +08:00
|
|
|
}, true);
|
2022-10-26 12:37:01 +08:00
|
|
|
|
|
|
|
// Manual binding to handle enabling distance spacing when the slider is interacted with.
|
|
|
|
distanceSpacingSlider.Current.BindValueChanged(spacing =>
|
|
|
|
{
|
|
|
|
DistanceSpacingMultiplier.Value = spacing.NewValue;
|
|
|
|
DistanceSnapToggle.Value = TernaryState.True;
|
|
|
|
});
|
|
|
|
DistanceSpacingMultiplier.BindValueChanged(spacing => distanceSpacingSlider.Current.Value = spacing.NewValue);
|
2022-04-28 10:50:55 +08:00
|
|
|
}
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-26 12:27:23 +08:00
|
|
|
protected override IEnumerable<TernaryButton> CreateTernaryButtons() => base.CreateTernaryButtons().Concat(new[]
|
|
|
|
{
|
|
|
|
new TernaryButton(DistanceSnapToggle, "Distance Snap", () => new SpriteIcon { Icon = FontAwesome.Solid.Ruler })
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleToggleViaKey(KeyboardEvent key)
|
|
|
|
{
|
|
|
|
bool altPressed = key.AltPressed;
|
|
|
|
|
|
|
|
if (altPressed != distanceSnapMomentary)
|
|
|
|
{
|
|
|
|
distanceSnapMomentary = altPressed;
|
|
|
|
DistanceSnapToggle.Value = DistanceSnapToggle.Value == TernaryState.False ? TernaryState.True : TernaryState.False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:06:16 +08:00
|
|
|
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-05-04 14:00:54 +08:00
|
|
|
switch (e.Action)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-05-04 14:00:54 +08:00
|
|
|
case GlobalAction.EditorIncreaseDistanceSpacing:
|
|
|
|
case GlobalAction.EditorDecreaseDistanceSpacing:
|
2022-10-21 15:10:55 +08:00
|
|
|
return AdjustDistanceSpacing(e.Action, adjust_step);
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-05-03 09:44:55 +08:00
|
|
|
return false;
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:06:16 +08:00
|
|
|
public virtual void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 14:00:54 +08:00
|
|
|
public bool OnScroll(KeyBindingScrollEvent<GlobalAction> e)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-05-04 14:00:54 +08:00
|
|
|
switch (e.Action)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-05-04 14:00:54 +08:00
|
|
|
case GlobalAction.EditorIncreaseDistanceSpacing:
|
|
|
|
case GlobalAction.EditorDecreaseDistanceSpacing:
|
2022-10-21 15:10:55 +08:00
|
|
|
return AdjustDistanceSpacing(e.Action, e.ScrollAmount * adjust_step);
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 14:00:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:10:55 +08:00
|
|
|
protected virtual bool AdjustDistanceSpacing(GlobalAction action, float amount)
|
2022-05-04 14:00:54 +08:00
|
|
|
{
|
|
|
|
if (DistanceSpacingMultiplier.Disabled)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (action == GlobalAction.EditorIncreaseDistanceSpacing)
|
|
|
|
DistanceSpacingMultiplier.Value += amount;
|
|
|
|
else if (action == GlobalAction.EditorDecreaseDistanceSpacing)
|
|
|
|
DistanceSpacingMultiplier.Value -= amount;
|
|
|
|
|
2022-10-26 12:37:01 +08:00
|
|
|
DistanceSnapToggle.Value = TernaryState.True;
|
2022-05-04 14:00:54 +08:00
|
|
|
return true;
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 14:00:23 +08:00
|
|
|
public virtual float GetBeatSnapDistanceAt(HitObject referenceObject, bool useReferenceSliderVelocity = true)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
2022-11-01 14:00:23 +08:00
|
|
|
return (float)(100 * (useReferenceSliderVelocity ? referenceObject.DifficultyControlPoint.SliderVelocity : 1) * EditorBeatmap.Difficulty.SliderMultiplier * 1 / BeatSnapProvider.BeatDivisor);
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual float DurationToDistance(HitObject referenceObject, double duration)
|
|
|
|
{
|
|
|
|
double beatLength = BeatSnapProvider.GetBeatLengthAtTime(referenceObject.StartTime);
|
|
|
|
return (float)(duration / beatLength * GetBeatSnapDistanceAt(referenceObject));
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual double DistanceToDuration(HitObject referenceObject, float distance)
|
|
|
|
{
|
|
|
|
double beatLength = BeatSnapProvider.GetBeatLengthAtTime(referenceObject.StartTime);
|
2022-11-01 16:14:30 +08:00
|
|
|
return distance / GetBeatSnapDistanceAt(referenceObject) * beatLength;
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:25:05 +08:00
|
|
|
public virtual double FindSnappedDuration(HitObject referenceObject, float distance)
|
2022-04-28 10:49:37 +08:00
|
|
|
=> BeatSnapProvider.SnapTime(referenceObject.StartTime + DistanceToDuration(referenceObject, distance), referenceObject.StartTime) - referenceObject.StartTime;
|
|
|
|
|
2022-05-05 15:25:05 +08:00
|
|
|
public virtual float FindSnappedDistance(HitObject referenceObject, float distance)
|
2022-04-28 10:49:37 +08:00
|
|
|
{
|
|
|
|
double startTime = referenceObject.StartTime;
|
|
|
|
|
|
|
|
double actualDuration = startTime + DistanceToDuration(referenceObject, distance);
|
|
|
|
|
|
|
|
double snappedEndTime = BeatSnapProvider.SnapTime(actualDuration, startTime);
|
|
|
|
|
|
|
|
double beatLength = BeatSnapProvider.GetBeatLengthAtTime(startTime);
|
|
|
|
|
|
|
|
// we don't want to exceed the actual duration and snap to a point in the future.
|
|
|
|
// as we are snapping to beat length via SnapTime (which will round-to-nearest), check for snapping in the forward direction and reverse it.
|
|
|
|
if (snappedEndTime > actualDuration + 1)
|
|
|
|
snappedEndTime -= beatLength;
|
|
|
|
|
|
|
|
return DurationToDistance(referenceObject, snappedEndTime - startTime);
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:30:32 +08:00
|
|
|
private partial class DistanceSpacingToast : Toast
|
|
|
|
{
|
2022-05-04 16:41:33 +08:00
|
|
|
private readonly ValueChangedEvent<double> change;
|
|
|
|
|
|
|
|
public DistanceSpacingToast(LocalisableString value, ValueChangedEvent<double> change)
|
|
|
|
: base(getAction(change).GetLocalisableDescription(), value, string.Empty)
|
2022-05-03 15:30:32 +08:00
|
|
|
{
|
2022-05-04 16:41:33 +08:00
|
|
|
this.change = change;
|
2022-05-03 15:30:32 +08:00
|
|
|
}
|
2022-05-04 16:41:33 +08:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuConfigManager config)
|
|
|
|
{
|
2022-05-04 16:59:29 +08:00
|
|
|
ShortcutText.Text = config.LookupKeyBindings(getAction(change)).ToUpper();
|
2022-05-04 16:41:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static GlobalAction getAction(ValueChangedEvent<double> change) => change.NewValue - change.OldValue > 0
|
|
|
|
? GlobalAction.EditorIncreaseDistanceSpacing
|
|
|
|
: GlobalAction.EditorDecreaseDistanceSpacing;
|
2022-05-03 15:30:32 +08:00
|
|
|
}
|
2022-04-28 10:49:37 +08:00
|
|
|
}
|
|
|
|
}
|