]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_label.cxx
no more FONT_(LARGE|SMALL) (didn't work, anyway, and isn't needed)
[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     _box(n->getBoolValue("box", false)),
34     _pointer_width(n->getFloatValue("pointer-width", 7.0)),
35     _pointer_length(n->getFloatValue("pointer-length", 5.0)),
36     _blink_condition(0),
37     _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
38     _blink_target(0.0),
39     _blink_state(true)
40 {
41     const SGPropertyNode *node = n->getNode("blinking/condition");
42     if (node)
43        _blink_condition = sgReadCondition(globals->get_props(), node);
44
45     const char *halign = n->getStringValue("halign", "center");
46     if (!strcmp(halign, "left"))
47         _halign = LEFT_ALIGN;
48     else if (!strcmp(halign, "right"))
49         _halign = RIGHT_ALIGN;
50     else
51         _halign = CENTER_ALIGN;
52
53     const char *pre = n->getStringValue("prefix", 0);
54     const char *post = n->getStringValue("postfix", 0);
55     const char *fmt = n->getStringValue("format", 0);
56
57     if (pre)
58         _format = pre;
59
60     if (fmt)
61         _format += fmt;
62     else
63         _format += "%s";
64
65     if (post)
66         _format += post;
67
68     _mode = check_format(_format.c_str());
69     if (_mode == INVALID) {
70         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
71         _format = "INVALID";
72         _mode = NONE;
73     }
74
75     float top, bot;
76     _hud->_font->getBBox("0", _hud->_font_size, 0.0, 0, 0, &bot, &top);
77     _text_y = _y + (_h - top + bot) / 2.0 - bot;
78     blink();
79 }
80
81
82 void HUD::Label::draw(void)
83 {
84     if (!(_mode == NONE || _input.isValid() && blink()))
85         return;
86
87     if (_box) {
88         float l, r, p;
89         float pw = _pointer_width / 2.0;
90
91         l = _center_x - pw;
92         r = _center_x + pw;
93         bool draw_parallel = fabsf(_pointer_width - _w) > 2.0; // draw lines left and right of arrow?
94
95         if (option_bottom()) {
96             if (draw_parallel) {
97                 draw_line(_x, _y, l, _y);
98                 draw_line(r, _y, _x + _w, _y);
99             }
100             p = _y - _pointer_length;
101             draw_line(l, _y, _center_x, p);
102             draw_line(_center_x, p, r, _y);
103         } else
104             draw_line(_x, _y, _x + _w, _y);
105
106         if (option_top()) {
107             if (draw_parallel) {
108                 draw_line(_x, _y + _h, l, _y + _h);
109                 draw_line(r, _y + _h, _x + _w, _y + _h);
110             }
111             p = _y + _h + _pointer_length;
112             draw_line(l, _y + _h, _center_x, p);
113             draw_line(_center_x, p, r, _y + _h);
114         } else
115             draw_line(_x + _w, _y + _h, _x, _y + _h);
116
117         l = _center_y - pw;
118         r = _center_y + pw;
119         draw_parallel = fabsf(_pointer_width - _h) > 2.0;
120
121         if (option_left()) {
122             if (draw_parallel) {
123                 draw_line(_x, _y, _x, l);
124                 draw_line(_x, r, _x, _y + _h);
125             }
126             p = _x - _pointer_length;
127             draw_line(_x, l, p, _center_y);
128             draw_line(p, _center_y, _x, r);
129         } else
130             draw_line(_x, _y + _h, _x, _y);
131
132         if (option_right()) {
133             if (draw_parallel) {
134                 draw_line(_x + _w, _y, _x + _w, l);
135                 draw_line(_x + _w, r, _x + _w, _y + _h);
136             }
137             p = _x + _w + _pointer_length;
138             draw_line(_x + _w, l, p, _center_y);
139             draw_line(p, _center_y, _x + _w, r);
140         } else
141             draw_line(_x + _w, _y, _x + _w, _y + _h);
142     }
143
144     const int BUFSIZE = 256;
145     char buf[BUFSIZE];
146     if (_mode == NONE)
147         snprintf(buf, BUFSIZE, _format.c_str());
148     else if (_mode == STRING)
149         snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
150     else if (_mode == INT)
151         snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
152     else if (_mode == LONG)
153         snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
154     else if (_mode == FLOAT)
155         snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
156     else if (_mode == DOUBLE) // not really supported yet
157         snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
158
159     float posincr;
160     float L, R, B, T;
161     _hud->_font->getBBox(buf, _hud->_font_size, 0.0, &L, &R, &B, &T);
162     float lenstr = R - L;
163
164     if (_halign == RIGHT_ALIGN)
165         posincr = _w - lenstr;
166     else if (_halign == CENTER_ALIGN)
167         posincr = (_w - lenstr) / 2.0;
168     else // LEFT_ALIGN
169         posincr = 0;
170
171     posincr += _hud->_font->getGap() / 2.0 - L;
172     draw_text(_x + posincr, _text_y, buf, get_digits());
173 }
174
175
176 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
177 //
178 HUD::Label::Format HUD::Label::check_format(const char *f) const
179 {
180     bool l = false;
181     Format fmt = STRING;
182
183     for (; *f; f++) {
184         if (*f == '%') {
185             if (f[1] == '%')
186                 f++;
187             else
188                 break;
189         }
190     }
191     if (*f++ != '%')
192         return NONE;
193     if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
194         f++;
195     while (*f && isdigit(*f))
196         f++;
197     if (*f == '.') {
198         f++;
199         while (*f && isdigit(*f))
200             f++;
201     }
202     if (*f == 'l')
203         l = true, f++;
204
205     if (*f == 'd')
206         fmt = l ? LONG : INT;
207     else if (*f == 'f')
208         fmt = l ? DOUBLE : FLOAT;
209     else if (*f == 's') {
210         if (l)
211             return INVALID;
212         fmt = STRING;
213     } else
214         return INVALID;
215
216     for (++f; *f; f++) {
217         if (*f == '%') {
218             if (f[1] == '%')
219                 f++;
220             else
221                 return INVALID;
222         }
223     }
224     return fmt;
225 }
226
227
228 bool HUD::Label::blink()
229 {
230     if (_blink_interval < 0.0f)
231         return true;
232
233     if (_blink_condition && !_blink_condition->test())
234         return true;
235
236     if (_hud->timer() < _blink_target)
237         return _blink_state;
238
239     _blink_target = _hud->timer() + _blink_interval;
240     return _blink_state = !_blink_state;
241 }
242
243