Esempio n. 1
0
        /// <inheritdoc/>
        protected override bool CanReplaceItem(IObjectNode collection, NodeIndex index, object newItem)
        {
            if (collection.Type != typeof(EntityComponentCollection))
            {
                return(base.CanReplaceItem(collection, index, newItem));
            }

            if (newItem == null)
            {
                return(false);
            }

            var componentType = newItem.GetType();

            // Cannot replace the transform component by another type of component
            if (collection.IndexedTarget(index).Type == typeof(TransformComponent) && componentType != typeof(TransformComponent))
            {
                return(false);
            }

            if (!EntityComponentAttributes.Get(componentType).AllowMultipleComponents)
            {
                // Cannot replace components that disallow multiple components, unless it is that specific component we're replacing
                var components = (EntityComponentCollection)collection.Retrieve();
                if (components.Where((x, i) => x.GetType() == componentType && i != index.Int).Any())
                {
                    return(false);
                }
            }
            return(base.CanReplaceItem(collection, index, newItem));
        }
Esempio n. 2
0
        /// <summary>
        /// Indicates whether the target node of the item corresponding to the given index in the collection contained in the given node should be visited or not.
        /// </summary>
        /// <param name="collectionNode">The node to evaluate.</param>
        /// <param name="index">The index of the item to evaluate.</param>
        /// <returns>True if the node of the item corresponding to the given index in the collection contained in the given node should be visited, false otherwise.</returns>
        /// <remarks>This method is invoked only when the given <see cref="IObjectNode"/> contains a collection with items being references.</remarks>
        protected internal virtual bool ShouldVisitTargetItem([NotNull] IObjectNode collectionNode, NodeIndex index)
        {
            if (collectionNode == null)
            {
                throw new ArgumentNullException(nameof(collectionNode));
            }
            var target = collectionNode.IndexedTarget(index);

            return(!visitedNodes.Contains(target));
        }
Esempio n. 3
0
        public override void AddItem(object value)
        {
            if (Container.IndexedTarget(Index) == null || !Container.IndexedTarget(Index).IsEnumerable)
            {
                throw new NodePresenterException($"{nameof(MemberNodePresenter)}.{nameof(AddItem)} cannot be invoked on members that are not collection.");
            }

            try
            {
                Container.IndexedTarget(Index).Add(value);
            }
            catch (Exception e)
            {
                throw new NodePresenterException("An error occurred while adding an item to the node, see the inner exception for more information.", e);
            }
        }
Esempio n. 4
0
 /// <inheritdoc/>
 protected override void ReplaceItem(IObjectNode collection, NodeIndex index, object newItem)
 {
     // If we're replacing the transform component, only manually copy allowed properties to the existing one.
     if (collection.Type == typeof(EntityComponentCollection) && newItem is TransformComponent newTransform)
     {
         var node = collection.IndexedTarget(index);
         node[nameof(TransformComponent.Position)].Update(newTransform.Position);
         node[nameof(TransformComponent.Rotation)].Update(newTransform.Rotation);
         node[nameof(TransformComponent.Scale)].Update(newTransform.Scale);
         return;
     }
     base.ReplaceItem(collection, index, newItem);
 }