1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 00:27:25 +08:00
osu-lazer/osu.Game/Screens/Edit/Timing/TimingSection.cs

146 lines
5.0 KiB
C#
Raw Normal View History

2019-10-28 11:42:17 +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.
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Beatmaps.Timing;
2020-09-07 16:39:13 +08:00
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays.Settings;
namespace osu.Game.Screens.Edit.Timing
{
internal class TimingSection : Section<TimingControlPoint>
{
private SettingsSlider<double> bpmSlider;
private SettingsEnumDropdown<TimeSignatures> timeSignature;
2020-09-07 16:39:13 +08:00
private BPMTextBox bpmTextEntry;
[BackgroundDependencyLoader]
private void load()
{
Flow.AddRange(new Drawable[]
{
bpmTextEntry = new BPMTextBox(),
bpmSlider = new BPMSlider(),
timeSignature = new SettingsEnumDropdown<TimeSignatures>
{
LabelText = "Time Signature"
},
});
}
protected override void OnControlPointChanged(ValueChangedEvent<TimingControlPoint> point)
{
if (point.NewValue != null)
{
bpmSlider.Bindable = point.NewValue.BeatLengthBindable;
2020-09-07 16:39:13 +08:00
bpmTextEntry.Bindable = point.NewValue.BeatLengthBindable;
timeSignature.Bindable = point.NewValue.TimeSignatureBindable;
}
}
protected override TimingControlPoint CreatePoint()
{
var reference = Beatmap.Value.Beatmap.ControlPointInfo.TimingPointAt(SelectedGroup.Value.Time);
return new TimingControlPoint
{
BeatLength = reference.BeatLength,
TimeSignature = reference.TimeSignature
};
}
2020-09-07 16:39:13 +08:00
private class BPMTextBox : LabelledTextBox
{
private readonly BindableNumber<double> beatLengthBindable = new TimingControlPoint().BeatLengthBindable;
2020-09-07 16:39:13 +08:00
public BPMTextBox()
{
Label = "BPM";
2020-09-07 16:39:13 +08:00
OnCommit += (val, isNew) =>
{
if (!isNew) return;
if (double.TryParse(Current.Value, out double doubleVal))
{
try
{
beatLengthBindable.Value = beatLengthToBpm(doubleVal);
}
catch
{
// will restore the previous text value on failure.
beatLengthBindable.TriggerChange();
}
}
};
beatLengthBindable.BindValueChanged(val =>
{
Current.Value = beatLengthToBpm(val.NewValue).ToString("N2");
}, true);
2020-09-07 16:39:13 +08:00
}
public Bindable<double> Bindable
{
get => beatLengthBindable;
set
{
// incoming will be beat length, not bpm
beatLengthBindable.UnbindBindings();
beatLengthBindable.BindTo(value);
}
}
}
private class BPMSlider : SettingsSlider<double>
{
private const double sane_minimum = 60;
2020-09-07 17:20:47 +08:00
private const double sane_maximum = 240;
private readonly BindableNumber<double> beatLengthBindable = new TimingControlPoint().BeatLengthBindable;
private readonly BindableDouble bpmBindable = new BindableDouble(60000 / TimingControlPoint.DEFAULT_BEAT_LENGTH)
{
MinValue = sane_minimum,
MaxValue = sane_maximum,
};
2020-09-07 16:39:13 +08:00
public BPMSlider()
{
beatLengthBindable.BindValueChanged(beatLength => updateCurrent(beatLengthToBpm(beatLength.NewValue)), true);
bpmBindable.BindValueChanged(bpm => beatLengthBindable.Value = beatLengthToBpm(bpm.NewValue));
2020-09-07 16:39:13 +08:00
base.Bindable = bpmBindable;
}
public override Bindable<double> Bindable
{
get => base.Bindable;
set
{
// incoming will be beat length, not bpm
beatLengthBindable.UnbindBindings();
beatLengthBindable.BindTo(value);
}
}
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;
}
}
2020-09-07 16:39:13 +08:00
private static double beatLengthToBpm(double beatLength) => 60000 / beatLength;
}
}