Panda3D
|
00001 // Filename: xFileDataObjectArray.cxx 00002 // Created by: drose (07Oct04) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "xFileDataObjectArray.h" 00016 #include "string_utils.h" 00017 #include "indent.h" 00018 00019 TypeHandle XFileDataObjectArray::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: XFileDataObjectArray::is_complex_object 00023 // Access: Public, Virtual 00024 // Description: Returns true if this kind of data object is a complex 00025 // object that can hold nested data elements, false 00026 // otherwise. 00027 //////////////////////////////////////////////////////////////////// 00028 bool XFileDataObjectArray:: 00029 is_complex_object() const { 00030 return true; 00031 } 00032 00033 //////////////////////////////////////////////////////////////////// 00034 // Function: XFileDataObjectArray::add_element 00035 // Access: Public, Virtual 00036 // Description: Adds the indicated element as a nested data element, 00037 // if this data object type supports it. Returns true 00038 // if added successfully, false if the data object type 00039 // does not support nested data elements. 00040 //////////////////////////////////////////////////////////////////// 00041 bool XFileDataObjectArray:: 00042 add_element(XFileDataObject *element) { 00043 _nested_elements.push_back(element); 00044 return true; 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: XFileDataObjectArray::write_data 00049 // Access: Public, Virtual 00050 // Description: Writes a suitable representation of this node to an 00051 // .x file in text mode. 00052 //////////////////////////////////////////////////////////////////// 00053 void XFileDataObjectArray:: 00054 write_data(ostream &out, int indent_level, const char *separator) const { 00055 if (!_nested_elements.empty()) { 00056 bool indented = false; 00057 for (size_t i = 0; i < _nested_elements.size() - 1; i++) { 00058 XFileDataObject *object = _nested_elements[i]; 00059 if (object->is_complex_object() || 00060 _nested_elements.size() > 16) { 00061 // If we have a "complex" nested object, or more than 16 00062 // elements in the array, output it on its own line. 00063 if (indented) { 00064 out << "\n"; 00065 indented = false; 00066 } 00067 object->write_data(out, indent_level, ","); 00068 00069 } else { 00070 // Otherwise, output them all on the same line. 00071 if (!indented) { 00072 indent(out, indent_level); 00073 indented = true; 00074 } 00075 out << *object << ", "; 00076 } 00077 } 00078 00079 // The last object in the set is different, because it gets 00080 // separator instead of a semicolon, and it always gets a newline. 00081 XFileDataObject *object = _nested_elements.back(); 00082 if (object->is_complex_object()) { 00083 if (indented) { 00084 out << "\n"; 00085 } 00086 object->write_data(out, indent_level, separator); 00087 00088 } else { 00089 if (!indented) { 00090 indent(out, indent_level); 00091 } 00092 out << *object << separator << "\n"; 00093 } 00094 } 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: XFileDataObjectArray::get_num_elements 00099 // Access: Protected, Virtual 00100 // Description: Returns the number of nested data elements within the 00101 // object. This may be, e.g. the size of the array, if 00102 // it is an array. 00103 //////////////////////////////////////////////////////////////////// 00104 int XFileDataObjectArray:: 00105 get_num_elements() const { 00106 return _nested_elements.size(); 00107 } 00108 00109 //////////////////////////////////////////////////////////////////// 00110 // Function: XFileDataObjectArray::get_element 00111 // Access: Protected, Virtual 00112 // Description: Returns the nth nested data element within the 00113 // object. 00114 //////////////////////////////////////////////////////////////////// 00115 XFileDataObject *XFileDataObjectArray:: 00116 get_element(int n) { 00117 nassertr(n >= 0 && n < (int)_nested_elements.size(), NULL); 00118 return _nested_elements[n]; 00119 }