1 // HUD_label.cxx -- HUD Label
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America [micheleamerica#geocities:com]
6 // Copyright (C) 2006 Melchior FRANZ [mfranz#aon:at]
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <simgear/props/condition.hxx>
30 HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
32 _input(n->getNode("input", false)),
33 _fontsize(fgGetInt("/sim/startup/xsize") > 1000 ? FONT_LARGE : FONT_SMALL), // FIXME
34 _box(n->getBoolValue("box", false)),
35 _pointer_width(n->getFloatValue("pointer-width", 7.0)),
36 _pointer_length(n->getFloatValue("pointer-length", 5.0)),
38 _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
42 const SGPropertyNode *node = n->getNode("blinking/condition");
44 _blink_condition = sgReadCondition(globals->get_props(), node);
46 const char *halign = n->getStringValue("halign", "center");
47 if (!strcmp(halign, "left"))
49 else if (!strcmp(halign, "right"))
50 _halign = RIGHT_ALIGN;
52 _halign = CENTER_ALIGN;
54 const char *pre = n->getStringValue("prefix", 0);
55 const char *post = n->getStringValue("postfix", 0);
56 const char *fmt = n->getStringValue("format", 0);
69 _mode = check_format(_format.c_str());
70 if (_mode == INVALID) {
71 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
77 _hud->_font->getBBox("0", _hud->_font_size, 0.0, 0, 0, 0, &top);
78 _text_y = _y + (_h - top) / 2.0;
83 void HUD::Label::draw(void)
85 if (!(_mode == NONE || _input.isValid() && blink()))
90 float pw = _pointer_width / 2.0;
94 bool draw_parallel = fabsf(_pointer_width - _w) > 2.0; // draw lines left and right of arrow?
96 if (option_bottom()) {
98 draw_line(_x, _y, l, _y);
99 draw_line(r, _y, _x + _w, _y);
101 p = _y - _pointer_length;
102 draw_line(l, _y, _center_x, p);
103 draw_line(_center_x, p, r, _y);
105 draw_line(_x, _y, _x + _w, _y);
109 draw_line(_x, _y + _h, l, _y + _h);
110 draw_line(r, _y + _h, _x + _w, _y + _h);
112 p = _y + _h + _pointer_length;
113 draw_line(l, _y + _h, _center_x, p);
114 draw_line(_center_x, p, r, _y + _h);
116 draw_line(_x + _w, _y + _h, _x, _y + _h);
120 draw_parallel = fabsf(_pointer_width - _h) > 2.0;
124 draw_line(_x, _y, _x, l);
125 draw_line(_x, r, _x, _y + _h);
127 p = _x - _pointer_length;
128 draw_line(_x, l, p, _center_y);
129 draw_line(p, _center_y, _x, r);
131 draw_line(_x, _y + _h, _x, _y);
133 if (option_right()) {
135 draw_line(_x + _w, _y, _x + _w, l);
136 draw_line(_x + _w, r, _x + _w, _y + _h);
138 p = _x + _w + _pointer_length;
139 draw_line(_x + _w, l, p, _center_y);
140 draw_line(p, _center_y, _x + _w, r);
142 draw_line(_x + _w, _y, _x + _w, _y + _h);
145 const int BUFSIZE = 256;
148 snprintf(buf, BUFSIZE, _format.c_str());
149 else if (_mode == STRING)
150 snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
151 else if (_mode == INT)
152 snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
153 else if (_mode == LONG)
154 snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
155 else if (_mode == FLOAT)
156 snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
157 else if (_mode == DOUBLE) // not really supported yet
158 snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
161 float lenstr = text_width(buf);
163 if (_halign == RIGHT_ALIGN)
164 posincr = _w - lenstr;
165 else if (_halign == CENTER_ALIGN)
166 posincr = (_w - lenstr) / 2.0;
170 if (_fontsize == FONT_SMALL)
171 draw_text(_x + posincr, _text_y, buf, get_digits());
172 else if (_fontsize == FONT_LARGE)
173 draw_text(_x + posincr, _text_y, buf, get_digits());
177 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
179 HUD::Label::Format HUD::Label::check_format(const char *f) const
194 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
196 while (*f && isdigit(*f))
200 while (*f && isdigit(*f))
207 fmt = l ? LONG : INT;
209 fmt = l ? DOUBLE : FLOAT;
210 else if (*f == 's') {
229 bool HUD::Label::blink()
231 if (_blink_interval < 0.0f)
234 if (_blink_condition && !_blink_condition->test())
237 if (_hud->timer() < _blink_target)
240 _blink_target = _hud->timer() + _blink_interval;
241 return _blink_state = !_blink_state;