From 2f8a476c3a07dcd706751e9274e8c7c9e4c9a0b8 Mon Sep 17 00:00:00 2001 From: mfranz Date: Tue, 1 Aug 2006 21:11:38 +0000 Subject: [PATCH] add optional property for s --- src/Instrumentation/HUD/HUD.hxx | 28 +++++++----- src/Instrumentation/HUD/HUD_instrument.cxx | 52 ++++++++++++++++++++++ src/Instrumentation/HUD/HUD_label.cxx | 52 ---------------------- src/Instrumentation/HUD/HUD_tape.cxx | 46 +++++++++++++------ 4 files changed, 101 insertions(+), 77 deletions(-) diff --git a/src/Instrumentation/HUD/HUD.hxx b/src/Instrumentation/HUD/HUD.hxx index 7cce03eac..684800392 100644 --- a/src/Instrumentation/HUD/HUD.hxx +++ b/src/Instrumentation/HUD/HUD.hxx @@ -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; diff --git a/src/Instrumentation/HUD/HUD_instrument.cxx b/src/Instrumentation/HUD/HUD_instrument.cxx index 3b00f0678..b52a3e148 100644 --- a/src/Instrumentation/HUD/HUD_instrument.cxx +++ b/src/Instrumentation/HUD/HUD_instrument.cxx @@ -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; +} + + diff --git a/src/Instrumentation/HUD/HUD_label.cxx b/src/Instrumentation/HUD/HUD_label.cxx index 0a932eae8..16a234873 100644 --- a/src/Instrumentation/HUD/HUD_label.cxx +++ b/src/Instrumentation/HUD/HUD_label.cxx @@ -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) diff --git a/src/Instrumentation/HUD/HUD_tape.cxx b/src/Instrumentation/HUD/HUD_tape.cxx index a3b8a2bc1..9b4609384 100644 --- a/src/Instrumentation/HUD/HUD_tape.cxx +++ b/src/Instrumentation/HUD/HUD_tape.cxx @@ -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); -- 2.39.5