1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 07:53:38 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/Timeline/SamplePointPiece.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

199 lines
7.4 KiB
C#
Raw Normal View History

// 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.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
2023-04-26 19:45:58 +08:00
using osu.Game.Audio;
2023-04-26 00:12:53 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Screens.Edit.Timing;
using osuTK;
2023-04-26 00:12:53 +08:00
using osuTK.Graphics;
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
{
public partial class SamplePointPiece : HitObjectPointPiece, IHasPopover
{
public readonly HitObject HitObject;
2023-04-26 19:45:58 +08:00
private readonly BindableList<HitSampleInfo> samplesBindable;
public SamplePointPiece(HitObject hitObject)
{
HitObject = hitObject;
2023-04-26 19:45:58 +08:00
samplesBindable = hitObject.SamplesBindable.GetBoundCopy();
}
2023-04-26 00:12:53 +08:00
protected override Color4 GetRepresentingColour(OsuColour colours) => colours.Pink;
[BackgroundDependencyLoader]
2022-01-15 08:06:39 +08:00
private void load()
{
2023-04-26 19:45:58 +08:00
samplesBindable.BindCollectionChanged((_, _) => updateText(), true);
}
protected override bool OnClick(ClickEvent e)
{
this.ShowPopover();
return true;
}
private void updateText()
{
2023-04-26 19:45:58 +08:00
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);
public partial class SampleEditPopover : OsuPopover
{
private readonly HitObject hitObject;
private LabelledTextBox bank = null!;
private IndeterminateSliderWithTextBoxInput<int> volume = null!;
[Resolved(canBeNull: true)]
private EditorBeatmap beatmap { get; set; } = null!;
public SampleEditPopover(HitObject hitObject)
{
this.hitObject = hitObject;
}
[BackgroundDependencyLoader]
private void load()
{
FillFlowContainer flow;
Children = new Drawable[]
{
flow = new FillFlowContainer
{
Width = 200,
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
bank = new LabelledTextBox
{
Label = "Bank Name",
},
2023-04-26 19:45:58 +08:00
volume = new IndeterminateSliderWithTextBoxInput<int>("Volume", new BindableInt(100)
{
MinValue = DrawableHitObject.MINIMUM_SAMPLE_VOLUME,
2023-04-26 19:45:58 +08:00
MaxValue = 100,
})
}
}
};
bank.TabbableContentContainer = flow;
volume.TabbableContentContainer = flow;
// 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();
2023-04-26 19:45:58 +08:00
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.
2023-04-26 19:45:58 +08:00
string? commonBank = getCommonBank(relevantSamples);
if (!string.IsNullOrEmpty(commonBank))
bank.Current.Value = commonBank;
2023-04-26 19:45:58 +08:00
int? commonVolume = getCommonVolume(relevantSamples);
if (commonVolume != null)
volume.Current.Value = commonVolume.Value;
updateBankPlaceholderText(relevantObjects);
bank.Current.BindValueChanged(val =>
{
updateBankFor(relevantObjects, val.NewValue);
updateBankPlaceholderText(relevantObjects);
});
2023-04-26 19:45:58 +08:00
// 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.
2023-04-26 19:45:58 +08:00
bank.OnCommit += (_, _) => bank.Current.Value = getCommonBank(relevantSamples);
volume.Current.BindValueChanged(val => updateVolumeFor(relevantObjects, val.NewValue));
}
protected override void LoadComplete()
{
base.LoadComplete();
ScheduleAfterChildren(() => GetContainingFocusManager().ChangeFocus(volume));
}
2023-04-26 19:45:58 +08:00
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)
{
if (string.IsNullOrEmpty(newBank))
return;
beatmap.BeginChange();
foreach (var h in objects)
{
2023-04-26 19:45:58 +08:00
for (int i = 0; i < h.Samples.Count; i++)
{
h.Samples[i] = h.Samples[i].With(newBank: newBank);
}
beatmap.Update(h);
}
beatmap.EndChange();
}
private void updateBankPlaceholderText(IEnumerable<HitObject> objects)
{
2023-04-26 19:45:58 +08:00
string? commonBank = getCommonBank(objects.Select(h => h.Samples).ToArray());
bank.PlaceholderText = string.IsNullOrEmpty(commonBank) ? "(multiple)" : string.Empty;
}
private void updateVolumeFor(IEnumerable<HitObject> objects, int? newVolume)
{
if (newVolume == null)
return;
beatmap.BeginChange();
foreach (var h in objects)
{
2023-04-26 19:45:58 +08:00
for (int i = 0; i < h.Samples.Count; i++)
{
h.Samples[i] = h.Samples[i].With(newVolume: newVolume.Value);
}
beatmap.Update(h);
}
beatmap.EndChange();
}
}
}
}