]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/HUD/HUD_label.cxx
Merge branch 'torsten/proplist' into next
[flightgear.git] / src / Instrumentation / HUD / HUD_label.cxx
index 3c33245c4d9cc27980909e4204f870fac7175e95..98a6a1df0974612cd95815a9f47e8af1c6e3be3a 100644 (file)
@@ -23,7 +23,6 @@
 #  include <config.h>
 #endif
 
-#include <simgear/props/condition.hxx>
 #include "HUD.hxx"
 
 
@@ -44,11 +43,13 @@ HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
 
     const char *halign = n->getStringValue("halign", "center");
     if (!strcmp(halign, "left"))
-        _halign = LEFT_ALIGN;
+        _halign = LEFT;
     else if (!strcmp(halign, "right"))
-        _halign = RIGHT_ALIGN;
+        _halign = RIGHT;
     else
-        _halign = CENTER_ALIGN;
+        _halign = HCENTER;
+
+    _halign |= VCENTER;
 
     const char *pre = n->getStringValue("prefix", 0);
     const char *post = n->getStringValue("postfix", 0);
@@ -67,21 +68,19 @@ HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
 
     _mode = check_format(_format.c_str());
     if (_mode == INVALID) {
-        SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
+        SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str()
+                << "' in <label> '" << _name << '\'');
         _format = "INVALID";
         _mode = NONE;
     }
 
-    float top, bot;
-    _hud->_font->getBBox("0", _hud->_font_size, 0.0, 0, 0, &bot, &top);
-    _text_y = _y + (_h - top + bot) / 2.0 - bot;
     blink();
 }
 
 
 void HUD::Label::draw(void)
 {
-    if (!(_mode == NONE || _input.isValid() && blink()))
+    if (!(_mode == NONE || (_input.isValid() && blink())))
         return;
 
     if (_box) {
@@ -142,7 +141,8 @@ void HUD::Label::draw(void)
     }
 
     const int BUFSIZE = 256;
-    char buf[BUFSIZE];
+    char buf[BUFSIZE+1];
+       buf[ BUFSIZE] = '\0';  // Be sure to terminate properly
     if (_mode == NONE)
         snprintf(buf, BUFSIZE, _format.c_str());
     else if (_mode == STRING)
@@ -156,72 +156,12 @@ void HUD::Label::draw(void)
     else if (_mode == DOUBLE) // not really supported yet
         snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
 
-    float posincr;
-    float L, R, B, T;
-    _hud->_font->getBBox(buf, _hud->_font_size, 0.0, &L, &R, &B, &T);
-    float lenstr = R - L;
-
-    if (_halign == RIGHT_ALIGN)
-        posincr = _w - lenstr;
-    else if (_halign == CENTER_ALIGN)
-        posincr = (_w - lenstr) / 2.0;
-    else // LEFT_ALIGN
-        posincr = 0;
-
-    posincr += _hud->_font->getGap() / 2.0 - L;
-    draw_text(_x + posincr, _text_y, buf, get_digits());
-}
-
-
-// make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
-//
-HUD::Label::Format HUD::Label::check_format(const char *f) const
-{
-    bool l = false;
-    Format fmt = STRING;
-
-    for (; *f; f++) {
-        if (*f == '%') {
-            if (f[1] == '%')
-                f++;
-            else
-                break;
-        }
-    }
-    if (*f++ != '%')
-        return NONE;
-    if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
-        f++;
-    while (*f && isdigit(*f))
-        f++;
-    if (*f == '.') {
-        f++;
-        while (*f && isdigit(*f))
-            f++;
-    }
-    if (*f == 'l')
-        l = true, f++;
-
-    if (*f == 'd')
-        fmt = l ? LONG : INT;
-    else if (*f == 'f')
-        fmt = l ? DOUBLE : FLOAT;
-    else if (*f == 's') {
-        if (l)
-            return INVALID;
-        fmt = STRING;
-    } else
-        return INVALID;
-
-    for (++f; *f; f++) {
-        if (*f == '%') {
-            if (f[1] == '%')
-                f++;
-            else
-                return INVALID;
-        }
-    }
-    return fmt;
+    if (_halign & HCENTER)
+        draw_text(_center_x, _center_y, buf, _halign, get_digits());
+    else if (_halign & LEFT)
+        draw_text(_x, _center_y, buf, _halign, get_digits());
+    else // if (_halign & RIGHT)
+        draw_text(_x + _w, _center_y, buf, _halign, get_digits());
 }