]> git.mxchange.org Git - simgear.git/commitdiff
Canvas: allow dispatching events to Canavs itself
authorThomas Geymayer <tomgey@gmail.com>
Thu, 29 May 2014 14:34:33 +0000 (16:34 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 29 May 2014 14:34:33 +0000 (16:34 +0200)
simgear/canvas/Canvas.cxx
simgear/canvas/Canvas.hxx
simgear/canvas/elements/CanvasElement.cxx
simgear/canvas/elements/CanvasElement.hxx
simgear/canvas/elements/CanvasImage.cxx
simgear/canvas/elements/CanvasImage.hxx

index 8b02f49c7b8b63cbbb7cccc279e6910561249b4a..46f2e27fa07ec9b5aec01c140f58d50432d0d6c8 100644 (file)
@@ -326,11 +326,20 @@ namespace canvas
                                  const EventListener& cb )
   {
     if( !_root_group.get() )
-      throw std::runtime_error("Canvas::AddEventListener: no root group!");
+      throw std::runtime_error("Canvas::addEventListener: no root group!");
 
     return _root_group->addEventListener(type, cb);
   }
 
+  //----------------------------------------------------------------------------
+  bool Canvas::dispatchEvent(const EventPtr& event)
+  {
+    if( !_root_group.get() )
+      throw std::runtime_error("Canvas::dispatchEvent: no root group!");
+
+    return _root_group->dispatchEvent(event);
+  }
+
   //----------------------------------------------------------------------------
   void Canvas::setSizeX(int sx)
   {
index 335b297d99f283b47f398b67ccd435bffe273ada..10872f22d7f0574e35066cb9ec96257ebb7d2955 100644 (file)
@@ -132,6 +132,7 @@ namespace canvas
       void update(double delta_time_sec);
 
       bool addEventListener(const std::string& type, const EventListener& cb);
+      bool dispatchEvent(const EventPtr& event);
 
       void setSizeX(int sx);
       void setSizeY(int sy);
index 61239d4dffba726907c6a7132b29c37a84febd9c..af378e28971b90617d34a4193636dd809c066df3 100644 (file)
@@ -159,11 +159,17 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  ElementPtr Element::getParent()
+  ElementPtr Element::getParent() const
   {
     return _parent;
   }
 
+  //----------------------------------------------------------------------------
+  CanvasWeakPtr Element::getCanvas() const
+  {
+    return _canvas;
+  }
+
   //----------------------------------------------------------------------------
   void Element::update(double dt)
   {
@@ -246,20 +252,31 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  bool Element::handleEvent(EventPtr event)
+  bool Element::handleEvent(const EventPtr& event)
   {
     ListenerMap::iterator listeners = _listener.find(event->getType());
     if( listeners == _listener.end() )
       return false;
 
     BOOST_FOREACH(EventListener const& listener, listeners->second)
-      listener(event);
+      try
+      {
+        listener(event);
+      }
+      catch( std::exception const& ex )
+      {
+        SG_LOG(
+          SG_GENERAL,
+          SG_WARN,
+          "canvas::Element: event handler error: '" << ex.what() << "'"
+        );
+      }
 
     return true;
   }
 
   //----------------------------------------------------------------------------
-  bool Element::dispatchEvent(EventPtr event)
+  bool Element::dispatchEvent(const EventPtr& event)
   {
     EventPropagationPath path;
     path.push_back( EventTarget(this) );
index 87548c68d4108edf25ce713e189603abcdc76409..01858f4b402db65b912d6ca37ffce8a3dbe0987f 100644 (file)
@@ -95,7 +95,8 @@ namespace canvas
       virtual ~Element() = 0;
       virtual void onDestroy();
 
-      ElementPtr getParent();
+      ElementPtr getParent() const;
+      CanvasWeakPtr getCanvas() const;
 
       /**
        * Called every frame to update internal state
@@ -111,8 +112,8 @@ namespace canvas
       virtual bool ascend(EventVisitor& visitor);
       virtual bool traverse(EventVisitor& visitor);
 
-      virtual bool handleEvent(EventPtr event);
-      bool dispatchEvent(EventPtr event);
+      virtual bool handleEvent(const EventPtr& event);
+      bool dispatchEvent(const EventPtr& event);
 
       /**
        *
index 8577459fe28c3159eacfa29e687e0aa9278beb52..03079f110c50f3a8e3e74b0365ffb98a38469782 100644 (file)
@@ -441,7 +441,7 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  bool Image::handleEvent(EventPtr event)
+  bool Image::handleEvent(const EventPtr& event)
   {
     bool handled = Element::handleEvent(event);
 
@@ -453,7 +453,6 @@ namespace canvas
     if( mouse_event )
     {
       mouse_event.reset( new MouseEvent(*mouse_event) );
-      event = mouse_event;
 
       mouse_event->client_pos = mouse_event->local_pos
                               - toOsg(_region.getMin());
index c9e3c081289f5ced3c7f8853ad276833e5074dde..eb730fc53c8b3890c2abe7c1d6ecaf5bee9db9b8 100644 (file)
@@ -85,7 +85,7 @@ namespace canvas
 
       const SGRect<float>& getRegion() const;
 
-      bool handleEvent(EventPtr event);
+      bool handleEvent(const EventPtr& event);
 
     protected: