Node.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. var stringhash = Runtime.LookupStringHash (typeof (T));
  24. int count;
  25. var ptr = NodeHelper.urho_node_get_components (handle, stringhash.Code, recursive ? 1 : 0, out count);
  26. if (ptr == IntPtr.Zero)
  27. return ZeroArray;
  28. var res = new Node[count];
  29. for (int i = 0; i < count; i++){
  30. var node = Marshal.ReadIntPtr(ptr, i * IntPtr.Size);
  31. res [i] = Runtime.LookupObject<Node> (node);
  32. }
  33. if (Component.IsDefinedInManagedCode<T>())
  34. //is not really efficient, but underlying Urho3D knows nothing about components defined in C#
  35. return res.Where(c => c.GetComponent<T>() != null).ToArray();
  36. return res;
  37. }
  38. public T CreateComponent<T> (StringHash type, CreateMode mode = CreateMode.Replicated, uint id = 0) where T:Component
  39. {
  40. var ptr = Node_CreateComponent (handle, type.Code, mode, id);
  41. return Runtime.LookupObject<T> (ptr);
  42. }
  43. public void RemoveComponent<T> ()
  44. {
  45. var stringHash = Runtime.LookupStringHash (typeof (T));
  46. RemoveComponent (stringHash);
  47. }
  48. public T CreateComponent<T> (CreateMode mode = CreateMode.Replicated, uint id = 0) where T:Component
  49. {
  50. var component = Activator.CreateInstance<T>();
  51. AddComponent(component, id, mode);
  52. return component;
  53. }
  54. /// <summary>
  55. /// Add a pre-created component.
  56. /// </summary>
  57. public void AddComponent (Component component, uint id = 0)
  58. {
  59. AddComponent (component, id, CreateMode.Replicated);
  60. }
  61. /// <summary>
  62. /// Create a child scene node (with specified ID if provided).
  63. /// </summary>
  64. public Node CreateChild (string name = "", uint id = 0, CreateMode mode = CreateMode.Replicated)
  65. {
  66. return CreateChild (name, mode, id);
  67. }
  68. /// <summary>
  69. /// 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.
  70. /// </summary>
  71. public void AddChild(Node node)
  72. {
  73. AddChild(node, uint.MaxValue);
  74. }
  75. /// <summary>
  76. /// Move the scene node in the chosen transform space.
  77. /// </summary>
  78. public void Translate(Vector3 delta)
  79. {
  80. Translate(delta, TransformSpace.Local);
  81. }
  82. public T GetComponent<T> (bool recursive = false) where T : Component
  83. {
  84. return (T)Components.FirstOrDefault(c => c is T);
  85. }
  86. }
  87. }