Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ModelNodeExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Linq;
5 
6 using SiliconStudio.Core.Reflection;
7 using SiliconStudio.Quantum.References;
8 
9 namespace SiliconStudio.Quantum
10 {
11  public static class ModelNodeExtensions
12  {
13  /// <summary>
14  /// Retrieve the child node of the given <see cref="IModelNode"/> that matches the given name.
15  /// </summary>
16  /// <param name="modelNode">The view model node to look into.</param>
17  /// <param name="name">The name of the child to retrieve.</param>
18  /// <returns>The child node that matches the given name, or <c>null</c> if no child matches.</returns>
19  public static IModelNode GetChild(this IModelNode modelNode, string name)
20  {
21  return modelNode.Children.FirstOrDefault(x => x.Name == name);
22  }
23 
24  /// <summary>
25  /// Retrieve the child node of the given <see cref="IModelNode"/> that matches the given name. If the node represents an object reference, it returns the referenced object.
26  /// </summary>
27  /// <param name="modelNode">The view model node to look into.</param>
28  /// <param name="name">The name of the child to retrieve.</param>
29  /// <returns>The child node that matches the given name, or the referenced node if the child hold an object reference, or <c>null</c> if no child matches.</returns>
30  public static IModelNode GetReferencedChild(this IModelNode modelNode, string name)
31  {
32  var child = modelNode.GetChild(name);
33  return child != null ? child.ResolveTarget() : null;
34  }
35 
36  /// <summary>
37  /// Gets the child of the given node that matches the given name. If the given node holds an object reference, resolve the target of the reference
38  /// and gets the child of the target node that matches the given name.
39  /// </summary>
40  /// <param name="modelNode">The model node.</param>
41  /// <param name="name">The name of the child to retrieve.</param>
42  /// <returns></returns>
43  public static IModelNode GetChildThroughReferences(this IModelNode modelNode, string name)
44  {
45  var child = modelNode.GetChild(name) ?? modelNode.ResolveTarget().GetChild(name);
46  return child;
47  }
48 
49  /// <summary>
50  /// Gets the target node of the given <see cref="IModelNode"/> if it holds an object reference, or returns the node itself otherwise
51  /// </summary>
52  /// <param name="modelNode">The node that may contains an object reference.</param>
53  /// <returns>The target node of the given <see cref="IModelNode"/> if it holds an object reference, or the node itself otherwise.</returns>
54  public static IModelNode ResolveTarget(this IModelNode modelNode)
55  {
56  var objReference = modelNode.Content.Reference as ObjectReference;
57  return objReference != null ? objReference.TargetNode : modelNode;
58  }
59 
60  /// <summary>
61  /// Gets whether a given <see cref="ITypeDescriptor"/> represents a collection or a dictionary of primitive values.
62  ///
63  /// </summary>
64  /// <param name="descriptor">The type descriptor to check.</param>
65  /// <returns><c>true</c> if the given <see cref="ITypeDescriptor"/> represents a collection or a dictionary of primitive values, <c>false</c> otherwise.</returns>
66  public static bool IsPrimitiveCollection(this ITypeDescriptor descriptor)
67  {
68  var collectionDescriptor = descriptor as CollectionDescriptor;
69  var dictionaryDescriptor = descriptor as DictionaryDescriptor;
70  Type elementType = null;
71  if (collectionDescriptor != null)
72  {
73  elementType = collectionDescriptor.ElementType;
74  }
75  else if (dictionaryDescriptor != null)
76  {
77  elementType = dictionaryDescriptor.ValueType;
78 
79  }
80  if (elementType != null)
81  {
82  if (elementType.IsNullable())
83  elementType = Nullable.GetUnderlyingType(elementType);
84 
85  return elementType.IsPrimitive || elementType == typeof(string) || elementType.IsEnum;
86  }
87  return false;
88  }
89  }
90 }
Provides a descriptor for a System.Collections.ICollection.
static IModelNode GetChildThroughReferences(this IModelNode modelNode, string name)
Gets the child of the given node that matches the given name. If the given node holds an object refer...
static IModelNode GetReferencedChild(this IModelNode modelNode, string name)
Retrieve the child node of the given IModelNode that matches the given name. If the node represents a...
Provides a descriptor for a System.Collections.IDictionary.
Provides access members of a type.
static bool IsPrimitiveCollection(this ITypeDescriptor descriptor)
Gets whether a given ITypeDescriptor represents a collection or a dictionary of primitive values...
static IModelNode ResolveTarget(this IModelNode modelNode)
Gets the target node of the given IModelNode if it holds an object reference, or returns the node its...
static IModelNode GetChild(this IModelNode modelNode, string name)
Retrieve the child node of the given IModelNode that matches the given name.
A class representing a reference to another object that has a different model.
The IModelNode interface represents a node in a model object. A model object is represented by a grap...
Definition: IModelNode.cs:16