]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_label.cxx
- don't use 10 pt font size for width calculations, when in fact we use
[flightgear.git] / src / Instrumentation / HUD / HUD_label.cxx
1 // HUD_label.cxx -- HUD Label
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  [micheleamerica#geocities:com]
6 // Copyright (C) 2006  Melchior FRANZ  [mfranz#aon:at]
7 //
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.
12 //
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.
17 //
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.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/props/condition.hxx>
27 #include "HUD.hxx"
28
29
30 HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
31     Item(hud, n, x, 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     _blink_condition(0),
36     _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
37     _blink_target(0.0),
38     _blink_state(true)
39 {
40     const SGPropertyNode *node = n->getNode("blinking/condition");
41     if (node)
42        _blink_condition = sgReadCondition(globals->get_props(), node);
43
44     const char *halign = n->getStringValue("halign", "center");
45     if (!strcmp(halign, "left"))
46         _halign = LEFT_ALIGN;
47     else if (!strcmp(halign, "right"))
48         _halign = RIGHT_ALIGN;
49     else
50         _halign = CENTER_ALIGN;
51
52     const char *pre = n->getStringValue("prefix", 0);
53     const char *post = n->getStringValue("postfix", 0);
54     const char *fmt = n->getStringValue("format", 0);
55
56     if (pre)
57         _format = pre;
58
59     if (fmt)
60         _format += fmt;
61     else
62         _format += "%s";
63
64     if (post)
65         _format += post;
66
67     _mode = check_format(_format.c_str());
68     if (_mode == INVALID) {
69         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
70         _format = "INVALID";
71         _mode = NONE;
72     }
73
74     float top;
75     _hud->_font->getBBox("0", _hud->_font_size, 0.0, 0, 0, 0, &top);
76     _text_y = _y + (_h - top) / 2.0;
77     blink();
78 }
79
80
81 void HUD::Label::draw(void)
82 {
83     if (!(_mode == NONE || _input.isValid() && blink()))
84         return;
85
86     if (_box) {
87         draw_line(_x, _y, _x + _w, _y);
88         draw_line(_x + _w, _y, _x + _w, _y + _h);
89         draw_line(_x + _w, _y + _h, _x, _y + _h);
90         draw_line(_x, _y + _h, _x, _y);
91     }
92
93     const int BUFSIZE = 256;
94     char buf[BUFSIZE];
95     if (_mode == NONE)
96         snprintf(buf, BUFSIZE, _format.c_str());
97     else if (_mode == STRING)
98         snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
99     else if (_mode == INT)
100         snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
101     else if (_mode == LONG)
102         snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
103     else if (_mode == FLOAT)
104         snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
105     else if (_mode == DOUBLE) // not really supported yet
106         snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
107
108     float posincr;
109     float lenstr = text_width(buf);
110
111     if (_halign == RIGHT_ALIGN)
112         posincr = _w - lenstr;
113     else if (_halign == CENTER_ALIGN)
114         posincr = (_w - lenstr) / 2.0;
115     else // LEFT_ALIGN
116         posincr = 0;
117
118     if (_fontsize == FONT_SMALL)
119         draw_text(_x + posincr, _text_y, buf, get_digits());
120     else if (_fontsize == FONT_LARGE)
121         draw_text(_x + posincr, _text_y, buf, get_digits());
122 }
123
124
125 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
126 //
127 HUD::Label::Format HUD::Label::check_format(const char *f) const
128 {
129     bool l = false;
130     Format fmt = STRING;
131
132     for (; *f; f++) {
133         if (*f == '%') {
134             if (f[1] == '%')
135                 f++;
136             else
137                 break;
138         }
139     }
140     if (*f++ != '%')
141         return NONE;
142     if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
143         f++;
144     while (*f && isdigit(*f))
145         f++;
146     if (*f == '.') {
147         f++;
148         while (*f && isdigit(*f))
149             f++;
150     }
151     if (*f == 'l')
152         l = true, f++;
153
154     if (*f == 'd')
155         fmt = l ? LONG : INT;
156     else if (*f == 'f')
157         fmt = l ? DOUBLE : FLOAT;
158     else if (*f == 's') {
159         if (l)
160             return INVALID;
161         fmt = STRING;
162     } else
163         return INVALID;
164
165     for (++f; *f; f++) {
166         if (*f == '%') {
167             if (f[1] == '%')
168                 f++;
169             else
170                 return INVALID;
171         }
172     }
173     return fmt;
174 }
175
176
177 bool HUD::Label::blink()
178 {
179     if (_blink_interval < 0.0f)
180         return true;
181
182     if (_blink_condition && !_blink_condition->test())
183         return true;
184
185     if (_hud->timer() < _blink_target)
186         return _blink_state;
187
188     _blink_target = _hud->timer() + _blink_interval;
189     return _blink_state = !_blink_state;
190 }
191
192