1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 20:02:58 +08:00

cleanup code duplication

This commit is contained in:
OliBomby 2023-05-08 15:14:25 +02:00
parent 4c36530435
commit bb8285e2ef

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Humanizer; using Humanizer;
@ -177,28 +178,34 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
private static string? getCommonBank(IList<HitSampleInfo>[] relevantSamples) => relevantSamples.Select(GetBankValue).Distinct().Count() == 1 ? GetBankValue(relevantSamples.First()) : 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 static int? getCommonVolume(IList<HitSampleInfo>[] relevantSamples) => relevantSamples.Select(GetVolumeValue).Distinct().Count() == 1 ? GetVolumeValue(relevantSamples.First()) : null;
private void updateBankFor(IEnumerable<HitObject> objects, string? newBank) private void updateFor(IEnumerable<HitObject> objects, Action<HitObject, IList<HitSampleInfo>> updateAction)
{ {
if (string.IsNullOrEmpty(newBank))
return;
beatmap.BeginChange(); beatmap.BeginChange();
foreach (var h in objects) foreach (var h in objects)
{ {
var samples = GetSamples(h); var samples = GetSamples(h);
updateAction(h, samples);
for (int i = 0; i < samples.Count; i++)
{
samples[i] = samples[i].With(newBank: newBank);
}
beatmap.Update(h); beatmap.Update(h);
} }
beatmap.EndChange(); beatmap.EndChange();
} }
private void updateBankFor(IEnumerable<HitObject> objects, string? newBank)
{
if (string.IsNullOrEmpty(newBank))
return;
updateFor(objects, (_, samples) =>
{
for (int i = 0; i < samples.Count; i++)
{
samples[i] = samples[i].With(newBank: newBank);
}
});
}
private void updateBankPlaceholderText(IEnumerable<HitObject> objects) private void updateBankPlaceholderText(IEnumerable<HitObject> objects)
{ {
string? commonBank = getCommonBank(objects.Select(GetSamples).ToArray()); string? commonBank = getCommonBank(objects.Select(GetSamples).ToArray());
@ -210,21 +217,13 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
if (newVolume == null) if (newVolume == null)
return; return;
beatmap.BeginChange(); updateFor(objects, (_, samples) =>
foreach (var h in objects)
{ {
var samples = GetSamples(h);
for (int i = 0; i < samples.Count; i++) for (int i = 0; i < samples.Count; i++)
{ {
samples[i] = samples[i].With(newVolume: newVolume.Value); samples[i] = samples[i].With(newVolume: newVolume.Value);
} }
});
beatmap.Update(h);
}
beatmap.EndChange();
} }
#region hitsound toggles #region hitsound toggles
@ -277,21 +276,14 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
if (string.IsNullOrEmpty(sampleName)) if (string.IsNullOrEmpty(sampleName))
return; return;
beatmap.BeginChange(); updateFor(objects, (h, samples) =>
foreach (var h in objects)
{ {
var samples = GetSamples(h);
// Make sure there isn't already an existing sample // Make sure there isn't already an existing sample
if (samples.Any(s => s.Name == sampleName)) if (samples.Any(s => s.Name == sampleName))
return; return;
samples.Add(h.GetSampleInfo(sampleName)); samples.Add(h.GetSampleInfo(sampleName));
beatmap.Update(h); });
}
beatmap.EndChange();
} }
private void removeHitSampleFor(IEnumerable<HitObject> objects, string sampleName) private void removeHitSampleFor(IEnumerable<HitObject> objects, string sampleName)
@ -299,22 +291,14 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
if (string.IsNullOrEmpty(sampleName)) if (string.IsNullOrEmpty(sampleName))
return; return;
beatmap.BeginChange(); updateFor(objects, (_, samples) =>
foreach (var h in objects)
{ {
var samples = GetSamples(h);
for (int i = 0; i < samples.Count; i++) for (int i = 0; i < samples.Count; i++)
{ {
if (samples[i].Name == sampleName) if (samples[i].Name == sampleName)
samples.RemoveAt(i--); samples.RemoveAt(i--);
} }
});
beatmap.Update(h);
}
beatmap.EndChange();
} }
protected override bool OnKeyDown(KeyDownEvent e) protected override bool OnKeyDown(KeyDownEvent e)