Panda3D

buttonNode.cxx

00001 // Filename: buttonNode.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 "buttonNode.h"
00016 #include "config_device.h"
00017 #include "dataNodeTransmit.h"
00018 #include "buttonEventList.h"
00019 #include "dcast.h"
00020 
00021 TypeHandle ButtonNode::_type_handle;
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: ButtonNode::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 ButtonNode::
00029 ButtonNode(ClientBase *client, const string &device_name) :
00030   DataNode(device_name)
00031 {
00032   _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
00033   _button_events = new ButtonEventList;
00034 
00035   nassertv(client != (ClientBase *)NULL);
00036   PT(ClientDevice) device =
00037     client->get_device(ClientButtonDevice::get_class_type(), device_name);
00038 
00039   if (device == (ClientDevice *)NULL) {
00040     device_cat.warning()
00041       << "Unable to open button device " << device_name << "\n";
00042     return;
00043   }
00044 
00045   if (!device->is_of_type(ClientButtonDevice::get_class_type())) {
00046     device_cat.error()
00047       << "Inappropriate device type " << device->get_type()
00048       << " created; expected a ClientButtonDevice.\n";
00049     return;
00050   }
00051 
00052   _button = DCAST(ClientButtonDevice, device);
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 //     Function: ButtonNode::Destructor
00057 //       Access: Public, Virtual
00058 //  Description:
00059 ////////////////////////////////////////////////////////////////////
00060 ButtonNode::
00061 ~ButtonNode() {
00062   // When the _button pointer destructs, the ClientButtonDevice
00063   // disconnects itself from the ClientBase, and everything that needs
00064   // to get turned off does.  Magic.
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: ButtonNode::output
00069 //       Access: Public, Virtual
00070 //  Description:
00071 ////////////////////////////////////////////////////////////////////
00072 void ButtonNode::
00073 output(ostream &out) const {
00074   DataNode::output(out);
00075 
00076   if (_button != (ClientButtonDevice *)NULL) {
00077     out << " (";
00078     _button->acquire();
00079     _button->output_buttons(out);
00080     _button->unlock();
00081     out << ")";
00082   }
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: ButtonNode::write
00087 //       Access: Public, Virtual
00088 //  Description:
00089 ////////////////////////////////////////////////////////////////////
00090 void ButtonNode::
00091 write(ostream &out, int indent_level) const {
00092   DataNode::write(out, indent_level);
00093 
00094   if (_button != (ClientButtonDevice *)NULL) {
00095     _button->acquire();
00096     _button->write_buttons(out, indent_level + 2);
00097     _button->unlock();
00098   }
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: ButtonNode::do_transmit_data
00103 //       Access: Protected, Virtual
00104 //  Description: The virtual implementation of transmit_data().  This
00105 //               function receives an array of input parameters and
00106 //               should generate an array of output parameters.  The
00107 //               input parameters may be accessed with the index
00108 //               numbers returned by the define_input() calls that
00109 //               were made earlier (presumably in the constructor);
00110 //               likewise, the output parameters should be set with
00111 //               the index numbers returned by the define_output()
00112 //               calls.
00113 ////////////////////////////////////////////////////////////////////
00114 void ButtonNode::
00115 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &, 
00116                  DataNodeTransmit &output) {
00117   if (is_valid()) {
00118     _button->poll();
00119     _button->acquire();
00120 
00121     (*_button_events) = (*_button->get_button_events());
00122 
00123     _button->get_button_events()->clear();
00124     _button->unlock();
00125 
00126     output.set_data(_button_events_output, EventParameter(_button_events));
00127   }
00128 }
 All Classes Functions Variables Enumerations