2019-10-28 03:42:17 +00: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.
|
|
|
|
|
2020-09-07 08:34:30 +00:00
|
|
|
using System;
|
2019-10-27 06:19:36 +00:00
|
|
|
using osu.Framework.Allocation;
|
2019-10-28 07:21:31 +00:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
2019-10-27 06:19:36 +00:00
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2019-10-28 07:21:31 +00:00
|
|
|
using osu.Game.Beatmaps.Timing;
|
2020-09-07 08:39:13 +00:00
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
2019-10-28 07:21:31 +00:00
|
|
|
using osu.Game.Overlays.Settings;
|
2019-10-27 06:19:36 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Timing
|
|
|
|
{
|
|
|
|
internal class TimingSection : Section<TimingControlPoint>
|
|
|
|
{
|
2020-09-07 08:34:30 +00:00
|
|
|
private SettingsSlider<double> bpmSlider;
|
2019-10-28 07:21:31 +00:00
|
|
|
private SettingsEnumDropdown<TimeSignatures> timeSignature;
|
2020-09-07 08:39:13 +00:00
|
|
|
private BPMTextBox bpmTextEntry;
|
2019-10-27 06:19:36 +00:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2019-10-28 07:21:31 +00:00
|
|
|
Flow.AddRange(new Drawable[]
|
2019-10-27 06:19:36 +00:00
|
|
|
{
|
2020-09-07 09:18:34 +00:00
|
|
|
bpmTextEntry = new BPMTextBox(),
|
|
|
|
bpmSlider = new BPMSlider(),
|
2019-10-28 07:21:31 +00:00
|
|
|
timeSignature = new SettingsEnumDropdown<TimeSignatures>
|
|
|
|
{
|
|
|
|
LabelText = "Time Signature"
|
|
|
|
},
|
2019-10-27 06:19:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-06 05:36:43 +00:00
|
|
|
protected override void OnControlPointChanged(ValueChangedEvent<TimingControlPoint> point)
|
2019-10-27 06:19:36 +00:00
|
|
|
{
|
2019-11-06 07:22:55 +00:00
|
|
|
if (point.NewValue != null)
|
2019-10-27 06:19:36 +00:00
|
|
|
{
|
2020-10-06 08:18:41 +00:00
|
|
|
bpmSlider.Current = point.NewValue.BeatLengthBindable;
|
|
|
|
bpmSlider.Current.BindValueChanged(_ => ChangeHandler?.SaveState());
|
2020-10-02 08:55:47 +00:00
|
|
|
|
2020-09-07 08:39:13 +00:00
|
|
|
bpmTextEntry.Bindable = point.NewValue.BeatLengthBindable;
|
2020-10-02 08:55:47 +00:00
|
|
|
// no need to hook change handler here as it's the same bindable as above
|
|
|
|
|
2020-10-06 08:18:41 +00:00
|
|
|
timeSignature.Current = point.NewValue.TimeSignatureBindable;
|
|
|
|
timeSignature.Current.BindValueChanged(_ => ChangeHandler?.SaveState());
|
2019-11-06 07:22:55 +00:00
|
|
|
}
|
2019-10-27 06:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override TimingControlPoint CreatePoint()
|
|
|
|
{
|
2021-01-04 07:38:15 +00:00
|
|
|
var reference = Beatmap.ControlPointInfo.TimingPointAt(SelectedGroup.Value.Time);
|
2019-10-27 06:19:36 +00:00
|
|
|
|
|
|
|
return new TimingControlPoint
|
|
|
|
{
|
|
|
|
BeatLength = reference.BeatLength,
|
|
|
|
TimeSignature = reference.TimeSignature
|
|
|
|
};
|
|
|
|
}
|
2019-10-28 07:21:31 +00:00
|
|
|
|
2020-09-07 08:39:13 +00:00
|
|
|
private class BPMTextBox : LabelledTextBox
|
|
|
|
{
|
2020-09-07 09:18:34 +00:00
|
|
|
private readonly BindableNumber<double> beatLengthBindable = new TimingControlPoint().BeatLengthBindable;
|
|
|
|
|
2020-09-07 08:39:13 +00:00
|
|
|
public BPMTextBox()
|
|
|
|
{
|
2020-09-07 09:18:34 +00:00
|
|
|
Label = "BPM";
|
|
|
|
|
2020-09-07 08:39:13 +00:00
|
|
|
OnCommit += (val, isNew) =>
|
|
|
|
{
|
|
|
|
if (!isNew) return;
|
|
|
|
|
2020-10-01 04:06:24 +00:00
|
|
|
try
|
2020-09-07 08:39:13 +00:00
|
|
|
{
|
2020-10-01 04:06:24 +00:00
|
|
|
if (double.TryParse(Current.Value, out double doubleVal) && doubleVal > 0)
|
2020-09-07 08:39:13 +00:00
|
|
|
beatLengthBindable.Value = beatLengthToBpm(doubleVal);
|
|
|
|
}
|
2020-10-01 04:06:24 +00:00
|
|
|
catch
|
|
|
|
{
|
|
|
|
// TriggerChange below will restore the previous text value on failure.
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is run regardless of parsing success as the parsed number may not actually trigger a change
|
|
|
|
// due to bindable clamping. Even in such a case we want to update the textbox to a sane visual state.
|
|
|
|
beatLengthBindable.TriggerChange();
|
2020-09-07 08:39:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beatLengthBindable.BindValueChanged(val =>
|
|
|
|
{
|
2020-09-07 09:18:34 +00:00
|
|
|
Current.Value = beatLengthToBpm(val.NewValue).ToString("N2");
|
|
|
|
}, true);
|
2020-09-07 08:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Bindable<double> Bindable
|
|
|
|
{
|
|
|
|
get => beatLengthBindable;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
// incoming will be beat length, not bpm
|
|
|
|
beatLengthBindable.UnbindBindings();
|
|
|
|
beatLengthBindable.BindTo(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 07:21:31 +00:00
|
|
|
private class BPMSlider : SettingsSlider<double>
|
|
|
|
{
|
2020-09-07 08:34:30 +00:00
|
|
|
private const double sane_minimum = 60;
|
2020-09-07 09:20:47 +00:00
|
|
|
private const double sane_maximum = 240;
|
2020-09-07 08:34:30 +00:00
|
|
|
|
2020-09-07 09:18:34 +00:00
|
|
|
private readonly BindableNumber<double> beatLengthBindable = new TimingControlPoint().BeatLengthBindable;
|
2020-09-30 09:08:55 +00:00
|
|
|
|
|
|
|
private readonly BindableDouble bpmBindable = new BindableDouble(60000 / TimingControlPoint.DEFAULT_BEAT_LENGTH)
|
|
|
|
{
|
|
|
|
MinValue = sane_minimum,
|
|
|
|
MaxValue = sane_maximum,
|
|
|
|
};
|
2019-10-28 07:21:31 +00:00
|
|
|
|
2020-09-07 08:39:13 +00:00
|
|
|
public BPMSlider()
|
|
|
|
{
|
2020-09-07 09:18:34 +00:00
|
|
|
beatLengthBindable.BindValueChanged(beatLength => updateCurrent(beatLengthToBpm(beatLength.NewValue)), true);
|
2020-09-30 09:08:55 +00:00
|
|
|
bpmBindable.BindValueChanged(bpm => beatLengthBindable.Value = beatLengthToBpm(bpm.NewValue));
|
2020-09-07 08:39:13 +00:00
|
|
|
|
2020-10-06 08:18:41 +00:00
|
|
|
base.Current = bpmBindable;
|
2020-10-02 08:55:47 +00:00
|
|
|
|
|
|
|
TransferValueOnCommit = true;
|
2020-09-07 08:39:13 +00:00
|
|
|
}
|
2019-10-28 07:21:31 +00:00
|
|
|
|
2020-10-06 08:18:41 +00:00
|
|
|
public override Bindable<double> Current
|
2019-10-28 07:21:31 +00:00
|
|
|
{
|
2020-10-06 08:18:41 +00:00
|
|
|
get => base.Current;
|
2019-10-28 07:21:31 +00:00
|
|
|
set
|
|
|
|
{
|
2020-09-07 08:34:30 +00:00
|
|
|
// incoming will be beat length, not bpm
|
2019-10-28 07:21:31 +00:00
|
|
|
beatLengthBindable.UnbindBindings();
|
|
|
|
beatLengthBindable.BindTo(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 08:34:30 +00:00
|
|
|
private void updateCurrent(double newValue)
|
|
|
|
{
|
|
|
|
// we use a more sane range for the slider display unless overridden by the user.
|
|
|
|
// if a value comes in outside our range, we should expand temporarily.
|
|
|
|
bpmBindable.MinValue = Math.Min(newValue, sane_minimum);
|
|
|
|
bpmBindable.MaxValue = Math.Max(newValue, sane_maximum);
|
|
|
|
|
|
|
|
bpmBindable.Value = newValue;
|
|
|
|
}
|
2019-10-28 07:21:31 +00:00
|
|
|
}
|
2020-09-07 08:39:13 +00:00
|
|
|
|
|
|
|
private static double beatLengthToBpm(double beatLength) => 60000 / beatLength;
|
2019-10-27 06:19:36 +00:00
|
|
|
}
|
|
|
|
}
|