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