2021-04-28 06:14:48 +00: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.
|
|
|
|
|
2021-05-11 08:49:00 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Specialized;
|
2022-12-16 11:18:02 +00:00
|
|
|
using System.Diagnostics;
|
2021-04-28 06:14:48 +00:00
|
|
|
using System.Linq;
|
2021-05-11 08:49:00 +00:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2023-01-26 09:21:04 +00:00
|
|
|
using osu.Framework.Input.Events;
|
2021-04-28 06:14:48 +00:00
|
|
|
using osu.Game.Rulesets.Edit;
|
|
|
|
using osu.Game.Screens.Edit.Compose.Components;
|
2023-01-26 09:21:04 +00:00
|
|
|
using osu.Game.Skinning;
|
2022-09-08 15:14:34 +00:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Input;
|
2021-04-28 06:14:48 +00:00
|
|
|
|
2023-01-26 09:21:04 +00:00
|
|
|
namespace osu.Game.Overlays.SkinEditor
|
2021-04-28 06:14:48 +00:00
|
|
|
{
|
2023-02-15 07:01:26 +00:00
|
|
|
public partial class SkinBlueprintContainer : BlueprintContainer<ISerialisableDrawable>
|
2021-04-28 06:14:48 +00:00
|
|
|
{
|
2023-03-04 12:33:21 +00:00
|
|
|
private readonly ISerialisableDrawableContainer targetContainer;
|
2021-04-28 06:14:48 +00:00
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
private readonly List<BindableList<ISerialisableDrawable>> targetComponents = new List<BindableList<ISerialisableDrawable>>();
|
2021-05-13 04:03:23 +00:00
|
|
|
|
2022-04-07 10:12:10 +00:00
|
|
|
[Resolved]
|
2023-01-26 08:46:19 +00:00
|
|
|
private SkinEditor editor { get; set; } = null!;
|
2022-04-07 10:12:10 +00:00
|
|
|
|
2023-03-04 12:33:21 +00:00
|
|
|
public SkinBlueprintContainer(ISerialisableDrawableContainer targetContainer)
|
2021-04-28 06:14:48 +00:00
|
|
|
{
|
2023-03-04 12:33:21 +00:00
|
|
|
this.targetContainer = targetContainer;
|
2021-04-28 06:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-04-07 10:12:10 +00:00
|
|
|
SelectedItems.BindTo(editor.SelectedComponents);
|
|
|
|
|
2023-03-04 12:33:21 +00:00
|
|
|
var bindableList = new BindableList<ISerialisableDrawable> { BindTarget = targetContainer.Components };
|
|
|
|
bindableList.BindCollectionChanged(componentsChanged, true);
|
2021-05-11 09:37:41 +00:00
|
|
|
|
2023-03-04 12:33:21 +00:00
|
|
|
targetComponents.Add(bindableList);
|
2021-04-28 09:59:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 08:46:19 +00:00
|
|
|
private void componentsChanged(object? sender, NotifyCollectionChangedEventArgs e) => Schedule(() =>
|
2021-04-28 09:59:55 +00:00
|
|
|
{
|
2021-05-11 08:49:00 +00:00
|
|
|
switch (e.Action)
|
|
|
|
{
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
2022-12-16 11:18:02 +00:00
|
|
|
Debug.Assert(e.NewItems != null);
|
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
foreach (var item in e.NewItems.Cast<ISerialisableDrawable>())
|
2021-05-11 08:49:00 +00:00
|
|
|
AddBlueprintFor(item);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
case NotifyCollectionChangedAction.Reset:
|
2022-12-16 11:18:02 +00:00
|
|
|
Debug.Assert(e.OldItems != null);
|
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
foreach (var item in e.OldItems.Cast<ISerialisableDrawable>())
|
2021-05-11 08:49:00 +00:00
|
|
|
RemoveBlueprintFor(item);
|
|
|
|
break;
|
2021-05-06 06:16:16 +00:00
|
|
|
|
2021-05-11 08:49:00 +00:00
|
|
|
case NotifyCollectionChangedAction.Replace:
|
2022-12-16 11:18:02 +00:00
|
|
|
Debug.Assert(e.NewItems != null);
|
|
|
|
Debug.Assert(e.OldItems != null);
|
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
foreach (var item in e.OldItems.Cast<ISerialisableDrawable>())
|
2021-05-11 08:49:00 +00:00
|
|
|
RemoveBlueprintFor(item);
|
2021-04-28 06:14:48 +00:00
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
foreach (var item in e.NewItems.Cast<ISerialisableDrawable>())
|
2021-05-11 08:49:00 +00:00
|
|
|
AddBlueprintFor(item);
|
|
|
|
break;
|
|
|
|
}
|
2022-04-07 10:12:10 +00:00
|
|
|
});
|
2021-04-28 06:14:48 +00:00
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
protected override void AddBlueprintFor(ISerialisableDrawable item)
|
2021-05-13 08:03:17 +00:00
|
|
|
{
|
|
|
|
if (!item.IsEditable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
base.AddBlueprintFor(item);
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:14:34 +00:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Left:
|
|
|
|
moveSelection(new Vector2(-1, 0));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Right:
|
|
|
|
moveSelection(new Vector2(1, 0));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Up:
|
|
|
|
moveSelection(new Vector2(0, -1));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Down:
|
|
|
|
moveSelection(new Vector2(0, 1));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-06 04:50:56 +00:00
|
|
|
protected override void SelectAll()
|
|
|
|
{
|
2022-10-07 01:46:07 +00:00
|
|
|
SelectedItems.AddRange(targetComponents.SelectMany(list => list).Except(SelectedItems).ToArray());
|
2022-10-06 04:50:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 15:14:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Move the current selection spatially by the specified delta, in screen coordinates (ie. the same coordinates as the blueprints).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="delta"></param>
|
|
|
|
private void moveSelection(Vector2 delta)
|
|
|
|
{
|
|
|
|
var firstBlueprint = SelectionHandler.SelectedBlueprints.FirstOrDefault();
|
|
|
|
|
|
|
|
if (firstBlueprint == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// convert to game space coordinates
|
|
|
|
delta = firstBlueprint.ToScreenSpace(delta) - firstBlueprint.ToScreenSpace(Vector2.Zero);
|
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
SelectionHandler.HandleMovement(new MoveSelectionEvent<ISerialisableDrawable>(firstBlueprint, delta));
|
2022-09-08 15:14:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
protected override SelectionHandler<ISerialisableDrawable> CreateSelectionHandler() => new SkinSelectionHandler();
|
2021-04-28 06:14:48 +00:00
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
protected override SelectionBlueprint<ISerialisableDrawable> CreateBlueprintFor(ISerialisableDrawable component)
|
2021-04-28 06:14:48 +00:00
|
|
|
=> new SkinBlueprint(component);
|
2022-04-07 10:12:10 +00:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
foreach (var list in targetComponents)
|
|
|
|
list.UnbindAll();
|
|
|
|
}
|
2021-04-28 06:14:48 +00:00
|
|
|
}
|
|
|
|
}
|