1 /************************************************************************
2  * This file is part of Wizznic.                                        *
3  * Copyright 2009-2015 Jimmy Christensen <dusted@dusted.dk>             *
4  * Wizznic is free software: you can redistribute it and/or modify      *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation, either version 3 of the License, or    *
7  * (at your option) any later version.                                  *
8  *                                                                      *
9  * Wizznic is distributed in the hope that it will be useful,           *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12  * GNU General Public License for more details.                         *
13  *                                                                      *
14  * You should have received a copy of the GNU General Public License    *
15  * along with Wizznic.  If not, see <http://www.gnu.org/licenses/>.     *
16  ************************************************************************/
17 
18 #include "input.h"
19 #include "ticks.h"
20 #include "defs.h"
21 #include "platform/dumplevelimages.h"
22 
23 #include "settings.h"
24 
25 static SDLKey inputChar=0;
26 static int joyCanMoveX=0;
27 static int joyCanMoveY=0;
28 
getChar()29 SDLKey getChar()
30 {
31   return(inputChar);
32 }
33 
resetMouseBtn()34 void resetMouseBtn()
35 {
36   getInpPointerState()->isDown=0;
37   getInpPointerState()->downTime=1;
38   getInpPointerState()->hitABox=0;
39 }
40 
41 static struct {
42   uint32_t button;
43   int state;
44   int time;
45 } button[C_NUM];
46 
getButton(int btn)47 int getButton(int btn)
48 {
49   return(button[btn].state);
50 }
51 
setButton(int btn)52 void setButton(int btn)
53 {
54   button[btn].state=1;
55   button[btn].time=0;
56 }
57 
58 
resetBtnTimer(int btn)59 void resetBtnTimer(int btn)
60 {
61   button[btn].time=0;
62 }
63 
resetBtn(int btn)64 void resetBtn(int btn)
65 {
66   button[btn].time=0;
67   button[btn].state=0;
68 }
69 
resetBtnAll()70 void resetBtnAll()
71 {
72   int i;
73   for(i=0; i < C_NUM; i++)
74   {
75     resetBtn(i);
76   }
77 }
78 
getBtnTime(int btn)79 int getBtnTime(int btn)
80 {
81   return(button[btn].time);
82 }
83 
runControls()84 int runControls()
85 {
86   SDL_Event event;
87   int i;
88   inputChar=0;
89   //Loop through buttons to update hold-down time
90   for(i=0; i < C_NUM; i++)
91   {
92     if( button[i].state )
93     {
94       button[i].time += getTicks();
95     }
96   }
97 
98   if( getInpPointerState()->timeSinceMoved < POINTER_SHOW_TIMEOUT)
99     getInpPointerState()->timeSinceMoved +=getTicks();
100 
101   while(SDL_PollEvent(&event))
102   {
103     switch(event.type)
104     {
105         #if defined (GP2X) || defined (WIZ)
106         case SDL_JOYBUTTONDOWN:
107           for(i=0; i < C_NUM; i++)
108           {
109             if( event.jbutton.button == button[i].button )
110             {
111               button[i].state=1;
112               button[i].time=0;
113             }
114           }
115 
116           switch( event.jbutton.button )
117           {
118            case GP2X_BUTTON_UPLEFT:
119               button[C_UP].state=1;
120               button[C_UP].time=0;
121               button[C_LEFT].state=1;
122               button[C_LEFT].time=0;
123             break;
124             case GP2X_BUTTON_UPRIGHT:
125               button[C_UP].state=1;
126               button[C_UP].time=0;
127               button[C_RIGHT].state=1;
128               button[C_RIGHT].time=0;
129             break;
130             case GP2X_BUTTON_DOWNLEFT:
131               button[C_DOWN].state=1;
132               button[C_DOWN].time=0;
133               button[C_LEFT].state=1;
134               button[C_LEFT].time=0;
135             break;
136             case GP2X_BUTTON_DOWNRIGHT:
137               button[C_DOWN].state=1;
138               button[C_DOWN].time=0;
139               button[C_RIGHT].state=1;
140               button[C_RIGHT].time=0;
141             break;
142             case GP2X_BUTTON_VOLDOWN:
143               //WIZ_AdjustVolume( VOLUME_DOWN );
144               applySettings();
145             break;
146             case GP2X_BUTTON_VOLUP:
147               //WIZ_AdjustVolume( VOLUME_UP );
148             applySettings();
149             break;
150           }
151         break;
152 
153         case SDL_JOYBUTTONUP:
154           for(i=0; i < C_NUM; i++)
155           {
156             if( event.jbutton.button == button[i].button )
157             {
158               button[i].state=0;
159               button[i].time=0;
160             }
161           }
162           switch( event.jbutton.button )
163           {
164             case GP2X_BUTTON_UPLEFT:
165               button[C_UP].state=0;
166               button[C_UP].time=0;
167               button[C_LEFT].state=0;
168               button[C_LEFT].time=0;
169             break;
170             case GP2X_BUTTON_UPRIGHT:
171               button[C_UP].state=0;
172               button[C_UP].time=0;
173               button[C_RIGHT].state=0;
174               button[C_RIGHT].time=0;
175             break;
176             case GP2X_BUTTON_DOWNLEFT:
177               button[C_DOWN].state=0;
178               button[C_DOWN].time=0;
179               button[C_LEFT].state=0;
180               button[C_LEFT].time=0;
181             break;
182             case GP2X_BUTTON_DOWNRIGHT:
183               button[C_DOWN].state=0;
184               button[C_DOWN].time=0;
185               button[C_RIGHT].state=0;
186               button[C_RIGHT].time=0;
187             break;
188           }
189         break;
190 
191 	#else
192 	//Joystick
193 	case SDL_JOYAXISMOTION:	// Analog joystick movement
194 		switch(event.jaxis.axis)
195 		{
196 			case 0:
197 				if(event.jaxis.value < -PLATFORM_JOY_DEADZONE && joyCanMoveX)
198 				{
199 					joyCanMoveX = 0;
200 					// left movement
201 					button[C_LEFT].state=1;
202 					button[C_LEFT].time=0;
203 				}
204 				else if(event.jaxis.value > PLATFORM_JOY_DEADZONE && joyCanMoveX)
205 				{
206 					joyCanMoveX = 0;
207 					// right movement
208 					button[C_RIGHT].state=1;
209 					button[C_RIGHT].time=0;
210 				}
211 				else if(event.jaxis.value > -PLATFORM_JOY_DEADZONE && event.jaxis.value < PLATFORM_JOY_DEADZONE)
212 				{
213 					joyCanMoveX = 1;
214 					// is at center
215 					button[C_LEFT].state=0;
216 					button[C_LEFT].time=0;
217 					button[C_RIGHT].state=0;
218 					button[C_RIGHT].time=0;
219 				}
220 			break;
221 			case 1:
222 				if(event.jaxis.value < -PLATFORM_JOY_DEADZONE && joyCanMoveY)
223 				{
224 					joyCanMoveY = 0;
225 					// up movement
226 					button[C_UP].state=1;
227 					button[C_UP].time=0;
228 				}
229 				else if(event.jaxis.value > PLATFORM_JOY_DEADZONE && joyCanMoveY)
230 				{
231 					joyCanMoveY = 0;
232 					// down movement
233 					button[C_DOWN].state=1;
234 					button[C_DOWN].time=0;
235 				}
236 				else if(event.jaxis.value > -PLATFORM_JOY_DEADZONE && event.jaxis.value < PLATFORM_JOY_DEADZONE)
237 				{
238 					joyCanMoveY = 1;
239 					// is at center
240 					button[C_UP].state=0;
241 					button[C_UP].time=0;
242 					button[C_DOWN].state=0;
243 					button[C_DOWN].time=0;
244 				}
245 			break;
246 
247 			default:
248 			break;
249 		}
250 	break;
251 
252 	case SDL_JOYHATMOTION:
253 		button[C_UP].state=(event.jhat.value & SDL_HAT_UP);
254 		button[C_UP].time=0;
255 		button[C_DOWN].state=(event.jhat.value & SDL_HAT_DOWN);
256 		button[C_DOWN].time=0;
257 		button[C_LEFT].state=(event.jhat.value & SDL_HAT_LEFT);
258 		button[C_LEFT].time=0;
259 		button[C_RIGHT].state=(event.jhat.value & SDL_HAT_RIGHT);
260 		button[C_RIGHT].time=0;
261 	break;
262 
263 	case SDL_JOYBUTTONDOWN:
264 		switch (event.jbutton.button)
265 		{
266 			case 0:
267 				button[C_BTNB].state = 1;
268 				button[C_BTNB].time=0;
269 			break;
270 			case 1:
271 				button[C_BTNX].state = 1;
272 				button[C_BTNX].time=0;
273 			break;
274 			case 2:
275 				button[C_BTNA].state = 1;
276 				button[C_BTNA].time=0;
277 			break;
278 			case 3:
279 				button[C_BTNB].state = 1;
280 				button[C_BTNB].time=0;
281 			break;
282 			case 4:
283 				button[C_BTNMENU].state = 1;
284 				button[C_BTNMENU].time=0;
285 			break;
286 			case 5:
287 				button[C_BTNSELECT].state = 1;
288 				button[C_BTNSELECT].time=0;
289 			break;
290 		}
291 	break;
292 
293 	case SDL_JOYBUTTONUP:
294 		switch (event.jbutton.button)
295 		{
296 			case 0:
297 				button[C_BTNB].state = 0;
298 				button[C_BTNB].time=0;
299 			break;
300 			case 1:
301 				button[C_BTNX].state = 0;
302 				button[C_BTNX].time=0;
303 			break;
304 			case 2:
305 				button[C_BTNA].state = 0;
306 				button[C_BTNA].time=0;
307 			break;
308 			case 3:
309 				button[C_BTNB].state = 0;
310 				button[C_BTNB].time=0;
311 			break;
312 			case 4:
313 				button[C_BTNMENU].state = 0;
314 				button[C_BTNMENU].time=0;
315 			break;
316 			case 5:
317 				button[C_BTNSELECT].state = 0;
318 				button[C_BTNSELECT].time=0;
319 			break;
320 		}
321 	break;
322 
323         //Keyboard
324         case SDL_KEYDOWN:
325           for(i=0; i < C_NUM; i++)
326           {
327             if( event.key.keysym.sym == button[i].button )
328             {
329               button[i].state=1;
330               button[i].time=0;
331             }
332           }
333 
334           if( event.key.keysym.sym > 31 && event.key.keysym.sym < 123 )
335           {
336             inputChar=event.key.keysym.sym;
337 
338             if( event.key.keysym.mod & KMOD_SHIFT )
339             {
340               inputChar=toupper(inputChar);
341             }
342           } else if( event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_RETURN )
343           {
344             inputChar=event.key.keysym.sym;
345 #if defined(PC)
346           } else if( event.key.keysym.sym == SDLK_F1 )
347           {
348             screenShot();
349 #endif
350           }
351         break;
352         case SDL_KEYUP:
353           for(i=0; i < C_NUM; i++)
354           {
355             if( event.key.keysym.sym == button[i].button )
356             {
357               button[i].state=0;
358               button[i].time=0;
359             }
360           }
361         break;
362         #endif
363 
364         //Handle pointer events
365         case SDL_MOUSEBUTTONDOWN:
366           if( getInpPointerState()->curX > -1 && getInpPointerState()->curX< 11 &&
367               getInpPointerState()->curY > -1 && getInpPointerState()->curY < 11 )
368           {
369             getInpPointerState()->downTime=0;
370             getInpPointerState()->isDown=1;
371             getInpPointerState()->hitABox=0; //Clear precvious state.
372           }
373         break;
374         case SDL_MOUSEBUTTONUP:
375           getInpPointerState()->isDown=0;
376           getInpPointerState()->timeSinceMoved=1;
377         break;
378         case SDL_MOUSEMOTION:
379 
380           getInpPointerState()->vpX = (event.motion.x/setting()->scaleFactor);
381           getInpPointerState()->vpY = (event.motion.y/setting()->scaleFactor);
382 
383           getInpPointerState()->curX = (getInpPointerState()->vpX-(boardOffsetX*setting()->scaleFactor))/(20);
384           getInpPointerState()->curY = (getInpPointerState()->vpY-(boardOffsetY*setting()->scaleFactor))/(20);
385 
386           getInpPointerState()->timeSinceMoved=0;
387 
388           if( getInpPointerState()->curX < 0 ) getInpPointerState()->curX = 0;
389           if( getInpPointerState()->curX > 10 ) getInpPointerState()->curX = 10;
390           if( getInpPointerState()->curY < 0 ) getInpPointerState()->curY = 0;
391           if( getInpPointerState()->curY > 10 ) getInpPointerState()->curY = 10;
392         break;
393 
394 
395         case SDL_QUIT:
396           return(1);
397         break;
398     }
399   }
400 
401   return(0);
402 }
403 
initControls()404 void initControls()
405 {
406   button[C_UP].button = PLATFORM_BUTTON_UP;
407   button[C_DOWN].button = PLATFORM_BUTTON_DOWN;
408   button[C_LEFT].button = PLATFORM_BUTTON_LEFT;
409   button[C_RIGHT].button = PLATFORM_BUTTON_RIGHT;
410   button[C_BTNX].button = PLATFORM_BUTTON_X;
411   button[C_BTNY].button = PLATFORM_BUTTON_Y;
412   button[C_BTNA].button = PLATFORM_BUTTON_A;
413   button[C_BTNB].button = PLATFORM_BUTTON_B;
414   button[C_SHOULDERA].button = PLATFORM_SHOULDER_LEFT;
415   button[C_SHOULDERB].button = PLATFORM_SHOULLER_RIGHT;
416   button[C_BTNMENU].button = PLATFORM_BUTTON_MENU;
417   button[C_BTNSELECT].button = PLATFORM_BUTTON_SELECT;
418   button[C_BTNVOLUP].button = PLATFORM_BUTTON_VOLUP;
419   button[C_BTNVOLDOWN].button = PLATFORM_BUTTON_VOLDOWN;
420 }
421 
422 
423