File: input_mouse.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (339 lines) | stat: -rw-r--r-- 7,116 bytes parent folder | download
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
	$Id: input_mouse.cpp,v 1.7 2001/12/29 14:44:37 sphair Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#include "Core/precomp.h"
#include <windows.h>
#include "API/Display/Display/display.h"
#include "API/Display/Input/mouse.h"
#include "input_mouse.h"
#include "API/Core/System/cl_assert.h"
#include "Display/Display/DirectDraw/displaycard_win32compatible.h"

#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL WM_MOUSELAST+1 
#endif

/******************************
	CL_Mouse_Win32
******************************/

#define NUM_MBUTTONS 3

CL_Mouse_Win32::CL_Mouse_Win32()
{
	cursor = new CL_InputCursor_Mouse_Win32();
	axes = new CL_InputAxis_Mouse_Win32[2];

	buttons = new CL_InputButton_Mouse_Win32*[NUM_MBUTTONS];
	for (int i=0; i<NUM_MBUTTONS; i++) buttons[i] = new CL_InputButton_Mouse_Win32(i);

	CL_System_Win32::add_listener(this);
}

CL_Mouse_Win32::~CL_Mouse_Win32()
{
	CL_System_Win32::remove_listener(this);

	delete cursor;
  delete[] axes;

	for (int i=0; i<NUM_MBUTTONS; i++) delete buttons[i];
	delete[] buttons;
}

char *CL_Mouse_Win32::get_name() const
{
	return "Standard mouse";
}

CL_InputDevice::InputDeviceType CL_Mouse_Win32::get_type() const
{
	return CL_InputDevice::type_mouse;
}

int CL_Mouse_Win32::get_num_buttons() const
{
	return NUM_MBUTTONS;
}

CL_InputButton *CL_Mouse_Win32::get_button(int button_num)
{
	return buttons[button_num];
}

int CL_Mouse_Win32::get_num_axes() const
{
	return 2;
}

CL_InputAxis *CL_Mouse_Win32::get_axis(int axis_num)
{
	cl_assert(((unsigned int) axis_num)<2);
	return &axes[axis_num];
}

int CL_Mouse_Win32::get_num_hats() const
{
	return 0;
}

CL_InputHat *CL_Mouse_Win32::get_hat(int hat_num)
{
	return NULL;
}

int CL_Mouse_Win32::get_num_cursors() const
{
	return 1;
}

CL_InputCursor *CL_Mouse_Win32::get_cursor(int cursor_num)
{
	return cursor;
}

void CL_Mouse_Win32::keep_alive()
{
	if (CL_Display::get_current_card() == NULL) return;

	POINT cursor_pos;
	GetCursorPos(&cursor_pos);

	CL_DisplayCard_Win32Compatible *win32card =
		(CL_DisplayCard_Win32Compatible *) CL_Display::get_current_card();

	BOOL res = ScreenToClient(win32card->get_hwnd(), &cursor_pos);

	if (cursor->x != cursor_pos.x ||
		cursor->y != cursor_pos.y)
	{
		cursor->x = cursor_pos.x;
		cursor->y = cursor_pos.y;

		CL_Input::sig_mouse_move(this, cursor->x, cursor->y);
		CL_Mouse::sig_move(cursor->x, cursor->y);
	}

	axes[0].center = float(CL_Display::get_width())/2.0f;
	axes[1].center = float(CL_Display::get_height())/2.0f;
	
	axes[0].pos = cursor_pos.x;
	axes[1].pos = cursor_pos.y;
}

bool CL_Mouse_Win32::received_event(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (CL_Display::get_current_card() == NULL) return false;

	bool up = false;
	switch (uMsg)
	{
	case WM_LBUTTONUP:
	case WM_MBUTTONUP:
	case WM_RBUTTONUP:
		up = true;
		// fall through
	case WM_LBUTTONDOWN:
	case WM_MBUTTONDOWN:
	case WM_RBUTTONDOWN:
		{
			int button_states = wParam;

			for (int i=0; i<NUM_MBUTTONS; i++)
			{
				if (buttons[i] == NULL) continue;

				// This code didn't work on Sphairs mouse:
				// bool new_state = (button_states&(1<<i)) ? true : false;

				// Hardcoding the bitposition instead:
				bool new_state = false;
				if(i == 0)
					new_state = (button_states & 1) ? true : false;
				else if(i == 1)
					new_state = (button_states & 16) ? true : false;
				else if(i == 2)
					new_state = (button_states & 2) ? true : false;

				if (new_state != buttons[i]->button_state)
				{
					buttons[i]->button_state = new_state;

					CL_Key key;
					key.ascii = -1;
					key.id = CL_MOUSE_LEFTBUTTON + i;
					key.x = cursor->x;
					key.y = cursor->y;

					if (up == false)
					{
						key.state = CL_Key::Pressed;
						CL_Input::sig_button_press(this, key);
					}
					else
					{
						key.state = CL_Key::Released;
						CL_Input::sig_button_release(this, key);
					}
				}
			}
		}
		break;

	case WM_MOUSEMOVE:
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);

			if (cursor->x != x ||
				cursor->y != y)
			{
				cursor->x = x;
				cursor->y = y;

				CL_Input::sig_mouse_move(this, cursor->x, cursor->y);
				CL_Mouse::sig_move(cursor->x, cursor->y);
			}

			axes[0].center = float(CL_Display::get_width())/2.0f;
			axes[1].center = float(CL_Display::get_height())/2.0f;
			
			axes[0].pos = x;
			axes[1].pos = y;
		}
		break;

	case WM_MOUSEWHEEL:
		{
			int keyflags = LOWORD(wParam);			/* key flags */ 
			short delta = (short) HIWORD(wParam);	/* wheel rotation */
			short x_pos = (short) LOWORD(lParam);	/* horizontal position of pointer */
			short y_pos = (short) HIWORD(lParam);	/* vertical position of pointer */ 

			// TODO: Add a Modifier field to CL_Key and put keyflags into it

			CL_Key key;
			key.ascii = -1;
			if(delta > 0)
				key.id = CL_MOUSE_WHEELUP;
			else
				key.id = CL_MOUSE_WHEELDOWN;

			key.x = cursor->x;
			key.y = cursor->y;

			key.state = CL_Key::Pressed;
			CL_Input::sig_button_press(this, key);

			key.state = CL_Key::Released;
			CL_Input::sig_button_release(this, key);
		}
		break;

	default:
		return false;
	}

	return true;
}

/*******************************
	 CL_InputButton_Mouse_Win32
*******************************/

CL_InputButton_Mouse_Win32::CL_InputButton_Mouse_Win32(int _key)
{
	key = (1<<_key);
	button_state = false;
}

CL_InputButton_Mouse_Win32::~CL_InputButton_Mouse_Win32()
{
}

bool CL_InputButton_Mouse_Win32::is_pressed()
{
	return button_state;
}

/*******************************
	 CL_InputCursor_Mouse_Win32
*******************************/

CL_InputCursor_Mouse_Win32::CL_InputCursor_Mouse_Win32()
{
	x = 0;
	y = 0;
}

CL_InputCursor_Mouse_Win32::~CL_InputCursor_Mouse_Win32()
{
}

float CL_InputCursor_Mouse_Win32::get_x()
{
	POINT cursor_pos;
	GetCursorPos(&cursor_pos);

	CL_DisplayCard_Win32Compatible *win32card =
		(CL_DisplayCard_Win32Compatible *) CL_Display::get_current_card();

	ScreenToClient(win32card->get_hwnd(), &cursor_pos);

	return cursor_pos.x;
}

float CL_InputCursor_Mouse_Win32::get_y()
{
	POINT cursor_pos;
	GetCursorPos(&cursor_pos);

	CL_DisplayCard_Win32Compatible *win32card =
		(CL_DisplayCard_Win32Compatible *) CL_Display::get_current_card();

	ScreenToClient(win32card->get_hwnd(), &cursor_pos);

	return cursor_pos.y;
}

float CL_InputCursor_Mouse_Win32::get_max_x()
{
	if (CL_Display::get_current_card() == NULL) return -1;
	return CL_Display::get_width();
}

float CL_InputCursor_Mouse_Win32::get_max_y()
{
	if (CL_Display::get_current_card() == NULL) return -1;
	return CL_Display::get_height();
}

/*******************************
	 CL_InputAxis_Mouse_Win32
*******************************/

CL_InputAxis_Mouse_Win32::CL_InputAxis_Mouse_Win32()
{
	pos = 0;
	center = 1;
}

CL_InputAxis_Mouse_Win32::~CL_InputAxis_Mouse_Win32()
{
}

float CL_InputAxis_Mouse_Win32::get_pos()
{
	return (pos - center) / center;
}