-
Notifications
You must be signed in to change notification settings - Fork 424
/
input.cpp
294 lines (256 loc) · 7.48 KB
/
input.cpp
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
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include "SDL.h"
#include <base/system.h>
#include <engine/shared/config.h>
#include <engine/graphics.h>
#include <engine/input.h>
#include <engine/keys.h>
#include "input.h"
//print >>f, "int inp_key_code(const char *key_name) { int i; if (!strcmp(key_name, \"-?-\")) return -1; else for (i = 0; i < 512; i++) if (!strcmp(key_strings[i], key_name)) return i; return -1; }"
// this header is protected so you don't include it from anywere
#define KEYS_INCLUDE
#include "keynames.h"
#undef KEYS_INCLUDE
void CInput::AddEvent(char *pText, int Key, int Flags)
{
if(m_NumEvents != INPUT_BUFFER_SIZE)
{
m_aInputEvents[m_NumEvents].m_Key = Key;
m_aInputEvents[m_NumEvents].m_Flags = Flags;
if(!pText)
m_aInputEvents[m_NumEvents].m_aText[0] = 0;
else
str_copy(m_aInputEvents[m_NumEvents].m_aText, pText, sizeof(m_aInputEvents[m_NumEvents].m_aText));
m_aInputEvents[m_NumEvents].m_InputCount = m_InputCounter;
m_NumEvents++;
}
}
CInput::CInput()
{
mem_zero(m_aInputCount, sizeof(m_aInputCount));
mem_zero(m_aInputState, sizeof(m_aInputState));
m_InputCounter = 1;
m_InputGrabbed = 0;
m_LastRelease = 0;
m_ReleaseDelta = -1;
m_NumEvents = 0;
m_MouseFocus = true;
m_VideoRestartNeeded = 0;
m_pClipboardText = NULL;
}
void CInput::Init()
{
m_pGraphics = Kernel()->RequestInterface<IEngineGraphics>();
MouseModeRelative();
}
void CInput::MouseRelative(float *x, float *y)
{
if(!m_MouseFocus || !m_InputGrabbed)
return;
#if defined(__ANDROID__) // No relative mouse on Android
int nx = 0, ny = 0;
SDL_GetMouseState(&nx, &ny);
*x = nx;
*y = ny;
#else
int nx = 0, ny = 0;
float Sens = ((g_Config.m_ClDyncam && g_Config.m_ClDyncamMousesens) ? g_Config.m_ClDyncamMousesens : g_Config.m_InpMousesens) / 100.0f;
SDL_GetRelativeMouseState(&nx,&ny);
*x = nx*Sens;
*y = ny*Sens;
#endif
}
void CInput::MouseModeAbsolute()
{
m_InputGrabbed = 0;
SDL_SetRelativeMouseMode(SDL_FALSE);
Graphics()->SetWindowGrab(false);
}
void CInput::MouseModeRelative()
{
m_InputGrabbed = 1;
SDL_SetRelativeMouseMode(SDL_TRUE);
Graphics()->SetWindowGrab(true);
}
int CInput::MouseDoubleClick()
{
if(m_ReleaseDelta >= 0 && m_ReleaseDelta < (time_freq() / 3))
{
m_LastRelease = 0;
m_ReleaseDelta = -1;
return 1;
}
return 0;
}
const char* CInput::GetClipboardText()
{
if(m_pClipboardText)
{
SDL_free(m_pClipboardText);
}
m_pClipboardText = SDL_GetClipboardText();
return m_pClipboardText;
}
void CInput::SetClipboardText(const char *Text)
{
SDL_SetClipboardText(Text);
}
void CInput::Clear()
{
mem_zero(m_aInputState, sizeof(m_aInputState));
mem_zero(m_aInputCount, sizeof(m_aInputCount));
m_NumEvents = 0;
}
bool CInput::KeyState(int Key) const
{
return m_aInputState[Key>=KEY_MOUSE_1 ? Key : SDL_GetScancodeFromKey(KeyToKeycode(Key))];
}
void CInput::NextFrame()
{
int i;
const Uint8 *pState = SDL_GetKeyboardState(&i);
if(i >= KEY_LAST)
i = KEY_LAST-1;
mem_copy(m_aInputState, pState, i);
}
int CInput::Update()
{
// keep the counter between 1..0xFFFF, 0 means not pressed
m_InputCounter = (m_InputCounter%0xFFFF)+1;
// these states must always be updated manually because they are not in the GetKeyState from SDL
int i = SDL_GetMouseState(NULL, NULL);
if(i&SDL_BUTTON(1)) m_aInputState[KEY_MOUSE_1] = 1; // 1 is left
if(i&SDL_BUTTON(3)) m_aInputState[KEY_MOUSE_2] = 1; // 3 is right
if(i&SDL_BUTTON(2)) m_aInputState[KEY_MOUSE_3] = 1; // 2 is middle
if(i&SDL_BUTTON(4)) m_aInputState[KEY_MOUSE_4] = 1;
if(i&SDL_BUTTON(5)) m_aInputState[KEY_MOUSE_5] = 1;
if(i&SDL_BUTTON(6)) m_aInputState[KEY_MOUSE_6] = 1;
if(i&SDL_BUTTON(7)) m_aInputState[KEY_MOUSE_7] = 1;
if(i&SDL_BUTTON(8)) m_aInputState[KEY_MOUSE_8] = 1;
if(i&SDL_BUTTON(9)) m_aInputState[KEY_MOUSE_9] = 1;
{
SDL_Event Event;
bool IgnoreKeys = false;
while(SDL_PollEvent(&Event))
{
int Key = -1;
int Scancode = 0;
int Action = IInput::FLAG_PRESS;
switch (Event.type)
{
case SDL_TEXTINPUT:
AddEvent(Event.text.text, 0, IInput::FLAG_TEXT);
break;
// handle keys
case SDL_KEYDOWN:
// See SDL_Keymod for possible modifiers:
// NONE = 0
// LSHIFT = 1
// RSHIFT = 2
// LCTRL = 64
// RCTRL = 128
// LALT = 256
// RALT = 512
// LGUI = 1024
// RGUI = 2048
// NUM = 4096
// CAPS = 8192
// MODE = 16384
// Sum if you want to ignore multiple modifiers.
if(!(Event.key.keysym.mod & g_Config.m_InpIgnoredModifiers))
{
Key = KeycodeToKey(Event.key.keysym.sym);
Scancode = Event.key.keysym.scancode;
}
break;
case SDL_KEYUP:
Action = IInput::FLAG_RELEASE;
Key = KeycodeToKey(Event.key.keysym.sym);
Scancode = Event.key.keysym.scancode;
break;
// handle mouse buttons
case SDL_MOUSEBUTTONUP:
Action = IInput::FLAG_RELEASE;
if(Event.button.button == 1) // ignore_convention
{
m_ReleaseDelta = time_get() - m_LastRelease;
m_LastRelease = time_get();
}
// fall through
case SDL_MOUSEBUTTONDOWN:
if(Event.button.button == SDL_BUTTON_LEFT) Key = KEY_MOUSE_1; // ignore_convention
if(Event.button.button == SDL_BUTTON_RIGHT) Key = KEY_MOUSE_2; // ignore_convention
if(Event.button.button == SDL_BUTTON_MIDDLE) Key = KEY_MOUSE_3; // ignore_convention
if(Event.button.button == SDL_BUTTON_X1) Key = KEY_MOUSE_4; // ignore_convention
if(Event.button.button == SDL_BUTTON_X2) Key = KEY_MOUSE_5; // ignore_convention
if(Event.button.button == 6) Key = KEY_MOUSE_6; // ignore_convention
if(Event.button.button == 7) Key = KEY_MOUSE_7; // ignore_convention
if(Event.button.button == 8) Key = KEY_MOUSE_8; // ignore_convention
if(Event.button.button == 9) Key = KEY_MOUSE_9; // ignore_convention
Scancode = Key;
break;
case SDL_MOUSEWHEEL:
if(Event.wheel.y > 0) Key = KEY_MOUSE_WHEEL_UP; // ignore_convention
if(Event.wheel.y < 0) Key = KEY_MOUSE_WHEEL_DOWN; // ignore_convention
Action |= IInput::FLAG_RELEASE;
Scancode = Key;
break;
case SDL_WINDOWEVENT:
// Ignore keys following a focus gain as they may be part of global
// shortcuts
switch (Event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
#if defined(SDL_VIDEO_DRIVER_X11)
Graphics()->Resize(Event.window.data1, Event.window.data2);
#elif defined(__ANDROID__)
m_VideoRestartNeeded = 1;
#endif
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
if(m_InputGrabbed)
MouseModeRelative();
m_MouseFocus = true;
IgnoreKeys = true;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
m_MouseFocus = false;
IgnoreKeys = true;
SDL_SetRelativeMouseMode(SDL_FALSE);
break;
#if defined(CONF_PLATFORM_MACOSX) // Todo: remove this when fixed in SDL
case SDL_WINDOWEVENT_MAXIMIZED:
MouseModeAbsolute();
MouseModeRelative();
break;
#endif
}
break;
// other messages
case SDL_QUIT:
return 1;
}
if(Key >= 0 && Key < g_MaxKeys && !IgnoreKeys)
{
if(Action&IInput::FLAG_PRESS)
{
m_aInputState[Scancode] = 1;
m_aInputCount[Key] = m_InputCounter;
}
AddEvent(0, Key, Action);
}
}
}
return 0;
}
int CInput::VideoRestartNeeded()
{
if( m_VideoRestartNeeded )
{
m_VideoRestartNeeded = 0;
return 1;
}
return 0;
}
IEngineInput *CreateEngineInput() { return new CInput; }