forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Editor] Collection supports in abstract properties
- Loading branch information
Showing
8 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...tride.Core.Assets.Editor/Quantum/NodePresenters/Keys/AbstractNodeCollectionEntriesData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Stride.Core.Assets.Editor.Quantum.NodePresenters.Commands; | ||
using Stride.Core.Presentation.Quantum.Presenters; | ||
|
||
namespace Stride.Core.Assets.Editor.Quantum.NodePresenters.Keys | ||
{ | ||
public static class AbstractNodeCollectionEntriesData | ||
{ | ||
public const string AbstractNodeCollectionMatchingEntries = nameof(AbstractNodeCollectionMatchingEntries); | ||
public static readonly PropertyKey<IEnumerable<AbstractNodeEntry>> Key = new PropertyKey<IEnumerable<AbstractNodeEntry>>(AbstractNodeCollectionMatchingEntries, typeof(AbstractNodeEntryData), new PropertyCombinerMetadata(CombineProperty)); | ||
|
||
public static object CombineProperty(IEnumerable<object> properties) | ||
{ | ||
var result = new HashSet<AbstractNodeEntry>(); | ||
var hashSets = new List<HashSet<AbstractNodeEntry>>(); | ||
hashSets.AddRange(properties.Cast<IEnumerable<AbstractNodeEntry>>().Select(x => new HashSet<AbstractNodeEntry>(x))); | ||
result = hashSets[0]; | ||
// We display only component types that are available for all entities | ||
for (var i = 1; i < hashSets.Count; ++i) | ||
{ | ||
result.IntersectWith(hashSets[i]); | ||
} | ||
return result.OrderBy(x => x.Order).ThenBy(x => x.DisplayValue); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...e.Assets.Editor/Quantum/NodePresenters/Updaters/AbstractNodeCollectionEntryNodeUpdater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Stride.Core.Assets.Editor.Quantum.NodePresenters.Commands; | ||
using Stride.Core.Assets.Editor.Quantum.NodePresenters.Keys; | ||
using Stride.Core.Annotations; | ||
using Stride.Core.Extensions; | ||
using Stride.Core.Reflection; | ||
using Stride.Core.Presentation.Quantum.Presenters; | ||
|
||
namespace Stride.Core.Assets.Editor.Quantum.NodePresenters.Updaters | ||
{ | ||
public sealed class AbstractNodeCollectionEntryNodeUpdater : AssetNodePresenterUpdaterBase | ||
{ | ||
public static IEnumerable<AbstractNodeEntry> FillDefaultAbstractNodeCollectionEntries(IAssetNodePresenter node, Type type) | ||
{ | ||
IEnumerable<AbstractNodeEntry> abstractNodeMatchingEntries = AbstractNodeType.GetInheritedInstantiableTypes(type); | ||
|
||
if (abstractNodeMatchingEntries != null) | ||
{ | ||
// Prepend the value that will allow to set the value to null, if this command is allowed. | ||
if (IsAllowingNull(node)) | ||
abstractNodeMatchingEntries = AbstractNodeValue.Null.Yield().Concat(abstractNodeMatchingEntries); | ||
} | ||
return abstractNodeMatchingEntries; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if <see cref="MemberCollectionAttribute.NotNullItems"/> is present and set. | ||
/// </summary> | ||
/// <param name="node">The node to check.</param> | ||
/// <returns>True if null is a possible choice for this node, otherwise false.</returns> | ||
public static bool IsAllowingNull(IAssetNodePresenter node) | ||
{ | ||
var abstractNodeAllowNull = true; | ||
var memberNode = node as MemberNodePresenter ?? (node as ItemNodePresenter)?.Parent as MemberNodePresenter; | ||
if (memberNode != null) | ||
{ | ||
var memberCollection = memberNode.MemberAttributes.OfType<MemberCollectionAttribute>().FirstOrDefault() | ||
?? memberNode.Descriptor.Attributes.OfType<MemberCollectionAttribute>().FirstOrDefault(); | ||
|
||
if (memberNode.IsEnumerable && memberCollection != null && memberCollection.NotNullItems) | ||
{ | ||
// Collections | ||
abstractNodeAllowNull = false; | ||
} | ||
else | ||
{ | ||
// Members | ||
abstractNodeAllowNull = !memberNode.MemberAttributes.OfType<NotNullAttribute>().Any(); | ||
} | ||
} | ||
return abstractNodeAllowNull; | ||
} | ||
|
||
protected override void UpdateNode(IAssetNodePresenter node) | ||
{ | ||
if (node.ValueIsAnyCollection(out _, out var type, out _) && type.IsAbstract && !IsReferenceType(type) && IsInstantiable(type)) | ||
{ | ||
var abstractNodeEntries = FillDefaultAbstractNodeCollectionEntries(node, type); | ||
node.AttachedProperties.Add(AbstractNodeCollectionEntriesData.Key, abstractNodeEntries); | ||
} | ||
} | ||
|
||
private static bool IsInstantiable(Type type) => TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<NonInstantiableAttribute>(type) == null; | ||
|
||
private static bool IsReferenceType(Type type) => AssetRegistry.IsContentType(type) || typeof(AssetReference).IsAssignableFrom(type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters