changeset 617:7efa2543699d

simplification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 May 2019 18:21:59 +0200
parents 97926984d5d0
children 0925b27e8750
files Framework/OpenGL/WebAssemblyOpenGLContext.cpp Framework/OpenGL/WebAssemblyOpenGLContext.h Framework/Scene2D/Internals/FixedPointAligner.cpp Framework/Scene2D/Internals/FixedPointAligner.h Framework/Scene2D/OpenGLCompositor.cpp Framework/Scene2D/OpenGLCompositor.h Framework/Scene2D/PointerEvent.cpp Framework/Scene2D/PointerEvent.h Framework/Scene2D/RotateSceneTracker.cpp Framework/Scene2D/RotateSceneTracker.h Framework/Scene2D/ZoomSceneTracker.cpp Framework/Scene2D/ZoomSceneTracker.h Samples/Sdl/BasicScene.cpp Samples/WebAssembly/BasicScene.cpp
diffstat 14 files changed, 195 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/OpenGL/WebAssemblyOpenGLContext.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/OpenGL/WebAssemblyOpenGLContext.cpp	Thu May 02 18:21:59 2019 +0200
@@ -63,6 +63,11 @@
         emscripten_webgl_destroy_context(context_);
       }
 
+      const std::string& GetCanvasIdentifier() const
+      {
+        return canvas_;
+      }
+
       void MakeCurrent()
       {
         if (emscripten_webgl_make_context_current(context_) != EMSCRIPTEN_RESULT_SUCCESS)
@@ -162,6 +167,12 @@
       assert(pimpl_.get() != NULL);
       pimpl_->UpdateSize();
     }
+
+    const std::string& WebAssemblyOpenGLContext::GetCanvasIdentifier() const
+    {
+      assert(pimpl_.get() != NULL);
+      return pimpl_->GetCanvasIdentifier();
+    }
   }
 }
 
--- a/Framework/OpenGL/WebAssemblyOpenGLContext.h	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/OpenGL/WebAssemblyOpenGLContext.h	Thu May 02 18:21:59 2019 +0200
@@ -53,6 +53,8 @@
       virtual unsigned int GetCanvasHeight() const;
 
       void UpdateSize();
+
+      const std::string& GetCanvasIdentifier() const;
     };
   }
 }
--- a/Framework/Scene2D/Internals/FixedPointAligner.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/Internals/FixedPointAligner.cpp	Thu May 02 18:21:59 2019 +0200
@@ -26,13 +26,10 @@
   namespace Internals
   {
     FixedPointAligner::FixedPointAligner(Scene2D& scene,
-                                         const ScenePoint2D& p,
-                                         unsigned int canvasWidth,
-                                         unsigned int canvasHeight) :
-      scene_(scene)
+                                         const ScenePoint2D& p) :
+      scene_(scene),
+      canvas_(p)
     {
-      canvas_ = ScenePoint2D(p.GetX() - static_cast<double>(canvasWidth) / 2.0,
-                             p.GetY() - static_cast<double>(canvasHeight) / 2.0);
       pivot_ = canvas_.Apply(scene_.GetCanvasToSceneTransform());
     }
 
--- a/Framework/Scene2D/Internals/FixedPointAligner.h	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/Internals/FixedPointAligner.h	Thu May 02 18:21:59 2019 +0200
@@ -39,9 +39,7 @@
 
     public:
       FixedPointAligner(Scene2D& scene,
-                        const ScenePoint2D& p,
-                        unsigned int canvasWidth,
-                        unsigned int canvasHeight);
+                        const ScenePoint2D& p);
 
       void Apply();
     };
--- a/Framework/Scene2D/OpenGLCompositor.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/OpenGLCompositor.cpp	Thu May 02 18:21:59 2019 +0200
@@ -202,4 +202,12 @@
     SetFont(index, dict);
   }
 #endif
+
+
+  ScenePoint2D OpenGLCompositor::GetPixelCenterCoordinates(int x, int y) const
+  {
+    return ScenePoint2D(
+      static_cast<double>(x) + 0.5 - static_cast<double>(canvasWidth_) / 2.0,
+      static_cast<double>(y) + 0.5 - static_cast<double>(canvasHeight_) / 2.0);
+  }
 }
--- a/Framework/Scene2D/OpenGLCompositor.h	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/OpenGLCompositor.h	Thu May 02 18:21:59 2019 +0200
@@ -69,5 +69,17 @@
                  unsigned int fontSize,
                  Orthanc::Encoding codepage);
 #endif
+
+    unsigned int GetCanvasWidth() const
+    {
+      return canvasWidth_;
+    }
+
+    unsigned int GetCanvasHeight() const
+    {
+      return canvasHeight_;
+    }
+
+    ScenePoint2D GetPixelCenterCoordinates(int x, int y) const;
   };
 }
--- a/Framework/Scene2D/PointerEvent.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/PointerEvent.cpp	Thu May 02 18:21:59 2019 +0200
@@ -46,15 +46,6 @@
   }
     
 
-  // Add the center of the pixel
-  void PointerEvent::AddIntegerPosition(int x,
-                                        int y)
-  {
-    AddPosition(static_cast<double>(x) + 0.5,
-                static_cast<double>(y) + 0.5);
-  }
-    
-    
   ScenePoint2D PointerEvent::GetPosition(size_t index) const
   {
     if (index < positions_.size())
--- a/Framework/Scene2D/PointerEvent.h	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/PointerEvent.h	Thu May 02 18:21:59 2019 +0200
@@ -41,16 +41,17 @@
 
     ScenePoint2D GetMainPosition() const;
     
+    void AddPosition(const ScenePoint2D& p)
+    {
+      positions_.push_back(p);
+    }
+
     void AddPosition(double x,
                      double y)
     {
       positions_.push_back(ScenePoint2D(x, y));
     }
 
-    // Add the center of the pixel
-    void AddIntegerPosition(int x,
-                            int y);
-    
     size_t GetPositionsCount() const
     {
       return positions_.size();
--- a/Framework/Scene2D/RotateSceneTracker.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/RotateSceneTracker.cpp	Thu May 02 18:21:59 2019 +0200
@@ -24,12 +24,10 @@
 namespace OrthancStone
 {
   RotateSceneTracker::RotateSceneTracker(Scene2D& scene,
-                                         const PointerEvent& event,
-                                         unsigned int canvasWidth,
-                                         unsigned int canvasHeight) :
+                                         const PointerEvent& event) :
     scene_(scene),
     click_(event.GetMainPosition()),
-    aligner_(scene, click_, canvasWidth, canvasHeight),
+    aligner_(scene, click_),
     isFirst_(true),
     originalSceneToCanvas_(scene.GetSceneToCanvasTransform())
   {
--- a/Framework/Scene2D/RotateSceneTracker.h	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/RotateSceneTracker.h	Thu May 02 18:21:59 2019 +0200
@@ -38,9 +38,7 @@
 
   public:
     RotateSceneTracker(Scene2D& scene,
-                       const PointerEvent& event,
-                       unsigned int canvasWidth,
-                       unsigned int canvasHeight);
+                       const PointerEvent& event);
 
     virtual void Update(const PointerEvent& event);
 
--- a/Framework/Scene2D/ZoomSceneTracker.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/ZoomSceneTracker.cpp	Thu May 02 18:21:59 2019 +0200
@@ -25,11 +25,10 @@
 {
   ZoomSceneTracker::ZoomSceneTracker(Scene2D& scene,
                                      const PointerEvent& event,
-                                     unsigned int canvasWidth,
                                      unsigned int canvasHeight) :
     scene_(scene),
     clickY_(event.GetMainPosition().GetY()),
-    aligner_(scene, event.GetMainPosition(), canvasWidth, canvasHeight),
+    aligner_(scene, event.GetMainPosition()),
     originalSceneToCanvas_(scene.GetSceneToCanvasTransform())
   {
     if (canvasHeight <= 3)
--- a/Framework/Scene2D/ZoomSceneTracker.h	Thu May 02 13:27:41 2019 +0200
+++ b/Framework/Scene2D/ZoomSceneTracker.h	Thu May 02 18:21:59 2019 +0200
@@ -39,7 +39,6 @@
   public:
     ZoomSceneTracker(Scene2D& scene,
                      const PointerEvent& event,
-                     unsigned int canvasWidth,
                      unsigned int canvasHeight);
 
     virtual void Update(const PointerEvent& event);
--- a/Samples/Sdl/BasicScene.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Samples/Sdl/BasicScene.cpp	Thu May 02 18:21:59 2019 +0200
@@ -157,13 +157,10 @@
 
 
 void HandleApplicationEvent(OrthancStone::Scene2D& scene,
+                            const OrthancStone::OpenGLCompositor& compositor,
                             const SDL_Event& event,
-                            std::auto_ptr<OrthancStone::IPointerTracker>& activeTracker,
-                            unsigned int windowWidth,
-                            unsigned int windowHeight)
+                            std::auto_ptr<OrthancStone::IPointerTracker>& activeTracker)
 {
-  bool hasPositionLayer = false;
-  
   if (event.type == SDL_MOUSEMOTION)
   {
     int scancodeCount = 0;
@@ -176,8 +173,8 @@
       // The "left-ctrl" key is down, while no tracker is present
 
       OrthancStone::PointerEvent e;
-      e.AddIntegerPosition(event.button.x - static_cast<int>(windowWidth) / 2,
-                           event.button.y - static_cast<int>(windowHeight) / 2);
+      e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
+
       OrthancStone::ScenePoint2D p = e.GetMainPosition().Apply(scene.GetCanvasToSceneTransform());
 
       char buf[64];
@@ -200,14 +197,16 @@
         layer->SetPosition(p.GetX(), p.GetY());
         scene.SetLayer(LAYER_POSITION, layer.release());
       }
-
-      hasPositionLayer = true;
+    }
+    else
+    {
+      scene.DeleteLayer(LAYER_POSITION);
     }
   }
   else if (event.type == SDL_MOUSEBUTTONDOWN)
   {
     OrthancStone::PointerEvent e;
-    e.AddIntegerPosition(event.button.x, event.button.y);
+    e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
 
     switch (event.button.button)
     {
@@ -216,13 +215,12 @@
         break;
 
       case SDL_BUTTON_RIGHT:
-        activeTracker.reset(new OrthancStone::ZoomSceneTracker
-                            (scene, e, windowWidth, windowHeight));
+        activeTracker.reset(new OrthancStone::ZoomSceneTracker(scene, e, 
+                                                               compositor.GetCanvasHeight()));
         break;
 
       case SDL_BUTTON_LEFT:
-        activeTracker.reset(new OrthancStone::RotateSceneTracker
-                            (scene, e, windowWidth, windowHeight));
+        activeTracker.reset(new OrthancStone::RotateSceneTracker(scene, e));
         break;
 
       default:
@@ -235,22 +233,20 @@
     switch (event.key.keysym.sym)
     {
       case SDLK_s:
-        scene.FitContent(windowWidth, windowHeight);
+        scene.FitContent(compositor.GetCanvasWidth(), 
+                         compositor.GetCanvasHeight());
         break;
               
       case SDLK_c:
-        TakeScreenshot("screenshot.png", scene, windowWidth, windowHeight);
+        TakeScreenshot("screenshot.png", scene, 
+                       compositor.GetCanvasWidth(), 
+                       compositor.GetCanvasHeight());
         break;
               
       default:
         break;
     }
   }
-
-  if (!hasPositionLayer)
-  {
-    scene.DeleteLayer(LAYER_POSITION);
-  }
 }
 
 
@@ -306,7 +302,7 @@
         if (tracker.get() != NULL)
         {
           OrthancStone::PointerEvent e;
-          e.AddIntegerPosition(event.button.x, event.button.y);
+          e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
           tracker->Update(e);
         }
       }
@@ -342,7 +338,7 @@
         }
       }
       
-      HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight());
+      HandleApplicationEvent(scene, compositor, event, tracker);
     }
 
     SDL_Delay(1);
--- a/Samples/WebAssembly/BasicScene.cpp	Thu May 02 13:27:41 2019 +0200
+++ b/Samples/WebAssembly/BasicScene.cpp	Thu May 02 18:21:59 2019 +0200
@@ -37,6 +37,7 @@
 
 // From Orthanc framework
 #include <Core/Images/Image.h>
+#include <Core/Logging.h>
 
 #include <stdio.h>
 
@@ -139,46 +140,140 @@
 
 
 
-
-class WebAssemblyCanvas : public boost::noncopyable
+namespace OrthancStone
 {
-private:
-  OrthancStone::OpenGL::WebAssemblyOpenGLContext  context_;
-  OrthancStone::Scene2D                           scene_;
-  OrthancStone::OpenGLCompositor                  compositor_;
+  class WebAssemblyCanvas : public boost::noncopyable
+  {
+  private:
+    OpenGL::WebAssemblyOpenGLContext  context_;
+    Scene2D                           scene_;
+    OpenGLCompositor                  compositor_;
 
-public:
-  WebAssemblyCanvas(const std::string& canvas) :
-    context_(canvas),
-    compositor_(context_, scene_)
-  {
-    compositor_.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, 
-                        FONT_SIZE, Orthanc::Encoding_Latin1);
-  }
+    void SetupEvents(const std::string& canvas);
+
+  public:
+    WebAssemblyCanvas(const std::string& canvas) :
+      context_(canvas),
+      compositor_(context_, scene_)
+    {
+      compositor_.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, 
+                          FONT_SIZE, Orthanc::Encoding_Latin1);
+      SetupEvents(canvas);
+    }
 
-  OrthancStone::Scene2D& GetScene()
-  {
-    return scene_;
-  }
+    Scene2D& GetScene()
+    {
+      return scene_;
+    }
+
+    void UpdateSize()
+    {
+      context_.UpdateSize();
+      compositor_.UpdateSize();
+      Refresh();
+    }
 
-  void UpdateSize()
-  {
-    context_.UpdateSize();
-    compositor_.UpdateSize();
-    Refresh();
-  }
+    void Refresh()
+    {
+      compositor_.Refresh();
+    }
 
-  void Refresh()
-  {
-    compositor_.Refresh();
-  }
-};
+    const std::string& GetCanvasIdentifier() const
+    {
+      return context_.GetCanvasIdentifier();
+    }
+
+    ScenePoint2D GetPixelCenterCoordinates(int x, int y) const
+    {
+      return compositor_.GetPixelCenterCoordinates(x, y);
+    }
+  };
 
 
 
-std::auto_ptr<WebAssemblyCanvas>  canvas1_;
-std::auto_ptr<WebAssemblyCanvas>  canvas2_;
-std::auto_ptr<WebAssemblyCanvas>  canvas3_;
+  class ActiveTracker : public boost::noncopyable
+  {
+  private:
+    std::auto_ptr<IPointerTracker>  tracker_;
+    std::string                     canvasIdentifier_;
+    bool                            insideCanvas_;
+    
+  public:
+    ActiveTracker(IPointerTracker* tracker,
+                  const WebAssemblyCanvas& canvas) :
+      tracker_(tracker),
+      canvasIdentifier_(canvas.GetCanvasIdentifier()),
+      insideCanvas_(true)
+    {
+    }
+  };
+}
+
+
+
+static OrthancStone::PointerEvent* ConvertMouseEvent(const EmscriptenMouseEvent& source,
+                                                     OrthancStone::WebAssemblyCanvas& canvas)
+{
+  std::auto_ptr<OrthancStone::PointerEvent> target(new OrthancStone::PointerEvent);
+
+  target->AddPosition(canvas.GetPixelCenterCoordinates(source.clientX, source.clientY));
+  target->SetAltModifier(source.altKey);
+  target->SetControlModifier(source.ctrlKey);
+  target->SetShiftModifier(source.shiftKey);
+
+  return target.release();
+}
+
+
+EM_BOOL OnMouseEvent(int eventType, 
+                     const EmscriptenMouseEvent *mouseEvent, 
+                     void *userData)
+{
+  if (userData != NULL)
+  {
+    OrthancStone::WebAssemblyCanvas& canvas = *reinterpret_cast<OrthancStone::WebAssemblyCanvas*>(userData);
+
+    switch (eventType)
+    {
+      case EMSCRIPTEN_EVENT_CLICK:
+      {
+        static unsigned int count = 0;
+        char buf[64];
+        sprintf(buf, "click %d", count++);
+
+        std::auto_ptr<OrthancStone::TextSceneLayer> layer(new OrthancStone::TextSceneLayer);
+        layer->SetText(buf);
+        canvas.GetScene().SetLayer(100, layer.release());
+        canvas.Refresh();
+        break;
+      }
+
+      case EMSCRIPTEN_EVENT_MOUSEDOWN:
+        LOG(ERROR) << "Mouse down";
+        break;
+
+      default:
+        break;
+    }
+  }
+
+  return true;
+}
+
+
+void OrthancStone::WebAssemblyCanvas::SetupEvents(const std::string& canvas)
+{
+  emscripten_set_click_callback(canvas.c_str(), this, false, OnMouseEvent);
+  //emscripten_set_mousedown_callback(canvas.c_str(), this, false, OnMouseEvent);
+}
+
+
+
+
+std::auto_ptr<OrthancStone::WebAssemblyCanvas>  canvas1_;
+std::auto_ptr<OrthancStone::WebAssemblyCanvas>  canvas2_;
+std::auto_ptr<OrthancStone::WebAssemblyCanvas>  canvas3_;
+std::auto_ptr<OrthancStone::ActiveTracker>      tracker_;
 
 
 EM_BOOL OnWindowResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
@@ -202,25 +297,6 @@
 }
 
 
-EM_BOOL OnMouseClick(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
-{
-  if (userData != NULL)
-  {
-    WebAssemblyCanvas& canvas = *reinterpret_cast<WebAssemblyCanvas*>(userData);
-
-    static unsigned int count = 0;
-    char buf[64];
-    sprintf(buf, "click %d", count++);
-
-    std::auto_ptr<OrthancStone::TextSceneLayer> layer(new OrthancStone::TextSceneLayer);
-    layer->SetText(buf);
-    canvas.GetScene().SetLayer(100, layer.release());
-    canvas.Refresh();
-  }
-
-  return true;
-}
-
 
 extern "C"
 {
@@ -233,21 +309,18 @@
   EMSCRIPTEN_KEEPALIVE
   void Initialize()
   {
-    canvas1_.reset(new WebAssemblyCanvas("mycanvas1"));
+    canvas1_.reset(new OrthancStone::WebAssemblyCanvas("mycanvas1"));
     PrepareScene(canvas1_->GetScene());
     canvas1_->UpdateSize();
 
-    canvas2_.reset(new WebAssemblyCanvas("mycanvas2"));
+    canvas2_.reset(new OrthancStone::WebAssemblyCanvas("mycanvas2"));
     PrepareScene(canvas2_->GetScene());
     canvas2_->UpdateSize();
 
-    canvas3_.reset(new WebAssemblyCanvas("mycanvas3"));
+    canvas3_.reset(new OrthancStone::WebAssemblyCanvas("mycanvas3"));
     PrepareScene(canvas3_->GetScene());
     canvas3_->UpdateSize();
 
     emscripten_set_resize_callback("#window", NULL, false, OnWindowResize);
-    emscripten_set_click_callback("mycanvas1", canvas1_.get(), false, OnMouseClick);
-    emscripten_set_click_callback("mycanvas2", canvas2_.get(), false, OnMouseClick);
-    emscripten_set_click_callback("mycanvas3", canvas3_.get(), false, OnMouseClick);
   }
 }