Update SamplePointPiece.cs

This commit is contained in:
OliBomby 2023-04-26 13:45:58 +02:00
parent 39d9f0c3f5
commit 87ca0f5335
1 changed files with 38 additions and 19 deletions

View File

@ -12,7 +12,7 @@
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Audio;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Rulesets.Objects;
@ -26,14 +26,12 @@ public partial class SamplePointPiece : HitObjectPointPiece, IHasPopover
{
public readonly HitObject HitObject;
private readonly Bindable<string> bank;
private readonly BindableNumber<int> volume;
private readonly BindableList<HitSampleInfo> samplesBindable;
public SamplePointPiece(HitObject hitObject)
{
HitObject = hitObject;
volume = hitObject.SampleControlPoint.SampleVolumeBindable.GetBoundCopy();
bank = hitObject.SampleControlPoint.SampleBankBindable.GetBoundCopy();
samplesBindable = hitObject.SamplesBindable.GetBoundCopy();
}
protected override Color4 GetRepresentingColour(OsuColour colours) => colours.Pink;
@ -41,8 +39,7 @@ public SamplePointPiece(HitObject hitObject)
[BackgroundDependencyLoader]
private void load()
{
volume.BindValueChanged(_ => updateText());
bank.BindValueChanged(_ => updateText(), true);
samplesBindable.BindCollectionChanged((_, _) => updateText(), true);
}
protected override bool OnClick(ClickEvent e)
@ -53,7 +50,17 @@ protected override bool OnClick(ClickEvent e)
private void updateText()
{
Label.Text = $"{bank.Value} {volume.Value}";
Label.Text = $"{GetBankValue(samplesBindable)} {GetVolumeValue(samplesBindable)}";
}
public static string? GetBankValue(IEnumerable<HitSampleInfo> samples)
{
return samples.FirstOrDefault()?.Bank;
}
public static int GetVolumeValue(ICollection<HitSampleInfo> samples)
{
return samples.Count == 0 ? 0 : samples.Max(o => o.Volume);
}
public Popover GetPopover() => new SampleEditPopover(HitObject);
@ -92,7 +99,11 @@ private void load()
{
Label = "Bank Name",
},
volume = new IndeterminateSliderWithTextBoxInput<int>("Volume", new SampleControlPoint().SampleVolumeBindable)
volume = new IndeterminateSliderWithTextBoxInput<int>("Volume", new BindableInt(100)
{
MinValue = 0,
MaxValue = 100,
})
}
}
};
@ -103,14 +114,14 @@ private void load()
// if the piece belongs to a currently selected object, assume that the user wants to change all selected objects.
// if the piece belongs to an unselected object, operate on that object alone, independently of the selection.
var relevantObjects = (beatmap.SelectedHitObjects.Contains(hitObject) ? beatmap.SelectedHitObjects : hitObject.Yield()).ToArray();
var relevantControlPoints = relevantObjects.Select(h => h.SampleControlPoint).ToArray();
var relevantSamples = relevantObjects.Select(h => h.Samples).ToArray();
// even if there are multiple objects selected, we can still display sample volume or bank if they all have the same value.
string? commonBank = getCommonBank(relevantControlPoints);
string? commonBank = getCommonBank(relevantSamples);
if (!string.IsNullOrEmpty(commonBank))
bank.Current.Value = commonBank;
int? commonVolume = getCommonVolume(relevantControlPoints);
int? commonVolume = getCommonVolume(relevantSamples);
if (commonVolume != null)
volume.Current.Value = commonVolume.Value;
@ -120,9 +131,9 @@ private void load()
updateBankFor(relevantObjects, val.NewValue);
updateBankPlaceholderText(relevantObjects);
});
// on commit, ensure that the value is correct by sourcing it from the objects' control points again.
// on commit, ensure that the value is correct by sourcing it from the objects' samples again.
// this ensures that committing empty text causes a revert to the previous value.
bank.OnCommit += (_, _) => bank.Current.Value = getCommonBank(relevantControlPoints);
bank.OnCommit += (_, _) => bank.Current.Value = getCommonBank(relevantSamples);
volume.Current.BindValueChanged(val => updateVolumeFor(relevantObjects, val.NewValue));
}
@ -133,8 +144,8 @@ protected override void LoadComplete()
ScheduleAfterChildren(() => GetContainingInputManager().ChangeFocus(volume));
}
private static string? getCommonBank(SampleControlPoint[] relevantControlPoints) => relevantControlPoints.Select(point => point.SampleBank).Distinct().Count() == 1 ? relevantControlPoints.First().SampleBank : null;
private static int? getCommonVolume(SampleControlPoint[] relevantControlPoints) => relevantControlPoints.Select(point => point.SampleVolume).Distinct().Count() == 1 ? relevantControlPoints.First().SampleVolume : null;
private static string? getCommonBank(IList<HitSampleInfo>[] relevantSamples) => relevantSamples.Select(GetBankValue).Distinct().Count() == 1 ? GetBankValue(relevantSamples.First()) : null;
private static int? getCommonVolume(IList<HitSampleInfo>[] relevantSamples) => relevantSamples.Select(GetVolumeValue).Distinct().Count() == 1 ? GetVolumeValue(relevantSamples.First()) : null;
private void updateBankFor(IEnumerable<HitObject> objects, string? newBank)
{
@ -145,7 +156,11 @@ private void updateBankFor(IEnumerable<HitObject> objects, string? newBank)
foreach (var h in objects)
{
h.SampleControlPoint.SampleBank = newBank;
for (int i = 0; i < h.Samples.Count; i++)
{
h.Samples[i] = h.Samples[i].With(newBank: newBank);
}
beatmap.Update(h);
}
@ -154,7 +169,7 @@ private void updateBankFor(IEnumerable<HitObject> objects, string? newBank)
private void updateBankPlaceholderText(IEnumerable<HitObject> objects)
{
string? commonBank = getCommonBank(objects.Select(h => h.SampleControlPoint).ToArray());
string? commonBank = getCommonBank(objects.Select(h => h.Samples).ToArray());
bank.PlaceholderText = string.IsNullOrEmpty(commonBank) ? "(multiple)" : string.Empty;
}
@ -167,7 +182,11 @@ private void updateVolumeFor(IEnumerable<HitObject> objects, int? newVolume)
foreach (var h in objects)
{
h.SampleControlPoint.SampleVolume = newVolume.Value;
for (int i = 0; i < h.Samples.Count; i++)
{
h.Samples[i] = h.Samples[i].With(newVolume: newVolume.Value);
}
beatmap.Update(h);
}