MercuryInput.h

Go to the documentation of this file.
00001 #ifndef _MERCURY_INPUT_H
00002 #define _MERCURY_INPUT_H
00003 
00004 #include <map>
00005 #include "MercuryString.h"
00006 #include "MercuryVector.h"
00007 
00009 enum EventType
00010 {
00011     IET_NONE,
00012     IET_DOWN,
00013     IET_RELEASE,
00014     IET_REPEAT,
00015 };
00016 
00018 class InputEvent
00019 {
00020 public:
00021     InputEvent( ) : DeviceNumber( 0 ), ButtonNumber( 0 ), Type( IET_NONE ) { }
00022     InputEvent( int DevNum, int BtnNum, EventType IET ) :
00023         DeviceNumber( DevNum ), ButtonNumber( BtnNum ), Type ( IET )
00024         {   }
00026     int DeviceNumber;
00028     int ButtonNumber;
00030     EventType Type;
00031 };
00032 
00034 class InputDevice
00035 {
00036 public:
00037     virtual ~InputDevice() {};
00038 
00039     virtual void    Init( MString Name ) = 0;
00040     virtual bool    IsButtonDown( int ButtonNumber ) = 0;
00041     virtual int     NumButtons() = 0;
00042     virtual void    Update( );
00043     virtual InputEvent  PopLastEvent() = 0;
00044 };
00045 
00047 class InputDeviceTracker
00048 {
00049 public:
00050     ~InputDeviceTracker();
00051     bool AddDevice( MString Name, InputDevice * Device );
00052     InputDevice* ToDevice( MString Name );
00053     MString ListAvailableDevices();
00054 private:
00055     std::map < MString, InputDevice * > *m_pDevices;
00056 };
00057 
00058 extern InputDeviceTracker AutoInputDeviceTracker;
00059 
00061 class CursorDevice
00062 {
00063 public: 
00064     CursorDevice();
00065     virtual ~CursorDevice() {};
00066 
00067     virtual void GetPosition( int &x, int &y ) = 0;
00068     virtual void Update( ) = 0;
00069     static void UpdateFocus( const MString &message, void * data, void * info );
00070 protected:
00071     int m_x, m_y;
00072     int m_Xoff, m_Yoff;
00073     static bool m_bHasFocus;
00074     bool    m_bSubscribed;
00075 };
00076 
00077 extern CursorDevice * DefaultCursorDevice;
00078 
00080 class MercuryInputManager
00081 {
00082 public:
00083     MercuryInputManager( );
00084     ~MercuryInputManager( ) { };
00085 
00087     void Update();
00088 
00090     bool IsButtonDown( int DeviceNumber, int ButtonNumber )
00091     { return m_vDevices[DeviceNumber]->IsButtonDown( ButtonNumber); }
00092 
00094     bool IsButtonDown( int code );
00095 
00097     InputEvent  PopLastEvent();
00098 
00100     bool GetCursorPosition( int &x, int &y ) 
00101     { if ( DefaultCursorDevice != NULL ) {
00102     DefaultCursorDevice->GetPosition( x, y ); 
00103     return true; } return false;}
00104 
00105 private:
00106     MVector< InputDevice * > m_vDevices;
00107 };
00108 
00109 extern MercuryInputManager * INPUTMAN;
00110 
00112 class InputMappingTracker   
00113 {
00114 public:
00115     ~InputMappingTracker() 
00116     { delete m_pMappings; }
00117 
00118     void ReadInMappingsFromINI();
00119     int MakeMappingCode( const MString& Name );
00120     int MakeMappingCode( const MString& Name, const MString Default );
00121 
00123     int InputEventNameToCode( const MString & IE );
00124     int InputEventToCode( const InputEvent & IE );
00125     void CodeToHardware( int code, MVector<InputEvent> &out );
00126 private:
00127     std::map < MString, int >   *m_pMappings;
00128     std::map < MString, int >   *m_pCodes;
00129     std::map < MString, MString >   *m_pAutoCodes;
00130     MVector< MVector<InputEvent> >      m_vDemapped;
00131     int m_iCurMapNumber;
00132 };
00133 
00134 extern InputMappingTracker AutoInputMappingTracker;
00135 
00137 #define KeyMapping( x ) \
00138     const int x = AutoInputMappingTracker.MakeMappingCode( #x )
00139 
00140 #define KeyMappingWithCode( x, y ) \
00141     const int x = AutoInputMappingTracker.MakeMappingCode( #x, y )
00142 
00143 class PStack;
00145 struct InputMessageStruct
00146 {
00147     InputMessageStruct();
00148     InputMessageStruct( PStack & toload );
00149     int device;
00150     int button;
00151     int code;
00152     EventType type;
00153 };
00154 
00156 class DeviceNull : public InputDevice
00157 {
00158 public:
00159     virtual ~DeviceNull() {};
00160 
00161     virtual void    Init( MString Name ) { };
00162     virtual bool    IsButtonDown( int ButtonNumber ) { return false; }
00163     virtual int     NumButtons() { return 0; }
00164     virtual void    Update( ) { }
00165     virtual InputEvent  PopLastEvent() { return InputEvent( 0,0,IET_NONE); }
00166 };
00167 
00168 enum AllKBKeys
00169 {
00170     KB_0,
00171     KB_MOUSE1,
00172     KB_MOUSE2,
00173     KB_3,
00174     KB_4,
00175     KB_5
00176 };
00177 
00178 char KeyToChar( char cHardwareChar, bool bShiftPressed );
00179 
00180 #endif
00181 
00182 /* 
00183  * Copyright (c) 2005-2006, Charles Lohr
00184  * All rights reserved.
00185  *
00186  * Redistribution and use in source and binary forms, with or
00187  * without modification, are permitted provided that the following
00188  * conditions are met:
00189  *  -   Redistributions of source code must retain the above
00190  *      copyright notice, this list of conditions and the following disclaimer.
00191  *  -   Redistributions in binary form must reproduce the above copyright
00192  *      notice, this list of conditions and the following disclaimer in
00193  *      the documentation and/or other materials provided with the distribution.
00194  *  -   Neither the name of the Mercury Engine nor the names of its
00195  *      contributors may be used to endorse or promote products derived from
00196  *      this software without specific prior written permission.
00197  *
00198  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00199  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00200  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00201  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00202  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00203  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00204  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00205  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00206  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00207  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00208  */
00209 

Hosted by SourceForge.net Logo