]> git.mxchange.org Git - flightgear.git/blobdiff - src/Input/input.cxx
Oops, don't forget that the axisnumber can actually be 0
[flightgear.git] / src / Input / input.cxx
index 6253a29901215a05e3a81bb4c676e0d136517c4c..98432a6f01803e7b43d3737015601296a27927e1 100644 (file)
@@ -179,12 +179,18 @@ FGInput::init ()
   fgRegisterMouseMotionHandler(mouseMotionHandler);
 }
 
+void
+FGInput::reinit ()
+{
+    init();
+}
+
 void 
 FGInput::update (double dt)
 {
   _update_keyboard();
   _update_joystick(dt);
-  _update_mouse();
+  _update_mouse(dt);
 }
 
 void
@@ -217,9 +223,6 @@ FGInput::makeDefault (bool status)
 void
 FGInput::doKey (int k, int modifiers, int x, int y)
 {
-  // static SGPropertyNode *heading_enabled
-  //     = fgGetNode("/autopilot/locks/heading", true);
-
                                 // Sanity check.
   if (k < 0 || k >= MAX_KEYS) {
     SG_LOG(SG_INPUT, SG_WARN, "Key value " << k << " out of range");
@@ -243,7 +246,6 @@ FGInput::doKey (int k, int modifiers, int x, int y)
       }
     }
   }
-
                                 // Key released.
   else {
     SG_LOG(SG_INPUT, SG_DEBUG, "User released key " << k
@@ -259,10 +261,6 @@ FGInput::doKey (int k, int modifiers, int x, int y)
       }
     }
   }
-
-
-                                // Use the old, default actions.
-  SG_LOG( SG_INPUT, SG_DEBUG, "(No user binding.)" );
 }
 
 void
@@ -304,19 +302,30 @@ FGInput::doMouseClick (int b, int updown, int x, int y)
 void
 FGInput::doMouseMotion (int x, int y)
 {
-  int modifiers = fgGetKeyModifiers();
+  // 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 = fgGetInt("/sim/startup/xsize", 800);
   int ysize = fgGetInt("/sim/startup/ysize", 600);
+
   mouse &m = _mouse_bindings[0];
-  if (m.current_mode < 0 || m.current_mode >= m.nModes)
-    return;
+
+  if (m.current_mode < 0 || m.current_mode >= m.nModes) {
+      m.x = x;
+      m.y = y;
+      return;
+  }
   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))
-    return;
+  if (mode.pass_through && puMouse(x, y)) {
+      m.x = x;
+      m.y = y;
+      return;
+  }
 
                                 // OK, PUI didn't want the event,
                                 // so we can play with it.
@@ -382,6 +391,7 @@ FGInput::_init_keyboard ()
 void
 FGInput::_init_joystick ()
 {
+  jsInit();
                                 // TODO: zero the old bindings first.
   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing joystick bindings");
   SGPropertyNode * js_nodes = fgGetNode("/input/joysticks");
@@ -458,22 +468,34 @@ FGInput::_init_joystick ()
     //
     // Initialize the axes.
     //
+    vector<SGPropertyNode_ptr> axes = js_node->getChildren("axis");
+    size_t nb_axes = axes.size();
     int j;
-    for (j = 0; j < naxes; j++) {
-      const SGPropertyNode * axis_node = js_node->getChild("axis", j);
-      if (axis_node == 0) {
-        SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for axis " << j);
-        axis_node = js_node->getChild("axis", j, true);
+    for (j = 0; j < (int)nb_axes; j++) {
+      const SGPropertyNode * axis_node = axes[j];
+      const SGPropertyNode * num_node = axis_node->getChild("number");
+      size_t n_axis = axis_node->getIndex();
+      if (num_node != 0) {
+          n_axis = num_node->getIntValue(TGT_PLATFORM, -1);
+
+          // Silently ignore platforms that are not specified within the
+          // <number></number> section
+          if (n_axis < 0)
+             continue;
       }
-      
-      axis &a = _joystick_bindings[i].axes[j];
 
-      js->setDeadBand(j, axis_node->getDoubleValue("dead-band", 0.0));
+      if (n_axis >= (size_t)naxes) {
+          SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for axis " << n_axis);
+          continue;
+      }
+      axis &a = _joystick_bindings[i].axes[n_axis];
+
+      js->setDeadBand(n_axis, axis_node->getDoubleValue("dead-band", 0.0));
 
       a.tolerance = axis_node->getDoubleValue("tolerance", 0.002);
-      minRange[j] = axis_node->getDoubleValue("min-range", minRange[j]);
-      maxRange[j] = axis_node->getDoubleValue("max-range", maxRange[j]);
-      center[j] = axis_node->getDoubleValue("center", center[j]);
+      minRange[n_axis] = axis_node->getDoubleValue("min-range", minRange[n_axis]);
+      maxRange[n_axis] = axis_node->getDoubleValue("max-range", maxRange[n_axis]);
+      center[n_axis] = axis_node->getDoubleValue("center", center[n_axis]);
 
       _read_bindings(axis_node, a.bindings, KEYMOD_NONE);
 
@@ -490,17 +512,29 @@ FGInput::_init_joystick ()
     //
     // Initialize the buttons.
     //
+    vector<SGPropertyNode_ptr> buttons = js_node->getChildren("button");
     char buf[32];
-    for (j = 0; j < nbuttons; j++) {
-      sprintf(buf, "%d", j);
-      SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << j);
-      _init_button(js_node->getChild("button", j),
-                   _joystick_bindings[i].buttons[j],
+    for (j = 0; (j < (int)buttons.size()) && (j < nbuttons); j++) {
+      const SGPropertyNode * button_node = buttons[j];
+      const SGPropertyNode * num_node = button_node->getChild("number");
+      size_t n_but = button_node->getIndex();
+      if (num_node != 0) {
+          n_but = num_node->getIntValue(TGT_PLATFORM,n_but);
+      }
+
+      if (n_but >= (size_t)nbuttons) {
+          SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but);
+          continue;
+      }
+
+      sprintf(buf, "%d", n_but);
+      SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but);
+      _init_button(button_node,
+                   _joystick_bindings[i].buttons[n_but],
                    buf);
       
       // get interval-sec property             
-      button &b = _joystick_bindings[i].buttons[j];
-      const SGPropertyNode * button_node = js_node->getChild("button", j);
+      button &b = _joystick_bindings[i].buttons[n_but];
       if (button_node != 0) {
         b.interval_sec = button_node->getDoubleValue("interval-sec",0.0);
         b.last_dt = 0.0;
@@ -522,7 +556,7 @@ FGInput::_init_joystick ()
 // cursors defined as textures referenced in the property tree.  This
 // list could then be eliminated. -Andy
 //
-struct {
+static struct {
   const char * name;
   int cursor;
 } mouse_cursor_map[] = {
@@ -704,12 +738,13 @@ FGInput::_update_joystick (double dt)
 }
 
 void
-FGInput::_update_mouse ()
+FGInput::_update_mouse ( double dt )
 {
   mouse &m = _mouse_bindings[0];
   int mode =  m.mode_node->getIntValue();
   if (mode != m.current_mode) {
     m.current_mode = mode;
+    m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
     if (mode >= 0 && mode < m.nModes) {
       fgSetMouseCursor(m.modes[mode].cursor);
       m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
@@ -720,6 +755,21 @@ FGInput::_update_mouse ()
       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
     }
   }
+
+  if ( fgGetBool( "/sim/mouse/hide-cursor", true ) ) {
+      if ( m.x != m.save_x || m.y != m.save_y ) {
+          m.timeout = fgGetDouble( "/sim/mouse/cursor-timeout-sec", 10.0 );
+          fgSetMouseCursor(m.modes[mode].cursor);
+      } else {
+          m.timeout -= dt;
+          if ( m.timeout <= 0.0 ) {
+              fgSetMouseCursor(MOUSE_CURSOR_NONE);
+              m.timeout = 0.0;
+          }
+      }
+      m.save_x = m.x;
+      m.save_y = m.y;
+  }
 }
 
 void