Panda3D
 All Classes Functions Variables Enumerations
clientButtonDevice.cxx
1 // Filename: clientButtonDevice.cxx
2 // Created by: drose (26Jan01)
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 
16 #include "clientButtonDevice.h"
17 
18 #include "indent.h"
19 
20 TypeHandle ClientButtonDevice::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: ClientButtonDevice::Constructor
24 // Access: Protected
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 ClientButtonDevice::
28 ClientButtonDevice(ClientBase *client, const string &device_name):
29  ClientDevice(client, get_class_type(), device_name)
30 {
31  _button_events = new ButtonEventList();
32 }
33 
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: ClientButtonDevice::set_button_state
37 // Access: Public
38 // Description: Sets the state of the indicated button index, where
39 // true indicates down, and false indicates up. This
40 // may generate a ButtonEvent if the button has an
41 // associated ButtonHandle. The caller should ensure
42 // that acquire() is in effect while this call is made.
43 ////////////////////////////////////////////////////////////////////
45 set_button_state(int index, bool down) {
46  ensure_button_index(index);
47  nassertv(index >= 0 && index < (int)_buttons.size());
48  _buttons[index]._state = down ? S_down : S_up;
49 
50  ButtonHandle handle = _buttons[index]._handle;
51  if (handle != ButtonHandle::none()) {
52  _button_events->add_event(ButtonEvent(handle, down ? ButtonEvent::T_down : ButtonEvent::T_up));
53  }
54 }
55 
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: ClientButtonDevice::ensure_button_index
59 // Access: Private
60 // Description: Guarantees that there is a slot in the array for the
61 // indicated index number, by filling the array up to
62 // that index if necessary.
63 ////////////////////////////////////////////////////////////////////
64 void ClientButtonDevice::
65 ensure_button_index(int index) {
66  nassertv(index >= 0);
67 
68  _buttons.reserve(index + 1);
69  while ((int)_buttons.size() <= index) {
70  _buttons.push_back(ButtonState());
71  }
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: ClientButtonDevice::output
76 // Access: Public, Virtual
77 // Description:
78 ////////////////////////////////////////////////////////////////////
79 void ClientButtonDevice::
80 output(ostream &out) const {
81  out << get_type() << " " << get_device_name() << " (";
82  output_buttons(out);
83  out << ")";
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: ClientButtonDevice::write
88 // Access: Public, Virtual
89 // Description:
90 ////////////////////////////////////////////////////////////////////
91 void ClientButtonDevice::
92 write(ostream &out, int indent_level) const {
93  indent(out, indent_level) << get_type() << " " << get_device_name() << ":\n";
94  write_buttons(out, indent_level + 2);
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: ClientButtonDevice::output_buttons
99 // Access: Public
100 // Description: Writes a one-line string of all of the current button
101 // states.
102 ////////////////////////////////////////////////////////////////////
104 output_buttons(ostream &out) const {
105  bool any_buttons = false;
106  Buttons::const_iterator bi;
107  for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
108  const ButtonState &state = (*bi);
109  if (state._state != S_unknown) {
110  if (any_buttons) {
111  out << ", ";
112  }
113  any_buttons = true;
114  out << (int)(bi - _buttons.begin()) << "=";
115  if (state._state == S_up) {
116  out << "up";
117  } else {
118  out << "down";
119  }
120  }
121  }
122 
123  if (!any_buttons) {
124  out << "no known buttons";
125  }
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: ClientButtonDevice::write_buttons
130 // Access: Public
131 // Description: Writes a multi-line description of the current button
132 // states.
133 ////////////////////////////////////////////////////////////////////
135 write_buttons(ostream &out, int indent_level) const {
136  bool any_buttons = false;
137  Buttons::const_iterator bi;
138  for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
139  const ButtonState &state = (*bi);
140  if (state._state != S_unknown) {
141  any_buttons = true;
142 
143  indent(out, indent_level)
144  << (int)(bi - _buttons.begin()) << ". ";
145 
146  if (state._handle != ButtonHandle::none()) {
147  out << "(" << state._handle << ") ";
148  }
149 
150  if (state._state == S_up) {
151  out << "up";
152  } else {
153  out << "down";
154  }
155  out << "\n";
156  }
157  }
158 
159  if (!any_buttons) {
160  indent(out, indent_level)
161  << "(no known buttons)\n";
162  }
163 }
static ButtonHandle none()
Returns a special zero-valued ButtonHandle that is used to indicate no button.
Definition: buttonHandle.I:205
void output_buttons(ostream &out) const
Writes a one-line string of all of the current button states.
Records a button event of some kind.
Definition: buttonEvent.h:53
Records a set of button events that happened recently.
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
An abstract base class for a family of client device interfaces–including trackers, buttons, dials, and other analog inputs.
Definition: clientBase.h:47
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_button_state(int index, bool down)
Sets the state of the indicated button index, where true indicates down, and false indicates up...
const string & get_device_name() const
Returns the device name reported to the ClientBase.
Definition: clientDevice.I:62
void write_buttons(ostream &out, int indent_level) const
Writes a multi-line description of the current button states.
Any of a number of different devices that might be attached to a ClientBase, including trackers...
Definition: clientDevice.h:35