Panda3D
animGroup.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file animGroup.cxx
10  * @author drose
11  * @date 1999-02-21
12  */
13 
14 #include "animGroup.h"
15 #include "animBundle.h"
16 #include "config_chan.h"
17 
18 #include "indent.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 #include "bamReader.h"
22 #include "bamWriter.h"
23 
24 
25 #include <algorithm>
26 
27 using std::string;
28 
29 TypeHandle AnimGroup::_type_handle;
30 
31 
32 /**
33  * The default constructor is protected: don't try to create an AnimGroup
34  * without a parent. To create an AnimChannel hierarchy, you must first
35  * create an AnimBundle, and use that to create any subsequent children.
36  */
37 AnimGroup::
38 AnimGroup(const string &name) :
39  Namable(name),
40  _children(get_class_type()),
41  _root(nullptr)
42 {
43 }
44 
45 /**
46  * Creates a new AnimGroup, just like this one, without copying any children.
47  * The new copy is added to the indicated parent. Intended to be called by
48  * make_copy() only.
49  */
50 AnimGroup::
51 AnimGroup(AnimGroup *parent, const AnimGroup &copy) :
52  Namable(copy),
53  _children(get_class_type())
54 {
55  if (parent != nullptr) {
56  parent->_children.push_back(this);
57  _root = parent->_root;
58  } else {
59  _root = nullptr;
60  }
61 }
62 
63 /**
64  * Creates the AnimGroup, and adds it to the indicated parent. The only way
65  * to delete it subsequently is to delete the entire hierarchy.
66  */
67 AnimGroup::
68 AnimGroup(AnimGroup *parent, const string &name) :
69  Namable(name),
70  _children(get_class_type())
71  {
72  nassertv(parent != nullptr);
73 
74  parent->_children.push_back(this);
75  _root = parent->_root;
76 }
77 
78 /**
79  *
80  */
81 AnimGroup::
82 ~AnimGroup() {
83 }
84 
85 
86 /**
87  * Returns the number of child nodes of the group.
88  */
89 int AnimGroup::
90 get_num_children() const {
91  return _children.size();
92 }
93 
94 
95 /**
96  * Returns the nth child of the group.
97  */
99 get_child(int n) const {
100  nassertr(n >= 0 && n < (int)_children.size(), nullptr);
101  return _children[n];
102 }
103 
104 /**
105  * Returns the first child found with the indicated name, or NULL if no such
106  * child exists. This method searches only the children of this particular
107  * AnimGroup; it does not recursively search the entire graph. See also
108  * find_child().
109  */
111 get_child_named(const string &name) const {
112  Children::const_iterator ci;
113  for (ci = _children.begin(); ci != _children.end(); ++ci) {
114  AnimGroup *child = (*ci);
115  if (child->get_name() == name) {
116  return child;
117  }
118  }
119 
120  return nullptr;
121 }
122 
123 /**
124  * Returns the first descendant found with the indicated name, or NULL if no
125  * such descendant exists. This method searches the entire graph beginning at
126  * this AnimGroup; see also get_child_named().
127  */
129 find_child(const string &name) const {
130  Children::const_iterator ci;
131  for (ci = _children.begin(); ci != _children.end(); ++ci) {
132  AnimGroup *child = (*ci);
133  if (child->get_name() == name) {
134  return child;
135  }
136  AnimGroup *result = child->find_child(name);
137  if (result != nullptr) {
138  return result;
139  }
140  }
141 
142  return nullptr;
143 }
144 
145 // An STL object to sort a list of children into alphabetical order.
146 class AnimGroupAlphabeticalOrder {
147 public:
148  bool operator()(const PT(AnimGroup) &a, const PT(AnimGroup) &b) const {
149  return a->get_name() < b->get_name();
150  }
151 };
152 
153 /**
154  * Sorts the children nodes at each level of the hierarchy into alphabetical
155  * order. This should be done after creating the hierarchy, to guarantee that
156  * the correct names will match up together when the AnimBundle is later bound
157  * to a PlayerRoot.
158  */
159 void AnimGroup::
161  sort(_children.begin(), _children.end(), AnimGroupAlphabeticalOrder());
162 
163  Children::iterator ci;
164  for (ci = _children.begin(); ci != _children.end(); ++ci) {
165  (*ci)->sort_descendants();
166  }
167 }
168 
169 
170 /**
171  * Returns the TypeHandle associated with the ValueType we are concerned with.
172  * This is provided to allow a bit of run-time checking that joints and
173  * channels are matching properly in type.
174  */
176 get_value_type() const {
177  return TypeHandle::none();
178 }
179 
180 /**
181  * Writes a one-line description of the group.
182  */
183 void AnimGroup::
184 output(std::ostream &out) const {
185  out << get_type() << " " << get_name();
186 }
187 
188 /**
189  * Writes a brief description of the group and all of its descendants.
190  */
191 void AnimGroup::
192 write(std::ostream &out, int indent_level) const {
193  indent(out, indent_level) << *this;
194  if (!_children.empty()) {
195  out << " {\n";
196  write_descendants(out, indent_level + 2);
197  indent(out, indent_level) << "}";
198  }
199  out << "\n";
200 }
201 
202 /**
203  * Writes a brief description of all of the group's descendants.
204  */
205 void AnimGroup::
206 write_descendants(std::ostream &out, int indent_level) const {
207  Children::const_iterator ci;
208 
209  for (ci = _children.begin(); ci != _children.end(); ++ci) {
210  (*ci)->write(out, indent_level);
211  }
212 }
213 
214 /**
215  * Returns a copy of this object, and attaches it to the indicated parent
216  * (which may be NULL only if this is an AnimBundle). Intended to be called
217  * by copy_subtree() only.
218  */
219 AnimGroup *AnimGroup::
220 make_copy(AnimGroup *parent) const {
221  return new AnimGroup(parent, *this);
222 }
223 
224 
225 /**
226  * Returns a full copy of the subtree at this node and below.
227  */
228 PT(AnimGroup) AnimGroup::
229 copy_subtree(AnimGroup *parent) const {
230  PT(AnimGroup) new_group = make_copy(parent);
231  nassertr(new_group->get_type() == get_type(), (AnimGroup *)this);
232 
233  Children::const_iterator ci;
234  for (ci = _children.begin(); ci != _children.end(); ++ci) {
235  (*ci)->copy_subtree(new_group);
236  }
237 
238  return new_group;
239 }
240 
241 /**
242  * Function to write the important information in the particular object to a
243  * Datagram
244  */
245 void AnimGroup::
246 write_datagram(BamWriter *manager, Datagram &me) {
247  me.add_string(get_name());
248  // Write out the root
249  manager->write_pointer(me, this->_root);
250  me.add_uint16(_children.size());
251  for(int i = 0; i < (int)_children.size(); i++) {
252  manager->write_pointer(me, _children[i]);
253  }
254 }
255 
256 /**
257  * Function that reads out of the datagram (or asks manager to read) all of
258  * the data that is needed to re-create this object and stores it in the
259  * appropiate place
260  */
261 void AnimGroup::
262 fillin(DatagramIterator &scan, BamReader *manager) {
263  set_name(scan.get_string());
264  manager->read_pointer(scan);
265  _num_children = scan.get_uint16();
266  for(int i = 0; i < _num_children; i++)
267  {
268  manager->read_pointer(scan);
269  }
270 }
271 
272 /**
273  * Takes in a vector of pointes to TypedWritable objects that correspond to
274  * all the requests for pointers that this object made to BamReader.
275  */
276 int AnimGroup::
278  _root = DCAST(AnimBundle, p_list[0]);
279  for (int i = 1; i < _num_children+1; i++) {
280  if (p_list[i] == TypedWritable::Null) {
281  chan_cat->warning() << get_type().get_name()
282  << " Ignoring null child" << std::endl;
283  } else {
284  _children.push_back(DCAST(AnimGroup, p_list[i]));
285  }
286  }
287  return _num_children+1;
288 }
289 
290 /**
291  * Factory method to generate a AnimGroup object
292  */
295  AnimGroup *me = new AnimGroup;
296  DatagramIterator scan;
297  BamReader *manager;
298 
299  parse_params(params, scan, manager);
300  me->fillin(scan, manager);
301  return me;
302 }
303 
304 /**
305  * Factory method to generate a AnimGroup object
306  */
307 void AnimGroup::
310 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Takes in a vector of pointes to TypedWritable objects that correspond to all the requests for pointer...
Definition: animGroup.cxx:277
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
static void register_with_read_factory()
Factory method to generate a AnimGroup object.
Definition: animGroup.cxx:308
This is the root of an AnimChannel hierarchy.
Definition: animBundle.h:29
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write(std::ostream &out, int indent_level) const
Writes a brief description of the group and all of its descendants.
Definition: animGroup.cxx:192
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
AnimGroup * find_child(const std::string &name) const
Returns the first descendant found with the indicated name, or NULL if no such descendant exists.
Definition: animGroup.cxx:129
void sort_descendants()
Sorts the children nodes at each level of the hierarchy into alphabetical order.
Definition: animGroup.cxx:160
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_datagram(BamWriter *manager, Datagram &me)
Writes the contents of this object to the datagram for shipping out to a Bam file.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::string get_string()
Extracts a variable-length string.
get_name
Returns the name of the type.
Definition: typeHandle.h:136
virtual void output(std::ostream &out) const
Writes a one-line description of the group.
Definition: animGroup.cxx:184
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
A base class for all things which can have a name.
Definition: namable.h:26
virtual TypeHandle get_value_type() const
Returns the TypeHandle associated with the ValueType we are concerned with.
Definition: animGroup.cxx:176
This is the base class for AnimChannel and AnimBundle.
Definition: animGroup.h:33
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
get_child
Returns the nth child of the group.
Definition: animGroup.h:45
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:219
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
PT(AnimGroup) AnimGroup
Returns a full copy of the subtree at this node and below.
Definition: animGroup.cxx:228
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
AnimGroup * get_child_named(const std::string &name) const
Returns the first child found with the indicated name, or NULL if no such child exists.
Definition: animGroup.cxx:111
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317
static TypedWritable * make_AnimGroup(const FactoryParams &params)
Factory method to generate a AnimGroup object.
Definition: animGroup.cxx:294