00001 // Filename: buttonEventList.cxx 00002 // Created by: drose (12Mar02) 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 "buttonEventList.h" 00016 #include "modifierButtons.h" 00017 #include "indent.h" 00018 00019 TypeHandle ButtonEventList::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: ButtonEventList::add_events 00023 // Access: Public 00024 // Description: Appends the events from the other list onto the end 00025 // of this one. 00026 //////////////////////////////////////////////////////////////////// 00027 void ButtonEventList:: 00028 add_events(const ButtonEventList &other) { 00029 _events.reserve(_events.size() + other._events.size()); 00030 Events::const_iterator ei; 00031 for (ei = other._events.begin(); ei != other._events.end(); ++ei) { 00032 _events.push_back(*ei); 00033 } 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: ButtonEventList::update_mods 00038 // Access: Public 00039 // Description: Updates the indicated ModifierButtons object with all 00040 // of the button up/down transitions indicated in the 00041 // list. 00042 //////////////////////////////////////////////////////////////////// 00043 void ButtonEventList:: 00044 update_mods(ModifierButtons &mods) const { 00045 Events::const_iterator ei; 00046 for (ei = _events.begin(); ei != _events.end(); ++ei) { 00047 (*ei).update_mods(mods); 00048 } 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: ButtonEventList::output 00053 // Access: Public, Virtual 00054 // Description: 00055 //////////////////////////////////////////////////////////////////// 00056 void ButtonEventList:: 00057 output(ostream &out) const { 00058 if (_events.empty()) { 00059 out << "(no buttons)"; 00060 } else { 00061 Events::const_iterator ei; 00062 ei = _events.begin(); 00063 out << "(" << (*ei); 00064 ++ei; 00065 while (ei != _events.end()) { 00066 out << " " << (*ei); 00067 ++ei; 00068 } 00069 out << ")"; 00070 } 00071 } 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Function: ButtonEventList::write 00075 // Access: Public 00076 // Description: 00077 //////////////////////////////////////////////////////////////////// 00078 void ButtonEventList:: 00079 write(ostream &out, int indent_level) const { 00080 indent(out, indent_level) << _events.size() << " events:\n"; 00081 Events::const_iterator ei; 00082 for (ei = _events.begin(); ei != _events.end(); ++ei) { 00083 indent(out, indent_level + 2) << (*ei) << "\n"; 00084 } 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: ButtonEventList::register_with_read_factory 00089 // Access: Public, Static 00090 // Description: Tells the BamReader how to create objects of type 00091 // Lens. 00092 //////////////////////////////////////////////////////////////////// 00093 void ButtonEventList:: 00094 register_with_read_factory() { 00095 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: ButtonEventList::write_datagram 00100 // Access: Public, Virtual 00101 // Description: Writes the contents of this object to the datagram 00102 // for shipping out to a Bam file. 00103 //////////////////////////////////////////////////////////////////// 00104 void ButtonEventList:: 00105 write_datagram(BamWriter *manager, Datagram &dg) { 00106 TypedWritable::write_datagram(manager, dg); 00107 00108 dg.add_uint16(_events.size()); 00109 Events::const_iterator ei; 00110 for (ei = _events.begin(); ei != _events.end(); ++ei) { 00111 (*ei).write_datagram(dg); 00112 } 00113 } 00114 00115 //////////////////////////////////////////////////////////////////// 00116 // Function: ButtonEventList::make_from_bam 00117 // Access: Protected, Static 00118 // Description: This function is called by the BamReader's factory 00119 // when a new object of type Lens is encountered 00120 // in the Bam file. It should create the Lens 00121 // and extract its information from the file. 00122 //////////////////////////////////////////////////////////////////// 00123 TypedWritable *ButtonEventList:: 00124 make_from_bam(const FactoryParams ¶ms) { 00125 ButtonEventList *list = new ButtonEventList; 00126 DatagramIterator scan; 00127 BamReader *manager; 00128 00129 parse_params(params, scan, manager); 00130 list->fillin(scan, manager); 00131 00132 return list; 00133 } 00134 00135 //////////////////////////////////////////////////////////////////// 00136 // Function: ButtonEventList::fillin 00137 // Access: Public 00138 // Description: This internal function is called by make_from_bam to 00139 // read in all of the relevant data from the BamFile for 00140 // the new ButtonEventList. 00141 // 00142 // This function is normally protected, but it is 00143 // declared public in this case so that MouseRecorder 00144 // may call it to read a ButtonEventList from the middle 00145 // of a datagram. 00146 //////////////////////////////////////////////////////////////////// 00147 void ButtonEventList:: 00148 fillin(DatagramIterator &scan, BamReader *manager) { 00149 TypedWritable::fillin(scan, manager); 00150 00151 int num_events = scan.get_uint16(); 00152 _events.clear(); 00153 _events.reserve(num_events); 00154 for (int i = 0; i < num_events; i++) { 00155 ButtonEvent event; 00156 event.read_datagram(scan); 00157 _events.push_back(event); 00158 } 00159 }