Panda3D
 All Classes Functions Variables Enumerations
buttonNode.cxx
1 // Filename: buttonNode.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 "buttonNode.h"
16 #include "config_device.h"
17 #include "dataNodeTransmit.h"
18 #include "buttonEventList.h"
19 #include "dcast.h"
20 
21 TypeHandle ButtonNode::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: ButtonNode::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 ButtonNode::
29 ButtonNode(ClientBase *client, const string &device_name) :
30  DataNode(device_name)
31 {
32  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
33  _button_events = new ButtonEventList;
34 
35  nassertv(client != (ClientBase *)NULL);
36  PT(ClientDevice) device =
37  client->get_device(ClientButtonDevice::get_class_type(), device_name);
38 
39  if (device == (ClientDevice *)NULL) {
40  device_cat.warning()
41  << "Unable to open button device " << device_name << "\n";
42  return;
43  }
44 
45  if (!device->is_of_type(ClientButtonDevice::get_class_type())) {
46  device_cat.error()
47  << "Inappropriate device type " << device->get_type()
48  << " created; expected a ClientButtonDevice.\n";
49  return;
50  }
51 
52  _button = DCAST(ClientButtonDevice, device);
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: ButtonNode::Destructor
57 // Access: Public, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 ButtonNode::
61 ~ButtonNode() {
62  // When the _button pointer destructs, the ClientButtonDevice
63  // disconnects itself from the ClientBase, and everything that needs
64  // to get turned off does. Magic.
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: ButtonNode::output
69 // Access: Public, Virtual
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 void ButtonNode::
73 output(ostream &out) const {
74  DataNode::output(out);
75 
76  if (_button != (ClientButtonDevice *)NULL) {
77  out << " (";
78  _button->acquire();
79  _button->output_buttons(out);
80  _button->unlock();
81  out << ")";
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: ButtonNode::write
87 // Access: Public, Virtual
88 // Description:
89 ////////////////////////////////////////////////////////////////////
90 void ButtonNode::
91 write(ostream &out, int indent_level) const {
92  DataNode::write(out, indent_level);
93 
94  if (_button != (ClientButtonDevice *)NULL) {
95  _button->acquire();
96  _button->write_buttons(out, indent_level + 2);
97  _button->unlock();
98  }
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: ButtonNode::do_transmit_data
103 // Access: Protected, Virtual
104 // Description: The virtual implementation of transmit_data(). This
105 // function receives an array of input parameters and
106 // should generate an array of output parameters. The
107 // input parameters may be accessed with the index
108 // numbers returned by the define_input() calls that
109 // were made earlier (presumably in the constructor);
110 // likewise, the output parameters should be set with
111 // the index numbers returned by the define_output()
112 // calls.
113 ////////////////////////////////////////////////////////////////////
114 void ButtonNode::
115 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
116  DataNodeTransmit &output) {
117  if (is_valid()) {
118  _button->poll();
119  _button->acquire();
120 
121  (*_button_events) = (*_button->get_button_events());
122 
123  _button->get_button_events()->clear();
124  _button->unlock();
125 
126  output.set_data(_button_events_output, EventParameter(_button_events));
127  }
128 }
The fundamental type of node for the data graph.
Definition: dataNode.h:64
An optional parameter associated with an event.
A device, attached to the ClientBase by a ButtonNode, that records the data from a single named butto...
Records a set of button events that happened recently.
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
An abstract base class for a family of client device interfaces–including trackers, buttons, dials, and other analog inputs.
Definition: clientBase.h:47
bool is_valid() const
Returns true if the ButtonNode is valid and connected to a server, false otherwise.
Definition: buttonNode.I:23
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...