src/keyboardEventHandler.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by                                                 *
00003  *         Pierre-yves JEZEQUEL, Julien MICHOT, Loic MOISAN,               *
00004  *         Julien PAPILLON, Sebastien PINEAUD, Barthelemy SERRES           *
00005  *                                                                         *
00006  *   https://sourceforge.net/projects/anidam                               *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  ***************************************************************************/
00023 #include "keyboardEventHandler.h"
00024 #include <iostream>
00025 
00026 bool keyboardEventHandler::addFunction(int whatKey, functionType newFunction)
00027 {
00028    if ( keyFuncMap.end() != keyFuncMap.find( whatKey ))
00029    {
00030       std::cout << "duplicate key '" << whatKey << "' ignored." << std::endl;
00031       return false;
00032    }
00033    else
00034    {
00035       keyFuncMap[whatKey].keyFunction = newFunction;
00036       return true;
00037    }
00038 }
00039 
00040 bool keyboardEventHandler::addFunction (int whatKey, keyStatusType keyPressStatus, functionType newFunction)
00041 {
00042    if (keyPressStatus == KEY_DOWN)
00043    {
00044       return addFunction(whatKey,newFunction);
00045    }
00046    else
00047    {
00048       if ( keyUPFuncMap.end() != keyUPFuncMap.find( whatKey )) 
00049       {
00050          std::cout << "duplicate key '" << whatKey << "' ignored." << std::endl;
00051          return false;
00052       }
00053       else
00054       {
00055          keyUPFuncMap[whatKey].keyFunction = newFunction;
00056          return true;
00057       }
00058    } // KEY_UP
00059 }
00060 
00061 bool keyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
00062 {
00063    bool newKeyDownEvent = false;
00064    bool newKeyUpEvent   = false;
00065 
00066    switch(ea.getEventType())
00067    {
00068    case(osgGA::GUIEventAdapter::KEYDOWN):
00069       {
00070          keyFunctionMap::iterator itr = keyFuncMap.find(ea.getKey());
00071          if (itr != keyFuncMap.end())
00072          {
00073             if ( (*itr).second.keyState == KEY_UP )
00074             {
00075                (*itr).second.keyState = KEY_DOWN;
00076                newKeyDownEvent = true;
00077             }
00078             if (newKeyDownEvent)
00079             {
00080                (*itr).second.keyFunction();
00081                newKeyDownEvent = false;
00082             }
00083             return true;
00084          }
00085          return false;
00086       }
00087    case(osgGA::GUIEventAdapter::KEYUP):
00088       {
00089          keyFunctionMap::iterator itr = keyFuncMap.find(ea.getKey());
00090          if (itr != keyFuncMap.end() )
00091          {
00092             (*itr).second.keyState = KEY_UP;
00093          }
00094          itr = keyUPFuncMap.find(ea.getKey());
00095          if (itr != keyUPFuncMap.end())
00096          {
00097             (*itr).second.keyFunction();
00098             return true;
00099          }
00100          return false; 
00101       }
00102    default:
00103       return false;
00104    }
00105 }

Generated on Tue Jun 5 16:56:48 2007 for Anidam by  doxygen 1.5.1