OpenBarnyard
 
Loading...
Searching...
No Matches
TInputDeviceMouse_Win.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
5
6//-----------------------------------------------------------------------------
7// Enables memory debugging.
8// Note: Should be the last include!
9//-----------------------------------------------------------------------------
10#include "Core/TMemoryDebugOn.h"
11
13
15
17{
18 HRESULT hr = m_poDXInputDevice->Acquire();
19
20 if ( FAILED( hr ) )
21 {
23 return TFALSE;
24 }
25
27
28 if ( hr != S_FALSE )
29 {
31 }
32
33 return TTRUE;
34}
35
37{
39 HRESULT hr = m_poDXInputDevice->Unacquire();
40 return SUCCEEDED( hr );
41}
42
44{
45 if ( m_poDXInputDevice )
46 {
47 m_poDXInputDevice->Release();
48 m_poDXInputDevice = NULL;
49 }
50}
51
53{
54 if ( IsAcquired() )
55 {
56 m_iDeltaPositionX = 0;
57 m_iDeltaPositionY = 0;
58 m_iDeltaPositionZ = 0;
59
60 HRESULT hRes = m_poDXInputDevice->Poll();
61
62 if ( SUCCEEDED( hRes ) )
63 {
64 DIMOUSESTATE state;
65 hRes = m_poDXInputDevice->GetDeviceState( sizeof( state ), &state );
66
67 if ( SUCCEEDED( hRes ) )
68 {
69 m_iDeltaPositionX += state.lX;
70 m_iPositionX += state.lX;
71 m_iDeltaPositionY += state.lY;
72 m_iPositionY += state.lY;
73 m_iDeltaPositionZ += state.lZ;
74 m_iPositionZ += state.lZ;
75
77 {
79
80 if ( !pDisplayParams->bWindowed )
81 {
82 TMath::Clip( m_iPositionX, 0, TINT( pDisplayParams->uiWidth ) );
83 TMath::Clip( m_iPositionY, 0, TINT( pDisplayParams->uiHeight ) );
84 }
85 }
86 }
87 }
88 }
89}
90
92{
93 DWORD dwItems = INFINITE;
94
95 if ( IsAcquired() )
96 {
97 HRESULT hr = m_poDXInputDevice->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), NULL, &dwItems, 0 );
98
99 if ( SUCCEEDED( hr ) )
100 {
102 return TTRUE;
103 }
104 }
105
106 return TFALSE;
107}
108
110{
111 m_dwButtonPrevious = m_dwButtonCurrent;
112 TINT iNumProcessed = ProcessRepeats( a_rEmitter, a_fDeltaTime );
113
114 DWORD dwItems = 32;
115 DIDEVICEOBJECTDATA dod[ 32 ];
116 TUtil::MemClear( dod, sizeof( dod ) );
117
118 HRESULT hRes = m_poDXInputDevice->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), dod, &dwItems, 0 );
119
120 if ( FAILED( hRes ) && hRes == ( DIERR_NOTACQUIRED | DIERR_INPUTLOST ) )
121 {
122 Unacquire();
123 Acquire();
124
125 hRes = m_poDXInputDevice->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), dod, &dwItems, 0 );
126
127 if ( hRes != DI_OK )
128 {
129 return 0;
130 }
131 }
132
133 for ( DWORD i = 0; i < dwItems; i++ )
134 {
135 switch ( dod[ i ].dwOfs )
136 {
137 case DIMOFS_BUTTON0:
138 case DIMOFS_BUTTON1:
139 case DIMOFS_BUTTON2:
140 {
141 TINT iButton =
142 ( dod[ i ].dwOfs == DIMOFS_BUTTON0 ) ? ( !m_bReverseButtons ? 0 : 1 ) :
143 ( dod[ i ].dwOfs == DIMOFS_BUTTON1 ) ? ( !m_bReverseButtons ? 1 : 0 ) :
144 2;
145
146 TUINT uiButtonFlag = ( 1 << iButton );
147
149
150 if ( dod[ i ].dwData & 0x80 )
151 {
152 TASSERT( ( m_dwButtonCurrent & uiButtonFlag ) == 0 );
153
154 if ( ( m_dwButtonCurrent & uiButtonFlag ) == 0 )
155 {
157 m_dwButtonCurrent |= uiButtonFlag;
158 }
159 }
160 else if ( ( m_dwButtonCurrent & uiButtonFlag ) != 0 )
161 {
163 m_dwButtonCurrent &= ~uiButtonFlag;
164 }
165
166 iNumProcessed += 1;
167 break;
168 }
169 }
170 }
171
172 if ( m_iDeltaPositionX != 0 || m_iDeltaPositionY != 0 )
173 {
174 a_rEmitter.Throw(
176 this,
177 0x3000b,
179 m_iDeltaPositionX,
180 m_iDeltaPositionY
181 )
182 );
183
184 iNumProcessed += 1;
185 }
186
187 if ( m_iDeltaPositionZ != 0 )
188 {
189 TTODO( "Throw scroll events" );
190 }
191
192 return iNumProcessed;
193}
194
196{
197 return m_DIDevCaps.dwButtons;
198}
199
201{
202 return m_DIDevCaps.dwAxes;
203}
204
206{
207 if ( ( 0x30000 < a_iDoodad ) && ( a_iDoodad < 0x30009 ) )
208 {
209 return ( m_dwButtonCurrent & ( 1 << ( ( a_iDoodad - 1 ) & 0x1F ) ) );
210 }
211
212 return TFALSE;
213}
214
216{
217 if ( a_iDoodad == AXIS_CURSOR )
218 {
219 if ( coord == 0 )
220 {
221 return m_iDeltaPositionX;
222 }
223
224 if ( coord == 1 )
225 {
226 return m_iDeltaPositionY;
227 }
228 }
229
230 return 0;
231}
232
234{
235 if ( a_iDoodad == AXIS_CURSOR )
236 {
237 if ( coord == 0 )
238 {
239 return TFLOAT( m_iDeltaPositionX );
240 }
241
242 if ( coord == 1 )
243 {
244 return TFLOAT( m_iDeltaPositionY );
245 }
246 }
247 else if ( a_iDoodad == AXIS_WHEEL )
248 {
249 return TFLOAT( m_iDeltaPositionZ );
250 }
251
252 return 0.0f;
253}
254
256{
257 return TTRUE;
258}
259
261{
262 m_bReverseButtons = TFALSE;
263 m_dwButtonCurrent = 0;
264 m_dwButtonPrevious = 0;
265
266 POINT cursorPos;
267 GetCursorPos( &cursorPos );
268
269 m_iPositionX = cursorPos.x;
270 m_iPositionY = cursorPos.y;
271 m_iPositionZ = 0;
272 m_iDeltaPositionX = 0;
273 m_iDeltaPositionY = 0;
274 m_iDeltaPositionZ = 0;
275 m_iUnk1 = 0;
276 m_iUnk2 = 0;
277 m_iUnk3 = 0;
278 m_iUnk4 = 0;
279 m_iUnk5 = 0;
280 m_iUnk6 = -1;
281 m_iUnk7 = -1;
282
283 DIPROPDWORD dwordProperty;
284 dwordProperty.diph.dwSize = sizeof( dwordProperty );
285 dwordProperty.diph.dwHeaderSize = sizeof( DIPROPHEADER );
286 dwordProperty.diph.dwObj = DIMOFS_Z;
287 dwordProperty.diph.dwHow = DIPH_BYOFFSET;
288
289 HRESULT hr = m_poDXInputDevice->GetProperty( DIPROP_GRANULARITY, &dwordProperty.diph );
290
291 if ( hr != DIERR_OBJECTNOTFOUND )
292 {
293 m_bInitiliased = ( hr != DI_OK ) - 1 & dwordProperty.diph.dwHeaderSize;
294 return TTRUE;
295 }
296
297 m_bInitiliased = FALSE;
298 return TTRUE;
299}
300
306
308{
309 if ( IsAcquired() )
310 {
311 m_dwButtonPrevious = m_dwButtonCurrent;
312 m_dwButtonCurrent = 0;
313
314 if ( SUCCEEDED( m_poDXInputDevice->Poll() ) )
315 {
316 DIMOUSESTATE mouseState;
317 HRESULT hRes = m_poDXInputDevice->GetDeviceState( sizeof( DIMOUSESTATE ), &mouseState );
318
319 if ( SUCCEEDED( hRes ) )
320 {
321 if ( mouseState.rgbButtons[ 0 ] & 0x80 ) // LeftClick
322 m_dwButtonCurrent |= m_bReverseButtons ? 2 : 1;
323
324 if ( mouseState.rgbButtons[ 1 ] & 0x80 ) // RightClick
325 m_dwButtonCurrent |= m_bReverseButtons ? 1 : 2;
326
327 if ( mouseState.rgbButtons[ 2 ] & 0x80 ) // Wheeling yeeet
328 m_dwButtonCurrent |= INPUT_DEVICE_MOUSE_WHEEL;
329 }
330 }
331 }
332}
333
335{
336 if ( ( 0x30000 < a_iDoodad ) && ( a_iDoodad < 0x30009 ) )
337 {
338 return ( m_dwButtonPrevious & ( ( a_iDoodad - 1 ) & 0x1F ) );
339 }
340
341 return TFALSE;
342}
343
344void TInputDXDeviceMouse::SetExclusive( HWND a_hWindow, TBOOL a_bExclusive )
345{
346 m_poDXInputDevice->SetCooperativeLevel(
347 a_hWindow,
348 a_bExclusive ? ( DISCL_EXCLUSIVE | DISCL_FOREGROUND ) : ( DISCL_NONEXCLUSIVE | DISCL_FOREGROUND )
349 );
350}
351
353{
355
356 TMath::Clip( m_iPositionX, 0, TINT( pDisplayParams->uiWidth ) );
357 TMath::Clip( m_iPositionY, 0, TINT( pDisplayParams->uiHeight ) );
358}
359
360TBOOL const TInputDXDeviceMouse::BindToDIDevice( HWND a_hMainWindow, LPCDIDEVICEINSTANCEA a_poDeviceInstance, IDirectInputDevice8A* a_poDXInputDevice, TBOOL a_bExclusive )
361{
362 TASSERT( a_poDeviceInstance != NULL );
363 TASSERT( a_poDXInputDevice != NULL );
364
365 Release();
366 TUtil::MemCopy( &m_oDeviceInstance, a_poDeviceInstance, sizeof( DIDEVICEINSTANCEA ) );
367
368 m_poDXInputDevice = a_poDXInputDevice;
369
370 m_DIDevCaps = { sizeof( DIDEVCAPS ) };
371 m_poDXInputDevice->GetCapabilities( &m_DIDevCaps );
372
373 HRESULT hRes = m_poDXInputDevice->SetDataFormat( &c_dfDIMouse );
374
375 if ( hRes != DI_OK )
376 return TFALSE;
377
378 if ( a_hMainWindow )
379 {
380 m_poDXInputDevice->SetCooperativeLevel( a_hMainWindow, DISCL_FOREGROUND | ( a_bExclusive ? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE ) );
381 }
382
383 DIPROPDWORD dipdw{};
384 dipdw.diph.dwSize = sizeof( DIPROPDWORD );
385 dipdw.diph.dwHeaderSize = sizeof( DIPROPHEADER );
386 dipdw.diph.dwObj = 0;
387 dipdw.diph.dwHow = DIPH_DEVICE;
388 dipdw.dwData = DIPROPAXISMODE_REL;
389 hRes = m_poDXInputDevice->SetProperty( DIPROP_AXISMODE, &dipdw.diph );
390
391 return hRes == DI_OK;
392}
393
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TTODO(DESC)
Definition Defines.h:134
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TDEFINE_CLASS(...)
Definition TObject.h:120
unsigned int TUINT
Definition Typedefs.h:8
float TFLOAT
Definition Typedefs.h:4
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TFORCEINLINE void Clip(T &rVal, const T &Min, const T &Max)
static constexpr TINT INPUT_DEVICE_MOUSE_BUTTONS
TINT ProcessRepeats(EventEmitter &a_rEmitter, TFLOAT a_fDeltaTime)
static constexpr TINT INPUT_DEVICE_MOUSE_WHEEL
TBOOL IsAcquired() const
void SetAcquired(TBOOL a_bAcquired)
TInputInterface::EventEmitter EventEmitter
virtual TBOOL WasDown(Doodad a_iDoodad) const
virtual TBOOL Unacquire() override
virtual TBOOL Acquire() override
virtual TINT GetButtonCount() const override
virtual TINT GetAxisInt(Doodad a_iDoodad, TINT axis) const override
virtual TINT ProcessEvents(EventEmitter &a_rEmitter, TFLOAT a_fDeltaTime) override
TBOOL const BindToDIDevice(HWND a_hMainWindow, LPCDIDEVICEINSTANCEA a_poDeviceInstance, IDirectInputDevice8A *a_poDXInputDevice, TBOOL a_bExclusive)
void SetExclusive(HWND a_hWindow, TBOOL a_bExclusive)
virtual void Update(TFLOAT a_fDeltaTime) override
virtual void Release() override
virtual TBOOL IsEnabled() const override
void SetCurrentPosition(TINT a_iX, TINT a_iY)
virtual TBOOL IsDown(Doodad a_iDoodad) const override
virtual TFLOAT GetAxisFloat(Doodad a_iDoodad, TINT axis) const override
virtual TBOOL Flush() override
virtual TINT GetAxisCount() const override
virtual DISPLAYPARAMS * GetCurrentDisplayParams()=0
void Throw(Data *pData)
Definition TEvent.h:56
static TFORCEINLINE TRenderInterface * GetSingleton()
Definition TSingleton.h:49
static void MemClear(void *ptr, TSIZE size)
Definition TUtil.h:91
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90