Panda3D
 All Classes Functions Variables Enumerations
mouseRecorder.cxx
1 // Filename: mouseRecorder.cxx
2 // Created by: drose (24Jan04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "mouseRecorder.h"
16 #include "recorderController.h"
17 #include "dataNodeTransmit.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 
21 TypeHandle MouseRecorder::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: MouseRecorder::Constructor
25 // Access: Published
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 MouseRecorder::
29 MouseRecorder(const string &name) :
30  DataNode(name)
31 {
32  _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type());
33  _pixel_size_input = define_input("pixel_size", EventStoreVec2::get_class_type());
34  _xy_input = define_input("xy", EventStoreVec2::get_class_type());
35  _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
36 
37  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
38  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
39  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
40  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
41 
42  _live_button_events = new ButtonEventList;
43  _save_button_events = new ButtonEventList;
44 
45  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
46  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: MouseRecorder::Destructor
51 // Access: Published, Virtual
52 // Description:
53 ////////////////////////////////////////////////////////////////////
54 MouseRecorder::
55 ~MouseRecorder() {
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: MouseRecorder::record_frame
60 // Access: Public, Virtual
61 // Description: Records the most recent data collected into the
62 // indicated datagram, and returns true if there is any
63 // interesting data worth recording, or false if the
64 // datagram is meaningless.
65 ////////////////////////////////////////////////////////////////////
66 void MouseRecorder::
68  nassertv(is_recording());
69  dg.add_bool(_has_mouse);
70  if (_has_mouse) {
71  _mouse_xy.write_datagram(dg);
72  _mouse_pixel_xy.write_datagram(dg);
73  }
74  _save_button_events->write_datagram(manager, dg);
75  _save_button_events->clear();
76 }
77 
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: MouseRecorder::play_frame
81 // Access: Public, Virtual
82 // Description: Reloads the most recent data collected from the
83 // indicated datagram.
84 ////////////////////////////////////////////////////////////////////
85 void MouseRecorder::
87  nassertv(is_playing());
88  _has_mouse = scan.get_bool();
89  if (_has_mouse) {
90  _mouse_xy.read_datagram(scan);
91  _mouse_pixel_xy.read_datagram(scan);
92  }
93  ButtonEventList button_events;
94  button_events.fillin(scan, manager);
95  _save_button_events->add_events(button_events);
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: MouseRecorder::output
100 // Access: Public, Virtual
101 // Description:
102 ////////////////////////////////////////////////////////////////////
103 void MouseRecorder::
104 output(ostream &out) const {
105  DataNode::output(out);
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: MouseRecorder::write
110 // Access: Public, Virtual
111 // Description:
112 ////////////////////////////////////////////////////////////////////
113 void MouseRecorder::
114 write(ostream &out, int indent_level) const {
115  DataNode::write(out, indent_level);
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: MouseRecorder::do_transmit_data
120 // Access: Protected, Virtual
121 // Description: The virtual implementation of transmit_data(). This
122 // function receives an array of input parameters and
123 // should generate an array of output parameters. The
124 // input parameters may be accessed with the index
125 // numbers returned by the define_input() calls that
126 // were made earlier (presumably in the constructor);
127 // likewise, the output parameters should be set with
128 // the index numbers returned by the define_output()
129 // calls.
130 ////////////////////////////////////////////////////////////////////
131 void MouseRecorder::
132 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &input,
133  DataNodeTransmit &output) {
134  bool has_mouse = false;
135  LPoint2 mouse_xy;
136  LPoint2 mouse_pixel_xy;
137 
138  _live_button_events->clear();
139 
140  if (is_playing()) {
141  // If we're playing back data, copy in the data from a previous
142  // call to play_frame().
143  has_mouse = _has_mouse;
144  mouse_xy = _mouse_xy;
145  mouse_pixel_xy = _mouse_pixel_xy;
146  _live_button_events->add_events(*_save_button_events);
147  _save_button_events->clear();
148 
149  } else {
150  // If we're not playing back data, query the data from the data
151  // graph
152 
153  if (input.has_data(_xy_input)) {
154  // The mouse is within the window. Get the current mouse position.
155  const EventStoreVec2 *xy;
156  DCAST_INTO_V(xy, input.get_data(_xy_input).get_ptr());
157  mouse_xy = xy->get_value();
158  DCAST_INTO_V(xy, input.get_data(_pixel_xy_input).get_ptr());
159  mouse_pixel_xy = xy->get_value();
160  has_mouse = true;
161  }
162 
163  // Look for button events.
164  if (input.has_data(_button_events_input)) {
165  const ButtonEventList *button_events;
166  DCAST_INTO_V(button_events, input.get_data(_button_events_input).get_ptr());
167  _live_button_events->add_events(*button_events);
168  }
169  }
170 
171  // Now rebuild the output data for our children.
172 
173  if (has_mouse) {
174  // Transmit the mouse position.
175  _pixel_xy->set_value(_mouse_pixel_xy);
176  _xy->set_value(_mouse_xy);
177  output.set_data(_xy_output, EventParameter(_xy));
178  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
179  }
180 
181  if (_live_button_events->get_num_events() != 0) {
182  output.set_data(_button_events_output, EventParameter(_live_button_events));
183  }
184 
185  if (is_recording()) {
186  // Save data for the record.
187  _has_mouse = has_mouse;
188  _mouse_xy = mouse_xy;
189  _mouse_pixel_xy = mouse_pixel_xy;
190  _save_button_events->add_events(*_live_button_events);
191  }
192 
193  // We always pass the pixel_size data through.
194  output.set_data(_pixel_size_output, input.get_data(_pixel_size_input));
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: MouseRecorder::register_with_read_factory
199 // Access: Public, Static
200 // Description: Tells the BamReader how to create objects of type
201 // Lens.
202 ////////////////////////////////////////////////////////////////////
203 void MouseRecorder::
205  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
206  RecorderController::get_factory()->register_factory(get_class_type(), make_recorder);
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: MouseRecorder::write_datagram
211 // Access: Public, Virtual
212 // Description: Writes the contents of this object to the datagram
213 // for shipping out to a Bam file.
214 ////////////////////////////////////////////////////////////////////
215 void MouseRecorder::
217  DataNode::write_datagram(manager, dg);
218 }
219 
220 ////////////////////////////////////////////////////////////////////
221 // Function: MouseRecorder::write_recorder
222 // Access: Public, Virtual
223 // Description: Writes the contents of this object to the datagram
224 // for encoding in the session file. This is very
225 // similar to write_datagram() for TypedWritable
226 // objects, but it is used specifically to write the
227 // Recorder object when generating the session file. In
228 // many cases, it will be the same as write_datagram().
229 ////////////////////////////////////////////////////////////////////
230 void MouseRecorder::
232  RecorderBase::write_recorder(manager, dg);
233  DataNode::write_recorder(manager, dg);
234 }
235 
236 ////////////////////////////////////////////////////////////////////
237 // Function: MouseRecorder::make_from_bam
238 // Access: Protected, Static
239 // Description: This function is called by the BamReader's factory
240 // when a new object of type Lens is encountered
241 // in the Bam file. It should create the Lens
242 // and extract its information from the file.
243 ////////////////////////////////////////////////////////////////////
244 TypedWritable *MouseRecorder::
245 make_from_bam(const FactoryParams &params) {
246  MouseRecorder *node = new MouseRecorder("");
247  DatagramIterator scan;
248  BamReader *manager;
249 
250  parse_params(params, scan, manager);
251  node->fillin(scan, manager);
252 
253  return node;
254 }
255 
256 ////////////////////////////////////////////////////////////////////
257 // Function: MouseRecorder::make_recorder
258 // Access: Protected, Static
259 // Description: This is similar to make_from_bam(), but it is
260 // designed for loading the RecorderBase object from the
261 // session log created by a RecorderController.
262 ////////////////////////////////////////////////////////////////////
263 RecorderBase *MouseRecorder::
264 make_recorder(const FactoryParams &params) {
265  MouseRecorder *node = new MouseRecorder("");
266  DatagramIterator scan;
267  BamReader *manager;
268 
269  parse_params(params, scan, manager);
270  node->fillin_recorder(scan, manager);
271 
272  return node;
273 }
274 
275 ////////////////////////////////////////////////////////////////////
276 // Function: MouseRecorder::fillin
277 // Access: Protected
278 // Description: This internal function is called by make_from_bam to
279 // read in all of the relevant data from the BamFile for
280 // the new MouseRecorder.
281 ////////////////////////////////////////////////////////////////////
282 void MouseRecorder::
283 fillin(DatagramIterator &scan, BamReader *manager) {
284  DataNode::fillin(scan, manager);
285 }
286 
287 ////////////////////////////////////////////////////////////////////
288 // Function: MouseRecorder::fillin_recorder
289 // Access: Protected
290 // Description: This internal function is called by make_from_bam to
291 // read in all of the relevant data from the BamFile for
292 // the new MouseRecorder.
293 ////////////////////////////////////////////////////////////////////
294 void MouseRecorder::
295 fillin_recorder(DatagramIterator &scan, BamReader *manager) {
296  RecorderBase::fillin_recorder(scan, manager);
297  DataNode::fillin_recorder(scan, manager);
298 }
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: dataNode.cxx:347
The fundamental type of node for the data graph.
Definition: dataNode.h:64
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
bool get_bool()
Extracts a boolean value.
An optional parameter associated with an event.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
void write_recorder(BamWriter *manager, Datagram &dg)
This method is provided for the benefit of classes (like MouseRecorder) that inherit from PandaMode a...
Definition: pandaNode.cxx:4201
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static RecorderFactory * get_factory()
Returns the global RecorderFactory for generating TypedWritable objects.
virtual void record_frame(BamWriter *manager, Datagram &dg)
Records the most recent data collected into the indicated datagram, and returns true if there is any ...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
bool is_recording() const
Returns true if this recorder is presently recording data for saving to a session file...
Definition: recorderBase.I:25
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is called by make_from_bam to read in all of the relevant data from the BamFil...
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:109
Records a set of button events that happened recently.
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:118
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
void read_datagram(DatagramIterator &source)
Reads the vector from the Datagram using get_stdfloat().
Definition: lvecBase2.h:1161
virtual void write_recorder(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for encoding in the session file.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This is the base class to a number of objects that record particular kinds of user input (like a Mous...
Definition: recorderBase.h:55
const EventParameter & get_data(int index) const
Extracts the data for the indicated index, if it has been stored, or the empty parameter if it has no...
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
virtual void write_recorder(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for encoding in the session file.
const Type & get_value() const
Retrieves the value stored in the parameter.
Definition: paramValue.I:136
void write_datagram(Datagram &destination) const
Writes the vector to the Datagram using add_stdfloat().
Definition: lvecBase2.h:1145
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
bool is_playing() const
Returns true if this recorder is presently playing back data from session file, false otherwise...
Definition: recorderBase.I:37
This object records any data generated by a particular MouseAndKeyboard node on the datagraph for a s...
Definition: mouseRecorder.h:38
A class to retrieve the individual data elements previously stored in a Datagram. ...
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
bool has_data(int index) const
Returns true if the indicated parameter has been stored, false otherwise.
Encapsulates the data generated from (or sent into) any particular DataNode.
virtual void play_frame(DatagramIterator &scan, BamReader *manager)
Reloads the most recent data collected from the indicated datagram.
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...