changeset 229:b0ba3b38a23c am

ScheduleRedraw can handle multiple viewports
author am@osimis.io
date Thu, 14 Jun 2018 16:51:43 +0200
parents 210c1ce8e1a6
children 7d2631320615
files Platforms/WebAssembly/Defaults.cpp Platforms/WebAssembly/Defaults.h Platforms/WebAssembly/WasmWebService.js Platforms/WebAssembly/default-library.js Platforms/WebAssembly/defaultLibrary.js Platforms/WebAssembly/defaults.js Platforms/WebAssembly/wasm-application.js Platforms/WebAssembly/wasm-application.ts Platforms/WebAssembly/wasm-viewport.ts
diffstat 9 files changed, 71 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/Platforms/WebAssembly/Defaults.cpp	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/Defaults.cpp	Thu Jun 14 16:51:43 2018 +0200
@@ -24,8 +24,9 @@
     if (v.get() == viewport) {
       return v;
     }
-    assert(false);
   }
+  assert(false);
+  return std::shared_ptr<OrthancStone::WidgetViewport>();
 }
 
 #ifdef __cplusplus
--- a/Platforms/WebAssembly/Defaults.h	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/Defaults.h	Thu Jun 14 16:51:43 2018 +0200
@@ -9,17 +9,17 @@
 #include <Applications/Wasm/BasicWasmApplication.h>
 #include <Applications/Wasm/BasicWasmApplicationContext.h>
 
-typedef OrthancStone::WidgetViewport* ViewportHandle; // the objects exchanged between JS and C++
+typedef OrthancStone::IViewport* ViewportHandle; // the objects exchanged between JS and C++
 
 #ifdef __cplusplus
 extern "C" {
 #endif
   
   // JS methods accessible from C++
-  extern void ScheduleRedraw();
+  extern void ScheduleWebViewportRedrawFromCpp(ViewportHandle cppViewportHandle);
   
   // C++ methods accessible from JS
-  extern void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport);
+  extern void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle cppViewportHandle);
 
 #ifdef __cplusplus
 }
@@ -48,11 +48,11 @@
       isScheduled_ = false;
     }
 
-    virtual void NotifyChange(const OrthancStone::IViewport &scene)
+    virtual void NotifyChange(const OrthancStone::IViewport &viewport)
     {
       if (!isScheduled_)
       {
-        ScheduleRedraw();
+        ScheduleWebViewportRedrawFromCpp((ViewportHandle)&viewport);  // loosing constness when transmitted to Web
         isScheduled_ = true;
       }
     }
--- a/Platforms/WebAssembly/WasmWebService.js	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/WasmWebService.js	Thu Jun 14 16:51:43 2018 +0200
@@ -43,9 +43,5 @@
     }
 
     xhr.send(new Uint8ClampedArray(HEAPU8.buffer, body, bodySize));
-  },
-
-  ScheduleRedraw: function() {
-    ScheduleRedraw();
   }
 });
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platforms/WebAssembly/default-library.js	Thu Jun 14 16:51:43 2018 +0200
@@ -0,0 +1,8 @@
+// this file contains the JS method you want to expose to C++ code
+
+mergeInto(LibraryManager.library, {
+  ScheduleWebViewportRedrawFromCpp: function(cppViewportHandle) {
+    ScheduleWebViewportRedraw(cppViewportHandle);
+    }
+  });
+  
\ No newline at end of file
--- a/Platforms/WebAssembly/defaultLibrary.js	Thu Jun 14 15:06:29 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-// this file contains the JS method you want to expose to C++ code
-
-mergeInto(LibraryManager.library, {
-    ScheduleRedraw: function() {
-      ScheduleRedraw();
-    }
-  });
-  
\ No newline at end of file
--- a/Platforms/WebAssembly/defaults.js	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/defaults.js	Thu Jun 14 16:51:43 2018 +0200
@@ -1,13 +0,0 @@
-var isPendingRedraw = false;
-
-function ScheduleRedraw()
-{
-  if (!isPendingRedraw) {
-    isPendingRedraw = true;
-    //console.log('Scheduling a refresh of the viewport, as its content changed');
-    window.requestAnimationFrame(function() {
-      isPendingRedraw = false;
-      viewport.Redraw();
-    });
-  }
-}
--- a/Platforms/WebAssembly/wasm-application.js	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/wasm-application.js	Thu Jun 14 16:51:43 2018 +0200
@@ -4,7 +4,6 @@
 var viewport = null;
 var WasmWebService_NotifyError = null;
 var WasmWebService_NotifySuccess = null;
-//var NotifyRestApiGet = null;
 var NotifyUpdateContent = null;
 
 function UpdateContentThread()
@@ -62,7 +61,7 @@
                                  
   console.log("Creating main viewport");
 
-  viewport = new Stone.WasmViewport(StoneFrameworkModule, 'canvas');
+  viewport = new Stone.WasmViewport(StoneFrameworkModule, canvasId);
   viewport.Initialize(CreateCppViewport());
 
   // Prevent scrolling
@@ -70,7 +69,7 @@
     event.preventDefault();
   }, false);
   
-  document.getElementById('canvas').onclick = function() {
+  document.getElementById(canvasId).onclick = function() {
     viewport.Redraw();
   };
 
--- a/Platforms/WebAssembly/wasm-application.ts	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/wasm-application.ts	Thu Jun 14 16:51:43 2018 +0200
@@ -1,12 +1,37 @@
 ///<reference path='stone-framework-loader.ts'/>
+///<reference path='wasm-viewport.ts'/>
 
-declare function InitializeWasmApplication() :void; // still in a js file
+module Stone {
+  
+    //  export declare type InitializationCallback = () => void;
+      
+    //  export declare var StoneFrameworkModule : any;
+      
+      //const ASSETS_FOLDER : string = "assets/lib";
+      //const WASM_FILENAME : string = "orthanc-framework";
+      
+      export class WasmApplication {
+    
+        private viewport_ : WasmViewport;
+        private canvasId_: string;
+    
+        private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object
+    
+        public constructor(canvasId: string) {
+          this.canvasId_ = canvasId;
+            //this.module_ = module;
+        }
+    }
+}
+    
+
+declare function InitializeWasmApplication(canvasId: string) :void; // still in a js file
 
 
 
 
 // Wait for the Orthanc Framework to be initialized (this initializes
-// the WebAssembly environment)
+// the WebAssembly environment) and then, create and initialize the Wasm application
 Stone.Framework.Initialize(true, function() {
-    InitializeWasmApplication();
+    InitializeWasmApplication("canvas");
 });
\ No newline at end of file
--- a/Platforms/WebAssembly/wasm-viewport.ts	Thu Jun 14 15:06:29 2018 +0200
+++ b/Platforms/WebAssembly/wasm-viewport.ts	Thu Jun 14 16:51:43 2018 +0200
@@ -1,3 +1,17 @@
+
+  var isPendingRedraw = false;
+
+  function ScheduleWebViewportRedraw(cppViewportHandle: any) : void
+  {
+    if (!isPendingRedraw) {
+      isPendingRedraw = true;
+      console.log('Scheduling a refresh of the viewport, as its content changed');
+      window.requestAnimationFrame(function() {
+        isPendingRedraw = false;
+        Stone.WasmViewport.GetFromCppViewport(cppViewportHandle).Redraw();
+      });
+    }
+  }
 
 module Stone {
   
@@ -7,9 +21,11 @@
   
   //const ASSETS_FOLDER : string = "assets/lib";
   //const WASM_FILENAME : string = "orthanc-framework";
-  
+
   export class WasmViewport {
 
+    private static cppWebViewportsMaps_ : Map<any, WasmViewport> = new Map<any, WasmViewport>();
+
     private module_ : any;
     private canvasId_ : string;
     private htmlCanvas_ : HTMLCanvasElement;
@@ -52,6 +68,14 @@
       return this.pimpl_;
     }
 
+    public static GetFromCppViewport(cppViewportHandle: any) : WasmViewport {
+      if (WasmViewport.cppWebViewportsMaps_[cppViewportHandle] !== undefined) {
+        return WasmViewport.cppWebViewportsMaps_[cppViewportHandle];
+      }
+      console.log("WasmViewport not found !");
+      return undefined;
+    }
+
     public Redraw() {
       if (this.imageData_ === null ||
           this.renderingBuffer_ === null ||
@@ -99,6 +123,7 @@
 
     public Initialize(cppViewport: any) {
       this.pimpl_ = cppViewport;
+      WasmViewport.cppWebViewportsMaps_[this.pimpl_] = this;
       
       console.log(this.pimpl_);
       // Force the rendering of the viewport for the first time