Node.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return res;
  34. }
  35. public T CreateComponent<T> (StringHash type, CreateMode mode = CreateMode.Replicated, uint id = 0) where T:Component
  36. {
  37. var ptr = Node_CreateComponent (handle, type.Code, mode, id);
  38. return Runtime.LookupObject<T> (ptr);
  39. }
  40. public void RemoveComponent<T> ()
  41. {
  42. var stringHash = Runtime.LookupStringHash (typeof (T));
  43. RemoveComponent (stringHash);
  44. }
  45. public T CreateComponent<T> (CreateMode mode = CreateMode.Replicated, uint id = 0) where T:Component, new()
  46. {
  47. var component = new T();
  48. AddComponent(component, id, mode);
  49. return component;
  50. }
  51. /// <summary>
  52. /// Add a pre-created component.
  53. /// </summary>
  54. public void AddComponent (Component component, uint id = 0)
  55. {
  56. AddComponent (component, id, CreateMode.Replicated);
  57. }
  58. /// <summary>
  59. /// Create a child scene node (with specified ID if provided).
  60. /// </summary>
  61. public Node CreateChild (string name = "", uint id = 0, CreateMode mode = CreateMode.Replicated)
  62. {
  63. return CreateChild (name, mode, id);
  64. }
  65. /// <summary>
  66. /// 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.
  67. /// </summary>
  68. public void AddChild(Node node)
  69. {
  70. AddChild(node, uint.MaxValue);
  71. }
  72. /// <summary>
  73. /// Move the scene node in the chosen transform space.
  74. /// </summary>
  75. public void Translate(Vector3 delta)
  76. {
  77. Translate(delta, TransformSpace.Local);
  78. }
  79. public T GetComponent<T> (bool recursive = false) where T : Component
  80. {
  81. return (T)Components.FirstOrDefault(c => c is T);
  82. }
  83. }
  84. }