Panda3D
|
00001 // Filename: mouseRecorder.cxx 00002 // Created by: drose (24Jan04) 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 "mouseRecorder.h" 00016 #include "recorderController.h" 00017 #include "dataNodeTransmit.h" 00018 #include "bamReader.h" 00019 #include "bamWriter.h" 00020 00021 TypeHandle MouseRecorder::_type_handle; 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: MouseRecorder::Constructor 00025 // Access: Published 00026 // Description: 00027 //////////////////////////////////////////////////////////////////// 00028 MouseRecorder:: 00029 MouseRecorder(const string &name) : 00030 DataNode(name) 00031 { 00032 _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type()); 00033 _xy_input = define_input("xy", EventStoreVec2::get_class_type()); 00034 _button_events_input = define_input("button_events", ButtonEventList::get_class_type()); 00035 00036 _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type()); 00037 _xy_output = define_output("xy", EventStoreVec2::get_class_type()); 00038 _button_events_output = define_output("button_events", ButtonEventList::get_class_type()); 00039 00040 _live_button_events = new ButtonEventList; 00041 _save_button_events = new ButtonEventList; 00042 00043 _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f)); 00044 _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f)); 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: MouseRecorder::Destructor 00049 // Access: Published, Virtual 00050 // Description: 00051 //////////////////////////////////////////////////////////////////// 00052 MouseRecorder:: 00053 ~MouseRecorder() { 00054 } 00055 00056 //////////////////////////////////////////////////////////////////// 00057 // Function: MouseRecorder::record_frame 00058 // Access: Public, Virtual 00059 // Description: Records the most recent data collected into the 00060 // indicated datagram, and returns true if there is any 00061 // interesting data worth recording, or false if the 00062 // datagram is meaningless. 00063 //////////////////////////////////////////////////////////////////// 00064 void MouseRecorder:: 00065 record_frame(BamWriter *manager, Datagram &dg) { 00066 nassertv(is_recording()); 00067 dg.add_bool(_has_mouse); 00068 if (_has_mouse) { 00069 _mouse_xy.write_datagram(dg); 00070 _mouse_pixel_xy.write_datagram(dg); 00071 } 00072 _save_button_events->write_datagram(manager, dg); 00073 _save_button_events->clear(); 00074 } 00075 00076 00077 //////////////////////////////////////////////////////////////////// 00078 // Function: MouseRecorder::play_frame 00079 // Access: Public, Virtual 00080 // Description: Reloads the most recent data collected from the 00081 // indicated datagram. 00082 //////////////////////////////////////////////////////////////////// 00083 void MouseRecorder:: 00084 play_frame(DatagramIterator &scan, BamReader *manager) { 00085 nassertv(is_playing()); 00086 _has_mouse = scan.get_bool(); 00087 if (_has_mouse) { 00088 _mouse_xy.read_datagram(scan); 00089 _mouse_pixel_xy.read_datagram(scan); 00090 } 00091 ButtonEventList button_events; 00092 button_events.fillin(scan, manager); 00093 _save_button_events->add_events(button_events); 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: MouseRecorder::output 00098 // Access: Public, Virtual 00099 // Description: 00100 //////////////////////////////////////////////////////////////////// 00101 void MouseRecorder:: 00102 output(ostream &out) const { 00103 DataNode::output(out); 00104 } 00105 00106 //////////////////////////////////////////////////////////////////// 00107 // Function: MouseRecorder::write 00108 // Access: Public, Virtual 00109 // Description: 00110 //////////////////////////////////////////////////////////////////// 00111 void MouseRecorder:: 00112 write(ostream &out, int indent_level) const { 00113 DataNode::write(out, indent_level); 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: MouseRecorder::do_transmit_data 00118 // Access: Protected, Virtual 00119 // Description: The virtual implementation of transmit_data(). This 00120 // function receives an array of input parameters and 00121 // should generate an array of output parameters. The 00122 // input parameters may be accessed with the index 00123 // numbers returned by the define_input() calls that 00124 // were made earlier (presumably in the constructor); 00125 // likewise, the output parameters should be set with 00126 // the index numbers returned by the define_output() 00127 // calls. 00128 //////////////////////////////////////////////////////////////////// 00129 void MouseRecorder:: 00130 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &input, 00131 DataNodeTransmit &output) { 00132 bool has_mouse = false; 00133 LPoint2 mouse_xy; 00134 LPoint2 mouse_pixel_xy; 00135 00136 _live_button_events->clear(); 00137 00138 if (is_playing()) { 00139 // If we're playing back data, copy in the data from a previous 00140 // call to play_frame(). 00141 has_mouse = _has_mouse; 00142 mouse_xy = _mouse_xy; 00143 mouse_pixel_xy = _mouse_pixel_xy; 00144 _live_button_events->add_events(*_save_button_events); 00145 _save_button_events->clear(); 00146 00147 } else { 00148 // If we're not playing back data, query the data from the data 00149 // graph 00150 00151 if (input.has_data(_xy_input)) { 00152 // The mouse is within the window. Get the current mouse position. 00153 const EventStoreVec2 *xy; 00154 DCAST_INTO_V(xy, input.get_data(_xy_input).get_ptr()); 00155 mouse_xy = xy->get_value(); 00156 DCAST_INTO_V(xy, input.get_data(_pixel_xy_input).get_ptr()); 00157 mouse_pixel_xy = xy->get_value(); 00158 has_mouse = true; 00159 } 00160 00161 // Look for button events. 00162 if (input.has_data(_button_events_input)) { 00163 const ButtonEventList *button_events; 00164 DCAST_INTO_V(button_events, input.get_data(_button_events_input).get_ptr()); 00165 _live_button_events->add_events(*button_events); 00166 } 00167 } 00168 00169 // Now rebuild the output data for our children. 00170 00171 if (has_mouse) { 00172 // Transmit the mouse position. 00173 _pixel_xy->set_value(_mouse_pixel_xy); 00174 _xy->set_value(_mouse_xy); 00175 output.set_data(_xy_output, EventParameter(_xy)); 00176 output.set_data(_pixel_xy_output, EventParameter(_pixel_xy)); 00177 } 00178 00179 if (_live_button_events->get_num_events() != 0) { 00180 output.set_data(_button_events_output, EventParameter(_live_button_events)); 00181 } 00182 00183 if (is_recording()) { 00184 // Save data for the record. 00185 _has_mouse = has_mouse; 00186 _mouse_xy = mouse_xy; 00187 _mouse_pixel_xy = mouse_pixel_xy; 00188 _save_button_events->add_events(*_live_button_events); 00189 } 00190 } 00191 00192 00193 //////////////////////////////////////////////////////////////////// 00194 // Function: MouseRecorder::register_with_read_factory 00195 // Access: Public, Static 00196 // Description: Tells the BamReader how to create objects of type 00197 // Lens. 00198 //////////////////////////////////////////////////////////////////// 00199 void MouseRecorder:: 00200 register_with_read_factory() { 00201 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00202 RecorderController::get_factory()->register_factory(get_class_type(), make_recorder); 00203 } 00204 00205 //////////////////////////////////////////////////////////////////// 00206 // Function: MouseRecorder::write_datagram 00207 // Access: Public, Virtual 00208 // Description: Writes the contents of this object to the datagram 00209 // for shipping out to a Bam file. 00210 //////////////////////////////////////////////////////////////////// 00211 void MouseRecorder:: 00212 write_datagram(BamWriter *manager, Datagram &dg) { 00213 DataNode::write_datagram(manager, dg); 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: MouseRecorder::write_recorder 00218 // Access: Public, Virtual 00219 // Description: Writes the contents of this object to the datagram 00220 // for encoding in the session file. This is very 00221 // similar to write_datagram() for TypedWritable 00222 // objects, but it is used specifically to write the 00223 // Recorder object when generating the session file. In 00224 // many cases, it will be the same as write_datagram(). 00225 //////////////////////////////////////////////////////////////////// 00226 void MouseRecorder:: 00227 write_recorder(BamWriter *manager, Datagram &dg) { 00228 RecorderBase::write_recorder(manager, dg); 00229 DataNode::write_recorder(manager, dg); 00230 } 00231 00232 //////////////////////////////////////////////////////////////////// 00233 // Function: MouseRecorder::make_from_bam 00234 // Access: Protected, Static 00235 // Description: This function is called by the BamReader's factory 00236 // when a new object of type Lens is encountered 00237 // in the Bam file. It should create the Lens 00238 // and extract its information from the file. 00239 //////////////////////////////////////////////////////////////////// 00240 TypedWritable *MouseRecorder:: 00241 make_from_bam(const FactoryParams ¶ms) { 00242 MouseRecorder *node = new MouseRecorder(""); 00243 DatagramIterator scan; 00244 BamReader *manager; 00245 00246 parse_params(params, scan, manager); 00247 node->fillin(scan, manager); 00248 00249 return node; 00250 } 00251 00252 //////////////////////////////////////////////////////////////////// 00253 // Function: MouseRecorder::make_recorder 00254 // Access: Protected, Static 00255 // Description: This is similar to make_from_bam(), but it is 00256 // designed for loading the RecorderBase object from the 00257 // session log created by a RecorderController. 00258 //////////////////////////////////////////////////////////////////// 00259 RecorderBase *MouseRecorder:: 00260 make_recorder(const FactoryParams ¶ms) { 00261 MouseRecorder *node = new MouseRecorder(""); 00262 DatagramIterator scan; 00263 BamReader *manager; 00264 00265 parse_params(params, scan, manager); 00266 node->fillin_recorder(scan, manager); 00267 00268 return node; 00269 } 00270 00271 //////////////////////////////////////////////////////////////////// 00272 // Function: MouseRecorder::fillin 00273 // Access: Protected 00274 // Description: This internal function is called by make_from_bam to 00275 // read in all of the relevant data from the BamFile for 00276 // the new MouseRecorder. 00277 //////////////////////////////////////////////////////////////////// 00278 void MouseRecorder:: 00279 fillin(DatagramIterator &scan, BamReader *manager) { 00280 DataNode::fillin(scan, manager); 00281 } 00282 00283 //////////////////////////////////////////////////////////////////// 00284 // Function: MouseRecorder::fillin_recorder 00285 // Access: Protected 00286 // Description: This internal function is called by make_from_bam to 00287 // read in all of the relevant data from the BamFile for 00288 // the new MouseRecorder. 00289 //////////////////////////////////////////////////////////////////// 00290 void MouseRecorder:: 00291 fillin_recorder(DatagramIterator &scan, BamReader *manager) { 00292 RecorderBase::fillin_recorder(scan, manager); 00293 DataNode::fillin_recorder(scan, manager); 00294 }