Object3dContainer.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package min3d.core;
  2. import java.util.ArrayList;
  3. import min3d.interfaces.IObject3dContainer;
  4. public class Object3dContainer extends Object3d implements IObject3dContainer
  5. {
  6. protected ArrayList<Object3d> _children = new ArrayList<Object3d>();
  7. public Object3dContainer()
  8. {
  9. super(0, 0, false, false, false);
  10. }
  11. /**
  12. * Adds container functionality to Object3d.
  13. *
  14. * Subclass Object3dContainer instead of Object3d if you
  15. * believe you may want to add children to that object.
  16. */
  17. public Object3dContainer(int $maxVerts, int $maxFaces)
  18. {
  19. super($maxVerts, $maxFaces, true,true,true);
  20. }
  21. public Object3dContainer(int $maxVerts, int $maxFaces, Boolean $useUvs, Boolean $useNormals, Boolean $useVertexColors)
  22. {
  23. super($maxVerts, $maxFaces, $useUvs,$useNormals,$useVertexColors);
  24. }
  25. /**
  26. * This constructor is convenient for cloning purposes
  27. */
  28. public Object3dContainer(Vertices $vertices, FacesBufferedList $faces, TextureList $textures)
  29. {
  30. super($vertices, $faces, $textures);
  31. }
  32. public void addChild(Object3d $o)
  33. {
  34. _children.add($o);
  35. $o.parent(this);
  36. $o.scene(this.scene());
  37. }
  38. public void addChildAt(Object3d $o, int $index)
  39. {
  40. _children.add($index, $o);
  41. $o.parent(this);
  42. $o.scene(this.scene());
  43. }
  44. public boolean removeChild(Object3d $o)
  45. {
  46. boolean b = _children.remove($o);
  47. if (b) {
  48. $o.parent(null);
  49. $o.scene(null);
  50. }
  51. return b;
  52. }
  53. public Object3d removeChildAt(int $index)
  54. {
  55. Object3d o = _children.remove($index);
  56. if (o != null) {
  57. o.parent(null);
  58. o.scene(null);
  59. }
  60. return o;
  61. }
  62. public Object3d getChildAt(int $index)
  63. {
  64. return _children.get($index);
  65. }
  66. /**
  67. * TODO: Use better lookup
  68. */
  69. public Object3d getChildByName(String $name)
  70. {
  71. for (int i = 0; i < _children.size(); i++)
  72. {
  73. if (_children.get(i).name().equals($name)) return _children.get(i);
  74. }
  75. return null;
  76. }
  77. public int getChildIndexOf(Object3d $o)
  78. {
  79. return _children.indexOf($o);
  80. }
  81. public int numChildren()
  82. {
  83. return _children.size();
  84. }
  85. /*package-private*/
  86. ArrayList<Object3d> children()
  87. {
  88. return _children;
  89. }
  90. public Object3dContainer clone()
  91. {
  92. Vertices v = _vertices.clone();
  93. FacesBufferedList f = _faces.clone();
  94. Object3dContainer clone = new Object3dContainer(v, f, _textures);
  95. clone.position().x = position().x;
  96. clone.position().y = position().y;
  97. clone.position().z = position().z;
  98. clone.rotation().x = rotation().x;
  99. clone.rotation().y = rotation().y;
  100. clone.rotation().z = rotation().z;
  101. clone.scale().x = scale().x;
  102. clone.scale().y = scale().y;
  103. clone.scale().z = scale().z;
  104. for(int i = 0; i< this.numChildren();i++)
  105. {
  106. clone.addChild(this.getChildAt(i));
  107. }
  108. return clone;
  109. }
  110. }