JoystickEvent.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 //
00028 // C++ Implementation: JoystickEvent
00029 //
00030 // Description: 
00031 //
00032 //
00033 // Author: Pierre-Jean Bensoussan, Digtal Trainers, (C) 2008
00034 //
00035 // Copyright: See COPYING file that comes with this distribution
00036 //
00037 //
00038 #include <sofa/core/objectmodel/JoystickEvent.h>
00039 #include <assert.h>
00040 
00041 namespace sofa
00042 {
00043 
00044 namespace core
00045 {
00046 
00047 namespace objectmodel
00048 {
00049 
00050 /*************
00051  * AxisEvent
00052  *************/
00053 
00054 JoystickEvent::AxisEvent::AxisEvent( const int index, const float value )
00055     : m_index( index )
00056     , m_value( value )
00057 {
00058     assert( (value >= -1.f) && (value <= 1.f) && "Value must be in range [-1, 1]" );
00059 }
00060 
00061 
00062 
00063 int JoystickEvent::AxisEvent::getIndex() const
00064 {
00065     return m_index;
00066 }
00067 
00068 
00069 
00070 float JoystickEvent::AxisEvent::getValue() const
00071 {
00072     return m_value;
00073 }
00074 
00075 
00076 
00077 /**************
00078  * ButtonEvent
00079  **************/
00080 
00081 JoystickEvent::ButtonEvent::ButtonEvent( const int buttons )
00082 {
00083     setButtons(buttons);
00084 }
00085 
00086 
00087 
00088 void JoystickEvent::ButtonEvent::setButtons(const int buttons)
00089 {
00090     int mask = 0x1;
00091     for(int i = 0; i < 32; ++i)
00092     {
00093         m_buttons[i] = ((buttons & mask) != 0);
00094         mask *= 2;
00095     }
00096 }
00097 
00098 
00099 
00100 bool JoystickEvent::ButtonEvent::getButton(const int i) const
00101 {
00102     assert((i < 32) && "Number of joystick button out of range");
00103     return m_buttons[i];
00104 }
00105 
00106 
00107 
00108 /***********
00109  * HatEvent
00110  ***********/
00111 
00112 JoystickEvent::HatEvent::HatEvent(const int index, const State state )
00113     : m_index( index )
00114     , m_state( state )  
00115 {
00116 }
00117 
00118 
00119 
00120 int JoystickEvent::HatEvent::getIndex() const
00121 {
00122     return m_index;
00123 }
00124 
00125 
00126 
00127 JoystickEvent::HatEvent::State JoystickEvent::HatEvent::getState() const
00128 {
00129     return m_state;
00130 }
00131 
00132 
00133 
00134 /****************
00135  * JoystickEvent
00136  ***************/
00137 
00138 JoystickEvent::JoystickEvent()
00139 {
00140     buttonEvent = 0;
00141 }
00142 
00143 
00144 
00145 JoystickEvent::~JoystickEvent()
00146 {
00147     if (buttonEvent) delete buttonEvent;
00148 
00149     for (unsigned int i=0; i < axisEvents.size(); i++)
00150         delete axisEvents[i];
00151     
00152     for (unsigned int i=0; i < hatEvents.size(); i++)
00153         delete hatEvents[i];
00154 }
00155 
00156     
00157 
00158 const JoystickEvent::AxisEvent *JoystickEvent::getAxisEvent(const int index) const
00159 {
00160     assert( (index < (int)axisEvents.size()) && "AxisEvents index out of range" );
00161 
00162     for (unsigned int i = 0; i < axisEvents.size(); i++)
00163     {
00164         if (axisEvents[i]->getIndex() == index)
00165             return axisEvents[i];
00166     }
00167 
00168     return NULL;
00169 }
00170 
00171 
00172 
00173 const std::vector<JoystickEvent::AxisEvent*> &JoystickEvent::getAxisEvents(void) const
00174 {
00175     return axisEvents;
00176 }
00177 
00178 
00179 
00180 unsigned int JoystickEvent::getAxisEventsSize(void) const
00181 {
00182     return axisEvents.size();
00183 }
00184 
00185 
00186 
00187 void JoystickEvent::addAxisEvent( JoystickEvent::AxisEvent * aEvent)
00188 {
00189     axisEvents.push_back(aEvent);
00190 }
00191 
00192 
00193 
00194 JoystickEvent::ButtonEvent *JoystickEvent::getButtonEvent(void) const
00195 {
00196     return buttonEvent;
00197 }
00198 
00199 
00200 
00201 bool JoystickEvent::getButton(unsigned int buttonIndex) const
00202 {
00203     if (getButtonEvent())
00204         return getButtonEvent()->getButton(buttonIndex);
00205 
00206     return false;
00207 }
00208 
00209 
00210 
00211 void JoystickEvent::setButtonEvent( JoystickEvent::ButtonEvent * bEvent)
00212 {
00213     buttonEvent = bEvent;
00214 }
00215 
00216 
00217 
00218 const JoystickEvent::HatEvent *JoystickEvent::getHatEvent(const int index) const
00219 {
00220     assert( (index < (int)hatEvents.size()) && "HatEvents index out of range" );
00221 
00222     for (unsigned int i = 0; i < hatEvents.size(); i++)
00223     {
00224         if (hatEvents[i]->getIndex() == index)
00225             return hatEvents[i];
00226     }
00227 
00228     return NULL;
00229 }
00230 
00231 
00232 
00233 const std::vector<JoystickEvent::HatEvent *> &JoystickEvent::getHatEvents(void) const
00234 {
00235     return hatEvents;
00236 }
00237 
00238 
00239 
00240 unsigned int JoystickEvent::getHatEventsSize(void) const
00241 {
00242     return hatEvents.size();
00243 }
00244 
00245 
00246 
00247 void JoystickEvent::addHatEvent( JoystickEvent::HatEvent * hEvent)
00248 {
00249     hatEvents.push_back(hEvent);
00250 }
00251 
00252 } // namespace tree
00253 
00254 } // namespace simulation
00255 
00256 } // namespace sofa

Generated on Sun Nov 23 02:29:25 2008 for Sofa by  doxygen 1.5.7.1