-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouse.cpp
114 lines (100 loc) · 3.62 KB
/
Mouse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "DX8InputManager.h"
DX8InputManager::CKMouse::CKMouse() : m_Device(NULL)
{
memset(&m_State, 0, sizeof(m_State));
memset(m_LastButtons, 0, sizeof(m_LastButtons));
memset(m_Buffer, 0, sizeof(m_Buffer));
m_NumberOfBuffer = 0;
}
void DX8InputManager::CKMouse::Init(HWND hWnd)
{
if (!m_Device) return;
if (FAILED(m_Device->SetDataFormat(&c_dfDIMouse)))
::OutputDebugString(TEXT("Input Manager = Failed : SetDataFormat (Mouse)"));
if (FAILED(m_Device->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
::OutputDebugString(TEXT("Input Manager = Failed : SetCooperativeLevel (Mouse)"));
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = DIPROPAXISMODE_REL;
if (FAILED(m_Device->SetProperty(DIPROP_AXISMODE, &dipdw.diph)))
::OutputDebugString(TEXT("Input Manager = Failed : SetProperty (Mouse) Relative Coord"));
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = 256;
if (FAILED(m_Device->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
::OutputDebugString(TEXT("Input Manager = Failed : SetProperty (Mouse) Buffered Data"));
dipdw.dwData = -1;
if (FAILED(m_Device->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
::OutputDebugString(TEXT("Input Manager = Failed : SetProperty (Mouse) Buffered Data"));
}
void DX8InputManager::CKMouse::Release()
{
if (m_Device)
{
// Unacquire the device.
m_Device->Unacquire();
m_Device->Release();
m_Device = NULL;
}
}
void DX8InputManager::CKMouse::Clear()
{
memset(m_LastButtons, 0, sizeof(m_LastButtons));
memset(m_State.rgbButtons, 0, sizeof(m_State.rgbButtons));
}
void DX8InputManager::CKMouse::Poll(CKBOOL pause)
{
if (!m_Device) return;
HRESULT hr;
*(CKDWORD *)m_LastButtons = *(CKDWORD *)m_State.rgbButtons;
m_NumberOfBuffer = MOUSE_BUFFER_SIZE;
hr = m_Device->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), m_Buffer, (LPDWORD)&m_NumberOfBuffer, 0);
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
{
m_Device->Acquire();
m_NumberOfBuffer = MOUSE_BUFFER_SIZE;
hr = m_Device->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), m_Buffer, (LPDWORD)&m_NumberOfBuffer, 0);
}
if (pause) m_NumberOfBuffer = 0;
if (hr <= DI_NOTATTACHED)
{
for (int i = 0; i < m_NumberOfBuffer; i++)
{
switch (m_Buffer[i].dwOfs)
{
case DIMOFS_BUTTON0:
case DIMOFS_BUTTON1:
case DIMOFS_BUTTON2:
case DIMOFS_BUTTON3:
if ((m_Buffer[i].dwData & 0x80) != 0)
m_State.rgbButtons[m_Buffer[i].dwOfs - DIMOFS_BUTTON0] |= KS_PRESSED;
else
m_State.rgbButtons[m_Buffer[i].dwOfs - DIMOFS_BUTTON0] |= KS_RELEASED;
break;
}
}
}
if (!pause)
{
POINT pt;
::GetCursorPos(&pt);
DIMOUSESTATE state;
memset(&state, 0, sizeof(DIMOUSESTATE));
m_Position.x = (float)pt.x;
m_Position.y = (float)pt.y;
hr = m_Device->GetDeviceState(sizeof(DIMOUSESTATE), &state);
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
{
m_Device->Acquire();
m_Device->GetDeviceState(sizeof(DIMOUSESTATE), &state);
}
m_State.lX = state.lX;
m_State.lY = state.lY;
m_State.lZ = state.lZ;
}
}