|
26 | 26 | #include "SDL_events_c.h"
|
27 | 27 | #include "SDL_pen_c.h"
|
28 | 28 |
|
| 29 | +static SDL_PenID pen_touching = 0; // used for synthetic mouse/touch events. |
| 30 | + |
29 | 31 | typedef struct SDL_Pen
|
30 | 32 | {
|
31 | 33 | SDL_PenID instance_id;
|
@@ -111,6 +113,7 @@ void SDL_QuitPen(void)
|
111 | 113 | SDL_free(pen_devices);
|
112 | 114 | pen_devices = NULL;
|
113 | 115 | pen_device_count = 0;
|
| 116 | + pen_touching = 0; |
114 | 117 | }
|
115 | 118 |
|
116 | 119 | #if 0 // not a public API at the moment.
|
@@ -309,7 +312,7 @@ void SDL_RemoveAllPenDevices(void (*callback)(SDL_PenID instance_id, void *handl
|
309 | 312 | SDL_UnlockRWLock(pen_device_rwlock);
|
310 | 313 | }
|
311 | 314 |
|
312 |
| -void SDL_SendPenTouch(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window *window, bool eraser, bool down) |
| 315 | +void SDL_SendPenTouch(Uint64 timestamp, SDL_PenID instance_id, SDL_Window *window, bool eraser, bool down) |
313 | 316 | {
|
314 | 317 | bool send_event = false;
|
315 | 318 | SDL_PenInputFlags input_state = 0;
|
@@ -363,10 +366,45 @@ void SDL_SendPenTouch(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window
|
363 | 366 | event.ptouch.down = down;
|
364 | 367 | SDL_PushEvent(&event);
|
365 | 368 | }
|
| 369 | + |
| 370 | + SDL_Mouse *mouse = SDL_GetMouse(); |
| 371 | + if (mouse && window) { |
| 372 | + if (mouse->pen_mouse_events) { |
| 373 | + if (down) { |
| 374 | + if (!pen_touching) { |
| 375 | + SDL_SendMouseMotion(timestamp, window, SDL_PEN_MOUSEID, false, x, y); |
| 376 | + SDL_SendMouseButton(timestamp, window, SDL_PEN_MOUSEID, SDL_BUTTON_LEFT, true); |
| 377 | + } |
| 378 | + } else { |
| 379 | + if (pen_touching == instance_id) { |
| 380 | + SDL_SendMouseButton(timestamp, window, SDL_PEN_MOUSEID, SDL_BUTTON_LEFT, false); |
| 381 | + } |
| 382 | + } |
| 383 | + } |
| 384 | + |
| 385 | + if (mouse->pen_touch_events) { |
| 386 | + const SDL_EventType touchtype = down ? SDL_EVENT_FINGER_DOWN : SDL_EVENT_FINGER_UP; |
| 387 | + const float normalized_x = x / (float)window->w; |
| 388 | + const float normalized_y = y / (float)window->h; |
| 389 | + if (!pen_touching || (pen_touching == instance_id)) { |
| 390 | + SDL_SendTouch(timestamp, SDL_PEN_TOUCHID, SDL_BUTTON_LEFT, window, touchtype, normalized_x, normalized_y, pen->axes[SDL_PEN_AXIS_PRESSURE]); |
| 391 | + } |
| 392 | + } |
| 393 | + } |
| 394 | + |
| 395 | + if (down) { |
| 396 | + if (!pen_touching) { |
| 397 | + pen_touching = instance_id; |
| 398 | + } |
| 399 | + } else { |
| 400 | + if (pen_touching == instance_id) { |
| 401 | + pen_touching = 0; |
| 402 | + } |
| 403 | + } |
366 | 404 | }
|
367 | 405 | }
|
368 | 406 |
|
369 |
| -void SDL_SendPenAxis(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window *window, SDL_PenAxis axis, float value) |
| 407 | +void SDL_SendPenAxis(Uint64 timestamp, SDL_PenID instance_id, SDL_Window *window, SDL_PenAxis axis, float value) |
370 | 408 | {
|
371 | 409 | SDL_assert((axis >= 0) && (axis < SDL_PEN_AXIS_COUNT)); // fix the backend if this triggers.
|
372 | 410 |
|
@@ -405,10 +443,19 @@ void SDL_SendPenAxis(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window *
|
405 | 443 | event.paxis.axis = axis;
|
406 | 444 | event.paxis.value = value;
|
407 | 445 | SDL_PushEvent(&event);
|
| 446 | + |
| 447 | + if (window && (axis == SDL_PEN_AXIS_PRESSURE) && (pen_touching == instance_id)) { |
| 448 | + SDL_Mouse *mouse = SDL_GetMouse(); |
| 449 | + if (mouse && mouse->pen_touch_events) { |
| 450 | + const float normalized_x = x / (float)window->w; |
| 451 | + const float normalized_y = y / (float)window->h; |
| 452 | + SDL_SendTouchMotion(timestamp, SDL_PEN_TOUCHID, SDL_BUTTON_LEFT, window, normalized_x, normalized_y, value); |
| 453 | + } |
| 454 | + } |
408 | 455 | }
|
409 | 456 | }
|
410 | 457 |
|
411 |
| -void SDL_SendPenMotion(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window *window, float x, float y) |
| 458 | +void SDL_SendPenMotion(Uint64 timestamp, SDL_PenID instance_id, SDL_Window *window, float x, float y) |
412 | 459 | {
|
413 | 460 | bool send_event = false;
|
414 | 461 | SDL_PenInputFlags input_state = 0;
|
@@ -440,10 +487,25 @@ void SDL_SendPenMotion(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window
|
440 | 487 | event.pmotion.x = x;
|
441 | 488 | event.pmotion.y = y;
|
442 | 489 | SDL_PushEvent(&event);
|
| 490 | + |
| 491 | + if (window && (pen_touching == instance_id)) { |
| 492 | + SDL_Mouse *mouse = SDL_GetMouse(); |
| 493 | + if (mouse) { |
| 494 | + if (mouse->pen_mouse_events) { |
| 495 | + SDL_SendMouseMotion(timestamp, window, SDL_PEN_MOUSEID, false, x, y); |
| 496 | + } |
| 497 | + |
| 498 | + if (mouse->pen_touch_events) { |
| 499 | + const float normalized_x = x / (float)window->w; |
| 500 | + const float normalized_y = y / (float)window->h; |
| 501 | + SDL_SendTouchMotion(timestamp, SDL_PEN_TOUCHID, SDL_BUTTON_LEFT, window, normalized_x, normalized_y, pen->axes[SDL_PEN_AXIS_PRESSURE]); |
| 502 | + } |
| 503 | + } |
| 504 | + } |
443 | 505 | }
|
444 | 506 | }
|
445 | 507 |
|
446 |
| -void SDL_SendPenButton(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window *window, Uint8 button, bool down) |
| 508 | +void SDL_SendPenButton(Uint64 timestamp, SDL_PenID instance_id, SDL_Window *window, Uint8 button, bool down) |
447 | 509 | {
|
448 | 510 | bool send_event = false;
|
449 | 511 | SDL_PenInputFlags input_state = 0;
|
@@ -492,6 +554,13 @@ void SDL_SendPenButton(Uint64 timestamp, SDL_PenID instance_id, const SDL_Window
|
492 | 554 | event.pbutton.button = button;
|
493 | 555 | event.pbutton.down = down;
|
494 | 556 | SDL_PushEvent(&event);
|
| 557 | + |
| 558 | + if (window && (pen_touching == instance_id)) { |
| 559 | + SDL_Mouse *mouse = SDL_GetMouse(); |
| 560 | + if (mouse && mouse->pen_mouse_events) { |
| 561 | + SDL_SendMouseButton(timestamp, window, SDL_PEN_MOUSEID, button + 1, down); |
| 562 | + } |
| 563 | + } |
495 | 564 | }
|
496 | 565 | }
|
497 | 566 | }
|
|
0 commit comments