comparison src/events/SDL_touch.c @ 4655:4c94f2023d62

Fixed bugs in input, cleaned up $1
author Jim Grandpre <jim.tla@gmail.com>
date Fri, 18 Jun 2010 01:43:02 -0400
parents 7dbcd71216df
children eed063a0bf5b
comparison
equal deleted inserted replaced
4654:7dbcd71216df 4655:4c94f2023d62
298 298
299 299
300 SDL_free(finger); 300 SDL_free(finger);
301 touch->num_fingers--; 301 touch->num_fingers--;
302 touch->fingers[index] = touch->fingers[touch->num_fingers]; 302 touch->fingers[index] = touch->fingers[touch->num_fingers];
303 return 0; 303 return 0;
304 } 304 }
305 305
306 306
307 int 307 int
308 SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure) 308 SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure)
309 { 309 {
310 int posted; 310 int posted;
311 SDL_Touch* touch = SDL_GetTouch(id); 311 SDL_Touch* touch = SDL_GetTouch(id);
312 312
313 if(down) { 313 if(down) {
314 SDL_Finger nf; 314 SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
315 nf.id = fingerid; 315 if(finger == NULL) {
316 nf.x = x; 316 SDL_Finger nf;
317 nf.y = y; 317 nf.id = fingerid;
318 nf.pressure = pressure; 318 nf.x = x;
319 nf.xdelta = 0; 319 nf.y = y;
320 nf.ydelta = 0; 320 nf.pressure = pressure;
321 nf.last_x = x; 321 nf.xdelta = 0;
322 nf.last_y = y; 322 nf.ydelta = 0;
323 nf.last_pressure = pressure; 323 nf.last_x = x;
324 SDL_AddFinger(touch,&nf); 324 nf.last_y = y;
325 //if(x < 0 || y < 0) return 0; //should defer if only a partial input 325 nf.last_pressure = pressure;
326 nf.down = SDL_FALSE;
327 SDL_AddFinger(touch,&nf);
328 finger = &nf;
329 }
330 else if(finger->down) return 0;
331 if(x < 0 || y < 0) return 0; //should defer if only a partial input
326 posted = 0; 332 posted = 0;
327 if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) { 333 if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
328 SDL_Event event; 334 SDL_Event event;
329 event.tfinger.type = SDL_FINGERDOWN; 335 event.tfinger.type = SDL_FINGERDOWN;
330 event.tfinger.touchId = (Uint8) id; 336 event.tfinger.touchId = (Uint8) id;
333 event.tfinger.state = touch->buttonstate; 339 event.tfinger.state = touch->buttonstate;
334 event.tfinger.windowID = touch->focus ? touch->focus->id : 0; 340 event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
335 event.tfinger.fingerId = fingerid; 341 event.tfinger.fingerId = fingerid;
336 posted = (SDL_PushEvent(&event) > 0); 342 posted = (SDL_PushEvent(&event) > 0);
337 } 343 }
344 if(posted) finger->down = SDL_TRUE;
338 return posted; 345 return posted;
339 } 346 }
340 else { 347 else {
341 SDL_DelFinger(touch,fingerid); 348 if(SDL_DelFinger(touch,fingerid) < 0) return 0;
342 posted = 0; 349 posted = 0;
343 if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) { 350 if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
344 SDL_Event event; 351 SDL_Event event;
345 event.tfinger.type = SDL_FINGERUP; 352 event.tfinger.type = SDL_FINGERUP;
346 event.tfinger.touchId = (Uint8) id; 353 event.tfinger.touchId = (Uint8) id;
362 SDL_Finger *finger = SDL_GetFinger(touch,fingerid); 369 SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
363 int posted; 370 int posted;
364 int xrel; 371 int xrel;
365 int yrel; 372 int yrel;
366 int x_max = 0, y_max = 0; 373 int x_max = 0, y_max = 0;
367 374
368 if (!touch || touch->flush_motion) { 375 if (!touch || touch->flush_motion) {
369 return 0; 376 return 0;
370 } 377 }
371 378
372 if(finger == NULL) { 379 if(finger == NULL || !finger->down) {
373 return SDL_SendFingerDown(id,fingerid,SDL_TRUE,x,y,pressure); 380 return SDL_SendFingerDown(id,fingerid,SDL_TRUE,x,y,pressure);
374 } else { 381 } else {
375 /* the relative motion is calculated regarding the last position */ 382 /* the relative motion is calculated regarding the last position */
376 if (relative) { 383 if (relative) {
377 xrel = x; 384 xrel = x;
378 yrel = y; 385 yrel = y;
379 x = (finger->last_x + x); 386 x = (finger->last_x + x);
380 y = (finger->last_y + y); 387 y = (finger->last_y + y);
381 } else { 388 } else {
382 if(x < 0) x = finger->last_x; /*If movement is only in one axis,*/ 389 if(x < 0) x = finger->last_x; /*If movement is only in one axis,*/
383 if(y < 0) y = finger->last_y; /*The other is marked as -1*/ 390 if(y < 0) y = finger->last_y; /*The other is marked as -1*/
384 if(pressure < 0) pressure = finger->last_pressure; 391 if(pressure < 0) pressure = finger->last_pressure;
385 xrel = x - finger->last_x; 392 xrel = x - finger->last_x;
386 yrel = y - finger->last_y; 393 yrel = y - finger->last_y;
387 } 394 }
388 395
389 /* Drop events that don't change state */ 396 /* Drop events that don't change state */
390 if (!xrel && !yrel) { 397 if (!xrel && !yrel) {
391 #if 0 398 #if 0
392 printf("Touch event didn't change state - dropped!\n"); 399 printf("Touch event didn't change state - dropped!\n");
393 #endif 400 #endif
394 return 0; 401 return 0;
395 } 402 }
396 403
397 /* Update internal touch coordinates */ 404 /* Update internal touch coordinates */
398 405
399 finger->x = x; 406 finger->x = x;
400 finger->y = y; 407 finger->y = y;
401 408
402 /*Should scale to window? Normalize? Maintain Aspect?*/ 409 /*Should scale to window? Normalize? Maintain Aspect?*/
403 //SDL_GetWindowSize(touch->focus, &x_max, &y_max); 410 //SDL_GetWindowSize(touch->focus, &x_max, &y_max);
404 411
405 /* make sure that the pointers find themselves inside the windows */ 412 /* make sure that the pointers find themselves inside the windows */
406 /* only check if touch->xmax is set ! */ 413 /* only check if touch->xmax is set ! */
407 /* 414 /*
408 if (x_max && touch->x > x_max) { 415 if (x_max && touch->x > x_max) {
409 touch->x = x_max; 416 touch->x = x_max;
410 } else if (touch->x < 0) { 417 } else if (touch->x < 0) {
411 touch->x = 0; 418 touch->x = 0;
412 } 419 }
413 420
414 if (y_max && touch->y > y_max) { 421 if (y_max && touch->y > y_max) {
415 touch->y = y_max; 422 touch->y = y_max;
416 } else if (touch->y < 0) { 423 } else if (touch->y < 0) {
417 touch->y = 0; 424 touch->y = 0;
418 } 425 }
419 */ 426 */
420 finger->xdelta = xrel; 427 finger->xdelta = xrel;
421 finger->ydelta = yrel; 428 finger->ydelta = yrel;
422 finger->pressure = pressure; 429 finger->pressure = pressure;
423 430
424 431
425 432
426 /* Post the event, if desired */ 433 /* Post the event, if desired */
427 posted = 0; 434 posted = 0;
428 if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) { 435 if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
429 SDL_Event event; 436 SDL_Event event;
430 event.tfinger.type = SDL_FINGERMOTION; 437 event.tfinger.type = SDL_FINGERMOTION;
431 event.tfinger.touchId = (Uint8) id; 438 event.tfinger.touchId = (Uint8) id;
432 event.tfinger.fingerId = (Uint8) fingerid; 439 event.tfinger.fingerId = (Uint8) fingerid;
433 event.tfinger.x = x; 440 event.tfinger.x = x;
434 event.tfinger.y = y; 441 event.tfinger.y = y;
435 event.tfinger.pressure = pressure; 442 event.tfinger.pressure = pressure;
436 event.tfinger.state = touch->buttonstate; 443 event.tfinger.state = touch->buttonstate;
437 event.tfinger.windowID = touch->focus ? touch->focus->id : 0; 444 event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
438 posted = (SDL_PushEvent(&event) > 0); 445 posted = (SDL_PushEvent(&event) > 0);
439 } 446 }
440 finger->last_x = finger->x; 447 finger->last_x = finger->x;
441 finger->last_y = finger->y; 448 finger->last_y = finger->y;
442 finger->last_pressure = finger->pressure; 449 finger->last_pressure = finger->pressure;
443 return posted; 450 return posted;
444 } 451 }
445 } 452 }
446 int 453 int
447 SDL_SendTouchButton(int id, Uint8 state, Uint8 button) 454 SDL_SendTouchButton(int id, Uint8 state, Uint8 button)
448 { 455 {
449 SDL_Touch *touch = SDL_GetTouch(id); 456 SDL_Touch *touch = SDL_GetTouch(id);