]> git.mxchange.org Git - flightgear.git/blobdiff - src/Cockpit/panel.cxx
Panel tweaks to support "shaped" panels.
[flightgear.git] / src / Cockpit / panel.cxx
index d8e7e4c2b197121db8c8fc6b639717afeca6f69b..16168f8c80d7a0d1443072dcb1e87297de08caf0 100644 (file)
@@ -68,22 +68,70 @@ FGTextureManager::createTexture (const string &relativePath)
 }
 
 
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGCropped Texture.
+////////////////////////////////////////////////////////////////////////
+
+
+FGCroppedTexture::FGCroppedTexture ()
+  : _path(""), _texture(0),
+    _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
+{
+}
+
+
+FGCroppedTexture::FGCroppedTexture (const string &path,
+                                   float minX, float minY,
+                                   float maxX, float maxY)
+  : _path(path), _texture(0),
+    _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
+{
+}
+
+
+FGCroppedTexture::~FGCroppedTexture ()
+{
+}
+
+
+ssgTexture *
+FGCroppedTexture::getTexture ()
+{
+  if (_texture == 0) {
+    _texture = FGTextureManager::createTexture(_path);
+  }
+  return _texture;
+}
+
+
 \f
 ////////////////////////////////////////////////////////////////////////
 // Implementation of FGPanel.
 ////////////////////////////////////////////////////////////////////////
 
 FGPanel * current_panel = NULL;
+static fntRenderer text_renderer;
+
 
-FGPanel::FGPanel (int x, int y, int w, int h)
+/**
+ * Constructor.
+ */
+FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
   : _mouseDown(false),
     _mouseInstrument(0),
-    _x(x), _y(y), _w(w), _h(h)
+    _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
+    _width(_winw), _height(int(_winh * 0.5768 + 1)),
+    _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232))
 {
   setVisibility(current_options.get_panel_status());
-  _panel_h = (int)(h * 0.5768 + 1);
 }
 
+
+/**
+ * Destructor.
+ */
 FGPanel::~FGPanel ()
 {
   for (instrument_list_type::iterator it = _instruments.begin();
@@ -94,12 +142,20 @@ FGPanel::~FGPanel ()
   }
 }
 
+
+/**
+ * Add an instrument to the panel.
+ */
 void
 FGPanel::addInstrument (FGPanelInstrument * instrument)
 {
   _instruments.push_back(instrument);
 }
 
+
+/**
+ * Update the panel.
+ */
 void
 FGPanel::update () const
 {
@@ -120,12 +176,14 @@ FGPanel::update () const
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();
-  gluOrtho2D(_x, _x + _w, _y, _y + _h);
+  gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
 
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();
 
+  glTranslated(_x_offset, _y_offset, 0);
+
                                // Draw the background
   glEnable(GL_TEXTURE_2D);
   glDisable(GL_LIGHTING);
@@ -143,10 +201,10 @@ FGPanel::update () const
   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(_x, _y, 0);
-  glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
-  glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
-  glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
+  glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
+  glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
+  glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
+  glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
   glEnd();
 
                                // Draw the instruments.
@@ -156,6 +214,7 @@ FGPanel::update () const
   for ( ; current != end; current++) {
     FGPanelInstrument * instr = *current;
     glLoadIdentity();
+    glTranslated(_x_offset, _y_offset, 0);
     glTranslated(instr->getXPos(), instr->getYPos(), 0);
     instr->draw();
   }
@@ -168,27 +227,66 @@ FGPanel::update () const
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 }
 
+
+/**
+ * Set the panel's visibility.
+ */
 void
 FGPanel::setVisibility (bool visibility)
 {
   _visibility = visibility;
 }
 
+
+/**
+ * Return true if the panel is visible.
+ */
 bool
 FGPanel::getVisibility () const
 {
   return _visibility;
 }
 
+
+/**
+ * Set the panel's background texture.
+ */
 void
 FGPanel::setBackground (ssgTexture * texture)
 {
   _bg = texture;
 }
 
+
+/**
+ * Set the panel's x-offset.
+ */
+void
+FGPanel::setXOffset (int offset)
+{
+  if (offset <= 0 && offset >= -_width + _winw)
+    _x_offset = offset;
+}
+
+
+/**
+ * Set the panel's y-offset.
+ */
+void
+FGPanel::setYOffset (int offset)
+{
+  if (offset <= 0 && offset >= -_height)
+    _y_offset = offset;
+}
+
+
+/**
+ * Perform a mouse action.
+ */
 bool
 FGPanel::doMouseAction (int button, int updown, int x, int y)
 {
+
                                // Note a released button and return
   // cerr << "Doing mouse action\n";
   if (updown == 1) {
@@ -197,10 +295,16 @@ FGPanel::doMouseAction (int button, int updown, int x, int y)
     return true;
   }
 
-  x = (int)(((float)x / current_view.get_winWidth()) * _w);
-  y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
+                               // Scale for the real window size.
+  x = int(((float)x / current_view.get_winWidth()) * _winw);
+  y = int(_winh - (((float)y / current_view.get_winHeight()) * _winh));
+
+                               // Adjust for offsets.
+  x -= _x_offset;
+  y -= _y_offset;
 
-  for (int i = 0; i < _instruments.size(); i++) {
+                               // Search for a matching instrument.
+  for (int i = 0; i < (int)_instruments.size(); i++) {
     FGPanelInstrument *inst = _instruments[i];
     int ix = inst->getXPos();
     int iy = inst->getYPos();
@@ -248,7 +352,7 @@ FGPanelAction::~FGPanelAction ()
 
 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
                                SGValue * value, float increment, 
-                               float min, float max, bool wrap=false)
+                               float min, float max, bool wrap)
   : FGPanelAction(button, x, y, w, h),
     _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
 {
@@ -327,15 +431,6 @@ FGPanelTransformation::FGPanelTransformation ()
 {
 }
 
-FGPanelTransformation::FGPanelTransformation (Type _type,
-                                             const SGValue * _value,
-                                             float _min, float _max,
-                                             float _factor, float _offset)
-  : type(_type), value(_value), min(_min), max(_max),
-    factor(_factor), offset(_offset)
-{
-}
-
 FGPanelTransformation::~FGPanelTransformation ()
 {
 }
@@ -450,7 +545,7 @@ FGLayeredInstrument::~FGLayeredInstrument ()
 void
 FGLayeredInstrument::draw ()
 {
-  for (int i = 0; i < _layers.size(); i++) {
+  for (int i = 0; i < (int)_layers.size(); i++) {
     glPushMatrix();
     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
     _layers[i]->draw();
@@ -473,8 +568,8 @@ FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
 }
 
 int
-FGLayeredInstrument::addLayer (CroppedTexture &texture,
-                              int w = -1, int h = -1)
+FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
+                              int w, int h)
 {
   return addLayer(new FGTexturedLayer(texture, w, h));
 }
@@ -551,7 +646,7 @@ FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
 ////////////////////////////////////////////////////////////////////////
 
 
-FGTexturedLayer::FGTexturedLayer (CroppedTexture &texture, int w, int h)
+FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
   : FGInstrumentLayer(w, h)
 {
   setTexture(texture);
@@ -570,17 +665,22 @@ FGTexturedLayer::draw ()
   int h2 = _h / 2;
 
   transform();
-  glBindTexture(GL_TEXTURE_2D, _texture.texture->getHandle());
+  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 * RAD_TO_DEG < 95.0 ) {
       glColor4fv( cur_light_params.scene_diffuse );
   } else {
       glColor4f(0.7, 0.2, 0.2, 1.0);
   }
-  glTexCoord2f(_texture.minX, _texture.minY); glVertex2f(-w2, -h2);
-  glTexCoord2f(_texture.maxX, _texture.minY); glVertex2f(w2, -h2);
-  glTexCoord2f(_texture.maxX, _texture.maxY); glVertex2f(w2, h2);
-  glTexCoord2f(_texture.minX, _texture.maxY); glVertex2f(-w2, h2);
+
+
+  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();
 }
 
@@ -590,18 +690,12 @@ FGTexturedLayer::draw ()
 // Implementation of FGTextLayer.
 ////////////////////////////////////////////////////////////////////////
 
-FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
-                         Chunk * chunk3)
-  : FGInstrumentLayer(w, h)
+FGTextLayer::FGTextLayer (int w, int h)
+  : FGInstrumentLayer(w, h), _pointSize(14.0)
 {
+  _then.stamp();
   _color[0] = _color[1] = _color[2] = 0.0;
   _color[3] = 1.0;
-  if (chunk1)
-    addChunk(chunk1);
-  if (chunk2)
-    addChunk(chunk2);
-  if (chunk3)
-    addChunk(chunk3);
 }
 
 FGTextLayer::~FGTextLayer ()
@@ -619,19 +713,19 @@ FGTextLayer::draw ()
   glPushMatrix();
   glColor4fv(_color);
   transform();
-  _renderer.setFont(guiFntHandle);
-  _renderer.setPointSize(14);
-  _renderer.begin();
-  _renderer.start3f(0, 0, 0);
-
-                               // Render each of the chunks.
-  chunk_list::const_iterator it = _chunks.begin();
-  chunk_list::const_iterator last = _chunks.end();
-  for ( ; it != last; it++) {
-    _renderer.puts((*it)->getValue());
+  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()));
 
-  _renderer.end();
+  text_renderer.end();
   glColor4f(1.0, 1.0, 1.0, 1.0);       // FIXME
   glPopMatrix();
 }
@@ -652,15 +746,27 @@ FGTextLayer::setColor (float r, float g, float b)
 }
 
 void
-FGTextLayer::setPointSize (const float size)
+FGTextLayer::setPointSize (float size)
 {
-  _renderer.setPointSize(size);
+  _pointSize = size;
 }
 
 void
 FGTextLayer::setFont(fntFont * font)
 {
-  _renderer.setFont(font);
+  text_renderer.setFont(font);
+}
+
+
+void
+FGTextLayer::recalc_value () const
+{
+  _value = "";
+  chunk_list::const_iterator it = _chunks.begin();
+  chunk_list::const_iterator last = _chunks.end();
+  for ( ; it != last; it++) {
+    _value += (*it)->getValue();
+  }
 }
 
 
@@ -669,37 +775,39 @@ FGTextLayer::setFont(fntFont * font)
 // Implementation of FGTextLayer::Chunk.
 ////////////////////////////////////////////////////////////////////////
 
-FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
+FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
   : _type(FGTextLayer::TEXT), _fmt(fmt)
 {
-  _value._text = text;
+  _text = text;
+  if (_fmt == "") 
+    _fmt = "%s";
 }
 
 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
-                          char * fmt = 0, float mult = 1.0)
+                          const string &fmt, float mult)
   : _type(type), _fmt(fmt), _mult(mult)
 {
-  if (_fmt == 0) {
+  if (_fmt == "") {
     if (type == TEXT_VALUE)
       _fmt = "%s";
     else
       _fmt = "%.2f";
   }
-  _value._value = value;
+  _value = value;
 }
 
-char *
+const char *
 FGTextLayer::Chunk::getValue () const
 {
   switch (_type) {
   case TEXT:
-    sprintf(_buf, _fmt, _value._text);
+    sprintf(_buf, _fmt.c_str(), _text.c_str());
     return _buf;
   case TEXT_VALUE:
-    sprintf(_buf, _fmt, _value._value->getStringValue().c_str());
+    sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
     break;
   case DOUBLE_VALUE:
-    sprintf(_buf, _fmt, _value._value->getFloatValue() * _mult);
+    sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
     break;
   }
   return _buf;