Panda3D

Loading...
Searching...
No Matches
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 */
25MouseRecorder::
26MouseRecorder(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 */
49MouseRecorder::
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::
59 record_frame(BamWriter *manager, Datagram &dg) {
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::
75 play_frame(DatagramIterator &scan, BamReader *manager) {
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::
91output(std::ostream &out) const {
92 DataNode::output(out);
93}
94
95/**
96 *
97 */
98 void MouseRecorder::
99write(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::
112do_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::
180 register_with_read_factory() {
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::
190 write_datagram(BamWriter *manager, Datagram &dg) {
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::
202 write_recorder(BamWriter *manager, Datagram &dg) {
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::
213make_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::
229make_recorder(const FactoryParams &params) {
230 MouseRecorder *node = new MouseRecorder("");
231 BamReaderParam *param = DCAST(BamReaderParam, params.get_param(0));
232
233 node->fillin_recorder((DatagramIterator &)param->get_iterator(), param->get_manager());
234
235 return node;
236}
237
238/**
239 * This internal function is called by make_from_bam to read in all of the
240 * relevant data from the BamFile for the new MouseRecorder.
241 */
242 void MouseRecorder::
243fillin(DatagramIterator &scan, BamReader *manager) {
244 DataNode::fillin(scan, manager);
245}
246
247/**
248 * This internal function is called by make_from_bam to read in all of the
249 * relevant data from the BamFile for the new MouseRecorder.
250 */
251 void MouseRecorder::
252fillin_recorder(DatagramIterator &scan, BamReader *manager) {
253 RecorderBase::fillin_recorder(scan, manager);
254 DataNode::fillin_recorder(scan, manager);
255}
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
parse_params
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
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
ButtonEventList
Records a set of button events that happened recently.
Definition buttonEventList.h:33
ButtonEventList::fillin
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...
Definition buttonEventList.cxx:125
DataGraphTraverser
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...
Definition dataGraphTraverser.h:32
DataNodeTransmit
Encapsulates the data generated from (or sent into) any particular DataNode.
Definition dataNodeTransmit.h:32
DataNodeTransmit::get_data
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...
Definition dataNodeTransmit.I:52
DataNodeTransmit::has_data
bool has_data(int index) const
Returns true if the indicated parameter has been stored, false otherwise.
Definition dataNodeTransmit.I:64
DataNode
The fundamental type of node for the data graph.
Definition dataNode.h:52
DataNode::write_datagram
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
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition datagramIterator.h:30
DatagramIterator::get_bool
bool get_bool()
Extracts a boolean value.
Definition datagramIterator.I:48
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
Datagram::add_bool
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
EventParameter::get_ptr
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
Definition eventParameter.I:219
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition factoryParams.h:36
FactoryParams::get_param
FactoryParam * get_param(int n) const
Returns the nth parameter that has been added to the set.
Definition factoryParams.cxx:45
Factory::register_factory
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
MouseRecorder
This object records any data generated by a particular MouseAndKeyboard node on the datagraph for a s...
Definition mouseRecorder.h:34
MouseRecorder::write_datagram
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 mouseRecorder.cxx:190
MouseRecorder::write_recorder
virtual void write_recorder(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for encoding in the session file.
Definition mouseRecorder.cxx:202
MouseRecorder::record_frame
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 ...
Definition mouseRecorder.cxx:59
MouseRecorder::play_frame
virtual void play_frame(DatagramIterator &scan, BamReader *manager)
Reloads the most recent data collected from the indicated datagram.
Definition mouseRecorder.cxx:75
MouseRecorder::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
Definition mouseRecorder.cxx:180
Namable::output
void output(std::ostream &out) const
Outputs the Namable.
Definition namable.I:61
PandaNode::write_recorder
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:3631
ParamValue::get_value
get_value
Retrieves the value stored in the parameter.
Definition paramValue.h:115
RecorderBase
This is the base class to a number of objects that record particular kinds of user input (like a Mous...
Definition recorderBase.h:46
RecorderBase::is_playing
bool is_playing() const
Returns true if this recorder is presently playing back data from session file, false otherwise.
Definition recorderBase.I:30
RecorderBase::is_recording
bool is_recording() const
Returns true if this recorder is presently recording data for saving to a session file,...
Definition recorderBase.I:20
RecorderBase::write_recorder
virtual void write_recorder(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for encoding in the session file.
Definition recorderBase.cxx:58
RecorderController::get_factory
static RecorderFactory * get_factory()
Returns the global RecorderFactory for generating TypedWritable objects.
Definition recorderController.I:219
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition typedWritable.h:35
dataNodeTransmit.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
mouseRecorder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
recorderController.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.