1
0
Fork 0

add optional <format> property for <tape>s

This commit is contained in:
mfranz 2006-08-01 21:11:38 +00:00
parent 762c7ddb89
commit 2f8a476c3a
4 changed files with 101 additions and 77 deletions

View file

@ -306,6 +306,17 @@ public:
virtual bool isEnabled(); virtual bool isEnabled();
protected: 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 float get_span() const { return _scr_span; }
inline int get_digits() const { return _digits; } inline int get_digits() const { return _digits; }
@ -345,17 +356,6 @@ public:
virtual void draw(); virtual void draw();
private: private:
enum Format {
INVALID,
NONE,
INT,
LONG,
FLOAT,
DOUBLE,
STRING,
};
Format check_format(const char *) const;
bool blink(); bool blink();
Input _input; Input _input;
@ -418,6 +418,7 @@ public:
protected: protected:
void draw_fixed_pointer(float, float, float, float, float, float); void draw_fixed_pointer(float, float, float, float, float, float);
void zoomed_scale(int, int); void zoomed_scale(int, int);
char *format_value(float);
private: private:
float _val_span; float _val_span;
@ -433,8 +434,13 @@ private:
float _marker_offset; float _marker_offset;
float _label_gap; float _label_gap;
bool _pointer; bool _pointer;
Format _label_fmt;
string _format;
int _zoom; int _zoom;
enum { BUFSIZE = 64 };
char _buf[BUFSIZE];
enum PointerType { FIXED, MOVING } _pointer_type; enum PointerType { FIXED, MOVING } _pointer_type;
enum TickType { LINE, CIRCLE } _tick_type; enum TickType { LINE, CIRCLE } _tick_type;
enum TickLength { VARIABLE, CONSTANT } _tick_length; enum TickLength { VARIABLE, CONSTANT } _tick_length;

View 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;
}

View 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() bool HUD::Label::blink()
{ {
if (_blink_interval < 0.0f) if (_blink_interval < 0.0f)

View 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)), _marker_offset(n->getFloatValue("marker-offset", 0.0)),
_label_gap(n->getFloatValue("label-gap-width", 0.0) / 2.0), _label_gap(n->getFloatValue("label-gap-width", 0.0) / 2.0),
_pointer(n->getBoolValue("enable-pointer", true)), _pointer(n->getBoolValue("enable-pointer", true)),
_format(n->getStringValue("format", "%d")),
_zoom(n->getIntValue("zoom")) _zoom(n->getIntValue("zoom"))
{ {
_half_width_units = range_to_show() / 2.0; _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" s = n->getStringValue("tick-length"); // "variable", "constant"
_tick_length = strcmp(s, "constant") ? 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_ys;
float marker_ye; float marker_ye;
float text_y = 0.0; float text_y = 0.0;
const int BUFSIZE = 80;
char buf[BUFSIZE];
int oddtype; int oddtype;
// int k; //odd or even values for ticks // FIXME odd scale // 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 } // end huds both
} else { // major div } else { // major div
int display_value = int(v);
if (_modulo) if (_modulo)
display_value %= _modulo; v = fmodf(v + _modulo, _modulo);
snprintf(buf, BUFSIZE, "%d", display_value);
float x; float x;
int align; int align;
@ -339,8 +342,10 @@ void HUD::Tape::draw(void) // (HUD_scale * pscale)
} }
if (!option_notext()) { if (!option_notext()) {
char *s = format_value(v);
float l, r, b, t; 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) if (b < _y || t > top)
continue; continue;
@ -348,7 +353,7 @@ void HUD::Tape::draw(void) // (HUD_scale * pscale)
if (_label_gap == 0.0 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
|| 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 } else { // major divs
int display_value = int(v);
if (_modulo) if (_modulo)
display_value %= _modulo; v = fmodf(v + _modulo, _modulo);
snprintf(buf, BUFSIZE, "%d", display_value);
float y; float y;
int align; int align;
@ -521,8 +523,10 @@ void HUD::Tape::draw(void) // (HUD_scale * pscale)
} }
if (!option_notext()) { if (!option_notext()) {
char *s = format_value(v);
float l, r, b, t; 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) if (l < _x || r > right)
continue; continue;
@ -530,7 +534,7 @@ void HUD::Tape::draw(void) // (HUD_scale * pscale)
if (_label_gap == 0.0 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
|| 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) void HUD::Tape::draw_fixed_pointer(float x1, float y1, float x2, float y2, float x3, float y3)
{ {
glBegin(GL_LINE_STRIP); glBegin(GL_LINE_STRIP);