1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 23:13:21 +08:00
osu-lazer/osu.Game/Screens/Edit/Timing/TimingSection.cs

123 lines
4.1 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Beatmaps.ControlPoints;
2020-09-07 16:39:13 +08:00
using osu.Game.Graphics.UserInterfaceV2;
namespace osu.Game.Screens.Edit.Timing
{
2022-11-24 13:32:20 +08:00
internal partial class TimingSection : Section<TimingControlPoint>
{
2023-01-14 07:23:21 +08:00
private LabelledTimeSignature timeSignature = null!;
private LabelledSwitchButton omitBarLine = null!;
2023-01-14 07:23:21 +08:00
private BPMTextBox bpmTextEntry = null!;
[BackgroundDependencyLoader]
private void load()
{
Flow.AddRange(new Drawable[]
{
2022-05-20 13:34:33 +08:00
new TapTimingControl(),
bpmTextEntry = new BPMTextBox(),
timeSignature = new LabelledTimeSignature
{
Label = "Time Signature"
},
omitBarLine = new LabelledSwitchButton { Label = "Skip Bar Line" },
});
}
protected override void LoadComplete()
{
base.LoadComplete();
bpmTextEntry.Current.BindValueChanged(_ => saveChanges());
omitBarLine.Current.BindValueChanged(_ => saveChanges());
timeSignature.Current.BindValueChanged(_ => saveChanges());
void saveChanges()
{
if (!isRebinding) ChangeHandler?.SaveState();
}
}
private bool isRebinding;
2023-01-14 07:23:21 +08:00
protected override void OnControlPointChanged(ValueChangedEvent<TimingControlPoint?> point)
{
if (point.NewValue != null)
{
isRebinding = true;
2020-10-02 16:55:47 +08:00
bpmTextEntry.Bindable = point.NewValue.BeatLengthBindable;
timeSignature.Current = point.NewValue.TimeSignatureBindable;
omitBarLine.Current = point.NewValue.OmitFirstBarLineBindable;
isRebinding = false;
}
}
protected override TimingControlPoint CreatePoint()
{
var reference = Beatmap.ControlPointInfo.TimingPointAt(SelectedGroup.Value.Time);
return new TimingControlPoint
{
BeatLength = reference.BeatLength,
TimeSignature = reference.TimeSignature,
OmitFirstBarLine = reference.OmitFirstBarLine,
};
}
2022-11-24 13:32:20 +08:00
private partial class BPMTextBox : LabelledTextBox
2020-09-07 16:39:13 +08:00
{
private readonly BindableNumber<double> beatLengthBindable = new TimingControlPoint().BeatLengthBindable;
2020-09-07 16:39:13 +08:00
public BPMTextBox()
{
Label = "BPM";
2022-06-24 20:25:23 +08:00
OnCommit += (_, isNew) =>
2020-09-07 16:39:13 +08:00
{
if (!isNew) return;
try
2020-09-07 16:39:13 +08:00
{
if (double.TryParse(Current.Value, out double doubleVal) && doubleVal > 0)
2020-09-07 16:39:13 +08:00
beatLengthBindable.Value = beatLengthToBpm(doubleVal);
}
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 16:39:13 +08:00
};
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 static double beatLengthToBpm(double beatLength) => 60000 / beatLength;
}
}