Panda3D

analogNode.cxx

00001 // Filename: analogNode.cxx
00002 // Created by:  drose (12Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "analogNode.h"
00016 #include "config_device.h"
00017 #include "dataNodeTransmit.h"
00018 #include "dcast.h"
00019 
00020 
00021 TypeHandle AnalogNode::_type_handle;
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: AnalogNode::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 AnalogNode::
00029 AnalogNode(ClientBase *client, const string &device_name) :
00030   DataNode(device_name)
00031 {
00032   _xy_output = define_output("xy", EventStoreVec2::get_class_type());
00033   _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
00034 
00035   nassertv(client != (ClientBase *)NULL);
00036   PT(ClientDevice) device =
00037     client->get_device(ClientAnalogDevice::get_class_type(), device_name);
00038 
00039   if (device == (ClientDevice *)NULL) {
00040     device_cat.warning()
00041       << "Unable to open analog device " << device_name << "\n";
00042     return;
00043   }
00044 
00045   if (!device->is_of_type(ClientAnalogDevice::get_class_type())) {
00046     device_cat.error()
00047       << "Inappropriate device type " << device->get_type()
00048       << " created; expected a ClientAnalogDevice.\n";
00049     return;
00050   }
00051 
00052   _analog = DCAST(ClientAnalogDevice, device);
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 //     Function: AnalogNode::Destructor
00057 //       Access: Public, Virtual
00058 //  Description:
00059 ////////////////////////////////////////////////////////////////////
00060 AnalogNode::
00061 ~AnalogNode() {
00062   // When the _analog pointer destructs, the ClientAnalogDevice
00063   // disconnects itself from the ClientBase, and everything that needs
00064   // to get turned off does.  Magic.
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: AnalogNode::write
00069 //       Access: Public, Virtual
00070 //  Description:
00071 ////////////////////////////////////////////////////////////////////
00072 void AnalogNode::
00073 write(ostream &out, int indent_level) const {
00074   DataNode::write(out, indent_level);
00075 
00076   if (_analog != (ClientAnalogDevice *)NULL) {
00077     _analog->acquire();
00078     _analog->write_controls(out, indent_level + 2);
00079     _analog->unlock();
00080   }
00081 }
00082 
00083 ////////////////////////////////////////////////////////////////////
00084 //     Function: AnalogNode::do_transmit_data
00085 //       Access: Protected, Virtual
00086 //  Description: The virtual implementation of transmit_data().  This
00087 //               function receives an array of input parameters and
00088 //               should generate an array of output parameters.  The
00089 //               input parameters may be accessed with the index
00090 //               numbers returned by the define_input() calls that
00091 //               were made earlier (presumably in the constructor);
00092 //               likewise, the output parameters should be set with
00093 //               the index numbers returned by the define_output()
00094 //               calls.
00095 ////////////////////////////////////////////////////////////////////
00096 void AnalogNode::
00097 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &, 
00098                  DataNodeTransmit &output) {
00099   if (is_valid()) {
00100     _analog->poll();
00101 
00102     LPoint2 out(0.0f, 0.0f);
00103 
00104     _analog->acquire();
00105     for (int i = 0; i < max_outputs; i++) {
00106       if (_outputs[i]._index >= 0 &&
00107           _analog->is_control_known(_outputs[i]._index)) {
00108         if (_outputs[i]._flip) {
00109           out[i] = -_analog->get_control_state(_outputs[i]._index);
00110         } else {
00111           out[i] = _analog->get_control_state(_outputs[i]._index);
00112         }
00113       }
00114     }
00115     _analog->unlock();
00116     _xy->set_value(out);
00117     output.set_data(_xy_output, EventParameter(_xy));
00118   }
00119 }
 All Classes Functions Variables Enumerations