Panda3D
analogNode.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 analogNode.cxx
10  * @author drose
11  * @date 2002-03-12
12  */
13 
14 #include "analogNode.h"
15 #include "config_device.h"
16 #include "dataNodeTransmit.h"
17 #include "dcast.h"
18 
19 
20 TypeHandle AnalogNode::_type_handle;
21 
22 /**
23  *
24  */
25 AnalogNode::
26 AnalogNode(ClientBase *client, const std::string &device_name) :
27  DataNode(device_name)
28 {
29  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
30  _xy = new EventStoreVec2(LPoint2(0));
31 
32  nassertv(client != nullptr);
33  PT(ClientDevice) device =
34  client->get_device(ClientAnalogDevice::get_class_type(), device_name);
35 
36  if (device == nullptr) {
37  device_cat.warning()
38  << "Unable to open analog device " << device_name << "\n";
39  return;
40  }
41 
42  if (!device->is_of_type(ClientAnalogDevice::get_class_type())) {
43  device_cat.error()
44  << "Inappropriate device type " << device->get_type()
45  << " created; expected a ClientAnalogDevice.\n";
46  return;
47  }
48 
49  _analog = device;
50 }
51 
52 /**
53  *
54  */
55 AnalogNode::
56 AnalogNode(InputDevice *device) :
57  DataNode(device->get_name()),
58  _analog(device)
59 {
60  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
61  _xy = new EventStoreVec2(LPoint2(0));
62 
63  nassertv(device != nullptr);
64 }
65 
66 /**
67  *
68  */
69 AnalogNode::
70 ~AnalogNode() {
71  // When the _analog pointer destructs, the ClientAnalogDevice disconnects
72  // itself from the ClientBase, and everything that needs to get turned off
73  // does. Magic.
74 }
75 
76 /**
77  *
78  */
79 void AnalogNode::
80 write(std::ostream &out, int indent_level) const {
81  DataNode::write(out, indent_level);
82 
83  if (_analog != nullptr) {
84  _analog->write_axes(out, indent_level + 2);
85  }
86 }
87 
88 /**
89  * The virtual implementation of transmit_data(). This function receives an
90  * array of input parameters and should generate an array of output
91  * parameters. The input parameters may be accessed with the index numbers
92  * returned by the define_input() calls that were made earlier (presumably in
93  * the constructor); likewise, the output parameters should be set with the
94  * index numbers returned by the define_output() calls.
95  */
96 void AnalogNode::
97 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
98  DataNodeTransmit &output) {
99  if (is_valid()) {
100  _analog->poll();
101 
102  LPoint2 out(0.0f, 0.0f);
103  for (int i = 0; i < max_outputs; i++) {
104  if (_outputs[i]._index >= 0) {
105  InputDevice::AxisState state = _analog->get_axis(_outputs[i]._index);
106  if (state.known) {
107  if (_outputs[i]._flip) {
108  out[i] = -state.value;
109  } else {
110  out[i] = state.value;
111  }
112  }
113  }
114  }
115  _xy->set_value(out);
116  output.set_data(_xy_output, EventParameter(_xy));
117  }
118 }
The fundamental type of node for the data graph.
Definition: dataNode.h:52
An optional parameter associated with an event.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:103
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
This is a structure representing a single input device.
Definition: inputDevice.h:53
bool is_valid() const
Returns true if the AnalogNode is valid and connected to a server, false otherwise.
Definition: analogNode.I:28
An abstract base class for a family of client device interfaces–including trackers,...
Definition: clientBase.h:43
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Encapsulates the data generated from (or sent into) any particular DataNode.
Any of a number of different devices that might be attached to a ClientBase, including trackers,...
Definition: clientDevice.h:27
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...