Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/device/clientButtonDevice.cxx

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

Generated on Fri May 2 00:36:10 2003 for Panda by doxygen1.3