Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 10be405f2508a131d47f78c3de302bc04d6041d2 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
   client/input.h
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include "audio/audio.h"
#include "core/core.h"
#include "client/client.h"
#include "client/input.h"
#include "client/chat.h"
#include "client/console.h"
#include "client/keyboard.h"
#include "client/video.h"
#include "render/camera.h"
#include "math/functions.h"
#include "render/text.h"

#include "SDL/SDL.h"

namespace client
{

core::Cvar *cl_mousecontrol = 0;

namespace input
{

//--- local variables ---------------------------------------------

Keyboard *keyboard = 0;

//bool free_control = true;
bool cl_mousecontrol_override = false;

// local controls
float local_direction = 0.0f;
float local_pitch = 0.0f;
float local_thrust = 0.0f;
float local_roll = 0.0f;

// last controlled entity
unsigned int last_control = 0;

// mouse cursor position
int mouse_x = 0;
int mouse_y = 0;

float mouse_pitch = 0;
float mouse_direction = 0;

// true if the mouse is in the deadzone
bool mouse_deadzone = false;
// true if the mouse has control
bool mouse_control = false;

const float thruster_offset = 0.05f;

// small hack to prevent two sounds triggered by the scrollwheel
bool singleclick = true;

//--- engine functions --------------------------------------------

void func_screenshot(std::string const & args)
{
	video::screenshot();
}

void func_ui_control(std::string const &args)
{
	if (!core::localcontrol())
		return;

	if (cl_mousecontrol->value() > 0) {
			(*cl_mousecontrol) = 0.0f;
	} else {
		(*cl_mousecontrol) = 1.0f;
	}

	if (!cl_mousecontrol->value()) {
		local_direction = 0.0f;
		local_pitch = 0.0f;
		local_roll = 0.0f;

		render::Camera::set_direction(0.0f);
		render::Camera::set_pitch(0.0f);
	}
	audio::play("ui/control");
}

void func_ui_console(std::string const &args)
{
	console()->toggle();
}

void func_ui_chat(std::string const &args)
{
	if (core::application()->connected()) {
		chat::toggle();
	}
}

void func_ui_view(std::string const &args)
{
	if (core::application()->connected() && core::localcontrol()) {
		render::Camera::next_mode();
		local_roll = 0;
		local_pitch = 0;
		local_direction = 0;
		audio::play("ui/view");
	}
}

void func_list_keys(std::string const &args)
{
	if (keyboard)
		keyboard->list_keys();	
}

void func_list_binds(std::string const &args)
{
	if (keyboard)
		keyboard->list_binds();	
}

void func_bind(std::string const &args)
{
	if (keyboard) {
		std::stringstream argstr(args);
		std::string keyname;
		if (argstr >> keyname) {
			if (args.size() > keyname.size()+1) {
				keyboard->bind(keyname, args.substr(keyname.size()+1));
				return;
			}
		}
	}

	core::Func *func = core::Func::find("bind");
	con_print << "bind " << func->info() << std::endl;
}

void func_unbind(std::string const &args)
{
	if (keyboard) {
		std::stringstream argstr(args);
		std::string keyname;
		if (argstr >> keyname) {
			keyboard->unbind(keyname);
			return;
		}
	}

	core::Func *func = core::Func::find("unbind");
	con_print << "unbind " << func->info() << std::endl;
}

//--- input functions ---------------------------------------------

void init()
{
	con_print << "^BInitializing input..." << std::endl;

	keyboard = new Keyboard();

	client::setkeyboardmode(false);

 	SDL_ShowCursor(SDL_DISABLE);
	SDL_WM_GrabInput(SDL_GRAB_ON);
//	SDL_EnableUNICODE(1);

	cl_mousecontrol = core::Cvar::get("cl_mousecontrol", "1", core::Cvar::Archive);
	cl_mousecontrol->set_info("[bool] set mouse control on or off");

	core::Func *func = 0;

	func = core::Func::add("ui_console", func_ui_console);
	func->set_info("toggle console on or off");

	func = core::Func::add("ui_chat", func_ui_chat);
	func->set_info("toggle chatbox on or of");

	func = core::Func::add("ui_view", func_ui_view);
	func->set_info("switch view mode");

	func = core::Func::add("ui_control",func_ui_control);
	func->set_info("toggle mouse control on or off");

	func = core::Func::add("list_keys", func_list_keys);
	func->set_info("list keyboard key names");

	func = core::Func::add("list_binds",func_list_binds);
	func->set_info("list keyboard binds");

	func = core::Func::add("bind", (core::FuncPtr) func_bind);
	func->set_info("[key] [str] bind a command to a key");

	func = core::Func::add("unbind", func_unbind);
	func->set_info("[key] unbind a key");

	func = core::Func::add("screenshot", func_screenshot);
	func->set_info("make a screenshot");

	keyboard->load_binds();
}

void shutdown()
{
	con_print << "^BShutting down input..." << std::endl;

	core::Func::remove("list_keys");
	core::Func::remove("list_binds");

	core::Func::remove("bind");
	core::Func::remove("unbind");

	core::Func::remove("screenshot");
	
	core::Func::remove("ui_console");
	core::Func::remove("ui_control");
	core::Func::remove("ui_chat");
	core::Func::remove("ui_view");
	
	keyboard->save_binds();
	delete keyboard;
	keyboard = 0;

 	SDL_ShowCursor(SDL_ENABLE);
	SDL_WM_GrabInput(SDL_GRAB_OFF);
//	SDL_EnableUNICODE(0);

}

void action_press(std::string const &action)
{
	/* -- thruster ------------------------------------ */
	if (action.compare("+thrust") == 0) {
		if (local_thrust < 1.0f && singleclick) {
			audio::play("ui/thruster");
			singleclick = false;
		}
		local_thrust += thruster_offset;
		

	} else if (action.compare("-thrust") == 0) {
		if (local_thrust > 0.0f && singleclick) {
			audio::play("ui/thruster");
			singleclick = false;
		}
		local_thrust -= 2.0f * thruster_offset;
	
	/* -- mouse control ------------------------------- */
	} else if (action.compare("+control") == 0) {
		cl_mousecontrol_override = true;

	/* -- directional control ------------------------- */
	} else if (action.compare("+left") == 0) {
		local_direction = 1.0f;

	} else if (action.compare("+right") == 0) {
		local_direction = -1.0f;

	} else if (action.compare("+up") == 0) {
		local_pitch = 1.0f;

	} else if (action.compare("+down") == 0) {
		local_pitch = -1.0f;

	} else if (action.compare("+rollleft") == 0) {
		local_roll = 1.0f;

	} else if (action.compare("+rollright") == 0) {
		local_roll = -1.0f;

	/* -- camera control ------------------------------ */
	} else if (action.compare("+camleft") == 0) {
		render::Camera::set_direction(1.0f);

	} else if (action.compare("+camright") == 0) {
		render::Camera::set_direction(-1.0f);

	} else if (action.compare("+camup") == 0) {
		render::Camera::set_pitch(1.0f);

	} else if (action.compare("+camdown") == 0) {
		render::Camera::set_pitch(-1.0f);
	} else
		con_warn << "Unknown action " << action << std::endl;
}

void action_release(std::string const &action)
{
	/* -- thruster ------------------------------------ */
	if (action.compare("+thrust") == 0) {
		

	} else if (action.compare("-thrust") == 0) {
		

	/* -- mouse control ------------------------------- */
	} else if (action.compare("+control") == 0) {
		cl_mousecontrol_override = false;
		if (!cl_mousecontrol->value()) {
			local_direction = 0.0f;
			local_pitch = 0.0f;
			local_roll = 0.0f;

			render::Camera::set_direction(0.0f);
			render::Camera::set_pitch(0.0f);
		}

	/* -- directional control ------------------------- */
	} else if (action.compare("+left") == 0) {
		local_direction = 0.0f;

	} else if (action.compare("+right") == 0) {
		local_direction = 0.0f;

	} else if (action.compare("+up") == 0) {
		local_pitch = 0.0f;

	} else if (action.compare("+down") == 0) {
		local_pitch = 0.0f;

	} else if (action.compare("+rollleft") == 0) {
		local_roll = 0.0f;

	} else if (action.compare("+rollright") == 0) {
		local_roll = 0.0f;

	/* -- camera control ------------------------------ */
	} else if (action.compare("+camleft") == 0) {
		render::Camera::set_direction(0.0f);

	} else if (action.compare("+camright") == 0) {
		render::Camera::set_direction(0.0f);

	} else if (action.compare("+camup") == 0) {
		render::Camera::set_pitch(0.0f);

	} else if (action.compare("+camdown") == 0) {
		render::Camera::set_pitch(0.0f);
	}
}

void frame(float seconds)
{
	if (core::localcontrol() && (last_control != core::localcontrol()->id())) {
		local_direction = 0.0f;
		local_pitch = 0.0f;
		local_roll = 0.0f;
		local_thrust = core::localcontrol()->thrust();
		last_control = core::localcontrol()->id();
		mouse_pitch = 0.0f;
		mouse_direction = 0.0f;
		mouse_x = video::width / 2;
		mouse_y = video::height / 2;
		render::Camera::reset();
		cl_mousecontrol_override = false;
	}

	SDL_Event event;
	Key *key = 0;
	bool pressed = false;
	singleclick = true;

	while (SDL_PollEvent(&event)) {
		pressed =  false;
		key = 0;

		switch (event.type) {

  			case SDL_MOUSEMOTION:
				mouse_x = event.motion.x;
				mouse_y = event.motion.y;
				break;

			case SDL_MOUSEBUTTONDOWN:
				key = keyboard->press(512 + event.button.button);
				pressed = true;
				break;

			case SDL_MOUSEBUTTONUP:
				key = keyboard->release(512 + event.button.button);
				pressed = false;
				break;

			case SDL_KEYDOWN:
				key = keyboard->press(event.key.keysym.sym);
				pressed = true;
				break;

			case SDL_KEYUP:
				key = keyboard->release(event.key.keysym.sym);
				pressed = false;
				break;

			case SDL_QUIT:
				core::application()->shutdown();
				return;
				break;
		}	
		
		if (key) {
			if (pressed) {
/*
				if (keysym.mod & KMOD_NUM) numlock = true else numlock = false;
				if (keysym.mod & KMOD_CAPS) capslock = true; else capslock = false;
				if ((keysym.mod & KMOD_LSHIFT) || (keysym.mod & KMOD_RSHIFT)) capslock != capslock;
*/
				// console key is always captured
				if (key->bind().compare("ui_console") == 0) {
					console()->toggle();
					local_direction = 0.0f;
					local_pitch = 0.0f;
					local_roll = 0.0f;

					render::Camera::set_direction(0.0f);
					render::Camera::set_pitch(0.0f);

				} else if (console()->visible()) {
					// send key events to the console
					if (event.type == SDL_KEYDOWN)
						console()->keypressed(translate_keysym(event.key.keysym));
	
				} else if (chat::visible()) {
					// send key events to the chat box
					if (event.type == SDL_KEYDOWN)
						chat::keypressed(translate_keysym(event.key.keysym));
	
				} else if (core::application()->connected() && core::localcontrol()) {

					char c = key->bind().c_str()[0];
					if (c == '+' || c == '-') {
						// action bind
						action_press(key->bind());

					} else if (c) {
						// normal bind
						core::cmd() << key->bind() << "\n";
					}
				} else if (core::application()->connected()) {
					
					char c = key->bind().c_str()[0];
					if (c && c != '+' && c != '-') {
						// normal bind
						core::cmd() << key->bind() << "\n";
					}
				}

			} else {

				if (core::application()->connected() && core::localcontrol() && !console()->visible() && !chat::visible()) {

					char c = key->bind().c_str()[0];
					if (c == '+' || c == '-') {
						// action bind
						action_release(key->bind());
					}
				}
			}
		}
	}
	
	mouse_control = false;
	mouse_deadzone = false;

	if (core::application()->connected() && core::localcontrol()) {

		mouse_control = !console()->visible() && ((cl_mousecontrol->value() > 0) || cl_mousecontrol_override);

		if (mouse_control) {
			// the mouse will not react if it is in the deadzone
			int deadzone_size = 8;
			mouse_deadzone = true;

			// direction
			int l = mouse_x - (video::width >> 1);
	
			if (abs(l) < ( deadzone_size >> 1 )) {
				// dead zone
				mouse_direction = 0;
			} else {
				l = (mouse_x - deadzone_size) - ((video::width - deadzone_size) >> 1);
				mouse_direction = float (-l) / (float) ((video::width - deadzone_size) >> 1);
				mouse_deadzone = false;
			}

			// pitch
			int h = mouse_y - (video::height >> 1);

			if (abs(h) < ( deadzone_size >> 1 )) {
				// dead zone
				mouse_pitch = 0;
			} else {
				h = (mouse_y - deadzone_size) - ((video::height - deadzone_size) >> 1);
				mouse_pitch = float (-h) / (float) ((video::height - deadzone_size) >> 1);
				mouse_deadzone = false;
			}

			if ((render::Camera::mode() == render::Camera::Track) || (render::Camera::mode() == render::Camera::Cockpit)) {
				local_direction = mouse_direction * math::absf(mouse_direction);
				local_pitch = mouse_pitch * math::absf(mouse_pitch);

			} else if (render::Camera::mode() == render::Camera::Free) {
				// squared values to smoothen camera movement
				render::Camera::set_direction( -mouse_direction * math::absf(mouse_direction));
				render::Camera::set_pitch(-mouse_pitch * math::absf(mouse_pitch));
			}

		} else {
			/*
			const float MIN_DELTA = 10e-10;
			const float ANGLE_DELTA = 0.01f;

			math::Axis target_axis; // FIXME
			float cosangle;
			const math::Axis & entity_axis= core::localcontrol()->axis();
			
			// auto-level: pitch
			local_pitch = 0;

			// project target_axis.up() into the plane with axis->left() normal
			math::Vector3f n = entity_axis.left();
			math::Vector3f p = target_axis.up();
			float u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
			p = target_axis.up() + u * n;
	
			float side = entity_axis.forward().x * p.x + 
				entity_axis.forward().y * p.y +
				entity_axis.forward().z * p.z;

			if ((fabs(side) - MIN_DELTA > 0)) {
				
				cosangle = math::dotproduct(p, entity_axis.up());
				if (fabs(cosangle) + ANGLE_DELTA < 1 ) {
					local_pitch = -3 * math::sgnf(side) * (1-cosangle);
				}
			}	

			// auto-level: roll
			local_roll = 0;

			// project target_axis.up() into the plane with axis.forward() normal
			n = entity_axis.forward();
			p = target_axis.up();
			u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
			p = target_axis.up() + u * n;

			side = entity_axis.left().x * p.x + 
				entity_axis.left().y * p.y +
				entity_axis.left().z * p.z;

			if ((fabs(side) - MIN_DELTA > 0)) {
				
				cosangle = math::dotproduct(p, entity_axis.up());
				if (fabs(cosangle) + ANGLE_DELTA < 1 ) {
					local_roll = 3 * math::sgnf(side) * (1-cosangle);
				}
			}
			*/
			
		}
		
		math::clamp(local_direction, -1.0f,  1.0f);
		math::clamp(local_pitch, -1.0f,  1.0f);
		math::clamp(local_thrust, 0.0f,  1.0f);
		math::clamp(local_roll, -1.0f,  1.0f);

		core::localcontrol()->set_thrust(local_thrust);
		core::localcontrol()->set_direction(local_direction);
		core::localcontrol()->set_pitch(local_pitch / render::Camera::aspect() );
		core::localcontrol()->set_roll(local_roll);
	}
}

} // namespace input

} // namespace client