Browse Source

Fix for multitouch causing issues

seibelj 8 years ago
parent
commit
bde7c3c4e9
1 changed files with 27 additions and 5 deletions
  1. 27 5
      demo/allegro5/nuklear_allegro5.h

+ 27 - 5
demo/allegro5/nuklear_allegro5.h

@@ -56,6 +56,8 @@ static struct nk_allegro5 {
     ALLEGRO_DISPLAY *dsp;
     unsigned int width;
     unsigned int height;
+    int is_touch_down;
+    int touch_down_id;
     struct nk_context ctx;
     struct nk_buffer cmds;
 } allegro5;
@@ -309,27 +311,45 @@ nk_allegro5_handle_event(ALLEGRO_EVENT *ev)
             }
             nk_input_button(ctx, button, ev->mouse.x, ev->mouse.y, ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN);
         } break;
+        /* This essentially converts touch events to mouse events */
         case ALLEGRO_EVENT_TOUCH_BEGIN:
         case ALLEGRO_EVENT_TOUCH_END: {
             if (ev->touch.display != allegro5.dsp) {
                 return;
             }
+            /* We only acknowledge one touch at a time. Otherwise, each touch
+               would be manipulating multiple nuklear elements, as if there
+               were multiple mouse cursors */
+            if (allegro5.is_touch_down && allegro5.touch_down_id != ev->touch.id) {
+                return;
+            }
             if (ev->type == ALLEGRO_EVENT_TOUCH_BEGIN) {
-                // FIXME: This is a hack to properly simulate
-                // touches as a mouse with nuklear. If you instantly jump
-                // from one place to another without an nk_input_end(), it
-                // confuses the nuklear state. nuklear expects smooth mouse
-                // movements, which is unlike a touch screen
+                allegro5.is_touch_down = 1;
+                allegro5.touch_down_id = ev->touch.id;
+                /* FIXME: This is a hack to properly simulate
+                   touches as a mouse with nuklear. If you instantly jump
+                   from one place to another without an nk_input_end(), it
+                   confuses the nuklear state. nuklear expects smooth mouse
+                   movements, which is unlike a touch screen */
                 nk_input_motion(ctx, (int)ev->touch.x, (int)ev->touch.y);
                 nk_input_end(ctx);
                 nk_input_begin(ctx);
             }
+            else {
+                allegro5.is_touch_down = 0;
+                allegro5.touch_down_id = -1;
+            }
             nk_input_button(ctx, NK_BUTTON_LEFT, (int)ev->touch.x, (int)ev->touch.y, ev->type == ALLEGRO_EVENT_TOUCH_BEGIN);
         } break;
         case ALLEGRO_EVENT_TOUCH_MOVE: {
             if (ev->touch.display != allegro5.dsp) {
                 return;
             }
+            /* Only acknowledge movements of a single touch, we are
+               simulating a mouse cursor */
+            if (!allegro5.is_touch_down || allegro5.touch_down_id != ev->touch.id) {
+                return;
+            }
             nk_input_motion(ctx, (int)ev->touch.x, (int)ev->touch.y);
         } break;
         case ALLEGRO_EVENT_KEY_DOWN:
@@ -442,6 +462,8 @@ nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp, unsigned in
     allegro5.dsp = dsp;
     allegro5.width = width;
     allegro5.height = height;
+    allegro5.is_touch_down = 0;
+    allegro5.touch_down_id = -1;
 
     nk_init_default(&allegro5.ctx, font);
     allegro5.ctx.clip.copy = nk_allegro5_clipboard_copy;