Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ButtonGroup.cpp

Go to the documentation of this file.
00001 
00003 //
00004 //  Copyright (c) 2002, Perry L. Miller IV
00005 //  All rights reserved.
00006 //  BSD License: http://www.opensource.org/licenses/bsd-license.html
00007 //
00009 
00011 //
00012 //  Wrapper class for a multiple button device.
00013 //
00015 
00016 #include "vrjGaPrecompiled.h"
00017 #include "ButtonGroup.h"
00018 
00019 #include <sstream>
00020 #include <stdexcept>
00021 
00022 using namespace vrjGA;
00023 
00024 
00026 //
00027 //  Constructor.
00028 //
00030 
00031 ButtonGroup::ButtonGroup() : BaseClass(),
00032   _buttons  (),
00033   _down     ( 0 ),
00034   _pressed  ( 0 ),
00035   _released ( 0 )
00036 {
00037 }
00038 
00039 
00041 //
00042 //  Destructor.
00043 //
00045 
00046 ButtonGroup::~ButtonGroup()
00047 {
00048 }
00049 
00050 
00052 //
00053 //  Update the internal state.
00054 //
00056 
00057 void ButtonGroup::update()
00058 {
00059   // Initialize.
00060   _down = _pressed = _released = 0;
00061 
00062   // Loop though the buttons.
00063   for ( Buttons::size_type i = 0; i < _buttons.size(); ++i )
00064   {
00065     // Get the digital interface.
00066     Buttons::value_type &button = _buttons.at ( i );
00067 
00068     // Query the button's state.
00069     switch ( button->state() )
00070     {
00071       case gadget::Digital::ON:
00072         _down |= button->mask();
00073         break;
00074       case gadget::Digital::OFF:
00075         break;
00076       case gadget::Digital::TOGGLE_ON:
00077         _pressed |= button->mask();
00078         break;
00079       case gadget::Digital::TOGGLE_OFF:
00080         _released |= button->mask();
00081         break;
00082       default:
00083       {
00084         std::ostringstream message;
00085         message << "Error 2194534052: unknown state of digital interface: " << button->state();
00086         throw std::logic_error ( message.str() );
00087       }
00088     }
00089   }
00090 }
00091 
00092 
00094 //
00095 //  Small predicate that returns true if the masks are the same.
00096 //
00098 
00099 namespace vrjGA
00100 {
00101   namespace Detail
00102   {
00103     struct SameMask : public std::unary_function < ButtonDevice::Ptr *, bool >
00104     {
00105       SameMask ( unsigned long mask ) : _mask ( mask ){}
00106       bool operator () ( const ButtonDevice::Ptr &b ) const
00107       {
00108         return ( ( 0x0 != b.get() ) && ( _mask == b->mask() ) );
00109       }
00110     private:
00111       unsigned long _mask;
00112     };
00113   };
00114 };
00115 
00116 
00118 //
00119 //  Add a device.
00120 //
00122 
00123 void ButtonGroup::add ( ButtonDevice *b )
00124 {
00125   // Only valid pointers.
00126   if ( 0x0 == b )
00127     throw std::invalid_argument ( "Error 3629364095: Null pointer." );
00128 
00129   // See if another button with the same mask is already in the group.
00130   if ( _buttons.end() != std::find_if ( _buttons.begin(), _buttons.end(), Detail::SameMask ( b->mask() ) ) )
00131   {
00132     std::ostringstream message;
00133     message << "Error 1927963521: Button mask value of " 
00134             << b->mask()
00135             << " is already in the button group.";
00136     throw std::runtime_error ( message.str() );
00137   }
00138 
00139   // Add the new button device.
00140   _buttons.push_back ( b );
00141 }

Generated on Thu Aug 12 16:42:14 2004 for vrjGA by doxygen 1.3.5