]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/HUD/HUD.cxx
Merge commit 'refs/merge-requests/1551' of git://gitorious.org/fg/flightgear into...
[flightgear.git] / src / Instrumentation / HUD / HUD.cxx
index 53a688e230671458324c476141b54cdfdbd1b910..d37f28a3d4813a83d142f6814f9cc3b57a514554 100644 (file)
 
 #include <simgear/constants.h>
 #include <simgear/misc/sg_path.hxx>
+#include <osg/GLU>
 
 #include <Main/globals.hxx>
 #include <Main/viewmgr.hxx>
+#include <Main/viewer.hxx>
 
 #include "HUD.hxx"
 
@@ -45,6 +47,7 @@ static float clamp(float f)
 
 
 HUD::HUD() :
+    _path(fgGetNode("/sim/hud/path[1]", "Huds/default.xml")),
     _current(fgGetNode("/sim/hud/current-color", true)),
     _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
     _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
@@ -70,10 +73,13 @@ HUD::HUD() :
     _font_renderer(new fntRenderer()),
     _font(0),
     _font_size(0.0),
-    _style(0)
+    _style(0),
+    _listener_active(false),
+    _clip_box(0)
 {
     SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
 
+    _path->addChangeListener(this);
     _visibility->addChangeListener(this);
     _3DenabledN->addChangeListener(this);
     _antialiasing->addChangeListener(this);
@@ -93,6 +99,7 @@ HUD::HUD() :
 
 HUD::~HUD()
 {
+    _path->removeChangeListener(this);
     _visibility->removeChangeListener(this);
     _3DenabledN->removeChangeListener(this);
     _antialiasing->removeChangeListener(this);
@@ -108,27 +115,35 @@ HUD::~HUD()
     _scr_heightN->removeChangeListener(this);
     _unitsN->removeChangeListener(this);
     delete _font_renderer;
+    delete _clip_box;
 
     deque<Item *>::const_iterator it, end = _items.end();
     for (it = _items.begin(); it != end; ++it)
         delete *it;
+    end = _ladders.end();
+    for (it = _ladders.begin(); it != end; ++it)
+        delete *it;
 }
 
 
 void HUD::init()
 {
+    const char* fontName = 0;
     _font_cache = globals->get_fontcache();
+    if (!_font) {
+        fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
+        _font = _font_cache->getTexFont(fontName);
+    }
     if (!_font)
-        _font = _font_cache->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
-    if (!_font)
-        throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
+        throw sg_io_exception("/sim/hud/font/name is not a texture font",
+                              sg_location(fontName));
 
     _font_size = fgGetFloat("/sim/hud/font/size", 8);
     _font_renderer->setFont(_font);
     _font_renderer->setPointSize(_font_size);
     _text_list.setFont(_font_renderer);
 
-    load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
+    _path->fireValueChanged();
 }
 
 
@@ -143,7 +158,7 @@ void HUD::draw(osg::State&)
     if (!isVisible())
         return;
 
-    if (!_items.size())
+    if (!_items.size() && !_ladders.size())
         return;
 
     if (is3D()) {
@@ -264,6 +279,8 @@ void HUD::common_draw()
     }
 
     setColor();
+    _clip_box->set();
+
     deque<Item *>::const_iterator it, end = _items.end();
     for (it = _items.begin(); it != end; ++it)
         if ((*it)->isEnabled())
@@ -279,6 +296,14 @@ void HUD::common_draw()
         glDisable(GL_LINE_STIPPLE);
     }
 
+    // ladders last, as they can have their own clip planes
+    end = _ladders.end();
+    for (it = _ladders.begin(); it != end; ++it)
+        if ((*it)->isEnabled())
+            (*it)->draw();
+
+    _clip_box->unset();
+
     if (isAntialiased()) {
         glDisable(GL_ALPHA_TEST);
         glDisable(GL_LINE_SMOOTH);
@@ -304,6 +329,7 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
     if (!level) {
         SG_LOG(SG_INPUT, TREE, endl << "load " << file);
         _items.erase(_items.begin(), _items.end());
+        _ladders.erase(_ladders.begin(), _ladders.end());
     } else if (level > MAXNEST) {
         SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
         return 0x1;
@@ -328,6 +354,9 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
         return 0x8;
     }
 
+    delete _clip_box;
+    _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
+
     for (int i = 0; i < root.nChildren(); i++) {
         SGPropertyNode *n = root.getChild(i);
         const char *d = n->getStringValue("name", 0);
@@ -371,6 +400,8 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
             item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
         } else if (!strcmp(name, "ladder")) {
             item = static_cast<Item *>(new Ladder(this, n, x, y));
+            _ladders.insert(_ladders.begin(), item);
+            continue;
         } else if (!strcmp(name, "runway")) {
             item = static_cast<Item *>(new Runway(this, n, x, y));
         } else if (!strcmp(name, "aiming-reticle")) {
@@ -389,6 +420,12 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
 
 void HUD::valueChanged(SGPropertyNode *node)
 {
+    if (_listener_active)
+        return;
+    _listener_active = true;
+    if (!strcmp(node->getName(), "path"))
+        load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
+
     if (!strcmp(node->getName(), "current-color")) {
         int i = node->getIntValue();
         if (i < 0)
@@ -428,6 +465,7 @@ void HUD::valueChanged(SGPropertyNode *node)
     _cl = clamp(_alpha_clamp->getFloatValue());
 
     _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
+    _listener_active = false;
 }
 
 
@@ -440,10 +478,8 @@ void HUD::setColor() const
 }
 
 
-
-
 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
-        float *x, float *y, float *l, float *r, float *t, float *b)
+        float *x, float *y, float *l, float *r, float *b, float *t)
 {
     fntFont *font = rend->getFont();
     float gap = font->getGap();
@@ -572,3 +608,57 @@ void TextList::draw()
 }
 
 
+ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
+    _active(false),
+    _xoffs(xoffset),
+    _yoffs(yoffset)
+{
+    if (!n)
+        return;
+
+    // const_cast is necessary because ATM there's no matching getChild(const ...)
+    // prototype and getNode(const ..., <bool>) is wrongly interpreted as
+    // getNode(const ..., <int>)
+    _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
+    _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
+    _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
+    _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
+
+    _left[0] = 1.0, _left[1] = _left[2] = 0.0;
+    _right[0] = -1.0, _right[1] = _right[2] = 0.0;
+    _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
+    _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
+    _active = true;
+}
+
+
+void ClipBox::set()
+{
+    if (!_active)
+        return;
+
+    _left[3] = -_left_node->getDoubleValue() - _xoffs;
+    _right[3] = _right_node->getDoubleValue() + _xoffs;
+    _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
+    _top[3] = _top_node->getDoubleValue() + _yoffs;
+
+    glClipPlane(GL_CLIP_PLANE0, _top);
+    glEnable(GL_CLIP_PLANE0);
+    glClipPlane(GL_CLIP_PLANE1, _bot);
+    glEnable(GL_CLIP_PLANE1);
+    glClipPlane(GL_CLIP_PLANE2, _left);
+    glEnable(GL_CLIP_PLANE2);
+    glClipPlane(GL_CLIP_PLANE3, _right);
+    glEnable(GL_CLIP_PLANE3);
+}
+
+
+void ClipBox::unset()
+{
+    if (_active) {
+        glDisable(GL_CLIP_PLANE0);
+        glDisable(GL_CLIP_PLANE1);
+        glDisable(GL_CLIP_PLANE2);
+        glDisable(GL_CLIP_PLANE3);
+    }
+}