File: InputManager.cpp

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (311 lines) | stat: -rw-r--r-- 7,557 bytes parent folder | download | duplicates (4)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*!
	@file
	@author		Albert Semenov
	@date		09/2009
*/

#include "Precompiled.h"
#include "InputManager.h"
#include "../InputConverter.h"

#ifdef INPUT_KEY_NAME
#include <MyGUI.h>
#endif

namespace input
{

	// указатель на менеджер, куда транслируються сообщения
	InputManager* InputManager::msInputManager = 0;

	// старая процедура, которую мы заменили
	LRESULT InputManager::msOldWindowProc = NULL;

	bool InputManager::msSkipMove = false;

	// наша процедура для фильтрации сообщений
	LRESULT CALLBACK InputManager::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		// если колесо не определенно
		#ifndef WM_MOUSEWHEEL
			#define WM_MOUSEWHEEL 0x020A
			#define __WM_REALMOUSELAST WM_MOUSEWHEEL
		#else
			#define __WM_REALMOUSELAST WM_MOUSELAST
		#endif // WM_MOUSEWHEEL

		// для взятия знаковых значений
		#define GET_HIWORD(param) ((short)HIWORD(param))
		#define GET_LOWORD(param) ((short)LOWORD(param))

		// на нас кидают файлы
		if (WM_DROPFILES == uMsg)
		{
			HDROP hDrop = (HDROP)wParam;
			wchar_t buff[MAX_PATH] = { 0 };
			UINT fcount = DragQueryFileW(hDrop, 0xFFFFFFFF, NULL, 0);

			for (UINT index = 0; index < fcount; ++index)
			{
				DragQueryFileW(hDrop, index, buff, MAX_PATH);
				msInputManager->onFileDrop(buff);
			}

			DragFinish(hDrop);
			return 0;
		}
		// нас пытаются закрыть
		else if (WM_CLOSE == uMsg)
		{
			if (!msInputManager->onWinodwClose((size_t)hWnd))
				return 0;
		}
		else if ((uMsg >= WM_MOUSEFIRST) && (uMsg <= __WM_REALMOUSELAST))
		{
			static int old_x = 0;
			static int old_y = 0;
			static int old_z = 0;
			static bool left_button = false;
			static bool right_button = false;

			switch (uMsg)
			{
			case WM_MOUSEMOVE:
			{
				int x = GET_LOWORD(lParam);
				int y = GET_HIWORD(lParam);

				if (x < 0) x = 0;
				else if (x > msInputManager->mWidth) x = msInputManager->mWidth;
				if (y < 0) y = 0;
				else if (y > msInputManager->mHeight) y = msInputManager->mHeight;

				old_x = x;
				old_y = y;

				if (msSkipMove)
					msSkipMove = false;
				else
					msInputManager->mouseMove(old_x, old_y, old_z);
			}

			break;

			case WM_MOUSEWHEEL:
				old_z += GET_HIWORD(wParam);
				msInputManager->mouseMove(old_x, old_y, old_z);
				break;

			case WM_LBUTTONDOWN:
				left_button = true;
				if (!right_button)
					::SetCapture(hWnd);
				msInputManager->mousePress(old_x, old_y, MyGUI::MouseButton::Left);
				break;

			case WM_LBUTTONDBLCLK:
				left_button = true;
				if (!right_button)
					::SetCapture(hWnd);
				msInputManager->mousePress(old_x, old_y, MyGUI::MouseButton::Left);
				break;

			case WM_RBUTTONDOWN:
				right_button = true;
				if (!left_button)
					::SetCapture(hWnd);
				msInputManager->mousePress(old_x, old_y, MyGUI::MouseButton::Right);
				break;

			case WM_RBUTTONDBLCLK:
				right_button = true;
				if (!left_button)
					::SetCapture(hWnd);
				msInputManager->mousePress(old_x, old_y, MyGUI::MouseButton::Right);
				break;

			case WM_MBUTTONDOWN:
				msInputManager->mousePress(old_x, old_y, MyGUI::MouseButton::Middle);
				break;

			case WM_LBUTTONUP:
				msInputManager->mouseRelease(old_x, old_y, MyGUI::MouseButton::Left);
				left_button = false;
				if (!right_button)
					::SetCapture(0);
				break;
			case WM_RBUTTONUP:
				right_button = false;
				if (!left_button)
					::SetCapture(0);
				msInputManager->mouseRelease(old_x, old_y, MyGUI::MouseButton::Right);
				break;
			case WM_MBUTTONUP:
				msInputManager->mouseRelease(old_x, old_y, MyGUI::MouseButton::Middle);
				break;
			}
		}
		else if (WM_KEYDOWN == uMsg)
		{
			bool repeat = (lParam & (1 >> 30)) != 0;
			if (!repeat)
			{
				int scan_code = VirtualKeyToScanCode(wParam);
				int text = VirtualKeyToText(wParam);
				msInputManager->injectKeyPress(MyGUI::KeyCode::Enum(scan_code), (MyGUI::Char)text);

#ifdef INPUT_KEY_NAME
				MyGUI::MYGUI_OUT("VirtKey : ", VirtualKeyToName(wParam), " to ScanCode : ", ScanCodeToName(scan_code));
#endif
			}
		}
		else if (WM_IME_CHAR == uMsg)
		{
			int text = 0;
#ifdef _UNICODE
			text = wParam;
#else
			char mbstr[3];
			BYTE hiByte = static_cast<BYTE>(wParam >> 8);
			BYTE loByte = wParam & 0x000000FF;
			if (hiByte == 0)
			{
				mbstr[0] = loByte;
				mbstr[1] = '\0';
			}
			else
			{
				mbstr[0] = hiByte;
				mbstr[1] = loByte;
				mbstr[2] = '\0';
			}

			wchar_t wstr[2];
			/*int num = */MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mbstr, -1, wstr, sizeof(wstr)/sizeof(wstr[0]));
			text = wstr[0];
#endif // _UNICODE
			msInputManager->injectKeyPress(MyGUI::KeyCode::None, (MyGUI::Char)text);
		}
		else if (WM_KEYUP == uMsg)
		{
			int scan_code = VirtualKeyToScanCode(wParam);
			MyGUI::KeyCode code = MyGUI::KeyCode::Enum(scan_code);

			// принтскрин приходит только отжатие
			if (code == MyGUI::KeyCode::SysRq)
				msInputManager->injectKeyPress(code, (MyGUI::Char)0);

			msInputManager->injectKeyRelease(code);
		}

		// вызываем полюбому
		return CallWindowProc((WNDPROC)msOldWindowProc, hWnd, uMsg, wParam, lParam);
	}

	InputManager::InputManager() :
		mHwnd(0),
		mWidth(0),
		mHeight(0),
		mMouseX(0),
		mMouseY(0),
		mMouseZ(0),
		mMouseMove(false)
	{
		assert(!msInputManager);
		msInputManager = this;
	}

	InputManager::~InputManager()
	{
		assert(msInputManager);
		msInputManager = 0;
	}

	void InputManager::createInput(size_t _handle)
	{
		mHwnd = (HWND)_handle;

		// подсовываем нашу функцию калбеков
		if (!msOldWindowProc)
		{
			msOldWindowProc = GetWindowLongPtr(mHwnd, GWLP_WNDPROC);
			SetWindowLongPtr(mHwnd, GWLP_WNDPROC, (LONG_PTR)windowProc);
		}

		// устанавливаем поддержку дропа файлов
		LONG_PTR style = GetWindowLongPtr(mHwnd, GWL_EXSTYLE);
		SetWindowLongPtr(mHwnd, GWL_EXSTYLE, style | WS_EX_ACCEPTFILES);

		MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &InputManager::frameEvent);
	}

	void InputManager::destroyInput()
	{
		MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &InputManager::frameEvent);

		// если мы подменили процедуру, то вернем на место
		if (msOldWindowProc)
		{
			SetWindowLongPtr((HWND)mHwnd, GWLP_WNDPROC, (LONG_PTR)msOldWindowProc);
			msOldWindowProc = 0;
		}
	}

	void InputManager::captureInput()
	{
	}

	void InputManager::setInputViewSize(int _width, int _height)
	{
		mWidth = _width;
		mHeight = _height;
	}

	void InputManager::setMousePosition(int _x, int _y)
	{
		POINT point = { _x, _y };
		::ClientToScreen(mHwnd, &point);

		msSkipMove = true;
		::SetCursorPos(point.x, point.y);
	}

	void InputManager::updateCursorPosition()
	{
	}

	void InputManager::frameEvent(float _time)
	{
		computeMouseMove();
	}

	void InputManager::computeMouseMove()
	{
		if (mMouseMove)
		{
			injectMouseMove(mMouseX, mMouseY, mMouseZ);
			mMouseMove = false;
		}
	}

	void InputManager::mouseMove(int _absx, int _absy, int _absz)
	{
		mMouseX = _absx;
		mMouseY = _absy;
		mMouseZ = _absz;
		mMouseMove = true;
	}

	void InputManager::mousePress(int _absx, int _absy, MyGUI::MouseButton _id)
	{
		computeMouseMove();
		injectMousePress(_absx, _absy, _id);
	}

	void InputManager::mouseRelease(int _absx, int _absy, MyGUI::MouseButton _id)
	{
		computeMouseMove();
		injectMouseRelease(_absx, _absy, _id);
	}

} // namespace input