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 _marker_offset(n->getFloatValue("marker-offset", 5)),
37 _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
41 const SGPropertyNode *node = n->getNode("blinking/condition");
43 _blink_condition = sgReadCondition(globals->get_props(), node);
45 const char *halign = n->getStringValue("halign", "center");
46 if (!strcmp(halign, "left"))
48 else if (!strcmp(halign, "right"))
49 _halign = RIGHT_ALIGN;
51 _halign = CENTER_ALIGN;
53 const char *pre = n->getStringValue("prefix", 0);
54 const char *post = n->getStringValue("postfix", 0);
55 const char *fmt = n->getStringValue("format", 0);
68 _mode = check_format(_format.c_str());
69 if (_mode == INVALID) {
70 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
76 _hud->_font->getBBox("0", _hud->_font_size, 0.0, 0, 0, 0, &top);
77 _text_y = _y + (_h - top) / 2.0;
82 void HUD::Label::draw(void)
84 if (!(_mode == NONE || _input.isValid() && blink()))
88 const float tan30 = 0.57735;
89 float base = _marker_offset * tan30;
95 if (option_bottom()) {
96 p = _y - _marker_offset;
97 draw_line(_x, _y, l, _y);
98 draw_line(l, _y, _center_x, p);
99 draw_line(_center_x, p, r, _y);
100 draw_line(r, _y, _x + _w, _y);
102 draw_line(_x, _y, _x + _w, _y);
105 p = _y + _h + _marker_offset;
106 draw_line(_x, _y + _h, l, _y + _h);
107 draw_line(l, _y + _h, _center_x, p);
108 draw_line(_center_x, p, r, _y + _h);
109 draw_line(r, _y + _h, _x + _w, _y + _h);
111 draw_line(_x + _w, _y + _h, _x, _y + _h);
113 l = _center_y - base;
114 r = _center_y + base;
117 p = _x - _marker_offset;
118 draw_line(_x, _y, _x, l);
119 draw_line(_x, l, p, _center_y);
120 draw_line(p, _center_y, _x, r);
121 draw_line(_x, r, _x, _y + _h);
123 draw_line(_x, _y + _h, _x, _y);
125 if (option_right()) {
126 p = _x + _w + _marker_offset;
127 draw_line(_x + _w, _y, _x + _w, l);
128 draw_line(_x + _w, l, p, _center_y);
129 draw_line(p, _center_y, _x + _w, r);
130 draw_line(_x + _w, r, _x + _w, _y + _h);
132 draw_line(_x + _w, _y, _x + _w, _y + _h);
135 const int BUFSIZE = 256;
138 snprintf(buf, BUFSIZE, _format.c_str());
139 else if (_mode == STRING)
140 snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
141 else if (_mode == INT)
142 snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
143 else if (_mode == LONG)
144 snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
145 else if (_mode == FLOAT)
146 snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
147 else if (_mode == DOUBLE) // not really supported yet
148 snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
151 float lenstr = text_width(buf);
153 if (_halign == RIGHT_ALIGN)
154 posincr = _w - lenstr;
155 else if (_halign == CENTER_ALIGN)
156 posincr = (_w - lenstr) / 2.0;
160 if (_fontsize == FONT_SMALL)
161 draw_text(_x + posincr, _text_y, buf, get_digits());
162 else if (_fontsize == FONT_LARGE)
163 draw_text(_x + posincr, _text_y, buf, get_digits());
167 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
169 HUD::Label::Format HUD::Label::check_format(const char *f) const
184 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
186 while (*f && isdigit(*f))
190 while (*f && isdigit(*f))
197 fmt = l ? LONG : INT;
199 fmt = l ? DOUBLE : FLOAT;
200 else if (*f == 's') {
219 bool HUD::Label::blink()
221 if (_blink_interval < 0.0f)
224 if (_blink_condition && !_blink_condition->test())
227 if (_hud->timer() < _blink_target)
230 _blink_target = _hud->timer() + _blink_interval;
231 return _blink_state = !_blink_state;