2
0

Node.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Node C# sugar
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Copyrigh 2015 Xamarin INc
  8. //
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Reflection;
  14. namespace Urho {
  15. internal partial class NodeHelper {
  16. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  17. internal extern static IntPtr urho_node_get_components(IntPtr node, int code, int recursive, out int count);
  18. }
  19. public partial class Node {
  20. static Node[] ZeroArray = new Node[0];
  21. public Node[] GetChildrenWithComponent<T> (bool recursive = false) where T: Component
  22. {
  23. Runtime.ValidateRefCounted(this);
  24. var stringhash = Runtime.LookupStringHash (typeof (T));
  25. int count;
  26. var ptr = NodeHelper.urho_node_get_components (handle, stringhash.Code, recursive ? 1 : 0, out count);
  27. if (ptr == IntPtr.Zero)
  28. return ZeroArray;
  29. var res = new Node[count];
  30. for (int i = 0; i < count; i++){
  31. var node = Marshal.ReadIntPtr(ptr, i * IntPtr.Size);
  32. res [i] = Runtime.LookupObject<Node> (node);
  33. }
  34. if (Component.IsDefinedInManagedCode<T>())
  35. //is not really efficient, but underlying Urho3D knows nothing about components defined in C#
  36. return res.Where(c => c.GetComponent<T>() != null).ToArray();
  37. return res;
  38. }
  39. public T CreateComponent<T> (StringHash type, CreateMode mode = CreateMode.Replicated, uint id = 0) where T:Component
  40. {
  41. Runtime.ValidateRefCounted(this);
  42. var ptr = Node_CreateComponent (handle, type.Code, mode, id);
  43. return Runtime.LookupObject<T> (ptr);
  44. }
  45. public void RemoveComponent<T> ()
  46. {
  47. Runtime.ValidateRefCounted(this);
  48. var stringHash = Runtime.LookupStringHash (typeof (T));
  49. RemoveComponent (stringHash);
  50. }
  51. public T CreateComponent<T> (CreateMode mode = CreateMode.Replicated, uint id = 0) where T:Component
  52. {
  53. Runtime.ValidateRefCounted(this);
  54. var component = Activator.CreateInstance<T>();
  55. AddComponent(component, id, mode);
  56. return component;
  57. }
  58. /// <summary>
  59. /// Add a pre-created component.
  60. /// </summary>
  61. public void AddComponent (Component component, uint id = 0)
  62. {
  63. Runtime.ValidateRefCounted(this);
  64. AddComponent (component, id, CreateMode.Replicated);
  65. }
  66. /// <summary>
  67. /// Create a child scene node (with specified ID if provided).
  68. /// </summary>
  69. public Node CreateChild (string name = "", uint id = 0, CreateMode mode = CreateMode.Replicated)
  70. {
  71. Runtime.ValidateRefCounted(this);
  72. return CreateChild (name, mode, id);
  73. }
  74. /// <summary>
  75. /// Add a child scene node at a specific index. If index is not explicitly specified or is greater than current children size, append the new child at the end.
  76. /// </summary>
  77. public void AddChild(Node node)
  78. {
  79. Runtime.ValidateRefCounted(this);
  80. AddChild(node, uint.MaxValue);
  81. }
  82. /// <summary>
  83. /// Move the scene node in the chosen transform space.
  84. /// </summary>
  85. public void Translate(Vector3 delta)
  86. {
  87. Runtime.ValidateRefCounted(this);
  88. Translate(delta, TransformSpace.Local);
  89. }
  90. public T GetComponent<T> (bool recursive = false) where T : Component
  91. {
  92. Runtime.ValidateRefCounted(this);
  93. return (T)Components.FirstOrDefault(c => c is T);
  94. }
  95. }
  96. }