diff Platforms/Wasm/Defaults.cpp @ 457:3b4df9925db6 am-touch-events

added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
author Alain Mazy <alain@mazy.be>
date Thu, 24 Jan 2019 16:42:27 +0100
parents 26b90b110719
children e74f9271d653
line wrap: on
line diff
--- a/Platforms/Wasm/Defaults.cpp	Wed Jan 23 13:58:51 2019 +0100
+++ b/Platforms/Wasm/Defaults.cpp	Thu Jan 24 16:42:27 2019 +0100
@@ -49,7 +49,7 @@
 
     viewports_.push_back(viewport);
 
-    printf("There are now %d viewports in C++\n", viewports_.size());
+    printf("There are now %lu viewports in C++\n", viewports_.size());
 
     viewport->SetStatusBar(statusBar_);
 
@@ -64,7 +64,7 @@
   void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) {
     viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;});
 
-    printf("There are now %d viewports in C++\n", viewports_.size());
+    printf("There are now %lu viewports in C++\n", viewports_.size());
   }
 
   void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) {
@@ -191,7 +191,7 @@
         return;  // Unknown button
     }
 
-    viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */);
+    viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None, std::vector<OrthancStone::Touch>());
   }
   
 
@@ -222,9 +222,82 @@
                                               int x,
                                               int y)
   {
-    viewport->MouseMove(x, y);
+    viewport->MouseMove(x, y, std::vector<OrthancStone::Touch>());
+  }
+
+  void GetTouchVector(std::vector<OrthancStone::Touch>& output,
+                      int touchCount,
+                      float x0,
+                      float y0,
+                      float x1,
+                      float y1,
+                      float x2,
+                      float y2)
+  {
+    // TODO: it might be nice to try to pass all the x0,y0 coordinates as arrays but that's not so easy to pass array between JS and C++
+    if (touchCount > 0)
+    {
+      output.push_back(OrthancStone::Touch(x0, y0));
+    }
+    if (touchCount > 1)
+    {
+      output.push_back(OrthancStone::Touch(x1, y1));
+    }
+    if (touchCount > 2)
+    {
+      output.push_back(OrthancStone::Touch(x2, y2));
+    }
+
   }
-  
+
+  void EMSCRIPTEN_KEEPALIVE ViewportTouchStart(ViewportHandle viewport,
+                                              int touchCount,
+                                              float x0,
+                                              float y0,
+                                              float x1,
+                                              float y1,
+                                              float x2,
+                                              float y2)
+  {
+    printf("touch start with %d touches\n", touchCount);
+
+    std::vector<OrthancStone::Touch> touches;
+    GetTouchVector(touches, touchCount, x0, y0, x1, y1, x2, y2);
+    viewport->TouchStart(touches);
+  }
+
+  void EMSCRIPTEN_KEEPALIVE ViewportTouchMove(ViewportHandle viewport,
+                                              int touchCount,
+                                              float x0,
+                                              float y0,
+                                              float x1,
+                                              float y1,
+                                              float x2,
+                                              float y2)
+  {
+    printf("touch move with %d touches\n", touchCount);
+
+    std::vector<OrthancStone::Touch> touches;
+    GetTouchVector(touches, touchCount, x0, y0, x1, y1, x2, y2);
+    viewport->TouchMove(touches);
+  }
+
+  void EMSCRIPTEN_KEEPALIVE ViewportTouchEnd(ViewportHandle viewport,
+                                              int touchCount,
+                                              float x0,
+                                              float y0,
+                                              float x1,
+                                              float y1,
+                                              float x2,
+                                              float y2)
+  {
+    printf("touch end with %d touches remaining\n", touchCount);
+
+    std::vector<OrthancStone::Touch> touches;
+    GetTouchVector(touches, touchCount, x0, y0, x1, y1, x2, y2);
+    viewport->TouchEnd(touches);
+  }
+
   void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport,
                                                int key,
                                                const char* keyChar,