Panda3D
inputDeviceNode.cxx
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file InputDeviceNode.cxx
10  * @author fireclaw
11  * @date 2016-07-14
12  */
13 
14 #include "config_device.h"
15 #include "inputDeviceNode.h"
16 #include "dataNodeTransmit.h"
17 #include "inputDeviceManager.h"
18 
19 TypeHandle InputDeviceNode::_type_handle;
20 
21 InputDeviceNode::
22 InputDeviceNode(InputDevice *device, const std::string &name) :
23  DataNode(name),
24  _device(device)
25 {
26  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
27 }
28 
29 /**
30  * Redirects the class to get the data from a different device.
31  */
33 set_device(InputDevice *device) {
34  _device = device;
35 }
36 
37 /**
38  * Returns the associated device.
39  */
40 PT(InputDevice) InputDeviceNode::
41 get_device() const {
42  return _device;
43 }
44 
45 /**
46  * The virtual implementation of transmit_data(). This function receives an
47  * array of input parameters and should generate an array of output
48  * parameters. The input parameters may be accessed with the index numbers
49  * returned by the define_input() calls that were made earlier (presumably in
50  * the constructor); likewise, the output parameters should be set with the
51  * index numbers returned by the define_output() calls.
52  */
53 void InputDeviceNode::
54 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
55  DataNodeTransmit &output) {
56 
57  if (_device == nullptr && !_device->is_connected()) {
58  return;
59  }
60 
61  _device->poll();
62 
63  // get all button events of the device and forward them to the data graph
64  if (_device->has_button_event()) {
65  PT(ButtonEventList) bel = _device->get_button_events();
66 
67  // Register the current state for each button.
68  for (int i = 0; i < bel->get_num_events(); ++i) {
69  const ButtonEvent &event = bel->get_event(i);
70  if (event._type == ButtonEvent::T_down) {
71  _button_states[event._button] = true;
72  } else if (event._type == ButtonEvent::T_down) {
73  _button_states[event._button] = false;
74  }
75  }
76 
77  output.set_data(_button_events_output, EventParameter(bel));
78  }
79 }
The fundamental type of node for the data graph.
Definition: dataNode.h:52
An optional parameter associated with an event.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Records a button event of some kind.
Definition: buttonEvent.h:46
Records a set of button events that happened recently.
set_device
Redirects the class to get the data from a different device.
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
This is a structure representing a single input device.
Definition: inputDevice.h:53
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Encapsulates the data generated from (or sent into) any particular DataNode.
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...