blob: 17ca724b4cf325a5749c5317896c207d55bc414f (
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
|
/*
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 "core/core.h"
#include "client/client.h"
#include "client/input.h"
#include "client/chat.h"
#include "client/console.h"
#include "client/camera.h"
#include "client/keyboard.h"
#include "client/video.h"
#include "render/text.h"
#include "SDL/SDL.h"
namespace client
{
core::Cvar *cl_mousecontrol = 0;
namespace input
{
// 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;
float mouse_direction;
// true if the mouse is in the deadzone
bool mouse_deadzone = false;
const float thruster_offset = 0.05f;
void init()
{
con_print << "Initializing input..." << std::endl;
client::setkeyboardmode(false);
SDL_ShowCursor(SDL_DISABLE);
SDL_WM_GrabInput(SDL_GRAB_ON);
// SDL_EnableUNICODE(1);
cl_mousecontrol = core::Cvar::get("cl_mousecontrol", "0", core::Cvar::Archive);
cl_mousecontrol->set_info("[bool] mouse control");
}
void shutdown()
{
con_print << "Shutting down input..." << std::endl;
// SDL_EnableUNICODE(0);
}
// handle key release for the game world
void keyreleased(const SDL_keysym &keysym)
{
switch (keysym.sym) {
case SDLK_LEFT:
camera::set_direction(0.0f);
break;
case SDLK_RIGHT:
camera::set_direction(0.0f);
break;
case SDLK_UP:
camera::set_pitch(0.0f);
break;
case SDLK_DOWN:
camera::set_pitch(0.0f);
break;
case SDLK_SPACE:
camera::next_mode();
local_roll = 0;
local_pitch = 0;
local_direction = 0;
break;
case SDLK_KP8: // turn down
local_pitch = 0.0f;
break;
case SDLK_KP2: // turn up
local_pitch = 0.0f;
break;
case SDLK_KP4: // turn left
local_direction = 0.0f;
break;
case SDLK_KP6: // turn right
local_direction = 0.0f;
break;
case SDLK_KP_DIVIDE: // roll left
local_roll = 0.0f;
break;
case SDLK_KP_MULTIPLY: // roll right
local_roll = 0.0f;
break;
default:
break;
}
}
// handle key press events for the game world
// http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlkey.html
void keypressed(const SDL_keysym &keysym)
{
switch (keysym.sym) {
case SDLK_LEFT:
camera::set_direction(1.0f);
break;
case SDLK_RIGHT:
camera::set_direction(-1.0f);
break;
case SDLK_UP:
camera::set_pitch(1.0f);
break;
case SDLK_DOWN:
camera::set_pitch(-1.0f);
break;
case SDLK_KP_PLUS:
local_thrust += thruster_offset;
break;
case SDLK_KP_MINUS:
local_thrust -= 2.0f * thruster_offset;
break;
case SDLK_KP8: // down
local_pitch = -1.0f;
break;
case SDLK_KP2: // up
local_pitch = 1.0f;
break;
case SDLK_KP4: // left
local_direction = 1.0f;
break;
case SDLK_KP6: // right
local_direction = -1.0f;
break;
case SDLK_KP5: // level
local_direction = 0;
local_pitch = 0;
local_roll = 0;
break;
case SDLK_KP_DIVIDE: // roll left
local_roll = 1.0f;
break;
case SDLK_KP_MULTIPLY: // roll light
local_roll = -1.0f;
break;
default:
break;
}
}
// handle mouse button events for the game world
// http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlmousebuttonevent.html
void mousebuttonpressed(const SDL_MouseButtonEvent &button)
{
switch (button.button) {
case SDL_BUTTON_WHEELUP:
local_thrust += thruster_offset;
break;
case SDL_BUTTON_WHEELDOWN:
local_thrust -= 2.0f * thruster_offset;
break;
}
}
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();
camera::reset();
}
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEMOTION:
mouse_x = event.motion.x;
mouse_y = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
if (!console::visible() && core::application()->connected() && core::localcontrol())
mousebuttonpressed(event.button);
break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_PRINT) {
video::screenshot();
} else if (!chat::visible() && !console::visible() &&
core::application()->connected() && core::localcontrol())
// send key events to the game world
keyreleased(event.key.keysym);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == '`' || event.key.keysym.sym == '~') {
last_control = 0;
console::toggle();
if (console::visible() && chat::visible())
chat::toggle();
} else if (console::visible()) {
// send key events to the console
console::keypressed(translate_keysym(event.key.keysym));
} else if (chat::visible()) {
if(event.key.keysym.sym == SDLK_ESCAPE) {
chat::toggle();
} else {
// send key events to the chatbox
chat::keypressed(translate_keysym(event.key.keysym));
}
} else if(core::application()->connected()) {
if ((event.key.keysym.sym == 't') || (event.key.keysym.sym == 'T')) {
chat::toggle();
} else if (core::localcontrol()) {
// send key events to the game world
keypressed(event.key.keysym);
}
}
break;
case SDL_QUIT:
core::application()->shutdown();
break;
}
}
if (!console::visible() && core::application()->connected() && core::localcontrol()) {
if (cl_mousecontrol->value()) {
// the mouse will not react if it is in the deadzone
int deadzone_size = 24;
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 ((camera::mode == camera::Track) || (camera::mode == camera::Cockpit)) {
local_direction = mouse_direction;
local_pitch = mouse_pitch;
} else if (camera::mode == camera::Free) {
camera::set_direction(-mouse_direction);
camera::set_pitch(-mouse_pitch);
}
} else {
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 / video::aspect );
core::localcontrol()->set_roll(local_roll);
}
}
} // namespace input
} // namespace client
|