Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/external/ois/win32/Win32Mouse.cpp @ 10035

Last change on this file since 10035 was 9674, checked in by landauf, 11 years ago

fixed build for MinGW with GCC 4.6

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1/*
2The zlib/libpng License
3
4Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
5
6This software is provided 'as-is', without any express or implied warranty. In no event will
7the authors be held liable for any damages arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose, including commercial
10applications, and to alter it and redistribute it freely, subject to the following
11restrictions:
12
13    1. The origin of this software must not be misrepresented; you must not claim that
14                you wrote the original software. If you use this software in a product,
15                an acknowledgment in the product documentation would be appreciated but is
16                not required.
17
18    2. Altered source versions must be plainly marked as such, and must not be
19                misrepresented as being the original software.
20
21    3. This notice may not be removed or altered from any source distribution.
22*/
23#include "win32/Win32Mouse.h"
24#include "win32/Win32InputManager.h"
25#include "OISException.h"
26#include "OISEvents.h"
27#include <cstddef>
28
29using namespace OIS;
30
31//--------------------------------------------------------------------------------------------------//
32Win32Mouse::Win32Mouse( InputManager* creator, IDirectInput8* pDI, bool buffered, DWORD coopSettings )
33        : Mouse(creator->inputSystemName(), buffered, 0, creator)
34{
35        mMouse = 0;
36        mDirectInput = pDI;
37        coopSetting = coopSettings;
38        mHwnd = 0;
39
40        static_cast<Win32InputManager*>(mCreator)->_setMouseUsed(true);
41}
42
43//--------------------------------------------------------------------------------------------------//
44void Win32Mouse::_initialize()
45{
46        DIPROPDWORD dipdw;
47
48        //Clear old state
49        mState.clear();
50
51    dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
52    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
53    dipdw.diph.dwObj        = 0;
54    dipdw.diph.dwHow        = DIPH_DEVICE;
55        dipdw.dwData            = MOUSE_DX_BUFFERSIZE;
56       
57        if( FAILED(mDirectInput->CreateDevice(GUID_SysMouse, &mMouse, NULL)) )
58                OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to create device" );
59
60        if( FAILED(mMouse->SetDataFormat(&c_dfDIMouse2)) )
61                OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set format" );
62       
63        mHwnd = ((Win32InputManager*)mCreator)->getWindowHandle();
64
65        if( FAILED(mMouse->SetCooperativeLevel(mHwnd, coopSetting)) )
66                OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set coop level" );
67       
68        if( FAILED(mMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph )) )
69                OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set property" );
70
71        HRESULT hr = mMouse->Acquire();
72        if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
73                OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to aquire mouse!" );
74}
75
76//--------------------------------------------------------------------------------------------------//
77Win32Mouse::~Win32Mouse()
78{
79        if (mMouse)
80        {
81                mMouse->Unacquire();
82                mMouse->Release();
83                mMouse = 0;
84        }
85
86        static_cast<Win32InputManager*>(mCreator)->_setMouseUsed(false);
87}
88
89//--------------------------------------------------------------------------------------------------//
90void Win32Mouse::capture()
91{
92        //Clear old relative values
93        mState.X.rel = mState.Y.rel = mState.Z.rel = 0;
94
95        DIDEVICEOBJECTDATA diBuff[MOUSE_DX_BUFFERSIZE];
96        DWORD entries = MOUSE_DX_BUFFERSIZE;
97
98        HRESULT hr = mMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
99        if( hr != DI_OK )
100        {
101                hr = mMouse->Acquire();
102                while( hr == DIERR_INPUTLOST ) 
103                        hr = mMouse->Acquire();
104
105                hr = mMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
106               
107                //Perhaps the user just tabbed away, and coop settings
108                //are nonexclusive..so just ignore
109                if( FAILED(hr) )
110                        return;
111        }
112
113        bool axesMoved = false;
114        //Accumulate all axis movements for one axesMove message..
115        //Buttons are fired off as they are found
116        for(unsigned int i = 0; i < entries; ++i )
117        {
118                switch( diBuff[i].dwOfs )
119                {
120                        case DIMOFS_BUTTON0:
121                                if(!_doMouseClick(0, diBuff[i])) return;
122                                break;
123                        case DIMOFS_BUTTON1:
124                                if(!_doMouseClick(1, diBuff[i])) return;
125                                break;
126                        case DIMOFS_BUTTON2:
127                                if(!_doMouseClick(2, diBuff[i])) return;
128                                break;
129                        case DIMOFS_BUTTON3:
130                                if(!_doMouseClick(3, diBuff[i])) return;
131                                break;
132                        case DIMOFS_BUTTON4:
133                                if(!_doMouseClick(4, diBuff[i])) return;
134                                break; 
135                        case DIMOFS_BUTTON5:
136                                if(!_doMouseClick(5, diBuff[i])) return;
137                                break;
138                        case DIMOFS_BUTTON6:
139                                if(!_doMouseClick(6, diBuff[i])) return;
140                                break;
141                        case DIMOFS_BUTTON7:
142                                if(!_doMouseClick(7, diBuff[i])) return;
143                                break;
144                        case DIMOFS_X:
145                                mState.X.rel += diBuff[i].dwData;
146                                axesMoved = true;
147                                break;
148                        case DIMOFS_Y:
149                                mState.Y.rel += diBuff[i].dwData;
150                                axesMoved = true;
151                                break;
152                        case DIMOFS_Z:
153                                mState.Z.rel += diBuff[i].dwData;
154                                axesMoved = true;
155                                break;
156                        default: break;
157                } //end switch
158        }//end for
159
160        if( axesMoved )
161        {
162                if( coopSetting & DISCL_NONEXCLUSIVE )
163                {
164                        //DirectInput provides us with meaningless values, so correct that
165                        POINT point;
166                        GetCursorPos(&point);
167                        ScreenToClient(mHwnd, &point);
168                        mState.X.abs = point.x;
169                        mState.Y.abs = point.y;
170                }
171                else
172                {
173                        mState.X.abs +=  mState.X.rel;
174                        mState.Y.abs +=  mState.Y.rel;
175                }
176                mState.Z.abs +=  mState.Z.rel;
177
178                //Clip values to window
179                if( mState.X.abs < 0 )
180                        mState.X.abs = 0;
181                else if( mState.X.abs > mState.width )
182                        mState.X.abs = mState.width;
183                if( mState.Y.abs < 0 )
184                        mState.Y.abs = 0;
185                else if( mState.Y.abs > mState.height )
186                        mState.Y.abs = mState.height;
187
188                //Do the move
189                if( mListener && mBuffered )
190                        mListener->mouseMoved( MouseEvent( this, mState ) );
191        }
192}
193
194//--------------------------------------------------------------------------------------------------//
195bool Win32Mouse::_doMouseClick( int mouseButton, DIDEVICEOBJECTDATA& di )
196{
197        if( di.dwData & 0x80 )
198        {
199                mState.buttons |= 1 << mouseButton; //turn the bit flag on
200                if( mListener && mBuffered )
201                        return mListener->mousePressed( MouseEvent( this, mState ), (MouseButtonID)mouseButton );
202        }
203        else
204        {
205                mState.buttons &= ~(1 << mouseButton); //turn the bit flag off
206                if( mListener && mBuffered )
207                        return mListener->mouseReleased( MouseEvent( this, mState ), (MouseButtonID)mouseButton );
208        }
209
210        return true;
211}
212
213//--------------------------------------------------------------------------------------------------//
214void Win32Mouse::setBuffered(bool buffered)
215{
216        mBuffered = buffered;
217}
Note: See TracBrowser for help on using the repository browser.