]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/CanvasEventManager.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / canvas / CanvasEventManager.cxx
index 89f25719107d0303cbb6adb1cb45fc0b5663414f..cf8ac789bf22e405439534ee3da9016269f38697 100644 (file)
@@ -17,8 +17,9 @@
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
 
 #include "CanvasEventManager.hxx"
-#include "MouseEvent.hxx"
+#include <simgear/canvas/events/MouseEvent.hxx>
 #include <simgear/canvas/elements/CanvasElement.hxx>
+#include <cmath>
 
 namespace simgear
 {
@@ -60,6 +61,16 @@ namespace canvas
     return !path.empty() && time > 0;
   }
 
+  //----------------------------------------------------------------------------
+  void EventManager::MouseEventInfo::set( const MouseEventPtr& event,
+                                          const EventPropagationPath& p )
+  {
+    path = p;
+    time = event->time;
+    button = event->button;
+    pos = event->screen_pos;
+  }
+
   //----------------------------------------------------------------------------
   EventManager::EventManager():
     _current_click_count(0)
@@ -71,39 +82,58 @@ namespace canvas
   bool EventManager::handleEvent( const MouseEventPtr& event,
                                   const EventPropagationPath& path )
   {
+    bool handled = false;
     switch( event->type )
     {
       case Event::MOUSE_DOWN:
-        _last_mouse_down = StampedPropagationPath(path, event->getTime());
+        _last_mouse_down.set(event, path);
         break;
       case Event::MOUSE_UP:
       {
-        if( _last_mouse_down.path.empty() )
-          // Ignore mouse up without any previous mouse down
-          return false;
+        // If the mouse has moved while a button was down (aka. dragging) we
+        // need to notify the original element that the mouse has left it, and
+        // the new element that it has been entered
+        if( _last_mouse_down.path != path )
+          handled |= handleMove(event, path);
 
         // normal mouseup
-        propagateEvent(event, path);
+        handled |= propagateEvent(event, path);
+
+        if( !_last_mouse_down.valid() )
+          // Ignore mouse up without any previous mouse down
+          return handled;
 
         // now handle click/dblclick
-        if( checkClickDistance(path, _last_mouse_down.path) )
-          handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
+        if( checkClickDistance(event->screen_pos, _last_mouse_down.pos) )
+          handled |=
+            handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
 
         _last_mouse_down.clear();
 
-        return true;
+        return handled;
       }
       case Event::DRAG:
         if( !_last_mouse_down.valid() )
           return false;
         else
+        {
+          // OSG does not set button for drag events.
+          event->button = _last_mouse_down.button;
           return propagateEvent(event, _last_mouse_down.path);
+        }
       case Event::MOUSE_MOVE:
-        handleMove(event, path);
+        handled |= handleMove(event, path);
         break;
       case Event::MOUSE_LEAVE:
         // Mouse leaves window and therefore also current mouseover element
         handleMove(event, EventPropagationPath());
+
+        // Event is only send if mouse is moved outside the window or dragging
+        // has ended somewhere outside the window. In both cases a mouse button
+        // has been released, so no more mouse down or click...
+        _last_mouse_down.clear();
+        _last_click.clear();
+
         return true;
       case Event::WHEEL:
         break;
@@ -111,11 +141,76 @@ namespace canvas
         return false;
     }
 
-    return propagateEvent(event, path);
+    return handled | propagateEvent(event, path);
+  }
+
+  //----------------------------------------------------------------------------
+  bool EventManager::propagateEvent( const EventPtr& event,
+                                     const EventPropagationPath& path )
+  {
+    event->target = path.back().element;
+    MouseEventPtr mouse_event = dynamic_cast<MouseEvent*>(event.get());
+
+    // Event propagation similar to DOM Level 3 event flow:
+    // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
+
+    // Position update only needed for drag event (as event needs to be
+    // delivered to element of initial mousedown, but with update positions)
+    if( mouse_event && mouse_event->type == MouseEvent::DRAG )
+    {
+      osg::Vec2f local_pos = mouse_event->client_pos;
+
+      // Capturing phase (currently just update position)
+      for( EventPropagationPath::const_iterator it = path.begin();
+                                                it != path.end();
+                                              ++it )
+      {
+        ElementPtr el = it->element.lock();
+        if( !el )
+          continue;
+
+        it->local_pos = local_pos = el->posToLocal(local_pos);
+      }
+    }
+
+    bool const do_bubble = event->canBubble();
+
+    // Bubbling phase
+    for( EventPropagationPath::const_reverse_iterator
+           it = path.rbegin();
+           it != path.rend();
+         ++it )
+    {
+      ElementPtr el = it->element.lock();
+
+      if( !el )
+      {
+        // Ignore element if it has been destroyed while traversing the event
+        // (eg. removed by another event handler)
+        if( do_bubble )
+          continue;
+        else
+          break;
+      }
+
+      // TODO provide functions to convert delta to local coordinates on demand.
+      //      Maybe also provide a clone method for events as local coordinates
+      //      might differ between different elements receiving the same event.
+      if( mouse_event )
+        mouse_event->local_pos = it->local_pos;
+
+      event->current_target = el;
+      el->handleEvent(event);
+
+      if( event->propagation_stopped || !do_bubble )
+        return true;
+    }
+
+    return true;
   }
 
   //----------------------------------------------------------------------------
-  void EventManager::handleClick( const MouseEventPtr& event,
+  bool EventManager::handleClick( const MouseEventPtr& event,
                                   const EventPropagationPath& path )
   {
     MouseEventPtr click(new MouseEvent(*event));
@@ -130,8 +225,10 @@ namespace canvas
 
       if( _current_click_count > 1 )
       {
-        // Reset current click count if moved too far
-        if( !checkClickDistance(path, _last_click.path) )
+        // Reset current click count if moved too far or different button has
+        // been clicked
+        if(   !checkClickDistance(event->screen_pos, _last_click.pos)
+            || _last_click.button != event->button )
           _current_click_count = 1;
       }
     }
@@ -145,104 +242,86 @@ namespace canvas
       dbl_click->type = Event::DBL_CLICK;
     }
 
-    propagateEvent(click, path);
+    bool handled = propagateEvent(click, path);
 
     if( dbl_click )
-      propagateEvent(dbl_click, getCommonAncestor(_last_click.path, path));
+      handled |= propagateEvent( dbl_click,
+                                 getCommonAncestor(_last_click.path, path) );
+
+    _last_click.set(event, path);
 
-    _last_click = StampedPropagationPath(path, event->getTime());
+    return handled;
   }
 
   //----------------------------------------------------------------------------
-  void EventManager::handleMove( const MouseEventPtr& event,
+  bool EventManager::handleMove( const MouseEventPtr& event,
                                  const EventPropagationPath& path )
   {
-    if( _last_mouse_over.path == path )
-      return;
+    EventPropagationPath& last_path = _last_mouse_over.path;
+    if( last_path == path )
+      return false;
+
+    bool handled = false;
 
-    if( !_last_mouse_over.path.empty() )
+    // Leave old element
+    if( !last_path.empty() )
     {
       MouseEventPtr mouseout(new MouseEvent(*event));
       mouseout->type = Event::MOUSE_OUT;
-      propagateEvent(mouseout, _last_mouse_over.path);
+      handled |= propagateEvent(mouseout, last_path);
+
+      // Send a mouseleave event to all ancestors of the currently left element
+      // which are not ancestor of the new element currently entered
+      EventPropagationPath path_leave = last_path;
+      for(size_t i = path_leave.size() - 1; i > 0; --i)
+      {
+        if( i < path.size() && path[i] == path_leave[i] )
+          break;
+
+        MouseEventPtr mouseleave(new MouseEvent(*event));
+        mouseleave->type = Event::MOUSE_LEAVE;
+        handled |= propagateEvent(mouseleave, path_leave);
+
+        path_leave.pop_back();
+      }
     }
 
+    // Enter new element
     if( !path.empty() )
     {
       MouseEventPtr mouseover(new MouseEvent(*event));
       mouseover->type = Event::MOUSE_OVER;
-      propagateEvent(mouseover, path);
-    }
-
-    _last_mouse_over.path = path;
-  }
-
-  //----------------------------------------------------------------------------
-  bool EventManager::propagateEvent( const EventPtr& event,
-                                     const EventPropagationPath& path )
-  {
-    event->target = path.back().element;
-    MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
-
-    // Event propagation similar to DOM Level 3 event flow:
-    // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
-
-    // Capturing phase
-//    for( EventTargets::iterator it = _target_path.begin();
-//                                it != _target_path.end();
-//                              ++it )
-//    {
-//      if( it->element )
-//        std::cout << it->element->getProps()->getPath() << " "
-//                  << "(" << it->local_pos.x() << "|" << it->local_pos.y() << ")\n";
-//    }
+      handled |= propagateEvent(mouseover, path);
 
-    // Bubbling phase
-    for( EventPropagationPath::const_reverse_iterator
-           it = path.rbegin();
-           it != path.rend();
-         ++it )
-    {
-      ElementPtr el = it->element.lock();
-
-      if( !el )
-        // Ignore element if it has been destroyed while traversing the event
-        // (eg. removed by another event handler)
-        continue;
-
-      // TODO provide functions to convert position and delta to local
-      //      coordinates on demand. Events shouldn't contain informations in
-      //      local coordinates as they might differe between different elements
-      //      receiving the same event.
-//      if( mouse_event && event->type != Event::DRAG )
-//      {
-//        // TODO transform pos and delta for drag events. Maybe we should just
-//        //      store the global coordinates and convert to local coordinates
-//        //      on demand.
-//
-//        // Position and delta are specified in local coordinate system of
-//        // current element
-//        mouse_event->pos = it->local_pos;
-//        mouse_event->delta = it->local_delta;
-//      }
+      // Send a mouseenter event to all ancestors of the currently entered
+      // element which are not ancestor of the old element currently being
+      // left
+      EventPropagationPath path_enter;
+      for(size_t i = 0; i < path.size(); ++i)
+      {
+        path_enter.push_back(path[i]);
 
-      el->callListeners(event);
+        if( i < last_path.size() && path[i] == last_path[i] )
+          continue;
 
-      if( event->propagation_stopped )
-        return true;
+        MouseEventPtr mouseenter(new MouseEvent(*event));
+        mouseenter->type = Event::MOUSE_ENTER;
+        handled |= propagateEvent(mouseenter, path_enter);
+      }
     }
 
-    return true;
+    _last_mouse_over.path = path;
+    return handled;
   }
 
   //----------------------------------------------------------------------------
   bool
-  EventManager::checkClickDistance( const EventPropagationPath& path1,
-                                    const EventPropagationPath& path2 ) const
+  EventManager::checkClickDistance( const osg::Vec2f& pos1,
+                                    const osg::Vec2f& pos2 ) const
   {
-    osg::Vec2 delta = path1.front().local_pos - path2.front().local_pos;
-    return delta.x() < drag_threshold
-        && delta.y() < drag_threshold;
+    osg::Vec2 delta = pos1 - pos2;
+    return std::fabs(delta.x()) < drag_threshold
+        && std::fabs(delta.y()) < drag_threshold;
   }
 
   //----------------------------------------------------------------------------
@@ -250,6 +329,9 @@ namespace canvas
   EventManager::getCommonAncestor( const EventPropagationPath& path1,
                                    const EventPropagationPath& path2 ) const
   {
+    if( path1.empty() || path2.empty() )
+      return EventPropagationPath();
+
     if( path1.back().element.lock() == path2.back().element.lock() )
       return path2;