pandaNode.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Filename: pandaNode.h
  2. // Created by: drose (20Feb02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef PANDANODE_H
  19. #define PANDANODE_H
  20. #include "pandabase.h"
  21. #include "cycleData.h"
  22. #include "cycleDataReader.h"
  23. #include "cycleDataWriter.h"
  24. #include "pipelineCycler.h"
  25. #include "renderState.h"
  26. #include "typedWritable.h"
  27. #include "boundedObject.h"
  28. #include "namable.h"
  29. #include "referenceCount.h"
  30. #include "luse.h"
  31. #include "ordered_vector.h"
  32. #include "pointerTo.h"
  33. ////////////////////////////////////////////////////////////////////
  34. // Class : PandaNode
  35. // Description : A basic node of the scene graph or data graph. This
  36. // is the base class of all specialized nodes, and also
  37. // serves as a generic node with no special properties.
  38. ////////////////////////////////////////////////////////////////////
  39. class EXPCL_PANDA PandaNode : public TypedWritable,
  40. public Namable, virtual public ReferenceCount {
  41. PUBLISHED:
  42. INLINE PandaNode(const string &name);
  43. PandaNode(const PandaNode &copy);
  44. void operator = (const PandaNode &copy);
  45. virtual ~PandaNode();
  46. INLINE int get_num_parents() const;
  47. INLINE PandaNode *get_parent(int n) const;
  48. INLINE int find_parent(PandaNode *node) const;
  49. INLINE int get_num_children() const;
  50. INLINE PandaNode *get_child(int n) const;
  51. INLINE int get_sort(int n) const;
  52. int find_child(PandaNode *node) const;
  53. int add_child(PandaNode *child, int sort = 0);
  54. void remove_child(int n);
  55. bool remove_child(PandaNode *child);
  56. void remove_all_children();
  57. INLINE void set_attrib(const RenderAttrib *attrib, int override = 0);
  58. INLINE const RenderAttrib *get_attrib(TypeHandle type) const;
  59. INLINE void clear_attrib(TypeHandle type);
  60. INLINE void set_state(const RenderState *state);
  61. INLINE const RenderState *get_state() const;
  62. INLINE void clear_state();
  63. virtual void output(ostream &out) const;
  64. virtual void write(ostream &out, int indent_level) const;
  65. private:
  66. class DownConnection {
  67. public:
  68. INLINE DownConnection(PandaNode *child, int sort);
  69. INLINE bool operator < (const DownConnection &other) const;
  70. INLINE PandaNode *get_child() const;
  71. INLINE int get_sort() const;
  72. private:
  73. // Child pointers are reference counted. That way, holding a
  74. // pointer to the root of a subgraph keeps the entire subgraph
  75. // around.
  76. PT(PandaNode) _child;
  77. int _sort;
  78. };
  79. typedef ov_multiset<DownConnection> Down;
  80. // Parent pointers are not reference counted. That way, parents and
  81. // children do not circularly reference each other. In fact, parent
  82. // pointers are just simple pointers, with no additional data. We
  83. // don't really need to keep the parent pointers around, but it's
  84. // nice to be able to walk up the graph.
  85. typedef ov_set<PandaNode *> Up;
  86. // This is the data that must be cycled between pipeline stages.
  87. class CData : public CycleData {
  88. public:
  89. INLINE CData();
  90. CData(const CData &copy);
  91. virtual CycleData *make_copy() const;
  92. Down _down;
  93. Up _up;
  94. BoundedObject _node_bounds;
  95. BoundedObject _subgraph_bounds;
  96. CPT(RenderState) _state_changes;
  97. };
  98. PipelineCycler<CData> _cycler;
  99. public:
  100. // Use this interface when you want to walk through the list of
  101. // children. This saves a tiny bit of overhead between each step,
  102. // by keeping the PipelineCycler open for reading the whole time.
  103. // However, it does not protect you from self-modifying loops.
  104. class Children {
  105. public:
  106. INLINE Children(const PipelineCycler<CData> &cycler);
  107. INLINE Children(const Children &copy);
  108. INLINE int get_num_children() const;
  109. INLINE PandaNode *get_child(int n) const;
  110. private:
  111. CDR(CData) _cdata;
  112. };
  113. INLINE Children get_children() const;
  114. public:
  115. static void register_with_read_factory();
  116. virtual void write_datagram(BamWriter *manager, Datagram &dg);
  117. protected:
  118. static TypedWritable *make_from_bam(const FactoryParams &params);
  119. void fillin(DatagramIterator &scan, BamReader *manager);
  120. public:
  121. static TypeHandle get_class_type() {
  122. return _type_handle;
  123. }
  124. static void init_type() {
  125. TypedWritable::init_type();
  126. ReferenceCount::init_type();
  127. register_type(_type_handle, "PandaNode",
  128. TypedWritable::get_class_type(),
  129. ReferenceCount::get_class_type());
  130. }
  131. virtual TypeHandle get_type() const {
  132. return get_class_type();
  133. }
  134. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  135. private:
  136. static TypeHandle _type_handle;
  137. friend class PandaNode::Children;
  138. };
  139. INLINE ostream &operator << (ostream &out, const PandaNode &node) {
  140. node.output(out);
  141. return out;
  142. }
  143. #include "pandaNode.I"
  144. #endif