1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 20:07:25 +08:00

Encapsulate distance spacing control handling to a "distance toolbox composite"

Encapsulated in a way which can allow further extensibility for the
right-side area of toolboxes.
This commit is contained in:
Salman Ahmed 2022-04-24 07:10:30 +03:00
parent 70a93c0e7e
commit 8ed39009fd
9 changed files with 137 additions and 111 deletions

View File

@ -24,6 +24,9 @@ using osuTK;
namespace osu.Game.Rulesets.Catch.Edit
{
/// <remarks>
/// todo: should inherit <see cref="IDistanceSnapProvider"/> once it supports distance spacing properly.
/// </remarks>
public class CatchHitObjectComposer : HitObjectComposer<CatchHitObject>
{
private const float distance_snap_radius = 50;
@ -32,8 +35,6 @@ namespace osu.Game.Rulesets.Catch.Edit
private readonly Bindable<TernaryState> distanceSnapToggle = new Bindable<TernaryState>();
protected override bool SupportsDistanceSpacing => false;
private InputManager inputManager;
public CatchHitObjectComposer(CatchRuleset ruleset)

View File

@ -26,8 +26,6 @@ namespace osu.Game.Rulesets.Mania.Edit
private ManiaBeatSnapGrid beatSnapGrid;
private InputManager inputManager;
protected override bool SupportsDistanceSpacing => false;
public ManiaHitObjectComposer(Ruleset ruleset)
: base(ruleset)
{

View File

@ -7,7 +7,7 @@ using osu.Framework.Bindables;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Osu.Edit;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Tests.Beatmaps;
@ -30,7 +30,7 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor
AddStep("seek to first control point", () => EditorClock.Seek(Beatmap.Value.Beatmap.ControlPointInfo.TimingPoints.First().Time));
AddStep("set distance spacing to 1", () =>
{
var distanceSpacing = (BindableDouble)Editor.ChildrenOfType<HitObjectComposer>().Single().DistanceSpacingMultiplier;
var distanceSpacing = (BindableDouble)Editor.ChildrenOfType<OsuHitObjectComposer>().Single().DistanceSpacingMultiplier;
distanceSpacing.Value = 1;
});
}

View File

@ -11,7 +11,6 @@ using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Osu.Edit;
using osu.Game.Rulesets.Osu.Edit.Blueprints.Sliders.Components;
@ -69,7 +68,7 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor
});
AddStep("set distance spacing to 1", () =>
{
var distanceSpacing = (BindableDouble)Editor.ChildrenOfType<HitObjectComposer>().Single().DistanceSpacingMultiplier;
var distanceSpacing = (BindableDouble)Editor.ChildrenOfType<OsuHitObjectComposer>().Single().DistanceSpacingMultiplier;
distanceSpacing.Value = 1;
});
}

View File

@ -24,9 +24,12 @@ using osuTK;
namespace osu.Game.Rulesets.Osu.Edit
{
public class OsuHitObjectComposer : HitObjectComposer<OsuHitObject>
[Cached(typeof(IDistanceSnapProvider))]
public class OsuHitObjectComposer : HitObjectComposer<OsuHitObject>, IDistanceSnapProvider
{
protected override bool SupportsDistanceSpacing => true;
private OsuToolboxComposite osuToolboxComposite;
public IBindable<double> DistanceSpacingMultiplier => osuToolboxComposite.DistanceSpacing;
public OsuHitObjectComposer(Ruleset ruleset)
: base(ruleset)
@ -76,6 +79,8 @@ namespace osu.Game.Rulesets.Osu.Edit
}
});
AddInternal(osuToolboxComposite = new OsuToolboxComposite());
selectedHitObjects = EditorBeatmap.SelectedHitObjects.GetBoundCopy();
selectedHitObjects.CollectionChanged += (_, __) => updateDistanceSnapGrid();
@ -130,6 +135,9 @@ namespace osu.Game.Rulesets.Osu.Edit
}
}
public override float GetBeatSnapDistanceAt(HitObject referenceObject)
=> (float)(base.GetBeatSnapDistanceAt(referenceObject) * DistanceSpacingMultiplier.Value);
public override SnapResult SnapScreenSpacePositionToValidPosition(Vector2 screenSpacePosition)
{
if (snapToVisibleBlueprints(screenSpacePosition, out var snapResult))

View File

@ -0,0 +1,119 @@
// 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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Settings.Sections;
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit;
using osuTK;
using osuTK.Input;
namespace osu.Game.Rulesets.Osu.Edit
{
/// <summary>
/// A toolbox composite for osu!-specific controls.
/// </summary>
// todo: once catch supports distance spacing, the control here should move out to a base "DistancingRulesetToolboxComposite" class or something better.
public class OsuToolboxComposite : CompositeDrawable
{
private ExpandingToolboxContainer expandingContainer;
private ExpandableSlider<double, SizeSlider<double>> distanceSpacingSlider;
private readonly Bindable<double> distanceSpacing = new BindableDouble(1.0)
{
MinValue = 0.1,
MaxValue = 6.0,
Precision = 0.01,
};
public IBindable<double> DistanceSpacing => distanceSpacing;
private bool distanceSpacingScrollActive;
[Resolved]
private EditorBeatmap editorBeatmap { get; set; }
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.Both;
InternalChild = expandingContainer = new ExpandingToolboxContainer
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Child = new EditorToolboxGroup("snapping")
{
Child = distanceSpacingSlider = new ExpandableSlider<double, SizeSlider<double>>
{
Current = { BindTarget = distanceSpacing },
KeyboardStep = 0.1f,
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
distanceSpacing.Value = editorBeatmap.BeatmapInfo.DistanceSpacing;
distanceSpacing.BindValueChanged(v =>
{
distanceSpacingSlider.ContractedLabelText = $"D. S. ({v.NewValue:0.##x})";
distanceSpacingSlider.ExpandedLabelText = $"Distance Spacing ({v.NewValue:0.##x})";
editorBeatmap.BeatmapInfo.DistanceSpacing = v.NewValue;
}, true);
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.AltPressed && e.Key == Key.D && !e.Repeat)
{
expandingContainer.Expanded.Value = true;
distanceSpacingScrollActive = true;
return true;
}
return base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyUpEvent e)
{
if (distanceSpacingScrollActive && (!e.AltPressed || e.Key == Key.D))
{
expandingContainer.Expanded.Value = false;
distanceSpacingScrollActive = false;
}
}
protected override bool OnScroll(ScrollEvent e)
{
if (distanceSpacingScrollActive)
{
distanceSpacing.Value += e.ScrollDelta.Y * (e.IsPrecise ? 0.01f : 0.1f);
return true;
}
return base.OnScroll(e);
}
private class ExpandingToolboxContainer : ExpandingContainer
{
public ExpandingToolboxContainer()
: base(130, 250)
{
RelativeSizeAxes = Axes.Y;
Padding = new MarginPadding { Left = 10 };
FillFlow.Spacing = new Vector2(10);
}
}
}
}

View File

@ -11,8 +11,6 @@ namespace osu.Game.Rulesets.Taiko.Edit
{
public class TaikoHitObjectComposer : HitObjectComposer<TaikoHitObject>
{
protected override bool SupportsDistanceSpacing => false;
public TaikoHitObjectComposer(TaikoRuleset ruleset)
: base(ruleset)
{

View File

@ -11,7 +11,7 @@ namespace osu.Game.Overlays.Settings.Sections
/// <summary>
/// A slider intended to show a "size" multiplier number, where 1x is 1.0.
/// </summary>
internal class SizeSlider<T> : OsuSliderBar<T>
public class SizeSlider<T> : OsuSliderBar<T>
where T : struct, IEquatable<T>, IComparable<T>, IConvertible, IFormattable
{
public override LocalisableString TooltipText => Current.Value.ToString(@"0.##x", NumberFormatInfo.CurrentInfo);

View File

@ -14,8 +14,6 @@ using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Settings.Sections;
using osu.Game.Rulesets.Configuration;
using osu.Game.Rulesets.Edit.Tools;
using osu.Game.Rulesets.Mods;
@ -58,26 +56,6 @@ namespace osu.Game.Rulesets.Edit
[Resolved]
protected IBeatSnapProvider BeatSnapProvider { get; private set; }
/// <summary>
/// Whether this composer supports a "distance spacing" multiplier for distance snap grids.
/// </summary>
/// <remarks>
/// Setting this to <see langword="true"/> displays a "distance spacing" slider and allows this composer to configure the value of <see cref="BeatmapInfo.DistanceSpacing"/>.
/// </remarks>
protected abstract bool SupportsDistanceSpacing { get; }
private readonly BindableDouble distanceSpacing = new BindableDouble
{
Default = 1.0,
MinValue = 0.1,
MaxValue = 6.0,
Precision = 0.01,
};
public override IBindable<double> DistanceSpacingMultiplier => distanceSpacing;
private SnappingToolboxContainer snappingToolboxContainer;
protected ComposeBlueprintContainer BlueprintContainer { get; private set; }
private DrawableEditorRulesetWrapper<TObject> drawableRulesetWrapper;
@ -161,36 +139,6 @@ namespace osu.Game.Rulesets.Edit
},
};
distanceSpacing.Value = EditorBeatmap.BeatmapInfo.DistanceSpacing;
if (SupportsDistanceSpacing)
{
ExpandableSlider<double, SizeSlider<double>> distanceSpacingSlider;
AddInternal(snappingToolboxContainer = new SnappingToolboxContainer
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Child = new EditorToolboxGroup("snapping")
{
Child = distanceSpacingSlider = new ExpandableSlider<double, SizeSlider<double>>
{
Current = { BindTarget = distanceSpacing },
KeyboardStep = 0.01f,
}
}
});
distanceSpacing.BindValueChanged(v =>
{
distanceSpacingSlider.ContractedLabelText = $"D. S. ({v.NewValue:0.##x})";
distanceSpacingSlider.ExpandedLabelText = $"Distance Spacing ({v.NewValue:0.##x})";
EditorBeatmap.BeatmapInfo.DistanceSpacing = v.NewValue;
}, true);
}
else
distanceSpacing.Disabled = true;
toolboxCollection.Items = CompositionTools
.Prepend(new SelectTool())
.Select(t => new RadioButton(t.Name, () => toolSelected(t), t.CreateIcon))
@ -265,17 +213,8 @@ namespace osu.Game.Rulesets.Edit
#region Tool selection logic
private bool distanceSpacingScrollActive;
protected override bool OnKeyDown(KeyDownEvent e)
{
if (SupportsDistanceSpacing && e.AltPressed && e.Key == Key.D && !e.Repeat)
{
snappingToolboxContainer.Expanded.Value = true;
distanceSpacingScrollActive = true;
return true;
}
if (e.ControlPressed || e.AltPressed || e.SuperPressed)
return false;
@ -305,28 +244,6 @@ namespace osu.Game.Rulesets.Edit
return base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyUpEvent e)
{
if (distanceSpacingScrollActive && (e.Key == Key.AltLeft || e.Key == Key.AltRight || e.Key == Key.D))
{
snappingToolboxContainer.Expanded.Value = false;
distanceSpacingScrollActive = false;
}
base.OnKeyUp(e);
}
protected override bool OnScroll(ScrollEvent e)
{
if (distanceSpacingScrollActive)
{
distanceSpacing.Value += e.ScrollDelta.Y * (e.IsPrecise ? 0.01f : 0.1f);
return true;
}
return base.OnScroll(e);
}
private bool checkLeftToggleFromKey(Key key, out int index)
{
if (key < Key.Number1 || key > Key.Number9)
@ -468,7 +385,7 @@ namespace osu.Game.Rulesets.Edit
public override float GetBeatSnapDistanceAt(HitObject referenceObject)
{
return (float)(100 * referenceObject.DifficultyControlPoint.SliderVelocity * EditorBeatmap.Difficulty.SliderMultiplier * distanceSpacing.Value / BeatSnapProvider.BeatDivisor);
return (float)(100 * referenceObject.DifficultyControlPoint.SliderVelocity * EditorBeatmap.Difficulty.SliderMultiplier / BeatSnapProvider.BeatDivisor);
}
public override float DurationToDistance(HitObject referenceObject, double duration)
@ -517,18 +434,6 @@ namespace osu.Game.Rulesets.Edit
FillFlow.Spacing = new Vector2(10);
}
}
private class SnappingToolboxContainer : ExpandingContainer
{
public SnappingToolboxContainer()
: base(130, 250)
{
RelativeSizeAxes = Axes.Y;
Padding = new MarginPadding { Left = 10 };
FillFlow.Spacing = new Vector2(10);
}
}
}
/// <summary>
@ -563,8 +468,6 @@ namespace osu.Game.Rulesets.Edit
#region IPositionSnapProvider
public abstract IBindable<double> DistanceSpacingMultiplier { get; }
public abstract SnapResult SnapScreenSpacePositionToValidTime(Vector2 screenSpacePosition);
public virtual SnapResult SnapScreenSpacePositionToValidPosition(Vector2 screenSpacePosition) =>