]> git.mxchange.org Git - flightgear.git/commitdiff
add optional <format> property for <tape>s
authormfranz <mfranz>
Tue, 1 Aug 2006 21:11:38 +0000 (21:11 +0000)
committermfranz <mfranz>
Tue, 1 Aug 2006 21:11:38 +0000 (21:11 +0000)
src/Instrumentation/HUD/HUD.hxx
src/Instrumentation/HUD/HUD_instrument.cxx
src/Instrumentation/HUD/HUD_label.cxx
src/Instrumentation/HUD/HUD_tape.cxx

index 7cce03eace8e9e6e1866433726abfded7db10027..684800392ffef2a1e71e796cc0469213e0de57e1 100644 (file)
@@ -306,6 +306,17 @@ public:
     virtual bool isEnabled();
 
 protected:
+    enum Format {
+        INVALID,
+        NONE,
+        INT,
+        LONG,
+        FLOAT,
+        DOUBLE,
+        STRING,
+    };
+
+    Format  check_format(const char *) const;
     inline float get_span()       const { return _scr_span; }
     inline int   get_digits()     const { return _digits; }
 
@@ -345,17 +356,6 @@ public:
     virtual void draw();
 
 private:
-    enum Format {
-        INVALID,
-        NONE,
-        INT,
-        LONG,
-        FLOAT,
-        DOUBLE,
-        STRING,
-    };
-
-    Format  check_format(const char *) const;
     bool    blink();
 
     Input   _input;
@@ -418,6 +418,7 @@ public:
 protected:
     void draw_fixed_pointer(float, float, float, float, float, float);
     void zoomed_scale(int, int);
+    char *format_value(float);
 
 private:
     float  _val_span;
@@ -433,8 +434,13 @@ private:
     float  _marker_offset;
     float  _label_gap;
     bool   _pointer;
+    Format _label_fmt;
+    string _format;
     int    _zoom;
 
+    enum { BUFSIZE = 64 };
+    char   _buf[BUFSIZE];
+
     enum PointerType { FIXED, MOVING } _pointer_type;
     enum TickType { LINE, CIRCLE } _tick_type;
     enum TickLength { VARIABLE, CONSTANT } _tick_length;
index 3b00f0678b79c6bf63e4e5a7f2cdec252e073fc0..b52a3e148239918231a0b6fc3bb3d1a14cc40ce5 100644 (file)
@@ -134,3 +134,55 @@ void HUD::Item::draw_bullet(float x, float y, float size)
 }
 
 
+// make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
+//
+HUD::Item::Format HUD::Item::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;
+}
+
+
index 0a932eae8801777cb25649fce49a9c0d1d1330cd..16a2348738d00ac9b708650e886bb8d64c7d7f30 100644 (file)
@@ -164,58 +164,6 @@ void HUD::Label::draw(void)
 }
 
 
-// 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;
-}
-
-
 bool HUD::Label::blink()
 {
     if (_blink_interval < 0.0f)
index a3b8a2bc1f2ffdb0fd5ae39204409bbd13da1d0a..9b4609384b0522ba4fb2d1f03523772c0c9f8373 100644 (file)
@@ -35,6 +35,7 @@ HUD::Tape::Tape(HUD *hud, const SGPropertyNode *n, float x, float y) :
     _marker_offset(n->getFloatValue("marker-offset", 0.0)),
     _label_gap(n->getFloatValue("label-gap-width", 0.0) / 2.0),
     _pointer(n->getBoolValue("enable-pointer", true)),
+    _format(n->getStringValue("format", "%d")),
     _zoom(n->getIntValue("zoom"))
 {
     _half_width_units = range_to_show() / 2.0;
@@ -48,6 +49,13 @@ HUD::Tape::Tape(HUD *hud, const SGPropertyNode *n, float x, float y) :
 
     s = n->getStringValue("tick-length");                    // "variable", "constant"
     _tick_length = strcmp(s, "constant") ? VARIABLE : CONSTANT;
+
+    _label_fmt = check_format(_format.c_str());
+    if (_label_fmt != INT && _label_fmt != LONG
+            && _label_fmt != FLOAT && _label_fmt != DOUBLE) {
+        _label_fmt = INT;
+        _format = "%d";
+    }
 }
 
 
@@ -62,8 +70,6 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
     float marker_ys;
     float marker_ye;
     float text_y = 0.0;
-    const int BUFSIZE = 80;
-    char buf[BUFSIZE];
     int oddtype;
 //    int k; //odd or even values for ticks            // FIXME odd scale
 
@@ -306,11 +312,8 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
                     } // end huds both
 
                 } else { // major div
-                    int display_value = int(v);
                     if (_modulo)
-                        display_value %= _modulo;
-
-                    snprintf(buf, BUFSIZE, "%d", display_value);
+                        v = fmodf(v + _modulo, _modulo);
 
                     float x;
                     int align;
@@ -339,8 +342,10 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
                     }
 
                     if (!option_notext()) {
+                        char *s = format_value(v);
+
                         float l, r, b, t;
-                        _hud->_text_list.align(buf, align, &x, &y, &l, &r, &b, &t);
+                        _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
 
                         if (b < _y || t > top)
                             continue;
@@ -348,7 +353,7 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
                         if (_label_gap == 0.0
                                 || b < _center_y - _label_gap && t < _center_y - _label_gap
                                 || b > _center_y + _label_gap && t > _center_y + _label_gap) {
-                            draw_text(x, y, buf);
+                            draw_text(x, y, s);
                         }
                     }
                 }
@@ -497,11 +502,8 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
                     }
 
                 } else { // major divs
-                    int display_value = int(v);
                     if (_modulo)
-                        display_value %= _modulo;
-
-                    snprintf(buf, BUFSIZE, "%d", display_value);
+                        v = fmodf(v + _modulo, _modulo);
 
                     float y;
                     int align;
@@ -521,8 +523,10 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
                     }
 
                     if (!option_notext()) {
+                        char *s = format_value(v);
+
                         float l, r, b, t;
-                        _hud->_text_list.align(buf, align, &x, &y, &l, &r, &b, &t);
+                        _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
 
                         if (l < _x || r > right)
                             continue;
@@ -530,7 +534,7 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
                         if (_label_gap == 0.0
                                 || l < _center_x - _label_gap && r < _center_x - _label_gap
                                 || l > _center_x + _label_gap && r > _center_x + _label_gap) {
-                            draw_text(x, y, buf);
+                            draw_text(x, y, s);
                         }
                     }
                 }
@@ -540,6 +544,20 @@ void HUD::Tape::draw(void) //  (HUD_scale * pscale)
 }
 
 
+char *HUD::Tape::format_value(float v)
+{
+    if (_label_fmt == INT)
+        snprintf(_buf, BUFSIZE, _format.c_str(), int(v + 0.5f));
+    else if (_label_fmt == LONG)
+        snprintf(_buf, BUFSIZE, _format.c_str(), long(v + 0.5f));
+    else if (_label_fmt == FLOAT)
+        snprintf(_buf, BUFSIZE, _format.c_str(), v);
+    else // _label_fmt == DOUBLE
+        snprintf(_buf, BUFSIZE, _format.c_str(), double(v));
+    return _buf;
+}
+
+
 void HUD::Tape::draw_fixed_pointer(float x1, float y1, float x2, float y2, float x3, float y3)
 {
     glBegin(GL_LINE_STRIP);