]> git.mxchange.org Git - flightgear.git/blobdiff - src/Cockpit/panel.cxx
Frederic Bouvier's assorted set of MSVC fixes
[flightgear.git] / src / Cockpit / panel.cxx
index 0199dd1789e1911c3409aa369f748985cb12efba..965d8c04de38fe9c88254f8d353c1210d48b97c2 100644 (file)
@@ -52,7 +52,7 @@
 // The number of polygon-offset "units" to place between layers.  In
 // principle, one is supposed to be enough.  In practice, I find that
 // my hardware/driver requires many more.
-#define POFF_UNITS 40
+#define POFF_UNITS 4
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -165,8 +165,8 @@ FGCroppedTexture::getTexture ()
 
 FGPanel * current_panel = NULL;
 static fntRenderer text_renderer;
-static fntTexFont *default_font;
-static fntTexFont *led_font;
+static fntTexFont *default_font = 0;
+static fntTexFont *led_font = 0;
 
 /**
  * Constructor.
@@ -279,14 +279,7 @@ FGPanel::update (double dt)
         return;
     }
 
-                               // If the mouse is down, do something
-    if (_mouseDown) {
-        _mouseDelay--;
-        if (_mouseDelay < 0) {
-            _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
-            _mouseDelay = 2;
-        }
-    }
+    updateMouseDelay();
 
                                // Now, draw the panel
     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
@@ -297,6 +290,22 @@ FGPanel::update (double dt)
         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
 }
 
+/**
+ * Handle repeatable mouse events.  Called from update() and from
+ * fgUpdate3DPanels().  This functionality needs to move into the
+ * input subsystem.  Counting a tick every two frames is clumsy...
+ */
+void FGPanel::updateMouseDelay()
+{
+    if (_mouseDown) {
+        _mouseDelay--;
+        if (_mouseDelay < 0) {
+            _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
+            _mouseDelay = 2;
+        }
+    }
+}
+
 
 void
 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
@@ -361,7 +370,7 @@ FGPanel::draw()
   // of an existing polygon.  Use an offset to prevent z-fighting.  In
   // 2D mode, this is a no-op.
   glEnable(GL_POLYGON_OFFSET_FILL);
-  glPolygonOffset(0, -POFF_UNITS);
+  glPolygonOffset(-1, -POFF_UNITS);
 
   // save some state
   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
@@ -373,12 +382,14 @@ FGPanel::draw()
   glEnable(GL_BLEND);
   glEnable(GL_ALPHA_TEST);
   glEnable(GL_COLOR_MATERIAL);
-  // glColor4f(1.0, 1.0, 1.0, 1.0);
-  if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
-      glColor4fv( cur_light_params.scene_diffuse );
-  } else {
-      glColor4f(0.7, 0.2, 0.2, 1.0);
+  sgVec4 panel_color;
+  sgCopyVec4( panel_color, cur_light_params.scene_diffuse );
+  if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
+      if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
+      if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
+      if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
   }
+  glColor4fv( panel_color );
   if (_bg != 0) {
     glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@@ -425,6 +436,21 @@ FGPanel::draw()
     glPopMatrix();
   }
 
+  // Draw yellow "hotspots" if directed to.  This is a panel authoring
+  // feature; not intended to be high performance or to look good.
+  if(fgGetBool("/sim/panel-hotspots")) {
+    glPushAttrib(GL_ALL_ATTRIB_BITS);
+    glDisable(GL_DEPTH_TEST);
+    glDisable(GL_TEXTURE_2D);
+    glColor3f(1, 1, 0);
+    
+    for(int i=0; i<_instruments.size(); i++)
+      _instruments[i]->drawHotspots();
+
+    glPopAttrib();
+  }
+
+
   // restore some original state
   glPopAttrib();
   glPolygonOffset(0, 0);
@@ -492,38 +518,23 @@ FGPanel::setYOffset (int offset)
 }
 
 /**
- * Perform a mouse action.
+ * Handle a mouse action in panel-local (not screen) coordinates.
+ * Used by the 3D panel code in Model/panelnode.cxx, in situations
+ * where the panel doesn't control its own screen location.
  */
 bool
-FGPanel::doMouseAction (int button, int updown, int x, int y)
+FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
 {
-                               // FIXME: this same code appears in update()
-  int xsize = _xsize_node->getIntValue();
-  int ysize = _ysize_node->getIntValue();
-  float aspect_adjust = get_aspect_adjust(xsize, ysize);
-
-                               // Note a released button and return
-  // cerr << "Doing mouse action\n";
+  // Note a released button and return
   if (updown == 1) {
+    if (_mouseInstrument != 0)
+        _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
     _mouseDown = false;
     _mouseInstrument = 0;
     return false;
   }
 
-                               // Scale for the real window size.
-  if (aspect_adjust < 1.0) {
-    x = int(((float)x / xsize) * WIN_W * aspect_adjust);
-    y = int(WIN_H - ((float(y) / ysize) * WIN_H));
-  } else {
-    x = int(((float)x / xsize) * WIN_W);
-    y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
-  }
-
-                               // Adjust for offsets.
-  x -= _x_offset;
-  y -= _y_offset;
-
-                               // Search for a matching instrument.
+  // Search for a matching instrument.
   for (int i = 0; i < (int)_instruments.size(); i++) {
     FGPanelInstrument *inst = _instruments[i];
     int ix = inst->getXPos();
@@ -537,13 +548,43 @@ FGPanel::doMouseAction (int button, int updown, int x, int y)
       _mouseButton = button;
       _mouseX = x - ix;
       _mouseY = y - iy;
-                               // Always do the action once.
-      return _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
+      // Always do the action once.
+      return _mouseInstrument->doMouseAction(_mouseButton, 0,
+                                             _mouseX, _mouseY);
     }
   }
   return false;
 }
 
+/**
+ * Perform a mouse action.
+ */
+bool
+FGPanel::doMouseAction (int button, int updown, int x, int y)
+{
+                               // FIXME: this same code appears in update()
+  int xsize = _xsize_node->getIntValue();
+  int ysize = _ysize_node->getIntValue();
+  float aspect_adjust = get_aspect_adjust(xsize, ysize);
+
+                               // Scale for the real window size.
+  if (aspect_adjust < 1.0) {
+    x = int(((float)x / xsize) * WIN_W * aspect_adjust);
+    y = int(WIN_H - ((float(y) / ysize) * WIN_H));
+  } else {
+    x = int(((float)x / xsize) * WIN_W);
+    y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
+  }
+
+                               // Adjust for offsets.
+  x -= _x_offset;
+  y -= _y_offset;
+
+  // Having fixed up the coordinates, fall through to the local
+  // coordinate handler.
+  return doLocalMouseAction(button, updown, x, y);
+} 
+
 
 \f
 ////////////////////////////////////////////////////////////////////////.
@@ -554,11 +595,14 @@ FGPanelAction::FGPanelAction ()
 {
 }
 
-FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
-  : _button(button), _x(x), _y(y), _w(w), _h(h)
+FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
+                              bool repeatable)
+    : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
 {
-  for (unsigned int i = 0; i < _bindings.size(); i++)
-    delete _bindings[i];
+  for (unsigned int i = 0; i < 2; i++) {
+      for (unsigned int j = 0; j < _bindings[i].size(); j++)
+          delete _bindings[i][j];
+  }
 }
 
 FGPanelAction::~FGPanelAction ()
@@ -566,19 +610,24 @@ FGPanelAction::~FGPanelAction ()
 }
 
 void
-FGPanelAction::addBinding (FGBinding * binding)
+FGPanelAction::addBinding (FGBinding * binding, int updown)
 {
-  _bindings.push_back(binding);
+  _bindings[updown].push_back(binding);
 }
 
-void
-FGPanelAction::doAction ()
+bool
+FGPanelAction::doAction (int updown)
 {
   if (test()) {
-    int nBindings = _bindings.size();
-    for (int i = 0; i < nBindings; i++) {
-      _bindings[i]->fire();
+    if ((updown != _last_state) || (updown == 0 && _repeatable)) {
+        int nBindings = _bindings[updown].size();
+        for (int i = 0; i < nBindings; i++)
+            _bindings[updown][i]->fire();
     }
+    _last_state = updown;
+    return true;
+  } else {
+    return false;
   }
 }
 
@@ -627,6 +676,25 @@ FGPanelInstrument::~FGPanelInstrument ()
   }
 }
 
+void
+FGPanelInstrument::drawHotspots()
+{
+  for(int i=0; i<_actions.size(); i++) {
+    FGPanelAction* a = _actions[i];
+    float x1 = getXPos() + a->getX();
+    float x2 = x1 + a->getWidth();
+    float y1 = getYPos() + a->getY();
+    float y2 = y1 + a->getHeight();
+
+    glBegin(GL_LINE_LOOP);
+    glVertex2f(x1, y1);
+    glVertex2f(x1, y2);
+    glVertex2f(x2, y2);
+    glVertex2f(x2, y1);
+    glEnd();
+  }
+}
+
 void
 FGPanelInstrument::setPosition (int x, int y)
 {
@@ -673,16 +741,15 @@ FGPanelInstrument::addAction (FGPanelAction * action)
 
                                // Coordinates relative to centre.
 bool
-FGPanelInstrument::doMouseAction (int button, int x, int y)
+FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
 {
   if (test()) {
     action_list_type::iterator it = _actions.begin();
     action_list_type::iterator last = _actions.end();
     for ( ; it != last; it++) {
-      if ((*it)->inArea(button, x, y)) {
-       (*it)->doAction();
-       return true;
-      }
+      if ((*it)->inArea(button, x, y) &&
+          (*it)->doAction(updown))
+        return true;
     }
   }
   return false;
@@ -780,11 +847,15 @@ FGInstrumentLayer::transform () const
     FGPanelTransformation *t = *it;
     if (t->test()) {
       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
+
+      if (t->has_mod)
+          val = fmod(val, t->mod);
       if (val < t->min) {
        val = t->min;
       } else if (val > t->max) {
        val = t->max;
       }
+
       if(t->table==0) {
        val = val * t->factor + t->offset;
       } else {
@@ -833,6 +904,7 @@ void
 FGGroupLayer::draw ()
 {
   if (test()) {
+    transform();
     int nLayers = _layers.size();
     for (int i = 0; i < nLayers; i++)
       _layers[i]->draw();
@@ -877,12 +949,14 @@ FGTexturedLayer::draw ()
     
                                // From Curt: turn on the panel
                                // lights after sundown.
-    if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
-      glColor4fv( cur_light_params.scene_diffuse );
-    } else {
-      glColor4f(0.7, 0.2, 0.2, 1.0);
+    sgVec4 panel_color;
+    sgCopyVec4( panel_color, cur_light_params.scene_diffuse );
+    if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
+        if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
+        if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
+        if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
     }
-
+    glColor4fv( panel_color );
 
     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
@@ -921,7 +995,7 @@ FGTextLayer::draw ()
   if (test()) {
     glColor4fv(_color);
     transform();
-    if ( _font_name == "led" ) {
+    if ( _font_name == "led" && led_font != 0) {
        text_renderer.setFont(led_font);
     } else {
        text_renderer.setFont(guiFntHandle);
@@ -1061,28 +1135,22 @@ FGTextLayer::Chunk::getValue () const
 // Implementation of FGSwitchLayer.
 ////////////////////////////////////////////////////////////////////////
 
-FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
-                             FGInstrumentLayer * layer1,
-                             FGInstrumentLayer * layer2)
-  : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
+FGSwitchLayer::FGSwitchLayer ()
+  : FGGroupLayer()
 {
 }
 
-FGSwitchLayer::~FGSwitchLayer ()
-{
-  delete _layer1;
-  delete _layer2;
-}
-
 void
 FGSwitchLayer::draw ()
 {
   if (test()) {
     transform();
-    if (_node->getBoolValue()) {
-      _layer1->draw();
-    } else {
-      _layer2->draw();
+    int nLayers = _layers.size();
+    for (int i = 0; i < nLayers; i++) {
+      if (_layers[i]->test()) {
+          _layers[i]->draw();
+          return;
+      }
     }
   }
 }