changeset 1351:1b8e37770d78 broker

ID vs CSS selector distinction in API and field names.
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 15 Apr 2020 12:57:36 +0200
parents c53a4667f895
children 159708a38af4
files Framework/OpenGL/WebAssemblyOpenGLContext.cpp Framework/OpenGL/WebAssemblyOpenGLContext.h Framework/Viewport/WebAssemblyCairoViewport.cpp Framework/Viewport/WebAssemblyViewport.cpp Framework/Viewport/WebAssemblyViewport.h Framework/Viewport/WebGLViewport.cpp
diffstat 6 files changed, 51 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/OpenGL/WebAssemblyOpenGLContext.cpp	Tue Apr 14 14:57:06 2020 +0200
+++ b/Framework/OpenGL/WebAssemblyOpenGLContext.cpp	Wed Apr 15 12:57:36 2020 +0200
@@ -37,26 +37,27 @@
     class WebAssemblyOpenGLContext::PImpl
     {
     private:
-      std::string                     canvas_;
+      std::string                     canvasSelector_;
       EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context_;
       unsigned int                    canvasWidth_;
       unsigned int                    canvasHeight_;
       bool                            isContextLost_;
 
     public:
-      PImpl(const std::string& canvas)
-        : canvas_(canvas)
+      PImpl(const std::string& canvasSelector)
+        : canvasSelector_(canvasSelector)
         , isContextLost_(false)
       {
         // Context configuration
         EmscriptenWebGLContextAttributes attr; 
         emscripten_webgl_init_context_attributes(&attr);
 
-        context_ = emscripten_webgl_create_context(canvas.c_str(), &attr);
+        context_ = emscripten_webgl_create_context(canvasSelector.c_str(), &attr);
         if (context_ == 0)
         {
-          std::string message("Cannot create an OpenGL context for canvas: ");
-          message += canvas;
+          std::string message("Cannot create an OpenGL context for the element with the following CSS selector: \"");
+          message += canvasSelector;
+          message += "\"  Please make sure the -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 flag has been passed to Emscripten when building.";
           LOG(ERROR) << message;
           throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, message);
         }
@@ -113,9 +114,9 @@
         }
       }
 
-      const std::string& GetCanvasIdentifier() const
+      const std::string& GetCanvasSelector() const
       {
-        return canvas_;
+        return canvasSelector_;
       }
 
       void MakeCurrent()
@@ -128,7 +129,7 @@
 
         if (emscripten_is_webgl_context_lost(context_))
         {
-          LOG(ERROR) << "OpenGL context has been lost for canvas: " << canvas_;
+          LOG(ERROR) << "OpenGL context has been lost for canvas selector: " << canvasSelector_;
           SetLostContext();
           throw StoneException(ErrorCode_WebGLContextLost);
         }
@@ -166,7 +167,7 @@
       void UpdateSize()
       {
         double w, h;
-        emscripten_get_element_css_size(canvas_.c_str(), &w, &h);
+        emscripten_get_element_css_size(canvasSelector_.c_str(), &w, &h);
 
         /**
          * Emscripten has the function emscripten_get_element_css_size()
@@ -190,13 +191,13 @@
           canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h));
         }
         
-        emscripten_set_canvas_element_size(canvas_.c_str(), canvasWidth_, canvasHeight_);
+        emscripten_set_canvas_element_size(canvasSelector_.c_str(), canvasWidth_, canvasHeight_);
       }
     };
 
 
-    WebAssemblyOpenGLContext::WebAssemblyOpenGLContext(const std::string& canvas) :
-      pimpl_(new PImpl(canvas))
+    WebAssemblyOpenGLContext::WebAssemblyOpenGLContext(const std::string& canvasSelector) :
+      pimpl_(new PImpl(canvasSelector))
     {
     }
 
@@ -245,10 +246,10 @@
       pimpl_->UpdateSize();
     }
 
-    const std::string& WebAssemblyOpenGLContext::GetCanvasIdentifier() const
+    const std::string& WebAssemblyOpenGLContext::GetCanvasSelector() const
     {
       assert(pimpl_.get() != NULL);
-      return pimpl_->GetCanvasIdentifier();
+      return pimpl_->GetCanvasSelector();
     }
   }
 }
--- a/Framework/OpenGL/WebAssemblyOpenGLContext.h	Tue Apr 14 14:57:06 2020 +0200
+++ b/Framework/OpenGL/WebAssemblyOpenGLContext.h	Wed Apr 15 12:57:36 2020 +0200
@@ -54,7 +54,7 @@
       boost::shared_ptr<PImpl>  pimpl_;
 
     public:
-      WebAssemblyOpenGLContext(const std::string& canvas);
+      WebAssemblyOpenGLContext(const std::string& canvasSelector);
 
       virtual bool IsContextLost() ORTHANC_OVERRIDE;
 
@@ -73,7 +73,7 @@
 
       void UpdateSize();
 
-      const std::string& GetCanvasIdentifier() const;
+      const std::string& GetCanvasSelector() const;
 
 
       /**
--- a/Framework/Viewport/WebAssemblyCairoViewport.cpp	Tue Apr 14 14:57:06 2020 +0200
+++ b/Framework/Viewport/WebAssemblyCairoViewport.cpp	Wed Apr 15 12:57:36 2020 +0200
@@ -31,7 +31,7 @@
                                                unsigned int& height)
   {
     double w, h;
-    emscripten_get_element_css_size(GetFullCanvasId().c_str(), &w, &h);
+    emscripten_get_element_css_size(GetCanvasCssSelector().c_str(), &w, &h);
 
     /**
      * Emscripten has the function emscripten_get_element_css_size()
@@ -105,7 +105,7 @@
         const ctx = document.getElementById(UTF8ToString($0)).getContext('2d');
         ctx.putImageData(img, 0, 0);
       },
-      GetShortCanvasId().c_str(), // $0
+      GetCanvasId().c_str(), // $0
       javascript_->GetBuffer(),   // $1
       javascript_->GetWidth(),    // $2
       javascript_->GetHeight());  // $3
@@ -116,7 +116,7 @@
   {
     unsigned int width, height;
     GetCanvasSize(width, height);
-    emscripten_set_canvas_element_size(GetFullCanvasId().c_str(), width, height);
+    emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), width, height);
 
     dynamic_cast<CairoCompositor&>(compositor).UpdateSize(width, height);
   }
@@ -128,7 +128,7 @@
   {
     unsigned int width, height;
     GetCanvasSize(width, height);
-    emscripten_set_canvas_element_size(GetFullCanvasId().c_str(), 
+    emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), 
                                        width, 
                                        height);
 
--- a/Framework/Viewport/WebAssemblyViewport.cpp	Tue Apr 14 14:57:06 2020 +0200
+++ b/Framework/Viewport/WebAssemblyViewport.cpp	Wed Apr 15 12:57:36 2020 +0200
@@ -139,7 +139,7 @@
   {
     WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
 
-    LOG(TRACE) << "mouse down: " << that->GetFullCanvasId();      
+    LOG(TRACE) << "mouse down: " << that->GetCanvasCssSelector();      
 
     if (that->compositor_.get() != NULL &&
         that->interactor_.get() != NULL)
@@ -213,8 +213,8 @@
 
   WebAssemblyViewport::WebAssemblyViewport(
     const std::string& canvasId, bool enableEmscriptenMouseEvents) :
-    shortCanvasId_(canvasId),
-    fullCanvasId_(canvasId),
+    canvasId_(canvasId),
+    canvasCssSelector_("#" + canvasId),
     interactor_(new DefaultViewportInteractor),
     enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents)
   {
@@ -226,10 +226,10 @@
     controller_.reset(new ViewportController(viewport));
 
     LOG(INFO) << "Initializing Stone viewport on HTML canvas: " 
-      << shortCanvasId_;
+      << canvasId_;
 
-    if (shortCanvasId_.empty() ||
-        shortCanvasId_[0] == '#')
+    if (canvasId_.empty() ||
+        canvasId_[0] == '#')
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
         "The canvas identifier must not start with '#'");
@@ -243,7 +243,7 @@
           event.preventDefault();
         }
       },
-      shortCanvasId_.c_str()   // $0
+      canvasId_.c_str()   // $0
       );
 
     // It is not possible to monitor the resizing of individual
@@ -256,17 +256,22 @@
     if (enableEmscriptenMouseEvents_)
     {
 
-      emscripten_set_mousedown_callback(fullCanvasId_.c_str(),
+      // if any of this function causes an error in the console, please
+      // make sure you are using the new (as of 1.39.x) version of 
+      // emscripten element lookup rules( pass 
+      // "-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1" to the linker.
+
+      emscripten_set_mousedown_callback(canvasCssSelector_.c_str(),
                                         reinterpret_cast<void*>(this),
                                         false,
                                         OnMouseDown);
 
-      emscripten_set_mousemove_callback(fullCanvasId_.c_str(),
+      emscripten_set_mousemove_callback(canvasCssSelector_.c_str(),
                                         reinterpret_cast<void*>(this),
                                         false,
                                         OnMouseMove);
 
-      emscripten_set_mouseup_callback(fullCanvasId_.c_str(),
+      emscripten_set_mouseup_callback(canvasCssSelector_.c_str(),
                                       reinterpret_cast<void*>(this),
                                       false,
                                       OnMouseUp);
@@ -283,17 +288,17 @@
     if (enableEmscriptenMouseEvents_)
     {
 
-      emscripten_set_mousedown_callback(fullCanvasId_.c_str(),
+      emscripten_set_mousedown_callback(canvasCssSelector_.c_str(),
                                         reinterpret_cast<void*>(this),
                                         false,
                                         OnMouseDown);
 
-      emscripten_set_mousemove_callback(fullCanvasId_.c_str(),
+      emscripten_set_mousemove_callback(canvasCssSelector_.c_str(),
                                         reinterpret_cast<void*>(this),
                                         false,
                                         OnMouseMove);
 
-      emscripten_set_mouseup_callback(fullCanvasId_.c_str(),
+      emscripten_set_mouseup_callback(canvasCssSelector_.c_str(),
                                       reinterpret_cast<void*>(this),
                                       false,
                                       OnMouseUp);
--- a/Framework/Viewport/WebAssemblyViewport.h	Tue Apr 14 14:57:06 2020 +0200
+++ b/Framework/Viewport/WebAssemblyViewport.h	Wed Apr 15 12:57:36 2020 +0200
@@ -48,8 +48,8 @@
   private:
     class WasmLock;
     
-    std::string                           shortCanvasId_;
-    std::string                           fullCanvasId_;
+    std::string                           canvasId_;
+    std::string                           canvasCssSelector_;
     std::unique_ptr<ICompositor>          compositor_;
     std::unique_ptr<ViewportController>   controller_;
     std::unique_ptr<IViewportInteractor>  interactor_;
@@ -105,14 +105,18 @@
     */
     void AcquireInteractor(IViewportInteractor* interactor);
 
-    const std::string& GetShortCanvasId() const
+    const std::string& GetCanvasId() const
     {
-      return shortCanvasId_;
+      return canvasId_;
     }
 
-    const std::string& GetFullCanvasId() const
+    /**
+    emscripten functions requires the css selector for the canvas. This is 
+    different from the canvas id (the syntax is '#mycanvasid')
+    */
+    const std::string& GetCanvasCssSelector() const
     {
-      return fullCanvasId_;
+      return canvasCssSelector_;
     }
   };
 }
--- a/Framework/Viewport/WebGLViewport.cpp	Tue Apr 14 14:57:06 2020 +0200
+++ b/Framework/Viewport/WebGLViewport.cpp	Wed Apr 15 12:57:36 2020 +0200
@@ -81,7 +81,7 @@
 
   WebGLViewport::WebGLViewport(const std::string& canvasId, bool enableEmscriptenMouseEvents) :
     WebAssemblyViewport(canvasId,enableEmscriptenMouseEvents),
-    context_(GetFullCanvasId())
+    context_(GetCanvasCssSelector())
   {
     AcquireCompositor(new OpenGLCompositor(context_));
   }