event_listener.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdmisc.h"
00027 
00028 #include "nel/misc/event_listener.h"
00029 #include "nel/misc/event_server.h"
00030 #include "nel/misc/events.h"
00031 
00032 
00033 namespace NLMISC {
00034 
00035 
00036 /*
00037  * Constructor
00038  */
00039 IEventListener::IEventListener()
00040 {
00041 }
00042 
00043 // ***************************************************************************
00044 // ***************************************************************************
00045 // CEventListenerAsync
00046 // ***************************************************************************
00047 // ***************************************************************************
00048 
00049 // ***************************************************************************
00050 CEventListenerAsync::CEventListenerAsync()
00051 {
00052         _KeyArray.resize (KeyCount);
00053         _KeyDownArray.resize (KeyCount);
00054         _KeyReleaseArray.resize (KeyCount);
00055         reset ();
00056 }
00057 // ***************************************************************************
00058 void CEventListenerAsync::addToServer (CEventServer& server)
00059 {
00060         server.addListener (EventKeyDownId, this);
00061         server.addListener (EventSetFocusId, this);
00062         server.addListener (EventKeyUpId, this);
00063 }
00064 // ***************************************************************************
00065 void CEventListenerAsync::removeFromServer (CEventServer& server)
00066 {
00067         server.removeListener (EventKeyUpId, this);
00068         server.removeListener (EventKeyDownId, this);
00069         server.removeListener (EventSetFocusId, this);
00070 }
00071 // ***************************************************************************
00072 bool CEventListenerAsync::isKeyDown (TKey key) const
00073 {
00074         return _KeyArray.get(key);
00075 }
00076 
00077 // ***************************************************************************
00078 bool CEventListenerAsync::isKeyPushed (TKey key, bool release)
00079 {
00080         bool    ret= _KeyDownArray.get(key) && !(_KeyReleaseArray.get(key));
00081         if(ret && release)
00082         {
00083                 _KeyReleaseArray.set(key, true);
00084         }
00085         return ret;
00086 }
00087 
00088 // ***************************************************************************
00089 void CEventListenerAsync::operator ()(const CEvent& event)
00090 {
00091         // Key down ?
00092         if (event==EventKeyDownId)
00093         {
00094                 CEventKeyDown *pEvent=(CEventKeyDown*)&event;
00095                 _KeyArray.set (pEvent->Key);
00096                 _KeyDownArray.set (pEvent->Key);
00097                 switch(pEvent->Key)
00098                 {
00099                         case KeyRCONTROL:
00100                         case KeyLCONTROL:
00101                                 _KeyArray.set (KeyCONTROL);
00102                                 _KeyDownArray.set (KeyCONTROL);
00103                         break;
00104                         case KeyRSHIFT:
00105                         case KeyLSHIFT:
00106                                 _KeyArray.set (KeySHIFT);
00107                                 _KeyDownArray.set (KeySHIFT);
00108                         break;
00109                         case KeyRMENU:
00110                         case KeyLMENU:
00111                                 _KeyArray.set (KeyMENU);
00112                                 _KeyDownArray.set (KeyMENU);
00113                         break;
00114                         default:
00115                         break;
00116                 }
00117         }
00118         // Key up ?
00119         if (event==EventKeyUpId)
00120         {
00121                 CEventKeyUp *pEvent=(CEventKeyUp*)&event;
00122                 
00123                 _KeyArray.clear (pEvent->Key);
00124                 
00125                 switch(pEvent->Key)
00126                 {
00127                         case KeyRCONTROL:
00128                         case KeyLCONTROL:
00129                                 // Do not "raise up" the key, until someone has get the state of this key.                                              
00130                                 if (!_KeyArray[KeyLCONTROL] && !_KeyArray[KeyRCONTROL])
00131                                 {
00132                                         _KeyArray.clear(KeyCONTROL);                                    
00133 
00134                                         if(_KeyReleaseArray.get(KeyCONTROL))
00135                                         {                                               
00136                                                 _KeyDownArray.clear (KeyCONTROL);                                               
00137                                                 _KeyReleaseArray.clear (KeyCONTROL);
00138                                         }
00139                                 }
00140                         break;
00141                         case KeyRSHIFT:
00142                         case KeyLSHIFT:
00143                                 if (!_KeyArray[KeyLSHIFT] && !_KeyArray[KeyRSHIFT])
00144                                 {
00145                                         _KeyArray.clear(KeySHIFT);                                      
00146 
00147                                         if(_KeyReleaseArray.get(KeySHIFT))
00148                                         {                                               
00149                                                 _KeyDownArray.clear (KeySHIFT);                                         
00150                                                 _KeyReleaseArray.clear (KeySHIFT);
00151                                         }
00152                                 }
00153                         break;
00154                         case KeyRMENU:
00155                         case KeyLMENU:
00156                                 if (!_KeyArray[KeyLMENU] && !_KeyArray[KeyRMENU])
00157                                 {
00158                                         _KeyArray.clear(KeyMENU);                                       
00159 
00160                                         if(_KeyReleaseArray.get(KeyMENU))
00161                                         {                                               
00162                                                 _KeyDownArray.clear (KeyMENU);                                          
00163                                                 _KeyReleaseArray.clear (KeyMENU);
00164                                         }
00165                                 }
00166                         break;                  
00167                         default: break;
00168                 }
00169 
00170 
00171                 // Do not "raise up" the key, until someone has get the state of this key.
00172                 if(_KeyReleaseArray.get(pEvent->Key))
00173                 {                       
00174                         _KeyDownArray.clear (pEvent->Key);
00175                         _KeyReleaseArray.clear (pEvent->Key);
00176                 }
00177 
00178         }
00179         // Activate false ?
00180         if (event==EventSetFocusId)
00181         {
00182                 CEventSetFocus *pEvent=(CEventSetFocus *)&event;
00183                 if (!pEvent->Get)
00184                 {
00185                         // Disactive all keys
00186                         _KeyArray.clearAll ();
00187                         _KeyDownArray.clearAll ();
00188                         _KeyReleaseArray.clearAll ();                   
00189                 }
00190         }
00191 }
00192 
00193 
00194 // ***************************************************************************
00195 void CEventListenerAsync::reset ()
00196 {
00197         _KeyArray.clearAll ();
00198         _KeyDownArray.clearAll ();
00199         _KeyReleaseArray.clearAll ();
00200 }
00201 
00202 
00203 } // NLMISC

Generated on Tue Mar 16 06:24:52 2004 for NeL by doxygen 1.3.6