1 #include "KeyPoll.h"
2 #include <stdio.h>
3 #include <string.h>
4 
setSensitivity(int _value)5 void KeyPoll::setSensitivity(int _value)
6 {
7 	switch (_value)
8 	{
9 		case 0:
10 			sensitivity = 28000;
11 			break;
12 		case 1:
13 			sensitivity = 16000;
14 			break;
15 		case 2:
16 			sensitivity = 8000;
17 			break;
18 		case 3:
19 			sensitivity = 4000;
20 			break;
21 		case 4:
22 			sensitivity = 2000;
23 			break;
24 	}
25 
26 }
27 
KeyPoll()28 KeyPoll::KeyPoll()
29 {
30 	xVel = 0;
31 	yVel = 0;
32 	setSensitivity(2);
33 
34 	quitProgram = 0;
35 	textentrymode=true;
36 	keybuffer="";
37 	leftbutton=0; rightbutton=0; middlebutton=0;
38 	mx=0; my=0;
39 	resetWindow = 0;
40 	toggleFullscreen = false;
41 	pressedbackspace=false;
42 
43 	useFullscreenSpaces = false;
44 	if (strcmp(SDL_GetPlatform(), "Mac OS X") == 0)
45 	{
46 		useFullscreenSpaces = true;
47 		const char *hint = SDL_GetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES);
48 		if (hint != NULL)
49 		{
50 			useFullscreenSpaces = (strcmp(hint, "1") == 0);
51 		}
52 	}
53 }
54 
enabletextentry()55 void KeyPoll::enabletextentry()
56 {
57 	keybuffer="";
58 	textentrymode = true;
59 	SDL_StartTextInput();
60 }
61 
disabletextentry()62 void KeyPoll::disabletextentry()
63 {
64 	textentrymode = false;
65 	SDL_StopTextInput();
66 }
67 
Poll()68 void KeyPoll::Poll()
69 {
70 	SDL_Event evt;
71 	while (SDL_PollEvent(&evt))
72 	{
73 		/* Keyboard Input */
74 		if (evt.type == SDL_KEYDOWN)
75 		{
76 			keymap[evt.key.keysym.sym] = true;
77 			if (evt.key.keysym.sym == SDLK_BACKSPACE)
78 			{
79 				pressedbackspace = true;
80 			}
81 			else if (	(	evt.key.keysym.sym == SDLK_RETURN ||
82 						evt.key.keysym.sym == SDLK_f	) &&
83 #ifdef __APPLE__ /* OSX prefers the command key over the alt keys. -flibit */
84 					keymap[SDLK_LGUI]	)
85 #else
86 					(	keymap[SDLK_LALT] ||
87 						keymap[SDLK_RALT]	)	)
88 #endif
89 			{
90 				toggleFullscreen = true;
91 			}
92 
93 			if (textentrymode)
94 			{
95 				if (evt.key.keysym.sym == SDLK_BACKSPACE)
96 				{
97 					keybuffer = keybuffer.substr(0, keybuffer.length() - 1);
98 				}
99 				else if (	evt.key.keysym.sym == SDLK_v &&
100 						keymap[SDLK_LCTRL]	)
101 				{
102 					keybuffer += SDL_GetClipboardText();
103 				}
104 			}
105 		}
106 		else if (evt.type == SDL_KEYUP)
107 		{
108 			keymap[evt.key.keysym.sym] = false;
109 			if (evt.key.keysym.sym == SDLK_BACKSPACE)
110 			{
111 				pressedbackspace = false;
112 			}
113 		}
114 		else if (evt.type == SDL_TEXTINPUT)
115 		{
116 			keybuffer += evt.text.text;
117 		}
118 
119 		/* Mouse Input */
120 		else if (evt.type == SDL_MOUSEMOTION)
121 		{
122 			mx = evt.motion.x;
123 			my = evt.motion.y;
124 		}
125 		else if (evt.type == SDL_MOUSEBUTTONDOWN)
126 		{
127 			if (evt.button.button == SDL_BUTTON_LEFT)
128 			{
129 				mx = evt.button.x;
130 				my = evt.button.y;
131 				leftbutton = 1;
132 			}
133 			else if (evt.button.button == SDL_BUTTON_RIGHT)
134 			{
135 				mx = evt.button.x;
136 				my = evt.button.y;
137 				rightbutton = 1;
138 			}
139 			else if (evt.button.button == SDL_BUTTON_MIDDLE)
140 			{
141 				mx = evt.button.x;
142 				my = evt.button.y;
143 				middlebutton = 1;
144 			}
145 		}
146 		else if (evt.type == SDL_MOUSEBUTTONUP)
147 		{
148 			if (evt.button.button == SDL_BUTTON_LEFT)
149 			{
150 				mx = evt.button.x;
151 				my = evt.button.y;
152 				leftbutton=0;
153 			}
154 			else if (evt.button.button == SDL_BUTTON_RIGHT)
155 			{
156 				mx = evt.button.x;
157 				my = evt.button.y;
158 				rightbutton=0;
159 			}
160 			else if (evt.button.button == SDL_BUTTON_MIDDLE)
161 			{
162 				mx = evt.button.x;
163 				my = evt.button.y;
164 				middlebutton=0;
165 			}
166 		}
167 
168 		/* Controller Input */
169 		else if (evt.type == SDL_CONTROLLERBUTTONDOWN)
170 		{
171 			buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true;
172 		}
173 		else if (evt.type == SDL_CONTROLLERBUTTONUP)
174 		{
175 			buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false;
176 		}
177 		else if (evt.type == SDL_CONTROLLERAXISMOTION)
178 		{
179 			if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX)
180 			{
181 				if (	evt.caxis.value > -sensitivity &&
182 					evt.caxis.value < sensitivity	)
183 				{
184 					xVel = 0;
185 				}
186 				else
187 				{
188 					xVel = (evt.caxis.value > 0) ? 1 : -1;
189 				}
190 			}
191 			if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY)
192 			{
193 				if (	evt.caxis.value > -sensitivity &&
194 					evt.caxis.value < sensitivity	)
195 				{
196 					yVel = 0;
197 				}
198 				else
199 				{
200 					yVel = (evt.caxis.value > 0) ? 1 : -1;
201 				}
202 			}
203 		}
204 		else if (evt.type == SDL_CONTROLLERDEVICEADDED)
205 		{
206 			SDL_GameController *toOpen = SDL_GameControllerOpen(evt.cdevice.which);
207 			printf(
208 				"Opened SDL_GameController ID #%i, %s\n",
209 				evt.cdevice.which,
210 				SDL_GameControllerName(toOpen)
211 			);
212 			controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen;
213 		}
214 		else if (evt.type == SDL_CONTROLLERDEVICEREMOVED)
215 		{
216 			SDL_GameController *toClose = controllers[evt.cdevice.which];
217 			controllers.erase(evt.cdevice.which);
218 			printf("Closing %s\n", SDL_GameControllerName(toClose));
219 			SDL_GameControllerClose(toClose);
220 		}
221 
222 		/* Window Events */
223 		else if (evt.type == SDL_WINDOWEVENT)
224 		{
225 			/* Window Resize */
226 			if (evt.window.event == SDL_WINDOWEVENT_RESIZED)
227 			{
228 				resetWindow = true;
229 			}
230 
231 			/* Window Focus */
232 			else if (evt.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
233 			{
234 				isActive = true;
235 				if (!useFullscreenSpaces)
236 				{
237 					SDL_Window *window = SDL_GetWindowFromID(evt.window.windowID);
238 					wasFullscreen = SDL_GetWindowFlags(window);
239 					SDL_SetWindowFullscreen(window, 0);
240 				}
241 				SDL_DisableScreenSaver();
242 			}
243 			else if (evt.window.event == SDL_WINDOWEVENT_FOCUS_LOST)
244 			{
245 				isActive = false;
246 				if (!useFullscreenSpaces)
247 				{
248 					SDL_SetWindowFullscreen(
249 						SDL_GetWindowFromID(evt.window.windowID),
250 						wasFullscreen
251 					);
252 				}
253 				SDL_EnableScreenSaver();
254 			}
255 
256 			/* Mouse Focus */
257 			else if (evt.window.event == SDL_WINDOWEVENT_ENTER)
258 			{
259 				SDL_DisableScreenSaver();
260 			}
261 			else if (evt.window.event == SDL_WINDOWEVENT_LEAVE)
262 			{
263 				SDL_EnableScreenSaver();
264 			}
265 		}
266 
267 		/* Quit Event */
268 		else if (evt.type == SDL_QUIT)
269 		{
270 			quitProgram = true;
271 		}
272 	}
273 }
274 
isDown(SDL_Keycode key)275 bool KeyPoll::isDown(SDL_Keycode key)
276 {
277 	return keymap[key];
278 }
279 
isUp(SDL_Keycode key)280 bool KeyPoll::isUp(SDL_Keycode key)
281 {
282 	return !keymap[key];
283 }
284 
isDown(std::vector<SDL_GameControllerButton> buttons)285 bool KeyPoll::isDown(std::vector<SDL_GameControllerButton> buttons)
286 {
287 	for (size_t i = 0; i < buttons.size(); i += 1)
288 	{
289 		if (buttonmap[buttons[i]])
290 		{
291 			return true;
292 		}
293 	}
294 	return false;
295 }
296 
isDown(SDL_GameControllerButton button)297 bool KeyPoll::isDown(SDL_GameControllerButton button)
298 {
299 	return buttonmap[button];
300 }
301 
controllerButtonDown()302 bool KeyPoll::controllerButtonDown()
303 {
304 	for (
305 		SDL_GameControllerButton button = SDL_CONTROLLER_BUTTON_A;
306 		button < SDL_CONTROLLER_BUTTON_DPAD_UP;
307 		button = (SDL_GameControllerButton) (button + 1)
308 	) {
309 		if (isDown(button))
310 		{
311 			return true;
312 		}
313 	}
314 	return false;
315 }
316 
controllerWantsLeft(bool includeVert)317 bool KeyPoll::controllerWantsLeft(bool includeVert)
318 {
319 	return (	buttonmap[SDL_CONTROLLER_BUTTON_DPAD_LEFT] ||
320 			xVel < 0 ||
321 			(	includeVert &&
322 				(	buttonmap[SDL_CONTROLLER_BUTTON_DPAD_UP] ||
323 					yVel < 0	)	)	);
324 }
325 
controllerWantsRight(bool includeVert)326 bool KeyPoll::controllerWantsRight(bool includeVert)
327 {
328 	return (	buttonmap[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] ||
329 			xVel > 0 ||
330 			(	includeVert &&
331 				(	buttonmap[SDL_CONTROLLER_BUTTON_DPAD_DOWN] ||
332 					yVel > 0	)	)	);
333 }
334