Panda3D

clientButtonDevice.cxx

00001 // Filename: clientButtonDevice.cxx
00002 // Created by:  drose (26Jan01)
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 
00016 #include "clientButtonDevice.h"
00017 
00018 #include "indent.h"
00019 
00020 TypeHandle ClientButtonDevice::_type_handle;
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: ClientButtonDevice::Constructor
00024 //       Access: Protected
00025 //  Description:
00026 ////////////////////////////////////////////////////////////////////
00027 ClientButtonDevice::
00028 ClientButtonDevice(ClientBase *client, const string &device_name):
00029   ClientDevice(client, get_class_type(), device_name)
00030 {
00031   _button_events = new ButtonEventList();
00032 }
00033 
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: ClientButtonDevice::set_button_state
00037 //       Access: Public
00038 //  Description: Sets the state of the indicated button index, where
00039 //               true indicates down, and false indicates up.  This
00040 //               may generate a ButtonEvent if the button has an
00041 //               associated ButtonHandle.  The caller should ensure
00042 //               that acquire() is in effect while this call is made.
00043 ////////////////////////////////////////////////////////////////////
00044 void ClientButtonDevice::
00045 set_button_state(int index, bool down) {
00046   ensure_button_index(index);
00047   nassertv(index >= 0 && index < (int)_buttons.size());
00048   _buttons[index]._state = down ? S_down : S_up;
00049 
00050   ButtonHandle handle = _buttons[index]._handle;
00051   if (handle != ButtonHandle::none()) {
00052     _button_events->add_event(ButtonEvent(handle, down ? ButtonEvent::T_down : ButtonEvent::T_up));
00053   }
00054 }
00055 
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: ClientButtonDevice::ensure_button_index
00059 //       Access: Private
00060 //  Description: Guarantees that there is a slot in the array for the
00061 //               indicated index number, by filling the array up to
00062 //               that index if necessary.
00063 ////////////////////////////////////////////////////////////////////
00064 void ClientButtonDevice::
00065 ensure_button_index(int index) {
00066   nassertv(index >= 0);
00067 
00068   _buttons.reserve(index + 1);
00069   while ((int)_buttons.size() <= index) {
00070     _buttons.push_back(ButtonState());
00071   }
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: ClientButtonDevice::output
00076 //       Access: Public, Virtual
00077 //  Description:
00078 ////////////////////////////////////////////////////////////////////
00079 void ClientButtonDevice::
00080 output(ostream &out) const {
00081   out << get_type() << " " << get_device_name() << " (";
00082   output_buttons(out);
00083   out << ")";
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: ClientButtonDevice::write
00088 //       Access: Public, Virtual
00089 //  Description:
00090 ////////////////////////////////////////////////////////////////////
00091 void ClientButtonDevice::
00092 write(ostream &out, int indent_level) const {
00093   indent(out, indent_level) << get_type() << " " << get_device_name() << ":\n";
00094   write_buttons(out, indent_level + 2);
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: ClientButtonDevice::output_buttons
00099 //       Access: Public
00100 //  Description: Writes a one-line string of all of the current button
00101 //               states.
00102 ////////////////////////////////////////////////////////////////////
00103 void ClientButtonDevice::
00104 output_buttons(ostream &out) const {
00105   bool any_buttons = false;
00106   Buttons::const_iterator bi;
00107   for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
00108     const ButtonState &state = (*bi);
00109     if (state._state != S_unknown) {
00110       if (any_buttons) {
00111         out << ", ";
00112       }
00113       any_buttons = true;
00114       out << (int)(bi - _buttons.begin()) << "=";
00115       if (state._state == S_up) {
00116         out << "up";
00117       } else {
00118         out << "down";
00119       }
00120     }
00121   }
00122 
00123   if (!any_buttons) {
00124     out << "no known buttons";
00125   }
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: ClientButtonDevice::write_buttons
00130 //       Access: Public
00131 //  Description: Writes a multi-line description of the current button
00132 //               states.
00133 ////////////////////////////////////////////////////////////////////
00134 void ClientButtonDevice::
00135 write_buttons(ostream &out, int indent_level) const {
00136   bool any_buttons = false;
00137   Buttons::const_iterator bi;
00138   for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
00139     const ButtonState &state = (*bi);
00140     if (state._state != S_unknown) {
00141       any_buttons = true;
00142 
00143       indent(out, indent_level)
00144         << (int)(bi - _buttons.begin()) << ". ";
00145 
00146       if (state._handle != ButtonHandle::none()) {
00147         out << "(" << state._handle << ") ";
00148       }
00149 
00150       if (state._state == S_up) {
00151         out << "up";
00152       } else {
00153         out << "down";
00154       }
00155       out << "\n";
00156     }
00157   }
00158 
00159   if (!any_buttons) {
00160     indent(out, indent_level)
00161       << "(no known buttons)\n";
00162   }
00163 }
 All Classes Functions Variables Enumerations