]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_label.cxx
Win32 fixes
[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     if (fmt)
59         _format += fmt;
60     else
61         _format += "%s";
62     if (post)
63         _format += post;
64
65     _mode = check_format(_format.c_str());
66     if (_mode == INVALID) {
67         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
68         _format = "INVALID";
69         _mode = NONE;
70     }
71
72     blink();
73 }
74
75
76 void HUD::Label::draw(void)
77 {
78     if (!(_mode == NONE || _input.isValid() && blink()))
79         return;
80
81     Rect scrn_rect = get_location();
82
83     if (_box) {
84         float x = scrn_rect.left;
85         float y = scrn_rect.top;
86         float w = scrn_rect.right;
87         float h = _hud->_font_size;                     // FIXME
88
89         glPushMatrix();
90         glLoadIdentity();
91
92         glBegin(GL_LINES);
93         glVertex2f(x - 2.0,  y - 2.0);
94         glVertex2f(x + w + 2.0, y - 2.0);
95         glVertex2f(x + w + 2.0, y + h + 2.0);
96         glVertex2f(x - 2.0,  y + h + 2.0);
97         glEnd();
98
99         glEnable(GL_LINE_STIPPLE);
100         glLineStipple(1, 0xAAAA);
101
102         glBegin(GL_LINES);
103         glVertex2f(x + w + 2.0, y - 2.0);
104         glVertex2f(x + w + 2.0, y + h + 2.0);
105         glVertex2f(x - 2.0, y + h + 2.0);
106         glVertex2f(x - 2.0, y - 2.0);
107         glEnd();
108
109         glDisable(GL_LINE_STIPPLE);
110         glPopMatrix();
111     }
112
113     const int BUFSIZE = 256;
114     char buf[BUFSIZE];
115     if (_mode == NONE)
116         snprintf(buf, BUFSIZE, _format.c_str());
117     else if (_mode == STRING)
118         snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
119     else if (_mode == INT)
120         snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
121     else if (_mode == LONG)
122         snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
123     else if (_mode == FLOAT)
124         snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
125     else if (_mode == DOUBLE) // not really supported yet
126         snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
127
128     float posincr;
129     float lenstr = text_width(buf);
130
131     if (_halign == RIGHT_ALIGN)
132         posincr = scrn_rect.right - lenstr;
133     else if (_halign == CENTER_ALIGN)
134         posincr = get_span() - (lenstr / 2);            // FIXME get_span() ? really?
135     else // LEFT_ALIGN
136         posincr = 0;
137
138     if (_fontsize == FONT_SMALL)
139         draw_text(scrn_rect.left + posincr, scrn_rect.top, buf, get_digits());
140     else if (_fontsize == FONT_LARGE)
141         draw_text(scrn_rect.left + posincr, scrn_rect.top, buf, get_digits());
142 }
143
144
145 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
146 //
147 HUD::Label::Format HUD::Label::check_format(const char *f) const
148 {
149     bool l = false;
150     Format fmt = STRING;
151
152     for (; *f; f++) {
153         if (*f == '%') {
154             if (f[1] == '%')
155                 f++;
156             else
157                 break;
158         }
159     }
160     if (*f++ != '%')
161         return NONE;
162     if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
163         f++;
164     while (*f && isdigit(*f))
165         f++;
166     if (*f == '.') {
167         f++;
168         while (*f && isdigit(*f))
169             f++;
170     }
171     if (*f == 'l')
172         l = true, f++;
173
174     if (*f == 'd')
175         fmt = l ? LONG : INT;
176     if (*f == 'f')
177         fmt = l ? DOUBLE : FLOAT;
178     else if (*f == 's') {
179         if (l)
180             return INVALID;
181         fmt = STRING;
182     } else
183         return INVALID;
184
185     for (++f; *f; f++) {
186         if (*f == '%') {
187             if (f[1] == '%')
188                 f++;
189             else
190                 return INVALID;
191         }
192     }
193     return fmt;
194 }
195
196
197 bool HUD::Label::blink()
198 {
199     if (_blink_interval < 0.0f)
200         return true;
201
202     if (_blink_condition && !_blink_condition->test())
203         return true;
204
205     if (_hud->timer() < _blink_target)
206         return _blink_state;
207
208     _blink_target = _hud->timer() + _blink_interval;
209     return _blink_state = !_blink_state;
210 }
211
212