Panda3D
mouseRecorder.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 mouseRecorder.cxx
10  * @author drose
11  * @date 2004-01-24
12  */
13 
14 #include "mouseRecorder.h"
15 #include "recorderController.h"
16 #include "dataNodeTransmit.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 
20 TypeHandle MouseRecorder::_type_handle;
21 
22 /**
23  *
24  */
25 MouseRecorder::
26 MouseRecorder(const std::string &name) :
27  DataNode(name)
28 {
29  _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type());
30  _pixel_size_input = define_input("pixel_size", EventStoreVec2::get_class_type());
31  _xy_input = define_input("xy", EventStoreVec2::get_class_type());
32  _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
33 
34  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
35  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
36  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
37  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
38 
39  _live_button_events = new ButtonEventList;
40  _save_button_events = new ButtonEventList;
41 
42  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
43  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
44 }
45 
46 /**
47  *
48  */
49 MouseRecorder::
50 ~MouseRecorder() {
51 }
52 
53 /**
54  * Records the most recent data collected into the indicated datagram, and
55  * returns true if there is any interesting data worth recording, or false if
56  * the datagram is meaningless.
57  */
58 void MouseRecorder::
60  nassertv(is_recording());
61  dg.add_bool(_has_mouse);
62  if (_has_mouse) {
63  _mouse_xy.write_datagram(dg);
64  _mouse_pixel_xy.write_datagram(dg);
65  }
66  _save_button_events->write_datagram(manager, dg);
67  _save_button_events->clear();
68 }
69 
70 
71 /**
72  * Reloads the most recent data collected from the indicated datagram.
73  */
74 void MouseRecorder::
76  nassertv(is_playing());
77  _has_mouse = scan.get_bool();
78  if (_has_mouse) {
79  _mouse_xy.read_datagram(scan);
80  _mouse_pixel_xy.read_datagram(scan);
81  }
82  ButtonEventList button_events;
83  button_events.fillin(scan, manager);
84  _save_button_events->add_events(button_events);
85 }
86 
87 /**
88  *
89  */
90 void MouseRecorder::
91 output(std::ostream &out) const {
92  DataNode::output(out);
93 }
94 
95 /**
96  *
97  */
98 void MouseRecorder::
99 write(std::ostream &out, int indent_level) const {
100  DataNode::write(out, indent_level);
101 }
102 
103 /**
104  * The virtual implementation of transmit_data(). This function receives an
105  * array of input parameters and should generate an array of output
106  * parameters. The input parameters may be accessed with the index numbers
107  * returned by the define_input() calls that were made earlier (presumably in
108  * the constructor); likewise, the output parameters should be set with the
109  * index numbers returned by the define_output() calls.
110  */
111 void MouseRecorder::
112 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &input,
113  DataNodeTransmit &output) {
114  bool has_mouse = false;
115  LPoint2 mouse_xy;
116  LPoint2 mouse_pixel_xy;
117 
118  _live_button_events->clear();
119 
120  if (is_playing()) {
121  // If we're playing back data, copy in the data from a previous call to
122  // play_frame().
123  has_mouse = _has_mouse;
124  mouse_xy = _mouse_xy;
125  mouse_pixel_xy = _mouse_pixel_xy;
126  _live_button_events->add_events(*_save_button_events);
127  _save_button_events->clear();
128 
129  } else {
130  // If we're not playing back data, query the data from the data graph
131 
132  if (input.has_data(_xy_input)) {
133  // The mouse is within the window. Get the current mouse position.
134  const EventStoreVec2 *xy;
135  DCAST_INTO_V(xy, input.get_data(_xy_input).get_ptr());
136  mouse_xy = xy->get_value();
137  DCAST_INTO_V(xy, input.get_data(_pixel_xy_input).get_ptr());
138  mouse_pixel_xy = xy->get_value();
139  has_mouse = true;
140  }
141 
142  // Look for button events.
143  if (input.has_data(_button_events_input)) {
144  const ButtonEventList *button_events;
145  DCAST_INTO_V(button_events, input.get_data(_button_events_input).get_ptr());
146  _live_button_events->add_events(*button_events);
147  }
148  }
149 
150  // Now rebuild the output data for our children.
151 
152  if (has_mouse) {
153  // Transmit the mouse position.
154  _pixel_xy->set_value(_mouse_pixel_xy);
155  _xy->set_value(_mouse_xy);
156  output.set_data(_xy_output, EventParameter(_xy));
157  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
158  }
159 
160  if (_live_button_events->get_num_events() != 0) {
161  output.set_data(_button_events_output, EventParameter(_live_button_events));
162  }
163 
164  if (is_recording()) {
165  // Save data for the record.
166  _has_mouse = has_mouse;
167  _mouse_xy = mouse_xy;
168  _mouse_pixel_xy = mouse_pixel_xy;
169  _save_button_events->add_events(*_live_button_events);
170  }
171 
172  // We always pass the pixel_size data through.
173  output.set_data(_pixel_size_output, input.get_data(_pixel_size_input));
174 }
175 
176 /**
177  * Tells the BamReader how to create objects of type Lens.
178  */
179 void MouseRecorder::
181  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
182  RecorderController::get_factory()->register_factory(get_class_type(), make_recorder);
183 }
184 
185 /**
186  * Writes the contents of this object to the datagram for shipping out to a
187  * Bam file.
188  */
189 void MouseRecorder::
191  DataNode::write_datagram(manager, dg);
192 }
193 
194 /**
195  * Writes the contents of this object to the datagram for encoding in the
196  * session file. This is very similar to write_datagram() for TypedWritable
197  * objects, but it is used specifically to write the Recorder object when
198  * generating the session file. In many cases, it will be the same as
199  * write_datagram().
200  */
201 void MouseRecorder::
203  RecorderBase::write_recorder(manager, dg);
204  DataNode::write_recorder(manager, dg);
205 }
206 
207 /**
208  * This function is called by the BamReader's factory when a new object of
209  * type Lens is encountered in the Bam file. It should create the Lens and
210  * extract its information from the file.
211  */
212 TypedWritable *MouseRecorder::
213 make_from_bam(const FactoryParams &params) {
214  MouseRecorder *node = new MouseRecorder("");
215  DatagramIterator scan;
216  BamReader *manager;
217 
218  parse_params(params, scan, manager);
219  node->fillin(scan, manager);
220 
221  return node;
222 }
223 
224 /**
225  * This is similar to make_from_bam(), but it is designed for loading the
226  * RecorderBase object from the session log created by a RecorderController.
227  */
228 RecorderBase *MouseRecorder::
229 make_recorder(const FactoryParams &params) {
230  MouseRecorder *node = new MouseRecorder("");
231  DatagramIterator scan;
232  BamReader *manager;
233 
234  parse_params(params, scan, manager);
235  node->fillin_recorder(scan, manager);
236 
237  return node;
238 }
239 
240 /**
241  * This internal function is called by make_from_bam to read in all of the
242  * relevant data from the BamFile for the new MouseRecorder.
243  */
244 void MouseRecorder::
245 fillin(DatagramIterator &scan, BamReader *manager) {
246  DataNode::fillin(scan, manager);
247 }
248 
249 /**
250  * This internal function is called by make_from_bam to read in all of the
251  * relevant data from the BamFile for the new MouseRecorder.
252  */
253 void MouseRecorder::
254 fillin_recorder(DatagramIterator &scan, BamReader *manager) {
255  RecorderBase::fillin_recorder(scan, manager);
256  DataNode::fillin_recorder(scan, manager);
257 }
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:309
The fundamental type of node for the data graph.
Definition: dataNode.h:52
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
bool get_bool()
Extracts a boolean value.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:110
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:3618
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 ...
get_value
Retrieves the value stored in the parameter.
Definition: paramValue.h:115
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
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...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool has_data(int index) const
Returns true if the indicated parameter has been stored, false otherwise.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:103
Records a set of button events that happened recently.
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_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:34
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
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:36
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
This is the base class to a number of objects that record particular kinds of user input (like a Mous...
Definition: recorderBase.h:46
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
virtual void write_recorder(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for encoding in the session file.
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...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
This object records any data generated by a particular MouseAndKeyboard node on the datagraph for a s...
Definition: mouseRecorder.h:34
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
bool is_playing() const
Returns true if this recorder is presently playing back data from session file, false otherwise.
Definition: recorderBase.I:30
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_recording() const
Returns true if this recorder is presently recording data for saving to a session file,...
Definition: recorderBase.I:20
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...