1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 09:17:48 +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.

446 lines
17 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.
2023-05-08 21:14:25 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using Humanizer;
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.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Rulesets.Objects;
using osu.Game.Screens.Edit.Components.TernaryButtons;
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;
using osuTK.Input;
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
{
public partial class SamplePointPiece : HitObjectPointPiece, IHasPopover
{
public readonly HitObject HitObject;
[Resolved]
protected EditorClock? EditorClock { get; private set; }
public SamplePointPiece(HitObject hitObject)
{
HitObject = hitObject;
}
2023-05-31 20:33:06 +08:00
public bool AlternativeColor { get; init; }
protected override Color4 GetRepresentingColour(OsuColour colours) => AlternativeColor ? colours.Pink2 : colours.Pink1;
2023-04-26 00:12:53 +08:00
[BackgroundDependencyLoader]
2022-01-15 08:06:39 +08:00
private void load()
{
2023-05-08 17:54:33 +08:00
HitObject.DefaultsApplied += _ => updateText();
updateText();
}
protected override bool OnClick(ClickEvent e)
{
this.ShowPopover();
return true;
}
protected override bool OnDoubleClick(DoubleClickEvent e)
{
EditorClock?.SeekSmoothlyTo(HitObject.StartTime);
this.ShowPopover();
return true;
}
private void updateText()
{
Label.Text = $"{abbreviateBank(GetBankValue(GetSamples()))} {GetVolumeValue(GetSamples())}";
}
private static string? abbreviateBank(string? bank)
{
return bank switch
{
2024-06-06 19:39:32 +08:00
HitSampleInfo.BANK_NORMAL => @"N",
HitSampleInfo.BANK_SOFT => @"S",
HitSampleInfo.BANK_DRUM => @"D",
_ => bank
};
2023-04-26 19:45:58 +08:00
}
public static string? GetBankValue(IEnumerable<HitSampleInfo> samples)
{
2023-05-08 22:12:03 +08:00
return samples.FirstOrDefault(o => o.Name == HitSampleInfo.HIT_NORMAL)?.Bank;
}
public static string? GetAdditionBankValue(IEnumerable<HitSampleInfo> samples)
{
return samples.FirstOrDefault(o => o.Name != HitSampleInfo.HIT_NORMAL)?.Bank;
2023-04-26 19:45:58 +08:00
}
public static int GetVolumeValue(ICollection<HitSampleInfo> samples)
{
return samples.Count == 0 ? 0 : samples.Max(o => o.Volume);
}
2024-06-06 20:57:25 +08:00
/// <summary>
/// Gets the samples to be edited by this sample point piece.
/// This could be the samples of the hit object itself, or of one of the nested hit objects. For example a slider repeat.
/// </summary>
/// <returns>The samples to be edited.</returns>
2023-05-08 18:38:53 +08:00
protected virtual IList<HitSampleInfo> GetSamples() => HitObject.Samples;
public virtual Popover GetPopover() => new SampleEditPopover(HitObject);
public partial class SampleEditPopover : OsuPopover
{
private readonly HitObject hitObject;
private LabelledTextBox bank = null!;
2023-05-08 22:12:03 +08:00
private LabelledTextBox additionBank = null!;
private IndeterminateSliderWithTextBoxInput<int> volume = null!;
private FillFlowContainer togglesCollection = null!;
private HitObject[] relevantObjects = null!;
private IList<HitSampleInfo>[] allRelevantSamples = null!;
2023-06-01 15:34:21 +08:00
/// <summary>
/// Gets the sub-set of samples relevant to this sample point piece.
/// For example, to edit node samples this should return the samples at the index of the node.
/// </summary>
/// <param name="ho">The hit object to get the relevant samples from.</param>
/// <returns>The relevant list of samples.</returns>
protected virtual IList<HitSampleInfo> GetRelevantSamples(HitObject ho) => ho.Samples;
2023-05-08 18:38:53 +08:00
[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[]
{
togglesCollection = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 5),
},
bank = new LabelledTextBox
{
Label = "Bank Name",
},
2023-05-08 22:12:03 +08:00
additionBank = new LabelledTextBox
{
Label = "Addition Bank",
},
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;
2023-05-08 22:12:03 +08:00
additionBank.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.
relevantObjects = (beatmap.SelectedHitObjects.Contains(hitObject) ? beatmap.SelectedHitObjects : hitObject.Yield()).ToArray();
allRelevantSamples = relevantObjects.Select(GetRelevantSamples).ToArray();
// even if there are multiple objects selected, we can still display sample volume or bank if they all have the same value.
int? commonVolume = getCommonVolume();
if (commonVolume != null)
volume.Current.Value = commonVolume.Value;
updatePrimaryBankState();
bank.Current.BindValueChanged(val =>
{
if (string.IsNullOrEmpty(val.NewValue))
return;
setBank(val.NewValue);
updatePrimaryBankState();
});
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.
bank.OnCommit += (_, _) => updatePrimaryBankState();
updateAdditionBankState();
2023-05-08 22:12:03 +08:00
additionBank.Current.BindValueChanged(val =>
{
if (string.IsNullOrEmpty(val.NewValue))
return;
setAdditionBank(val.NewValue);
updateAdditionBankState();
2023-05-08 22:12:03 +08:00
});
additionBank.OnCommit += (_, _) => updateAdditionBankState();
2023-05-08 22:12:03 +08:00
volume.Current.BindValueChanged(val =>
{
if (val.NewValue != null)
setVolume(val.NewValue.Value);
});
createStateBindables();
updateTernaryStates();
togglesCollection.AddRange(createTernaryButtons().Select(b => new DrawableTernaryButton(b) { RelativeSizeAxes = Axes.None, Size = new Vector2(40, 40) }));
}
private string? getCommonBank() => allRelevantSamples.Select(GetBankValue).Distinct().Count() == 1 ? GetBankValue(allRelevantSamples.First()) : null;
private string? getCommonAdditionBank() => allRelevantSamples.Select(GetAdditionBankValue).Where(o => o is not null).Distinct().Count() == 1 ? GetAdditionBankValue(allRelevantSamples.First()) : null;
private int? getCommonVolume() => allRelevantSamples.Select(GetVolumeValue).Distinct().Count() == 1 ? GetVolumeValue(allRelevantSamples.First()) : null;
private void updatePrimaryBankState()
{
string? commonBank = getCommonBank();
bank.Current.Value = commonBank;
bank.PlaceholderText = string.IsNullOrEmpty(commonBank) ? "(multiple)" : string.Empty;
}
private void updateAdditionBankState()
{
string? commonAdditionBank = getCommonAdditionBank();
additionBank.PlaceholderText = string.IsNullOrEmpty(commonAdditionBank) ? "(multiple)" : string.Empty;
additionBank.Current.Value = commonAdditionBank;
bool anyAdditions = allRelevantSamples.Any(o => o.Any(s => s.Name != HitSampleInfo.HIT_NORMAL));
if (anyAdditions)
additionBank.Show();
else
additionBank.Hide();
}
/// <summary>
/// Applies the given update action on all samples of <see cref="allRelevantSamples"/>
/// and invokes the necessary update notifiers for the beatmap and hit objects.
/// </summary>
/// <param name="updateAction">The action to perform on each element of <see cref="allRelevantSamples"/>.</param>
private void updateAllRelevantSamples(Action<HitObject, IList<HitSampleInfo>> updateAction)
{
beatmap.BeginChange();
foreach (var relevantHitObject in relevantObjects)
{
var relevantSamples = GetRelevantSamples(relevantHitObject);
updateAction(relevantHitObject, relevantSamples);
beatmap.Update(relevantHitObject);
2023-05-08 21:14:25 +08:00
}
beatmap.EndChange();
}
private void setBank(string newBank)
2023-05-08 21:14:25 +08:00
{
updateAllRelevantSamples((_, relevantSamples) =>
2023-05-08 21:14:25 +08:00
{
for (int i = 0; i < relevantSamples.Count; i++)
2023-04-26 19:45:58 +08:00
{
if (relevantSamples[i].Name != HitSampleInfo.HIT_NORMAL) continue;
2023-05-08 22:12:03 +08:00
relevantSamples[i] = relevantSamples[i].With(newBank: newBank);
2023-05-08 22:12:03 +08:00
}
});
}
private void setAdditionBank(string newBank)
2023-05-08 22:12:03 +08:00
{
updateAllRelevantSamples((_, relevantSamples) =>
2023-05-08 22:12:03 +08:00
{
for (int i = 0; i < relevantSamples.Count; i++)
2023-05-08 22:12:03 +08:00
{
if (relevantSamples[i].Name == HitSampleInfo.HIT_NORMAL) continue;
2023-05-08 22:12:03 +08:00
relevantSamples[i] = relevantSamples[i].With(newBank: newBank);
2023-04-26 19:45:58 +08:00
}
2023-05-08 21:14:25 +08:00
});
}
private void setVolume(int newVolume)
{
updateAllRelevantSamples((_, relevantSamples) =>
{
for (int i = 0; i < relevantSamples.Count; i++)
2023-04-26 19:45:58 +08:00
{
relevantSamples[i] = relevantSamples[i].With(newVolume: newVolume);
2023-04-26 19:45:58 +08:00
}
2023-05-08 21:14:25 +08:00
});
}
#region hitsound toggles
private readonly Dictionary<string, Bindable<TernaryState>> selectionSampleStates = new Dictionary<string, Bindable<TernaryState>>();
2023-08-17 04:07:36 +08:00
private readonly List<string> banks = new List<string>();
private void createStateBindables()
{
foreach (string sampleName in HitSampleInfo.AllAdditions)
{
var bindable = new Bindable<TernaryState>
{
Description = sampleName.Replace("hit", string.Empty).Titleize()
};
bindable.ValueChanged += state =>
{
switch (state.NewValue)
{
case TernaryState.False:
removeHitSample(sampleName);
break;
case TernaryState.True:
addHitSample(sampleName);
break;
}
};
selectionSampleStates[sampleName] = bindable;
}
2023-08-17 04:07:36 +08:00
banks.AddRange(HitSampleInfo.AllBanks);
}
private void updateTernaryStates()
{
foreach ((string sampleName, var bindable) in selectionSampleStates)
{
2023-06-01 15:34:21 +08:00
bindable.Value = SelectionHandler<HitObject>.GetStateFromSelection(relevantObjects, h => GetRelevantSamples(h).Any(s => s.Name == sampleName));
}
}
private IEnumerable<TernaryButton> createTernaryButtons()
{
foreach ((string sampleName, var bindable) in selectionSampleStates)
yield return new TernaryButton(bindable, string.Empty, () => ComposeBlueprintContainer.GetIconForSample(sampleName));
}
private void addHitSample(string sampleName)
{
if (string.IsNullOrEmpty(sampleName))
return;
updateAllRelevantSamples((h, relevantSamples) =>
{
// Make sure there isn't already an existing sample
if (relevantSamples.Any(s => s.Name == sampleName))
return;
// First try inheriting the sample info from the node samples instead of the samples of the hitobject
var relevantSample = relevantSamples.FirstOrDefault(s => s.Name != HitSampleInfo.HIT_NORMAL) ?? relevantSamples.FirstOrDefault();
relevantSamples.Add(relevantSample?.With(sampleName) ?? h.CreateHitSampleInfo(sampleName));
2023-05-08 21:14:25 +08:00
});
updateAdditionBankState();
}
private void removeHitSample(string sampleName)
{
if (string.IsNullOrEmpty(sampleName))
return;
updateAllRelevantSamples((_, relevantSamples) =>
{
for (int i = 0; i < relevantSamples.Count; i++)
{
if (relevantSamples[i].Name == sampleName)
relevantSamples.RemoveAt(i--);
}
2023-05-08 21:14:25 +08:00
});
updateAdditionBankState();
}
protected override bool OnKeyDown(KeyDownEvent e)
{
2023-08-17 04:07:36 +08:00
if (e.ControlPressed || e.AltPressed || e.SuperPressed || !checkRightToggleFromKey(e.Key, out int rightIndex))
return base.OnKeyDown(e);
2023-08-17 04:07:36 +08:00
if (e.ShiftPressed)
{
2023-08-17 05:09:04 +08:00
string? newBank = banks.ElementAtOrDefault(rightIndex);
if (string.IsNullOrEmpty(newBank))
return true;
setBank(newBank);
updatePrimaryBankState();
setAdditionBank(newBank);
updateAdditionBankState();
2023-08-17 04:07:36 +08:00
}
else
{
var item = togglesCollection.ElementAtOrDefault(rightIndex);
2023-08-17 04:07:36 +08:00
if (item is not DrawableTernaryButton button) return base.OnKeyDown(e);
button.Button.Toggle();
}
return true;
}
private bool checkRightToggleFromKey(Key key, out int index)
{
switch (key)
{
case Key.W:
index = 0;
break;
case Key.E:
index = 1;
break;
case Key.R:
index = 2;
break;
default:
index = -1;
break;
}
return index >= 0;
}
#endregion
}
}
}