2021-04-27 14:40:35 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-04-27 14:40:35 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Humanizer;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
|
|
|
using osu.Game.Audio;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Rulesets.Edit;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Compose.Components
|
|
|
|
{
|
2021-04-28 12:43:16 +08:00
|
|
|
public partial class EditorSelectionHandler : SelectionHandler<HitObject>
|
2021-04-27 14:40:35 +08:00
|
|
|
{
|
|
|
|
[Resolved]
|
|
|
|
protected EditorBeatmap EditorBeatmap { get; private set; }
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
createStateBindables();
|
|
|
|
|
|
|
|
// bring in updates from selection changes
|
|
|
|
EditorBeatmap.HitObjectUpdated += _ => Scheduler.AddOnce(UpdateTernaryStates);
|
|
|
|
|
2022-06-24 20:25:23 +08:00
|
|
|
SelectedItems.CollectionChanged += (_, _) => Scheduler.AddOnce(UpdateTernaryStates);
|
2021-04-27 14:40:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void DeleteItems(IEnumerable<HitObject> items) => EditorBeatmap.RemoveRange(items);
|
|
|
|
|
|
|
|
#region Selection State
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The state of "new combo" for all selected hitobjects.
|
|
|
|
/// </summary>
|
|
|
|
public readonly Bindable<TernaryState> SelectionNewComboState = new Bindable<TernaryState>();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The state of each sample type for all selected hitobjects. Keys match with <see cref="HitSampleInfo"/> constant specifications.
|
|
|
|
/// </summary>
|
|
|
|
public readonly Dictionary<string, Bindable<TernaryState>> SelectionSampleStates = new Dictionary<string, Bindable<TernaryState>>();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Set up ternary state bindables and bind them to selection/hitobject changes (in both directions)
|
|
|
|
/// </summary>
|
|
|
|
private void createStateBindables()
|
|
|
|
{
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach (string sampleName in HitSampleInfo.AllAdditions)
|
2021-04-27 14:40:35 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new combo
|
|
|
|
SelectionNewComboState.ValueChanged += state =>
|
|
|
|
{
|
|
|
|
switch (state.NewValue)
|
|
|
|
{
|
|
|
|
case TernaryState.False:
|
|
|
|
SetNewCombo(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TernaryState.True:
|
|
|
|
SetNewCombo(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when context menu ternary states may need to be recalculated (selection changed or hitobject updated).
|
|
|
|
/// </summary>
|
|
|
|
protected virtual void UpdateTernaryStates()
|
|
|
|
{
|
2021-04-27 17:06:27 +08:00
|
|
|
SelectionNewComboState.Value = GetStateFromSelection(SelectedItems.OfType<IHasComboInformation>(), h => h.NewCombo);
|
2021-04-27 14:40:35 +08:00
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach ((string sampleName, var bindable) in SelectionSampleStates)
|
2021-04-27 14:40:35 +08:00
|
|
|
{
|
2021-04-27 17:06:27 +08:00
|
|
|
bindable.Value = GetStateFromSelection(SelectedItems, h => h.Samples.Any(s => s.Name == sampleName));
|
2021-04-27 14:40:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2021-04-28 10:42:10 +08:00
|
|
|
#region Ternary state changes
|
2021-04-27 14:40:35 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a hit sample to all selected <see cref="HitObject"/>s.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sampleName">The name of the hit sample.</param>
|
|
|
|
public void AddHitSample(string sampleName)
|
|
|
|
{
|
|
|
|
EditorBeatmap.PerformOnSelection(h =>
|
|
|
|
{
|
|
|
|
// Make sure there isn't already an existing sample
|
|
|
|
if (h.Samples.Any(s => s.Name == sampleName))
|
|
|
|
return;
|
|
|
|
|
2023-05-08 20:42:15 +08:00
|
|
|
h.Samples.Add(h.GetSampleInfo(sampleName));
|
2021-05-23 20:19:38 +08:00
|
|
|
EditorBeatmap.Update(h);
|
2021-04-27 14:40:35 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-28 10:42:10 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Removes a hit sample from all selected <see cref="HitObject"/>s.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sampleName">The name of the hit sample.</param>
|
|
|
|
public void RemoveHitSample(string sampleName)
|
|
|
|
{
|
2021-05-23 20:19:38 +08:00
|
|
|
EditorBeatmap.PerformOnSelection(h =>
|
|
|
|
{
|
|
|
|
h.SamplesBindable.RemoveAll(s => s.Name == sampleName);
|
|
|
|
EditorBeatmap.Update(h);
|
|
|
|
});
|
2021-04-28 10:42:10 +08:00
|
|
|
}
|
|
|
|
|
2021-04-27 14:40:35 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Set the new combo state of all selected <see cref="HitObject"/>s.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Whether to set or unset.</param>
|
|
|
|
/// <exception cref="InvalidOperationException">Throws if any selected object doesn't implement <see cref="IHasComboInformation"/></exception>
|
|
|
|
public void SetNewCombo(bool state)
|
|
|
|
{
|
|
|
|
EditorBeatmap.PerformOnSelection(h =>
|
|
|
|
{
|
|
|
|
var comboInfo = h as IHasComboInformation;
|
|
|
|
|
|
|
|
if (comboInfo == null || comboInfo.NewCombo == state) return;
|
|
|
|
|
|
|
|
comboInfo.NewCombo = state;
|
|
|
|
EditorBeatmap.Update(h);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Context Menu
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Provide context menu items relevant to current selection. Calling base is not required.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="selection">The current selection.</param>
|
|
|
|
/// <returns>The relevant menu items.</returns>
|
2021-04-28 12:43:16 +08:00
|
|
|
protected override IEnumerable<MenuItem> GetContextMenuItemsForSelection(IEnumerable<SelectionBlueprint<HitObject>> selection)
|
|
|
|
{
|
|
|
|
if (SelectedBlueprints.All(b => b.Item is IHasComboInformation))
|
|
|
|
{
|
2021-05-20 18:34:53 +08:00
|
|
|
yield return new TernaryStateToggleMenuItem("New combo") { State = { BindTarget = SelectionNewComboState } };
|
2021-04-28 12:43:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
yield return new OsuMenuItem("Sound")
|
|
|
|
{
|
|
|
|
Items = SelectionSampleStates.Select(kvp =>
|
2021-05-20 18:34:53 +08:00
|
|
|
new TernaryStateToggleMenuItem(kvp.Value.Description) { State = { BindTarget = kvp.Value } }).ToArray()
|
2021-04-28 12:43:16 +08:00
|
|
|
};
|
|
|
|
}
|
2021-04-27 14:40:35 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|