]> 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 f7d32778b26235f16e0622d810ec34941bfb1abc..6d052c8817f544e76db793991f40e92ef09e2e71 100644 (file)
@@ -49,13 +49,19 @@ 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, std::list<SGSharedPtr<SGPickCallback> > > {
-public:
+class ActivePickCallbacks:
+  public std::map<int, SGPickCallbackList>
+{
+  public:
     void update( double dt, unsigned int keyModState );
     void init( int button, const osgGA::GUIEventAdapter* ea );
 };
@@ -71,14 +77,14 @@ void ActivePickCallbacks::init( int button, const osgGA::GUIEventAdapter* ea )
   // 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;
+  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;
     }
@@ -89,7 +95,7 @@ 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, keyModState);
     }
@@ -130,12 +136,25 @@ struct mouse {
     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 ) ),
@@ -145,12 +164,15 @@ public:
         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);
     }
@@ -188,39 +210,52 @@ public:
         }
     }
 
+    void scheduleHoverPick(const osg::Vec2d& windowPos)
+    {
+      hoverPickScheduled = true;
+      hoverPos = windowPos;
+    }
+  
     void doHoverPick(const osg::Vec2d& windowPos)
     {
-        std::vector<SGSceneryPick> pickList;
-        SGPickCallback::Priority priority = SGPickCallback::PriorityScenery;
-      
         FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_ARROW;
         bool explicitCursor = false;
         bool didPick = false;
-      
-        if (globals->get_renderer()->pick(pickList, windowPos)) {
+
+        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());
+            }
             
-            std::vector<SGSceneryPick>::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
-        } else { // of have valid pick
+            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;
@@ -228,7 +263,9 @@ public:
         
         FGMouseCursor::instance()->setCursor(cur);
         if (!didPick) {
-          updateHover();
+          SGPropertyNode_ptr args(new SGPropertyNode);
+          globals->get_commands()->execute("update-hover", args);
+
         }
     }
     
@@ -236,24 +273,32 @@ public:
     {
         FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND;
         
-        BOOST_FOREACH(SGPickCallback* cb, activePickCallbacks[0]) {
-            cb->mouseMoved(ea);
-            std::string curName(cb->getCursor());
-            if (!curName.empty()) {
-                cur = FGMouseCursor::cursorFromString(curName.c_str());
-            }
+        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);
     }
-    
-    void updateHover()
-    {
-        SGPropertyNode_ptr args(new SGPropertyNode);
-        globals->get_commands()->execute("update-hover", args);
-    }
 
-    
     // implement the property-change-listener interfacee
     virtual void valueChanged( SGPropertyNode * node )
     {
@@ -271,25 +316,32 @@ public:
             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;
 };
 
 
@@ -326,8 +378,16 @@ FGMouseInput::~FGMouseInput()
 
 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) {
@@ -386,10 +446,22 @@ void FGMouseInput::init()
       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
   }
 
@@ -399,6 +471,10 @@ void FGMouseInput::init()
 
 void FGMouseInput::update ( double dt )
 {
+    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) {
@@ -415,9 +491,13 @@ void FGMouseInput::update ( double dt )
     }
   }
 
-  // if delay is <= 0, disable tooltips
+  if ((mode == 0) && d->hoverPickScheduled) {
+    d->doHoverPick(d->hoverPos);
+    d->hoverPickScheduled = false;
+  }
+  
   if ( !d->tooltipTimeoutDone &&
-      (d->tooltipDelayMsec > 0) &&
+      d->tooltipsEnabled &&
       (m.timeSinceLastMove.elapsedMSec() > d->tooltipDelayMsec))
   {
       d->tooltipTimeoutDone = true;
@@ -474,6 +554,11 @@ mouse_mode::~mouse_mode ()
 
 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 = d->mice[0];
@@ -491,12 +576,26 @@ void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindo
   // requested, and return if one of
   // them consumes the event.
 
-  if (updown != MOUSE_BUTTON_DOWN) {
+  osg::Vec2d windowPos;
+  flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
+
+  SGSceneryPicks pickList;
+  globals->get_renderer()->pick(pickList, windowPos);
+
+  if( updown != MOUSE_BUTTON_DOWN )
+  {
     // Execute the mouse up event in any case, may be we should
     // stop processing here?
-    while (!d->activePickCallbacks[b].empty()) {
-      d->activePickCallbacks[b].front()->buttonReleased(ea->getModKeyMask());
-      d->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();
     }
   }
 
@@ -518,8 +617,6 @@ void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindo
       }
     } else {
       // do a hover pick now, to fix up cursor
-      osg::Vec2d windowPos;
-      flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
       d->doHoverPick(windowPos);
     } // mouse button was released
   } // of pass-through mode
@@ -554,7 +651,7 @@ void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea)
   if (modeIndex == 0) {
     osg::Vec2d windowPos;
     flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y());
-    d->doHoverPick(windowPos);
+    d->scheduleHoverPick(windowPos);
     // mouse has moved, so we may need to issue tooltip-timeout command again
     d->tooltipTimeoutDone = false;
   }
@@ -603,6 +700,11 @@ void FGMouseInput::processMotion(int x, int y, const osgGA::GUIEventAdapter* ea)
 
 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 (m.current_mode < 0 || m.current_mode >= m.nModes) {
@@ -614,6 +716,10 @@ void FGMouseInput::doMouseMotion (int x, int y, const osgGA::GUIEventAdapter* ea
   m.timeSinceLastMove.stamp();
   FGMouseCursor::instance()->mouseMoved();
 
+  // TODO Get rid of this as soon as soon as cursor hide timeout works globally
+  if( ea->getHandled() )
+    return;
+
   processMotion(x, y, ea);
     
   m.x = x;