Skip to content

Commit fa40bee

Browse files
committed
[Editor] FetchAsset command
1 parent 97e5dfc commit fa40bee

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

sources/editor/Stride.Core.Assets.Editor/Quantum/NodePresenters/Commands/FetchAssetCommand.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Stride.Core.Assets.Editor.Quantum.NodePresenters.Commands;
1111

12-
public class FetchAssetCommand : NodePresenterCommandBase
12+
internal sealed class FetchAssetCommand : NodePresenterCommandBase
1313
{
1414
/// <summary>
1515
/// The name of this command.
@@ -18,15 +18,15 @@ public class FetchAssetCommand : NodePresenterCommandBase
1818
/// <summary>
1919
/// The current session.
2020
/// </summary>
21-
protected readonly SessionViewModel Session;
21+
private readonly SessionViewModel session;
2222

2323
/// <summary>
2424
/// Initializes a new instance of the <see cref="FetchAssetCommand"/> class.
2525
/// </summary>
2626
/// <param name="session">The current session.</param>
2727
public FetchAssetCommand(SessionViewModel session)
2828
{
29-
Session = session;
29+
this.session = session;
3030
}
3131

3232
/// <inheritdoc/>
@@ -38,14 +38,16 @@ public FetchAssetCommand(SessionViewModel session)
3838
/// <inheritdoc/>
3939
public override bool CanAttach(INodePresenter nodePresenter)
4040
{
41-
var type = nodePresenter.Descriptor.GetInnerCollectionType();
42-
return AssetRegistry.CanBeAssignedToContentTypes(type, checkIsUrlType: true);
41+
if (nodePresenter.Descriptor?.GetInnerCollectionType() is { } type)
42+
return AssetRegistry.CanBeAssignedToContentTypes(type, checkIsUrlType: true);
43+
44+
return false;
4345
}
4446

4547
/// <inheritdoc/>
4648
public override Task Execute(INodePresenter nodePresenter, object? parameter, object? preExecuteResult)
4749
{
48-
return Fetch(Session, nodePresenter.Value);
50+
return Fetch(session, nodePresenter.Value);
4951
}
5052

5153
/// <summary>
@@ -58,9 +60,7 @@ public static async Task Fetch(SessionViewModel session, object content)
5860
var asset = ContentReferenceHelper.GetReferenceTarget(session, content);
5961
if (asset != null)
6062
{
61-
// FIXME xplat-editor
62-
await Task.Yield();
63-
//await session.Dispatcher.InvokeAsync(() => session.AssetCollection.SelectAssetCommand.Execute(asset));
63+
await session.Dispatcher.InvokeAsync(() => session.AssetCollection.SelectAssetCommand.Execute(asset));
6464
}
6565
}
6666
}

sources/editor/Stride.Core.Assets.Editor/ViewModels/AssetCollectionViewModel.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Collections.Specialized;
66
using Stride.Core.Assets.Editor.Components.Properties;
77
using Stride.Core.Assets.Presentation.ViewModels;
8+
using Stride.Core.Extensions;
89
using Stride.Core.Presentation.Collections;
10+
using Stride.Core.Presentation.Commands;
911
using Stride.Core.Presentation.ViewModels;
1012

1113
namespace Stride.Core.Assets.Editor.ViewModels;
@@ -26,6 +28,8 @@ public AssetCollectionViewModel(SessionViewModel session)
2628
// Initialize the view model that will manage the properties of the assets selected on the main asset view
2729
AssetViewProperties = new SessionObjectPropertiesViewModel(session);
2830

31+
SelectAssetCommand = new AnonymousCommand<AssetViewModel>(ServiceProvider, x => SelectAssets(x.Yield()!));
32+
2933
selectedContent.CollectionChanged += SelectedContentCollectionChanged;
3034
SelectedLocations.CollectionChanged += SelectedLocationCollectionChanged;
3135
}
@@ -61,7 +65,32 @@ public object? SingleSelectedContent
6165
get => singleSelectedContent;
6266
private set => SetValue(ref singleSelectedContent, value);
6367
}
64-
68+
69+
public ICommandBase SelectAssetCommand { get; }
70+
71+
public void SelectAssets(IEnumerable<AssetViewModel> assetsToSelect)
72+
{
73+
Dispatcher.EnsureAccess();
74+
75+
var assetList = assetsToSelect.ToList();
76+
77+
// Ensure the location of the assets to select are themselves selected.
78+
var locations = new HashSet<DirectoryBaseViewModel>(assetList.Select(x => x.Directory));
79+
if (locations.All(x => !SelectedLocations.Contains(x)))
80+
{
81+
SelectedLocations.Clear();
82+
SelectedLocations.AddRange(locations);
83+
}
84+
85+
// Don't reselect if the current selection is the same
86+
if (assetList.Count != SelectedAssets.Count || !assetList.All(x => SelectedAssets.Contains(x)))
87+
{
88+
selectedContent.Clear();
89+
// FIXME xplat-editor filters
90+
selectedContent.AddRange(assetList);
91+
}
92+
}
93+
6594
internal IReadOnlyCollection<DirectoryBaseViewModel> GetSelectedDirectories(bool includeSubDirectoriesOfSelected)
6695
{
6796
var selectedDirectories = new List<DirectoryBaseViewModel>();
@@ -162,7 +191,7 @@ private async void SelectedContentCollectionChanged(object? sender, NotifyCollec
162191
}
163192

164193
AssetViewProperties.UpdateTypeAndName(SelectedAssets, x => x.TypeDisplayName, x => x.Url, "assets");
165-
await AssetViewProperties.GenerateSelectionPropertiesAsync(SelectedAssets);
194+
await AssetViewProperties.GenerateSelectionPropertiesAsync(SelectedAssets);
166195
}
167196

168197
private void SelectedLocationCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)

0 commit comments

Comments
 (0)