]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_instrument.cxx
Fix bug 191, uninitialised HUD color.
[flightgear.git] / src / Instrumentation / HUD / HUD_instrument.cxx
1 // HUD_instrument.cxx -- HUD Common Instrument Base
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/math/SGLimits.hxx>
27 #include "HUD.hxx"
28
29
30 HUD::Item::Item(HUD *hud, const SGPropertyNode *n, float x, float y) :
31     _hud(hud),
32     _name(n->getStringValue("name", "[unnamed]")),
33     _options(0),
34     _condition(0),
35     _digits(n->getIntValue("digits"))
36 {
37     const SGPropertyNode *node = n->getNode("condition");
38     if (node)
39         _condition = sgReadCondition(globals->get_props(), node);
40
41     _x = n->getFloatValue("x") + x;
42     _y = n->getFloatValue("y") + y;
43     _w = n->getFloatValue("width");
44     _h = n->getFloatValue("height");
45
46     vector<SGPropertyNode_ptr> opt = n->getChildren("option");
47     for (unsigned int i = 0; i < opt.size(); i++) {
48         const char *o = opt[i]->getStringValue();
49         if (!strcmp(o, "vertical"))
50             _options |= VERTICAL;
51         else if (!strcmp(o, "horizontal"))
52             _options |= HORIZONTAL;
53         else if (!strcmp(o, "top"))
54             _options |= TOP;
55         else if (!strcmp(o, "left"))
56             _options |= LEFT;
57         else if (!strcmp(o, "bottom"))
58             _options |= BOTTOM;
59         else if (!strcmp(o, "right"))
60             _options |= RIGHT;
61         else if (!strcmp(o, "both"))
62             _options |= (LEFT|RIGHT);
63         else if (!strcmp(o, "noticks"))
64             _options |= NOTICKS;
65         else if (!strcmp(o, "notext"))
66             _options |= NOTEXT;
67         else
68             SG_LOG(SG_INPUT, SG_WARN, "HUD: unsupported option: " << o);
69     }
70
71     // Set up convenience values for centroid of the box and
72     // the span values according to orientation
73
74     if (_options & VERTICAL) {
75         _scr_span = _h;
76     } else {
77         _scr_span = _w;
78     }
79
80     _center_x = _x + _w / 2.0;
81     _center_y = _y + _h / 2.0;
82 }
83
84
85 bool HUD::Item::isEnabled()
86 {
87     return _condition ? _condition->test() : true;
88 }
89
90
91 void HUD::Item::draw_line(float x1, float y1, float x2, float y2)
92 {
93     _hud->_line_list.add(LineSegment(x1, y1, x2, y2));
94 }
95
96
97 void HUD::Item::draw_stipple_line(float x1, float y1, float x2, float y2)
98 {
99     _hud->_stipple_line_list.add(LineSegment(x1, y1, x2, y2));
100 }
101
102
103 void HUD::Item::draw_text(float x, float y, const char *msg, int align, int digit)
104 {
105     _hud->_text_list.add(x, y, msg, align, digit);
106 }
107
108
109 void HUD::Item::draw_circle(float xoffs, float yoffs, float r) const
110 {
111     glBegin(GL_LINE_LOOP);
112     float step = SG_PI / r;
113     for (float alpha = 0; alpha < SG_PI * 2.0; alpha += step) {
114         float x = r * cos(alpha);
115         float y = r * sin(alpha);
116         glVertex2f(x + xoffs, y + yoffs);
117     }
118     glEnd();
119 }
120
121 void HUD::Item::draw_arc(float xoffs, float yoffs, float t0, float t1, float r) const
122 {
123     glBegin(GL_LINE_STRIP);
124     float step = SG_PI / r;
125     t0 = t0 * SG_DEGREES_TO_RADIANS;
126     t1 = t1 * SG_DEGREES_TO_RADIANS;
127
128     for (float alpha = t0; alpha < t1; alpha += step) {
129         float x = r * cos(alpha);
130         float y = r * sin(alpha);
131         glVertex2f(x + xoffs, y + yoffs);
132     }
133     glEnd();
134 }
135
136 void HUD::Item::draw_bullet(float x, float y, float size)
137 {
138     glEnable(GL_POINT_SMOOTH);
139     glPointSize(size);
140
141     glBegin(GL_POINTS);
142     glVertex2f(x, y);
143     glEnd();
144
145     glPointSize(1.0);
146     glDisable(GL_POINT_SMOOTH);
147 }
148
149
150 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
151 //
152 HUD::Item::Format HUD::Item::check_format(const char *f) const
153 {
154     bool l = false;
155     Format fmt = STRING;
156
157     for (; *f; f++) {
158         if (*f == '%') {
159             if (f[1] == '%')
160                 f++;
161             else
162                 break;
163         }
164     }
165     if (*f++ != '%')
166         return NONE;
167     if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
168         f++;
169     while (*f && isdigit(*f))
170         f++;
171     if (*f == '.') {
172         f++;
173         while (*f && isdigit(*f))
174             f++;
175     }
176     if (*f == 'l')
177         l = true, f++;
178
179     if (*f == 'd')
180         fmt = l ? LONG : INT;
181     else if (*f == 'f')
182         fmt = l ? DOUBLE : FLOAT;
183     else if (*f == 's') {
184         if (l)
185             return INVALID;
186         fmt = STRING;
187     } else
188         return INVALID;
189
190     for (++f; *f; f++) {
191         if (*f == '%') {
192             if (f[1] == '%')
193                 f++;
194             else
195                 return INVALID;
196         }
197     }
198     return fmt;
199 }
200
201