-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelativeMouseInput.cpp
More file actions
203 lines (162 loc) · 3.36 KB
/
Copy pathRelativeMouseInput.cpp
File metadata and controls
203 lines (162 loc) · 3.36 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "RelativeMouseInput.h"
#include <assert.h>
CRelativeMouseInput::CRelativeMouseInput()
: UserCapture(0), UserData(0), ObservedWindow(0), bCaptured(false)
{
}
CRelativeMouseInput::~CRelativeMouseInput()
{
Current.ButtonMask = MIB_None;
UpdateCaptureState();
}
bool CRelativeMouseInput::IsCaptured(const char* User) const
{
if(User)
{
return UserCapture == User;
}
else
{
return UserCapture != 0;
}
}
void CRelativeMouseInput::Create(HWND Window)
{
assert(Window);
ObservedWindow = Window;
}
// to give the mouse some area to move
static POINT GetScreenCenter()
{
POINT ret;
ret.x = GetSystemMetrics(SM_CXSCREEN) / 2;
ret.y = GetSystemMetrics(SM_CYSCREEN) / 2;
return ret;
}
void CRelativeMouseInput::WndProc(HWND Wnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if(Wnd != ObservedWindow)
{
// Create missing or too early
return;
}
switch(message)
{
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MOUSEMOVE:
{
Current.ButtonMask = (MouseInputButtons)(wParam & MIB_All);
// UpdateCaptureState();
// glm::ivec2 GlobalPos(LOWORD(lParam), HIWORD(lParam));
}
break;
}
if(IsCaptured())
{
POINT NewScreenPos;
GetCursorPos(&NewScreenPos);
POINT Center = GetScreenCenter();
int AbsX = NewScreenPos.x - Center.x;
int AbsY = NewScreenPos.y - Center.y;
Current.RelativeX += NewScreenPos.x - DragLastPos.x;
Current.RelativeY += NewScreenPos.y - DragLastPos.y;
// when the mouse is leaving the zone we move it back in
// assuming a minimal screen resolution of 640x480 200 is a good size
int Zone = 200;
bool MoveCusror = false;
if(AbsX < -Zone)
{
MoveCusror = true;
AbsX += Zone;
}
else if(AbsX > Zone)
{
MoveCusror = true;
AbsX -= Zone;
}
if(AbsY < -Zone)
{
MoveCusror = true;
AbsY += Zone;
}
else if(AbsY > Zone)
{
MoveCusror = true;
AbsY -= Zone;
}
DragLastPos.x = Center.x + AbsX;
DragLastPos.y = Center.y + AbsY;
if(MoveCusror)
{
SetCursorPos(DragLastPos.x, DragLastPos.y);
}
}
else
{
GetCursorPos((POINT*)&Current.AbsolutePos);
}
}
bool CRelativeMouseInput::SetUserCapture(const char* User, void *Data)
{
assert(User);
if(UserCapture)
{
return false;
}
UserCapture = User;
UserData = Data;
UpdateCaptureState();
return true;
}
void CRelativeMouseInput::ResetUserCapture(const char* User)
{
if(UserCapture != User)
return;
UserCapture = 0;
UserData = 0;
UpdateCaptureState();
}
void CRelativeMouseInput::UpdateCaptureState()
{
bool bNewCaptureCaptureState = UserCapture != 0;
if(bNewCaptureCaptureState == bCaptured)
{
return;
}
if(!UserCapture)
{
int f =0;
}
bCaptured = bNewCaptureCaptureState;
if(bNewCaptureCaptureState)
{
GetCursorPos((POINT*)&Current.AbsolutePos);
ShowCursor(false);
SetCapture(ObservedWindow);
POINT Center = GetScreenCenter();
SetCursorPos(Center.x, Center.y);
DragLastPos = glm::ivec2(Center.x, Center.y);
/*
RECT rect;
GetWindowRect(ObservedWindow, &rect);
ClipCursor(&rect);
*/
}
else
{
// to disable WndProc
HWND BackupObservedWindow = ObservedWindow;
ObservedWindow = 0;
ReleaseCapture();
SetCursorPos(Current.AbsolutePos.x, Current.AbsolutePos.y);
ShowCursor(true);
Current.RelativeX = 0;
Current.RelativeY = 0;
ObservedWindow = BackupObservedWindow;
}
}