00001 // Filename: buttonNode.cxx 00002 // Created by: drose (12Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "buttonNode.h" 00020 #include "config_device.h" 00021 #include "dataNodeTransmit.h" 00022 #include "buttonEventList.h" 00023 #include "dcast.h" 00024 00025 TypeHandle ButtonNode::_type_handle; 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: ButtonNode::Constructor 00029 // Access: Public 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 ButtonNode:: 00033 ButtonNode(ClientBase *client, const string &device_name) : 00034 DataNode(device_name) 00035 { 00036 _button_events_output = define_output("button_events", ButtonEventList::get_class_type()); 00037 _button_events = new ButtonEventList; 00038 00039 nassertv(client != (ClientBase *)NULL); 00040 PT(ClientDevice) device = 00041 client->get_device(ClientButtonDevice::get_class_type(), device_name); 00042 00043 if (device == (ClientDevice *)NULL) { 00044 device_cat.warning() 00045 << "Unable to open button device " << device_name << "\n"; 00046 return; 00047 } 00048 00049 if (!device->is_of_type(ClientButtonDevice::get_class_type())) { 00050 device_cat.error() 00051 << "Inappropriate device type " << device->get_type() 00052 << " created; expected a ClientButtonDevice.\n"; 00053 return; 00054 } 00055 00056 _button = DCAST(ClientButtonDevice, device); 00057 } 00058 00059 //////////////////////////////////////////////////////////////////// 00060 // Function: ButtonNode::Destructor 00061 // Access: Public, Virtual 00062 // Description: 00063 //////////////////////////////////////////////////////////////////// 00064 ButtonNode:: 00065 ~ButtonNode() { 00066 // When the _button pointer destructs, the ClientButtonDevice 00067 // disconnects itself from the ClientBase, and everything that needs 00068 // to get turned off does. Magic. 00069 } 00070 00071 //////////////////////////////////////////////////////////////////// 00072 // Function: ButtonNode::output 00073 // Access: Public, Virtual 00074 // Description: 00075 //////////////////////////////////////////////////////////////////// 00076 void ButtonNode:: 00077 output(ostream &out) const { 00078 DataNode::output(out); 00079 00080 if (_button != (ClientButtonDevice *)NULL) { 00081 out << " ("; 00082 _button->lock(); 00083 _button->output_buttons(out); 00084 _button->unlock(); 00085 out << ")"; 00086 } 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: ButtonNode::write 00091 // Access: Public, Virtual 00092 // Description: 00093 //////////////////////////////////////////////////////////////////// 00094 void ButtonNode:: 00095 write(ostream &out, int indent_level) const { 00096 DataNode::write(out, indent_level); 00097 00098 if (_button != (ClientButtonDevice *)NULL) { 00099 _button->lock(); 00100 _button->write_buttons(out, indent_level + 2); 00101 _button->unlock(); 00102 } 00103 } 00104 00105 //////////////////////////////////////////////////////////////////// 00106 // Function: ButtonNode::do_transmit_data 00107 // Access: Protected, Virtual 00108 // Description: The virtual implementation of transmit_data(). This 00109 // function receives an array of input parameters and 00110 // should generate an array of output parameters. The 00111 // input parameters may be accessed with the index 00112 // numbers returned by the define_input() calls that 00113 // were made earlier (presumably in the constructor); 00114 // likewise, the output parameters should be set with 00115 // the index numbers returned by the define_output() 00116 // calls. 00117 //////////////////////////////////////////////////////////////////// 00118 void ButtonNode:: 00119 do_transmit_data(const DataNodeTransmit &, DataNodeTransmit &output) { 00120 if (is_valid()) { 00121 _button->poll(); 00122 _button->lock(); 00123 00124 (*_button_events) = (*_button->get_button_events()); 00125 00126 _button->get_button_events()->clear(); 00127 _button->unlock(); 00128 00129 output.set_data(_button_events_output, EventParameter(_button_events)); 00130 } 00131 }