]> git.mxchange.org Git - flightgear.git/blobdiff - src/Input/FGMouseInput.cxx
Allow using the system version of flite and the HTS engine
[flightgear.git] / src / Input / FGMouseInput.cxx
index f55e7c41c82304b101dbea21732e62884e0249f0..6d052c8817f544e76db793991f40e92ef09e2e71 100644 (file)
 
 #include "FGMouseInput.hxx"
 
+#include <boost/foreach.hpp>
 #include <osgGA/GUIEventAdapter>
+
+#include <simgear/scene/util/SGPickCallback.hxx>
+#include <simgear/timing/timestamp.hxx>
+#include <simgear/scene/model/SGPickAnimation.hxx>
+
+#include "FGButton.hxx"
 #include "Main/globals.hxx"
+#include <Viewer/renderer.hxx>
+#include <plib/pu.h>
+#include <Model/panelnode.hxx>
+#include <Cockpit/panel.hxx>
+#include <Viewer/FGEventHandler.hxx>
+#include <GUI/MouseCursor.hxx>
 
 using std::ios_base;
 
+const int MAX_MICE = 1;
+const int MAX_MOUSE_BUTTONS = 8;
+
+typedef std::vector<SGSceneryPick> SGSceneryPicks;
+typedef SGSharedPtr<SGPickCallback> SGPickCallbackPtr;
+typedef std::list<SGPickCallbackPtr> SGPickCallbackList;
+
+////////////////////////////////////////////////////////////////////////
+
+/**
+ * List of currently pressed mouse button events
+ */
+class ActivePickCallbacks:
+  public std::map<int, SGPickCallbackList>
+{
+  public:
+    void update( double dt, unsigned int keyModState );
+    void init( int button, const osgGA::GUIEventAdapter* ea );
+};
+
+
 void ActivePickCallbacks::init( int button, const osgGA::GUIEventAdapter* ea )
 {
+  osg::Vec2d windowPos;
+  flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+    
   // Get the list of hit callbacks. Take the first callback that
   // accepts the mouse button press and ignore the rest of them
   // That is they get sorted by distance and by scenegraph depth.
   // The nearest one is the first one and the deepest
   // (the most specialized one in the scenegraph) is the first.
-  std::vector<SGSceneryPick> pickList;
-  if (!globals->get_renderer()->pick(pickList, ea)) {
+  SGSceneryPicks pickList;
+  if (!globals->get_renderer()->pick(pickList, windowPos)) {
     return;
   }
 
-  std::vector<SGSceneryPick>::const_iterator i;
+  SGSceneryPicks::const_iterator i;
   for (i = pickList.begin(); i != pickList.end(); ++i) {
-    if (i->callback->buttonPressed(button, ea, i->info)) {
+    if (i->callback->buttonPressed(button, *ea, i->info)) {
         (*this)[button].push_back(i->callback);
         return;
     }
   }
 }
 
-void ActivePickCallbacks::update( double dt )
+void ActivePickCallbacks::update( double dt, unsigned int keyModState )
 {
   // handle repeatable mouse press events
   for( iterator mi = begin(); mi != end(); ++mi ) {
-    std::list<SGSharedPtr<SGPickCallback> >::iterator li;
+    SGPickCallbackList::iterator li;
     for (li = mi->second.begin(); li != mi->second.end(); ++li) {
-      (*li)->update(dt);
+      (*li)->update(dt, keyModState);
     }
   }
 }
 
+////////////////////////////////////////////////////////////////////////
+
+
+/**
+ * Settings for a mouse mode.
+ */
+struct mouse_mode {
+    mouse_mode ();
+    virtual ~mouse_mode ();
+    FGMouseCursor::Cursor cursor;
+    bool constrained;
+    bool pass_through;
+    FGButton * buttons;
+    SGBindingList x_bindings[KEYMOD_MAX];
+    SGBindingList y_bindings[KEYMOD_MAX];
+};
+
+
+/**
+ * Settings for a mouse.
+ */
+struct mouse {
+    mouse ();
+    virtual ~mouse ();
+    int x, y;
+    SGPropertyNode_ptr mode_node;
+    SGPropertyNode_ptr mouse_button_nodes[MAX_MOUSE_BUTTONS];
+    int nModes;
+    int current_mode;
+    
+    SGTimeStamp timeSinceLastMove;
+    mouse_mode * modes;
+};
+
+static
+const SGSceneryPick*
+getPick( const SGSceneryPicks& pick_list,
+         const SGPickCallback* cb )
+{
+  for(size_t i = 0; i < pick_list.size(); ++i)
+    if( pick_list[i].callback == cb )
+      return &pick_list[i];
+
+  return 0;
+}
+
+////////////////////////////////////////////////////////////////////////
+
+class FGMouseInput::FGMouseInputPrivate : public SGPropertyChangeListener
+{
+public:
+    FGMouseInputPrivate() :
+        initialized(false),
+        haveWarped(false),
+        xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
+        ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
+        xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
+        yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
+        mouseXNode(fgGetNode("/devices/status/mice/mouse/x", true)),
+        mouseYNode(fgGetNode("/devices/status/mice/mouse/y", true))
+    {
+        tooltipTimeoutDone = false;
+        hoverPickScheduled = false;
+        tooltipsEnabled = false;
+        
+        fgGetNode("/sim/mouse/hide-cursor", true )->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/cursor-timeout-sec", true )->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/right-button-mode-cycle-enabled", true)->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/tooltip-delay-msec", true)->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/click-shows-tooltip", true)->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/tooltips-enabled", true)->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/drag-sensitivity", true)->addChangeListener(this, true);
+        fgGetNode("/sim/mouse/invert-mouse-wheel", true)->addChangeListener(this, true);
+    }
+  
+    void centerMouseCursor(mouse& m)
+    {    
+      // center the cursor
+      m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
+      m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
+      fgWarpMouse(m.x, m.y);
+      haveWarped = true;
+    }
+  
+    void constrainMouse(int x, int y)
+    {
+        int new_x=x,new_y=y;
+        int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
+        int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
+        
+        bool need_warp = false;
+        if (x <= (xsize * .25) || x >= (xsize * .75)) {
+          new_x = int(xsize * .5);
+          need_warp = true;
+        }
+
+        if (y <= (ysize * .25) || y >= (ysize * .75)) {
+          new_y = int(ysize * .5);
+          need_warp = true;
+        }
+
+        if (need_warp)
+        {
+          fgWarpMouse(new_x, new_y);
+          haveWarped = true;
+        }
+    }
+
+    void scheduleHoverPick(const osg::Vec2d& windowPos)
+    {
+      hoverPickScheduled = true;
+      hoverPos = windowPos;
+    }
+  
+    void doHoverPick(const osg::Vec2d& windowPos)
+    {
+        FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_ARROW;
+        bool explicitCursor = false;
+        bool didPick = false;
+
+        SGPickCallback::Priority priority = SGPickCallback::PriorityScenery;
+        SGSceneryPicks pickList;
+        globals->get_renderer()->pick(pickList, windowPos);
+
+        SGSceneryPicks::const_iterator i;
+        for( i = pickList.begin(); i != pickList.end(); ++i )
+        {
+            bool done = i->callback->hover(windowPos, i->info);
+            std::string curName(i->callback->getCursor());
+            if (!curName.empty()) {
+                explicitCursor = true;
+                cur = FGMouseCursor::cursorFromString(curName.c_str());
+            }
+            
+            // if the callback is of higher prioirty (lower enum index),
+            // record that.
+            if (i->callback->getPriority() < priority) {
+                priority = i->callback->getPriority();
+            }
+
+            if (done) {
+                didPick = true;
+                break;
+            }
+        } // of picks iteration
+
+        // Check if any pick from the previous iteration has disappeared. If so
+        // notify the callback that the mouse has left its element.
+        for( i = _previous_picks.begin(); i != _previous_picks.end(); ++i )
+        {
+          if( !getPick(pickList, i->callback) )
+            i->callback->mouseLeave(windowPos);
+        }
+        _previous_picks = pickList;
+      
+        if (!explicitCursor && (priority == SGPickCallback::PriorityPanel)) {
+            cur = FGMouseCursor::CURSOR_HAND;
+        }
+        
+        FGMouseCursor::instance()->setCursor(cur);
+        if (!didPick) {
+          SGPropertyNode_ptr args(new SGPropertyNode);
+          globals->get_commands()->execute("update-hover", args);
+
+        }
+    }
+    
+    void doMouseMoveWithCallbacks(const osgGA::GUIEventAdapter* ea)
+    {
+        FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND;
+        
+        osg::Vec2d windowPos;
+        flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+
+        SGSceneryPicks pickList;
+        if( !globals->get_renderer()->pick(pickList, windowPos) )
+          return;
+
+        for( ActivePickCallbacks::iterator mi = activePickCallbacks.begin();
+                                           mi != activePickCallbacks.end();
+                                         ++mi )
+        {
+          SGPickCallbackList::iterator li;
+          for( li = mi->second.begin(); li != mi->second.end(); ++li )
+          {
+            const SGSceneryPick* pick = getPick(pickList, *li);
+            (*li)->mouseMoved(*ea, pick ? &pick->info : 0);
+
+            std::string curName((*li)->getCursor());
+            if( !curName.empty() )
+              cur = FGMouseCursor::cursorFromString(curName.c_str());
+          }
+        }
+
+        FGMouseCursor::instance()->setCursor(cur);
+    }
+
+    // implement the property-change-listener interfacee
+    virtual void valueChanged( SGPropertyNode * node )
+    {
+        if (node->getNameString() == "drag-sensitivity") {
+            SGKnobAnimation::setDragSensitivity(node->getDoubleValue());
+        } else if (node->getNameString() == "invert-mouse-wheel") {
+            SGKnobAnimation::setAlternateMouseWheelDirection(node->getBoolValue());
+        } else if (node->getNameString() == "hide-cursor") {
+            hideCursor = node->getBoolValue();
+        } else if (node->getNameString() == "cursor-timeout-sec") {
+            cursorTimeoutMsec = node->getDoubleValue() * 1000;
+        } else if (node->getNameString() == "tooltip-delay-msec") {
+            tooltipDelayMsec = node->getIntValue();
+        } else if (node->getNameString() == "right-button-mode-cycle-enabled") {
+            rightClickModeCycle = node->getBoolValue();
+        } else if (node->getNameString() == "click-shows-tooltip") {
+            clickTriggersTooltip = node->getBoolValue();
+        } else if (node->getNameString() == "tooltips-enabled") {
+            tooltipsEnabled = node->getBoolValue();
+        }
+    }
+    
+    ActivePickCallbacks activePickCallbacks;
+    SGSceneryPicks _previous_picks;
+
+    mouse mice[MAX_MICE];
+    
+    bool initialized;
+    bool hideCursor, haveWarped;
+    bool tooltipTimeoutDone;
+    bool clickTriggersTooltip;
+    int tooltipDelayMsec, cursorTimeoutMsec;
+    bool rightClickModeCycle;
+    bool tooltipsEnabled;
+    
+    SGPropertyNode_ptr xSizeNode;
+    SGPropertyNode_ptr ySizeNode;
+    SGPropertyNode_ptr xAccelNode;
+    SGPropertyNode_ptr yAccelNode;
+    SGPropertyNode_ptr mouseXNode, mouseYNode;
+  
+    bool hoverPickScheduled;
+    osg::Vec2d hoverPos;
+};
+
 
-#include <plib/pu.h>
-#include <Model/panelnode.hxx>
-#include <Cockpit/panel.hxx>
 ////////////////////////////////////////////////////////////////////////
 // The Mouse Input Implementation
 ////////////////////////////////////////////////////////////////////////
 
-const FGMouseInput::MouseCursorMap FGMouseInput::mouse_cursor_map[] = {
-    { "none", MOUSE_CURSOR_NONE },
-    { "inherit", MOUSE_CURSOR_POINTER },
-    { "wait", MOUSE_CURSOR_WAIT },
-    { "crosshair", MOUSE_CURSOR_CROSSHAIR },
-    { "left-right", MOUSE_CURSOR_LEFTRIGHT },
-    { 0, 0 }
-};
+static FGMouseInput* global_mouseInput = NULL;
+
+static void mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
+{
+    if(global_mouseInput)
+        global_mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
+}
+
+static void mouseMotionHandler(int x, int y, const osgGA::GUIEventAdapter* ea)
+{
+    if (global_mouseInput != 0)
+        global_mouseInput->doMouseMotion(x, y, ea);
+}
+
 
-FGMouseInput * FGMouseInput::mouseInput = NULL;
 
 FGMouseInput::FGMouseInput() :
-  haveWarped(false),
-  xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
-  ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
-  xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
-  yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
-  hideCursorNode(fgGetNode("/sim/mouse/hide-cursor", true ) ),
-  cursorTimeoutNode(fgGetNode("/sim/mouse/cursor-timeout-sec", true ) )
+  d(new FGMouseInputPrivate)
 {
-  if( mouseInput == NULL )
-    mouseInput = this;
+    global_mouseInput = this;
 }
 
 FGMouseInput::~FGMouseInput()
 {
-  if( mouseInput == this )
-    mouseInput = NULL;
+    global_mouseInput = NULL;
 }
 
 void FGMouseInput::init()
 {
+    if (d->initialized) {
+        SG_LOG(SG_INPUT, SG_WARN, "Duplicate init of FGMouseInput");
+
+        return;
+    }
+    
+    d->initialized = true;
+    
   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
-  string module = "";
+  std::string module = "";
 
   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
   if (mouse_nodes == 0) {
@@ -117,7 +398,7 @@ void FGMouseInput::init()
   int j;
   for (int i = 0; i < MAX_MICE; i++) {
     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
-    mouse &m = bindings[i];
+    mouse &m = d->mice[i];
 
                                 // Grab node pointers
     std::ostringstream buf;
@@ -134,43 +415,54 @@ void FGMouseInput::init()
       m.mouse_button_nodes[j]->setBoolValue(false);
     }
 
-                                // Read all the modes
+   // Read all the modes
     m.nModes = mouse_node->getIntValue("mode-count", 1);
     m.modes = new mouse_mode[m.nModes];
 
     for (int j = 0; j < m.nModes; j++) {
       int k;
-
-                                // Read the mouse cursor for this mode
       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
-      const char * cursor_name =
-        mode_node->getStringValue("cursor", "inherit");
-      m.modes[j].cursor = MOUSE_CURSOR_POINTER;
-      for (k = 0; mouse_cursor_map[k].name != 0; k++) {
-        if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
-          m.modes[j].cursor = mouse_cursor_map[k].cursor;
-          break;
-        }
-      }
 
-                                // Read other properties for this mode
+    // Read the mouse cursor for this mode
+      m.modes[j].cursor = FGMouseCursor::cursorFromString(mode_node->getStringValue("cursor", "inherit"));
+        
+      // Read other properties for this mode
       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
 
-                                // Read the button bindings for this mode
+      // Read the button bindings for this mode
       m.modes[j].buttons = new FGButton[MAX_MOUSE_BUTTONS];
       std::ostringstream buf;
       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
         buf.seekp(ios_base::beg);
         buf << "mouse button " << k;
-        SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
         m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module );
       }
 
-                                // Read the axis bindings for this mode
+      // Read the axis bindings for this mode
       read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module );
       read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module );
-    }
+      
+      if (mode_node->hasChild("x-axis-ctrl")) {
+        read_bindings(mode_node->getChild("x-axis-ctrl"), m.modes[j].x_bindings, KEYMOD_CTRL, module );
+      }
+      if (mode_node->hasChild("x-axis-shift")) {
+        read_bindings(mode_node->getChild("x-axis-shift"), m.modes[j].x_bindings, KEYMOD_SHIFT, module );
+      }
+      if (mode_node->hasChild("x-axis-ctrl-shift")) {
+        read_bindings(mode_node->getChild("x-axis-ctrl-shift"), m.modes[j].x_bindings, KEYMOD_CTRL|KEYMOD_SHIFT, module );
+      }
+      
+      if (mode_node->hasChild("y-axis-ctrl")) {
+        read_bindings(mode_node->getChild("y-axis-ctrl"), m.modes[j].y_bindings, KEYMOD_CTRL, module );
+      }
+      if (mode_node->hasChild("y-axis-shift")) {
+        read_bindings(mode_node->getChild("y-axis-shift"), m.modes[j].y_bindings, KEYMOD_SHIFT, module );
+      }
+      if (mode_node->hasChild("y-axis-ctrl-shift")) {
+        read_bindings(mode_node->getChild("y-axis-ctrl-shift"), m.modes[j].y_bindings, KEYMOD_CTRL|KEYMOD_SHIFT, module );
+      }
+    } // of modes iteration
   }
 
   fgRegisterMouseClickHandler(mouseClickHandler);
@@ -179,70 +471,73 @@ void FGMouseInput::init()
 
 void FGMouseInput::update ( double dt )
 {
-  double cursorTimeout = cursorTimeoutNode ? cursorTimeoutNode->getDoubleValue() : 10.0;
-
-  mouse &m = bindings[0];
+    if (!d->initialized) {
+        SG_LOG(SG_INPUT, SG_WARN, "update of mouse before init");
+    }
+    
+  mouse &m = d->mice[0];
   int mode =  m.mode_node->getIntValue();
   if (mode != m.current_mode) {
+    // current mode has changed
     m.current_mode = mode;
-    m.timeout = cursorTimeout;
+    m.timeSinceLastMove.stamp();
+      
     if (mode >= 0 && mode < m.nModes) {
-      fgSetMouseCursor(m.modes[mode].cursor);
-      m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
-      m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
-      fgWarpMouse(m.x, m.y);
-      haveWarped = true;
+      FGMouseCursor::instance()->setCursor(m.modes[mode].cursor);
+      d->centerMouseCursor(m);
     } else {
-      SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
-      fgSetMouseCursor(MOUSE_CURSOR_POINTER);
+      SG_LOG(SG_INPUT, SG_WARN, "Mouse mode " << mode << " out of range");
+      FGMouseCursor::instance()->setCursor(FGMouseCursor::CURSOR_ARROW);
     }
   }
 
-  if ( hideCursorNode ==NULL || hideCursorNode->getBoolValue() ) {
-      if ( m.x != m.save_x || m.y != m.save_y ) {
-          m.timeout = cursorTimeout;
-          if (fgGetMouseCursor() == MOUSE_CURSOR_NONE)
-              fgSetMouseCursor(m.modes[mode].cursor);
-      } else {
-          m.timeout -= dt;
-          if ( m.timeout <= 0.0 ) {
-              fgSetMouseCursor(MOUSE_CURSOR_NONE);
-              m.timeout = 0.0;
-          }
+  if ((mode == 0) && d->hoverPickScheduled) {
+    d->doHoverPick(d->hoverPos);
+    d->hoverPickScheduled = false;
+  }
+  
+  if ( !d->tooltipTimeoutDone &&
+      d->tooltipsEnabled &&
+      (m.timeSinceLastMove.elapsedMSec() > d->tooltipDelayMsec))
+  {
+      d->tooltipTimeoutDone = true;
+      SGPropertyNode_ptr arg(new SGPropertyNode);
+      globals->get_commands()->execute("tooltip-timeout", arg);
+  }
+  
+  if ( d->hideCursor ) {
+      if ( m.timeSinceLastMove.elapsedMSec() > d->cursorTimeoutMsec) {
+          FGMouseCursor::instance()->hideCursorUntilMouseMove();
+          m.timeSinceLastMove.stamp();
       }
-      m.save_x = m.x;
-      m.save_y = m.y;
   }
-
-  activePickCallbacks.update( dt );
+    
+  d->activePickCallbacks.update( dt, fgGetKeyModifiers() );
 }
 
-FGMouseInput::mouse::mouse ()
+mouse::mouse ()
   : x(-1),
     y(-1),
-    save_x(-1),
-    save_y(-1),
     nModes(1),
     current_mode(0),
-    timeout(0),
     modes(NULL)
 {
 }
 
-FGMouseInput::mouse::~mouse ()
+mouse::~mouse ()
 {
   delete [] modes;
 }
 
-FGMouseInput::mouse_mode::mouse_mode ()
-  : cursor(MOUSE_CURSOR_POINTER),
+mouse_mode::mouse_mode ()
+  : cursor(FGMouseCursor::CURSOR_ARROW),
     constrained(false),
     pass_through(false),
     buttons(NULL)
 {
 }
 
-FGMouseInput::mouse_mode::~mouse_mode ()
+mouse_mode::~mouse_mode ()
 {
                                 // FIXME: memory leak
 //   for (int i = 0; i < KEYMOD_MAX; i++) {
@@ -252,45 +547,79 @@ FGMouseInput::mouse_mode::~mouse_mode ()
 //     for (j = 0; j < y_bindings[i].size(); j++)
 //       delete bindings[i][j];
 //   }
-  delete [] buttons;
+  if (buttons) {
+    delete [] buttons;
+  }
 }
 
 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
 {
+    if (!d->initialized) {
+        // can occur during reset
+        return;
+    }
+    
   int modifiers = fgGetKeyModifiers();
 
-  mouse &m = bindings[0];
+  mouse &m = d->mice[0];
   mouse_mode &mode = m.modes[m.current_mode];
-
                                 // Let the property manager know.
   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
 
-                                // Pass on to PUI and the panel if
-                                // requested, and return if one of
-                                // them consumes the event.
+  if (!d->rightClickModeCycle && (b == 2)) {
+    // in spring-loaded look mode, ignore right clicks entirely here
+    return;
+  }
+  
+  // Pass on to PUI and the panel if
+  // requested, and return if one of
+  // them consumes the event.
+
+  osg::Vec2d windowPos;
+  flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+
+  SGSceneryPicks pickList;
+  globals->get_renderer()->pick(pickList, windowPos);
 
-  if (updown != MOUSE_BUTTON_DOWN) {
+  if( updown != MOUSE_BUTTON_DOWN )
+  {
     // Execute the mouse up event in any case, may be we should
     // stop processing here?
-    while (!activePickCallbacks[b].empty()) {
-      activePickCallbacks[b].front()->buttonReleased();
-      activePickCallbacks[b].pop_front();
+
+    SGPickCallbackList& callbacks = d->activePickCallbacks[b];
+
+    while( !callbacks.empty() )
+    {
+      SGPickCallbackPtr& cb = callbacks.front();
+      const SGSceneryPick* pick = getPick(pickList, cb);
+      cb->buttonReleased(ea->getModKeyMask(), *ea, pick ? &pick->info : 0);
+
+      callbacks.pop_front();
     }
   }
 
   if (mode.pass_through) {
     // remove once PUI uses standard picking mechanism
     if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
-      return;
-    else {
-      // pui didn't want the click event so compute a
-      // scenegraph intersection point corresponding to the mouse click
-      if (updown == MOUSE_BUTTON_DOWN) {
-        activePickCallbacks.init( b, ea );
+      return; // pui handled it
+
+    // pui didn't want the click event so compute a
+    // scenegraph intersection point corresponding to the mouse click
+    if (updown == MOUSE_BUTTON_DOWN) {
+      d->activePickCallbacks.init( b, ea );
+        
+      if (d->clickTriggersTooltip) {
+            SGPropertyNode_ptr args(new SGPropertyNode);
+            args->setStringValue("reason", "click");
+            globals->get_commands()->execute("tooltip-timeout", args);
+            d->tooltipTimeoutDone = true;
       }
-    }
-  }
+    } else {
+      // do a hover pick now, to fix up cursor
+      d->doHoverPick(windowPos);
+    } // mouse button was released
+  } // of pass-through mode
 
   // OK, PUI and the panel didn't want the click
   if (b >= MAX_MOUSE_BUTTONS) {
@@ -299,99 +628,105 @@ void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindo
     return;
   }
 
-  m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
+  m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);  
 }
 
-void FGMouseInput::doMouseMotion (int x, int y)
+void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea)
 {
-  // Don't call fgGetKeyModifiers() here, until we are using a
-  // toolkit that supports getting the mods from outside a key
-  // callback.  Glut doesn't.
-  int modifiers = KEYMOD_NONE;
-
-  int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
-  int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
-
-  mouse &m = bindings[0];
-
-  if (m.current_mode < 0 || m.current_mode >= m.nModes) {
-      m.x = x;
-      m.y = y;
-      return;
+  if (!d->activePickCallbacks[0].empty()) {
+    d->doMouseMoveWithCallbacks(ea);
+    return;
+  }
+  
+  mouse &m = d->mice[0];
+  int modeIndex = m.current_mode;
+  // are we in spring-loaded look mode?
+  if (!d->rightClickModeCycle) {
+    if (m.mouse_button_nodes[2]->getBoolValue()) {
+      // right mouse is down, force look mode
+      modeIndex = 3;
+    }
   }
-  mouse_mode &mode = m.modes[m.current_mode];
 
-                                // Pass on to PUI if requested, and return
-                                // if PUI consumed the event.
-  if (mode.pass_through && puMouse(x, y)) {
-      m.x = x;
-      m.y = y;
-      return;
+  if (modeIndex == 0) {
+    osg::Vec2d windowPos;
+    flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+    d->scheduleHoverPick(windowPos);
+    // mouse has moved, so we may need to issue tooltip-timeout command again
+    d->tooltipTimeoutDone = false;
   }
   
-  if (haveWarped)
+  mouse_mode &mode = m.modes[modeIndex];
+  
+  // Pass on to PUI if requested, and return
+  // if PUI consumed the event.
+  if (mode.pass_through && puMouse(x, y)) {
+    return;
+  }
+
+  if (d->haveWarped)
   {
-      // don't fire mouse-movement events at the first update after warping the mouse,
-      // just remember the new mouse position
-      haveWarped = false;
+    // don't fire mouse-movement events at the first update after warping the mouse,
+    // just remember the new mouse position
+    d->haveWarped = false;
   }
   else
   {
-                                // OK, PUI didn't want the event,
-                                // so we can play with it.
-      if (x != m.x) {
-        int delta = x - m.x;
-        xAccelNode->setIntValue( delta );
-        for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
-          mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
-      }
-      if (y != m.y) {
-        int delta = y - m.y;
-        yAccelNode->setIntValue( -delta );
-        for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
-          mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
-      }
+    int modifiers = fgGetKeyModifiers();
+    int xsize = d->xSizeNode ? d->xSizeNode->getIntValue() : 800;
+    int ysize = d->ySizeNode ? d->ySizeNode->getIntValue() : 600;
+      
+    // OK, PUI didn't want the event,
+    // so we can play with it.
+    if (x != m.x) {
+      int delta = x - m.x;
+      d->xAccelNode->setIntValue( delta );
+      for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
+        mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
+    }
+    if (y != m.y) {
+      int delta = y - m.y;
+      d->yAccelNode->setIntValue( -delta );
+      for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
+        mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
+    }
   }
-                                // Constrain the mouse if requested
+  
+  // Constrain the mouse if requested
   if (mode.constrained) {
-    int new_x=x,new_y=y;
-    
-    bool need_warp = false;
-    if (x <= (xsize * .25) || x >= (xsize * .75)) {
-      new_x = int(xsize * .5);
-      need_warp = true;
-    }
+    d->constrainMouse(x, y);
+  }
+}
 
-    if (y <= (ysize * .25) || y >= (ysize * .75)) {
-      new_y = int(ysize * .5);
-      need_warp = true;
+void FGMouseInput::doMouseMotion (int x, int y, const osgGA::GUIEventAdapter* ea)
+{
+    if (!d->initialized) {
+        // can occur during reset
+        return;
     }
+    
+  mouse &m = d->mice[0];
 
-    if (need_warp)
-    {
-      fgWarpMouse(new_x, new_y);
-      haveWarped = true;
-      SG_LOG(SG_INPUT, SG_DEBUG, "Mouse warp: " << x << ", " << y << " => " << new_x << ", " << new_y);
-    }
+  if (m.current_mode < 0 || m.current_mode >= m.nModes) {
+      m.x = x;
+      m.y = y;
+      return;
   }
 
-  if (m.x != x)
-      fgSetInt("/devices/status/mice/mouse/x", m.x = x);
+  m.timeSinceLastMove.stamp();
+  FGMouseCursor::instance()->mouseMoved();
 
-  if (m.y != y)
-      fgSetInt("/devices/status/mice/mouse/y", m.y = y);
-}
+  // TODO Get rid of this as soon as soon as cursor hide timeout works globally
+  if( ea->getHandled() )
+    return;
 
-void FGMouseInput::mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
-{
-    if(mouseInput)
-      mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
+  processMotion(x, y, ea);
+    
+  m.x = x;
+  m.y = y;
+  d->mouseXNode->setIntValue(x);
+  d->mouseYNode->setIntValue(y);
 }
 
-void FGMouseInput::mouseMotionHandler(int x, int y)
-{
-    if (mouseInput != 0)
-        mouseInput->doMouseMotion(x, y);
-}