]> git.mxchange.org Git - flightgear.git/blobdiff - src/Cockpit/panel.cxx
Tiled panel background support from Jim Wilson.
[flightgear.git] / src / Cockpit / panel.cxx
index c844d74972359d22c800937539be4b799bd866e4..69c2741eabf829e82426912c11d6401ce9a0ef59 100644 (file)
@@ -26,6 +26,7 @@
 #  include <windows.h>
 #endif
 
+#include <stdio.h>     // sprintf
 #include <string.h>
 
 #include <plib/ssg.h>
 #define WIN_W 1024
 #define WIN_H 768
 
+#if defined( NONE ) && defined( _MSC_VER )
+#  pragma message( "A sloppy coder has defined NONE as a macro!!!" )
+#  undef NONE
+#elif defined( NONE )
+#  pragma warn A sloppy coder has defined NONE as a macro!!!
+#  undef NONE
+#endif
+
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -166,6 +175,7 @@ FGPanel::FGPanel ()
     _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
     _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
     _bound(false),
+    _jitter(0.0),
     _xsize_node(fgGetNode("/sim/startup/xsize", true)),
     _ysize_node(fgGetNode("/sim/startup/ysize", true))
 {
@@ -221,6 +231,8 @@ FGPanel::bind ()
   fgSetArchivable("/sim/panel/x-offset");
   fgTie("/sim/panel/y-offset", &_y_offset);
   fgSetArchivable("/sim/panel/y-offset");
+  fgTie("/sim/panel/jitter", &_jitter);
+  fgSetArchivable("/sim/panel/jitter");
   _bound = true;
 }
 
@@ -242,11 +254,12 @@ FGPanel::unbind ()
  * Update the panel.
  */
 void
-FGPanel::update ()
+FGPanel::update (int dt)
 {
                                // Do nothing if the panel isn't visible.
-    if (!fgPanelVisible())
+    if ( !fgPanelVisible() ) {
         return;
+    }
 
                                // If the mouse is down, do something
     if (_mouseDown) {
@@ -270,6 +283,31 @@ FGPanel::update ()
 void
 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
 {
+                               // Calculate accelerations
+                               // and jiggle the panel accordingly
+                               // The factors and bounds are just
+                               // initial guesses; using sqrt smooths
+                               // out the spikes.
+  double x_offset = _x_offset;
+  double y_offset = _y_offset;
+
+  if (_jitter != 0.0) {
+    double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
+    double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
+    double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
+
+    double a_zx_pilot = a_z_pilot - a_x_pilot;
+    
+    int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
+                  (a_y_pilot < 0 ? -1 : 1);
+    int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
+                  (a_zx_pilot < 0 ? -1 : 1);
+
+                               // adjustments in screen coordinates
+    x_offset += x_adjust;
+    y_offset += y_adjust;
+  }
+
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();
@@ -280,7 +318,7 @@ FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
   glPushMatrix();
   glLoadIdentity();
 
-  glTranslated(_x_offset, _y_offset, 0);
+  glTranslated(x_offset, y_offset, 0);
 
                                // Draw the background
   glEnable(GL_TEXTURE_2D);
@@ -294,16 +332,40 @@ FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
   } else {
       glColor4f(0.7, 0.2, 0.2, 1.0);
   }
-  glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
-  // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-  // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-  glBegin(GL_POLYGON);
-  glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
-  glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
-  glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
-  glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
-  glEnd();
+  if (_bg != 0) {
+    glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
+    // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+    // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+    glBegin(GL_POLYGON);
+    glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
+    glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
+    glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
+    glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
+    glEnd();
+  } else {
+    for (int i = 0; i < 4; i ++) {
+      // top row of textures...(1,3,5,7)
+      glBindTexture(GL_TEXTURE_2D, _mbg[i*2]->getHandle());
+      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+      glBegin(GL_POLYGON);
+      glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y + (_height/2), 0);
+      glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2), 0);
+      glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y + _height, 0);
+      glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y + _height, 0);
+      glEnd();
+      // bottom row of textures...(2,4,6,8)
+      glBindTexture(GL_TEXTURE_2D, _mbg[(i*2)+1]->getHandle());
+      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+      glBegin(GL_POLYGON);
+      glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y, 0);
+      glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y, 0);
+      glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2), 0);
+      glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y + (_height/2), 0);
+      glEnd();
+    }
+
+  }
 
                                // Draw the instruments.
   instrument_list_type::const_iterator current = _instruments.begin();
@@ -312,7 +374,7 @@ FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
   for ( ; current != end; current++) {
     FGPanelInstrument * instr = *current;
     glLoadIdentity();
-    glTranslated(_x_offset, _y_offset, 0);
+    glTranslated(x_offset, y_offset, 0);
     glTranslated(instr->getXPos(), instr->getYPos(), 0);
     instr->draw();
   }
@@ -355,6 +417,15 @@ FGPanel::setBackground (ssgTexture * texture)
   _bg = texture;
 }
 
+/**
+ * Set the panel's multiple background textures.
+ */
+void
+FGPanel::setMultiBackground (ssgTexture * texture, int idx)
+{
+  _bg = 0;
+  _mbg[idx] = texture;
+}
 
 /**
  * Set the panel's x-offset.
@@ -445,6 +516,8 @@ FGPanelAction::FGPanelAction ()
 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
   : _button(button), _x(x), _y(y), _w(w), _h(h)
 {
+  for (unsigned int i = 0; i < _bindings.size(); i++)
+    delete _bindings[i];
 }
 
 FGPanelAction::~FGPanelAction ()
@@ -452,7 +525,7 @@ FGPanelAction::~FGPanelAction ()
 }
 
 void
-FGPanelAction::addBinding (const FGBinding &binding)
+FGPanelAction::addBinding (FGBinding * binding)
 {
   _bindings.push_back(binding);
 }
@@ -460,9 +533,11 @@ FGPanelAction::addBinding (const FGBinding &binding)
 void
 FGPanelAction::doAction ()
 {
-  int nBindings = _bindings.size();
-  for (int i = 0; i < nBindings; i++) {
-    _bindings[i].fire();
+  if (test()) {
+    int nBindings = _bindings.size();
+    for (int i = 0; i < nBindings; i++) {
+      _bindings[i]->fire();
+    }
   }
 }
 
@@ -479,6 +554,7 @@ FGPanelTransformation::FGPanelTransformation ()
 
 FGPanelTransformation::~FGPanelTransformation ()
 {
+  delete table;
 }
 
 
@@ -558,12 +634,14 @@ FGPanelInstrument::addAction (FGPanelAction * action)
 bool
 FGPanelInstrument::doMouseAction (int button, int x, int y)
 {
-  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 (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;
+      }
     }
   }
   return false;
@@ -591,11 +669,13 @@ FGLayeredInstrument::~FGLayeredInstrument ()
 void
 FGLayeredInstrument::draw ()
 {
-  for (int i = 0; i < (int)_layers.size(); i++) {
-    glPushMatrix();
-    glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
-    _layers[i]->draw();
-    glPopMatrix();
+  if (test()) {
+    for (int i = 0; i < (int)_layers.size(); i++) {
+      glPushMatrix();
+      glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
+      _layers[i]->draw();
+      glPopMatrix();
+    }
   }
 }
 
@@ -656,28 +736,30 @@ FGInstrumentLayer::transform () const
   transformation_list::const_iterator last = _transformations.end();
   while (it != last) {
     FGPanelTransformation *t = *it;
-    float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
-    if (val < t->min) {
-      val = t->min;
-    } else if (val > t->max) {
-      val = t->max;
-    }
-    if(t->table==0) {
+    if (t->test()) {
+      float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
+      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 {
+      } else {
        val = t->table->interpolate(val) * t->factor + t->offset;
-    }
-
-    switch (t->type) {
-    case FGPanelTransformation::XSHIFT:
-      glTranslatef(val, 0.0, 0.0);
-      break;
-    case FGPanelTransformation::YSHIFT:
-      glTranslatef(0.0, val, 0.0);
-      break;
-    case FGPanelTransformation::ROTATION:
-      glRotatef(-val, 0.0, 0.0, 1.0);
-      break;
+      }
+      
+      switch (t->type) {
+      case FGPanelTransformation::XSHIFT:
+       glTranslatef(val, 0.0, 0.0);
+       break;
+      case FGPanelTransformation::YSHIFT:
+       glTranslatef(0.0, val, 0.0);
+       break;
+      case FGPanelTransformation::ROTATION:
+       glRotatef(-val, 0.0, 0.0, 1.0);
+       break;
+      }
     }
     it++;
   }
@@ -690,6 +772,38 @@ FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
 }
 
 
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGGroupLayer.
+////////////////////////////////////////////////////////////////////////
+
+FGGroupLayer::FGGroupLayer ()
+{
+}
+
+FGGroupLayer::~FGGroupLayer ()
+{
+  for (unsigned int i = 0; i < _layers.size(); i++)
+    delete _layers[i];
+}
+
+void
+FGGroupLayer::draw ()
+{
+  if (test()) {
+    int nLayers = _layers.size();
+    for (int i = 0; i < nLayers; i++)
+      _layers[i]->draw();
+  }
+}
+
+void
+FGGroupLayer::addLayer (FGInstrumentLayer * layer)
+{
+  _layers.push_back(layer);
+}
+
+
 \f
 ////////////////////////////////////////////////////////////////////////
 // Implementation of FGTexturedLayer.
@@ -711,27 +825,29 @@ FGTexturedLayer::~FGTexturedLayer ()
 void
 FGTexturedLayer::draw ()
 {
-  int w2 = _w / 2;
-  int h2 = _h / 2;
-
-  transform();
-  glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
-  glBegin(GL_POLYGON);
-
+  if (test()) {
+    int w2 = _w / 2;
+    int h2 = _h / 2;
+    
+    transform();
+    glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
+    glBegin(GL_POLYGON);
+    
                                // From Curt: turn on the panel
                                // lights after sundown.
-  if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
+    if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
       glColor4fv( cur_light_params.scene_diffuse );
-  } else {
+    } else {
       glColor4f(0.7, 0.2, 0.2, 1.0);
-  }
+    }
 
 
-  glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
-  glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
-  glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
-  glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
-  glEnd();
+    glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
+    glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
+    glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
+    glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
+    glEnd();
+  }
 }
 
 
@@ -760,24 +876,26 @@ FGTextLayer::~FGTextLayer ()
 void
 FGTextLayer::draw ()
 {
-  glPushMatrix();
-  glColor4fv(_color);
-  transform();
-  text_renderer.setFont(guiFntHandle);
-  text_renderer.setPointSize(_pointSize);
-  text_renderer.begin();
-  text_renderer.start3f(0, 0, 0);
-
-  _now.stamp();
-  if (_now - _then > 100000) {
-    recalc_value();
-    _then = _now;
-  }
-  text_renderer.puts((char *)(_value.c_str()));
+  if (test()) {
+    glPushMatrix();
+    glColor4fv(_color);
+    transform();
+    text_renderer.setFont(guiFntHandle);
+    text_renderer.setPointSize(_pointSize);
+    text_renderer.begin();
+    text_renderer.start3f(0, 0, 0);
+
+    _now.stamp();
+    if (_now - _then > 100000) {
+      recalc_value();
+      _then = _now;
+    }
+    text_renderer.puts((char *)(_value.c_str()));
 
-  text_renderer.end();
-  glColor4f(1.0, 1.0, 1.0, 1.0);       // FIXME
-  glPopMatrix();
+    text_renderer.end();
+    glColor4f(1.0, 1.0, 1.0, 1.0);     // FIXME
+    glPopMatrix();
+  }
 }
 
 void
@@ -849,18 +967,22 @@ FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
 const char *
 FGTextLayer::Chunk::getValue () const
 {
-  switch (_type) {
-  case TEXT:
-    sprintf(_buf, _fmt.c_str(), _text.c_str());
+  if (test()) {
+    switch (_type) {
+    case TEXT:
+      sprintf(_buf, _fmt.c_str(), _text.c_str());
+      return _buf;
+    case TEXT_VALUE:
+      sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
+      break;
+    case DOUBLE_VALUE:
+      sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
+      break;
+    }
     return _buf;
-  case TEXT_VALUE:
-    sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
-    break;
-  case DOUBLE_VALUE:
-    sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
-    break;
+  } else {
+    return "";
   }
-  return _buf;
 }
 
 
@@ -885,13 +1007,17 @@ FGSwitchLayer::~FGSwitchLayer ()
 void
 FGSwitchLayer::draw ()
 {
-  transform();
-  if (_node->getBoolValue()) {
-    _layer1->draw();
-  } else {
-    _layer2->draw();
+  if (test()) {
+    transform();
+    if (_node->getBoolValue()) {
+      _layer1->draw();
+    } else {
+      _layer2->draw();
+    }
   }
 }
 
 \f
 // end of panel.cxx
+
+