Panda3D
analogNode.cxx
1 // Filename: analogNode.cxx
2 // Created by: drose (12Mar02)
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 "analogNode.h"
16 #include "config_device.h"
17 #include "dataNodeTransmit.h"
18 #include "dcast.h"
19 
20 
21 TypeHandle AnalogNode::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: AnalogNode::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 AnalogNode::
29 AnalogNode(ClientBase *client, const string &device_name) :
30  DataNode(device_name)
31 {
32  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
33  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
34 
35  nassertv(client != (ClientBase *)NULL);
36  PT(ClientDevice) device =
37  client->get_device(ClientAnalogDevice::get_class_type(), device_name);
38 
39  if (device == (ClientDevice *)NULL) {
40  device_cat.warning()
41  << "Unable to open analog device " << device_name << "\n";
42  return;
43  }
44 
45  if (!device->is_of_type(ClientAnalogDevice::get_class_type())) {
46  device_cat.error()
47  << "Inappropriate device type " << device->get_type()
48  << " created; expected a ClientAnalogDevice.\n";
49  return;
50  }
51 
52  _analog = DCAST(ClientAnalogDevice, device);
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: AnalogNode::Destructor
57 // Access: Public, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 AnalogNode::
61 ~AnalogNode() {
62  // When the _analog pointer destructs, the ClientAnalogDevice
63  // disconnects itself from the ClientBase, and everything that needs
64  // to get turned off does. Magic.
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: AnalogNode::write
69 // Access: Public, Virtual
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 void AnalogNode::
73 write(ostream &out, int indent_level) const {
74  DataNode::write(out, indent_level);
75 
76  if (_analog != (ClientAnalogDevice *)NULL) {
77  _analog->acquire();
78  _analog->write_controls(out, indent_level + 2);
79  _analog->unlock();
80  }
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: AnalogNode::do_transmit_data
85 // Access: Protected, Virtual
86 // Description: The virtual implementation of transmit_data(). This
87 // function receives an array of input parameters and
88 // should generate an array of output parameters. The
89 // input parameters may be accessed with the index
90 // numbers returned by the define_input() calls that
91 // were made earlier (presumably in the constructor);
92 // likewise, the output parameters should be set with
93 // the index numbers returned by the define_output()
94 // 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 
104  _analog->acquire();
105  for (int i = 0; i < max_outputs; i++) {
106  if (_outputs[i]._index >= 0 &&
107  _analog->is_control_known(_outputs[i]._index)) {
108  if (_outputs[i]._flip) {
109  out[i] = -_analog->get_control_state(_outputs[i]._index);
110  } else {
111  out[i] = _analog->get_control_state(_outputs[i]._index);
112  }
113  }
114  }
115  _analog->unlock();
116  _xy->set_value(out);
117  output.set_data(_xy_output, EventParameter(_xy));
118  }
119 }
The fundamental type of node for the data graph.
Definition: dataNode.h:64
An optional parameter associated with an event.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:109
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
A device, attached to the ClientBase by a AnalogNode, that records the data from a single named analo...
An abstract base class for a family of client device interfaces–including trackers, buttons, dials, and other analog inputs.
Definition: clientBase.h:47
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
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:35
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...