AaronCameron.net
Because you all make me very, very tired.
Not a Member? - Login or Create an Account
Sunday the 20th of May 2012 @ 03:42pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cEventManager.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "events/cEventManager.h"
00026 
00027 #include "events/types.h"
00028 
00029 #include "events/cEventInterface.h"
00030 #include "events/cKeyEvent.h"
00031 #include "events/cMouseButtonEvent.h"
00032 #include "events/cMouseMotionEvent.h"
00033 #include "events/cQuitEvent.h"
00034 #include "events/cActiveEvent.h"
00035 #include "events/cJoystickButtonEvent.h"
00036 #include "events/cJoystickHatEvent.h"
00037 
00038 namespace n2l
00039 {
00040 
00041     tBool       cEventManager::smUseMouseMap( false );
00042 
00043     tVector2f   cEventManager::smMouseSystemDimensions( 0.0f,0.0f );
00044     tMousePos   cEventManager::smMouseLocalTopLeft( 0.0f,0.0f );
00045     tMousePos   cEventManager::smMouseLocalBottomRight( 0.0f,0.0f );
00046 
00047     tMousePos   cEventManager::smMouseOffset( 0.0f,0.0f );
00048     tVector2f   cEventManager::smMouseScaler( 1.0f,1.0f );
00049 
00050     /**************************************************************************/
00051     const tBool cEventManager::popEvent( cAutoPtr<const cEventInterface>
00052         &o_iEvent )
00053     {
00054         SDL_Event rawEvent;
00055         if  (!SDL_PollEvent(&rawEvent)) return false;
00056         // Do what we have to do
00057         switch (rawEvent.type)
00058         {
00059             //-----------------------------------------------------------
00060             case SDL_QUIT:
00061                 o_iEvent = new cQuitEvent();
00062                 break;
00063 
00064             //-----------------------------------------------------------
00065             case SDL_KEYDOWN:
00066             case SDL_KEYUP:
00067                 o_iEvent = new cKeyEvent( rawEvent.key );
00068                 break;
00069 
00070             //-----------------------------------------------------------
00071             case SDL_JOYBUTTONUP:
00072             case SDL_JOYBUTTONDOWN:
00073                 o_iEvent = new cJoystickButtonEvent( rawEvent.jbutton );
00074                 break;
00075 
00076             //-----------------------------------------------------------
00077             case SDL_JOYHATMOTION:
00078                 o_iEvent = new cJoystickHatEvent( rawEvent.jhat );
00079                 break;
00080 
00081             //-----------------------------------------------------------
00082             case SDL_MOUSEBUTTONDOWN:
00083             case SDL_MOUSEBUTTONUP:
00084                 o_iEvent = new cMouseButtonEvent( rawEvent.button );
00085                 break;
00086 
00087             //-----------------------------------------------------------
00088             case SDL_MOUSEMOTION:
00089                 o_iEvent = new cMouseMotionEvent( rawEvent.motion );
00090                 break;
00091 
00092             //-----------------------------------------------------------
00093             case SDL_ACTIVEEVENT:
00094                 o_iEvent = new cActiveEvent( rawEvent.active );
00095                 break;
00096 
00097             //-----------------------------------------------------------
00098             default:
00099                 // We have nothing to do, but there may still be
00100                 // events to process, so call ourselves again.
00101                 return popEvent( o_iEvent );
00102 
00103         } // switch (rawEvent.type)
00104 
00105 
00106         return true;
00107     }
00108 
00109     /**************************************************************************/
00110     void cEventManager::mouseMapCoordinates(const tMousePos &iTopLeft,
00111         const tMousePos &iBottomRight )
00112     {
00113         smMouseLocalTopLeft = iTopLeft;
00114         smMouseLocalBottomRight = iBottomRight;
00115         smUseMouseMap = true;
00116         updateInternalMouseValues();
00117     }
00118 
00119     /**************************************************************************/
00120     const tBool cEventManager::getMouseMapCoordinates( tMousePos &oTopLeft,
00121         tMousePos &oBottomRight )
00122     {
00123         oTopLeft = smMouseLocalTopLeft;
00124         oBottomRight = smMouseLocalBottomRight;
00125 
00126         return smUseMouseMap;
00127     }
00128 
00129     /**************************************************************************/
00130     const tMousePos &cEventManager::mousePos()
00131     {
00132         static tMousePos smMousePos(0.0f,0.0f);
00133         static tSint x,y;
00134         SDL_GetMouseState(&x,&y);
00135         smMousePos = mouseSystemToLocal(x,y);
00136         return smMousePos;
00137     }
00138 
00139     /**************************************************************************/
00140     void cEventManager::mousePos( const tMousePos &iPos,
00141         const tBool iBlockEvent )
00142     {
00143         if (iBlockEvent) {
00144 //          tMousePos posAdj = iPos - cEventManager::mousePos();
00145 
00146 //          SDL_SetEventFilter( mouseMotionFilter );
00147             SDL_WarpMouse( tUint(iPos.x()), tUint(iPos.y()) );
00148 
00149 //          SDL_MouseMotionEvent event = {
00150 //              SDL_MOUSEMOTION, 0,
00151 //              0,0,
00152 //              5,5
00153 //              tUint(iPos.x()+posAdj.x()), tUint(iPos.y()+posAdj.y()),
00154 //              tSint(posAdj.x()), tSint(posAdj.y())
00155 //          };
00156 //          SDL_PushEvent( (SDL_Event*)(&event) );
00157 
00158 //          SDL_SetEventFilter( 0 );
00159         } else
00160             SDL_WarpMouse( tUint(iPos.x()), tUint(iPos.y()) );
00161 
00162     }
00163 
00164     /**************************************************************************/
00165     const tBool cEventManager::mouseButtonDown( const tMouseButton iButton )
00166     {
00167         if (iButton<=MouseButton_Unknown ||
00168             iButton>=MouseButton_NumMouseButtons)
00169             throw cOutOfBoundsException("cEventManager::mouseButtonDown",
00170                                         "No such button");
00171         return SDL_GetMouseState(0,0)&SDL_BUTTON(iButton);
00172     }
00173 
00174     /**************************************************************************/
00175     void cEventManager::mouseSystemDimensions(  const tUint iWidth,
00176         const tUint iHeight )
00177     {
00178         smMouseSystemDimensions.set( iWidth,iHeight );
00179         updateInternalMouseValues();
00180     }
00181 
00182     /**************************************************************************/
00183     const tVector2f & cEventManager::mouseSystemToLocal( const tSint iX,
00184         const tSint iY )
00185     {
00186         static tVector2f local;
00187 
00188         // Set to the system space
00189         local.set(iX,iY);
00190         
00191         // Do we do anything?
00192         if (!smUseMouseMap) return local;
00193         
00194         // Apply our modifications
00195         local *= smMouseScaler;
00196         local += smMouseOffset;
00197         
00198         return local;
00199     }
00200 
00201     /**************************************************************************/
00202     void cEventManager::updateInternalMouseValues()
00203     {
00204         static const tVector2f One(1.0f,1.0f);
00205         if (!smUseMouseMap) return;
00206 
00207         smMouseOffset = smMouseLocalTopLeft;
00208         smMouseScaler = (smMouseLocalBottomRight-smMouseLocalTopLeft) * 
00209                         (One/smMouseSystemDimensions);
00210     }
00211 
00212     /**************************************************************************/
00213     int cEventManager::mouseMotionFilter( const SDL_Event *iEvent )
00214     {
00215         if (iEvent->type == SDL_MOUSEMOTION) return 0;
00216         return 1;
00217     }
00218     
00219 }
©2012 Aaron Cameron