1 // HUD_instrument.cxx -- HUD Common Instrument Base
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America [micheleamerica#geocities:com]
6 // Copyright (C) 2006 Melchior FRANZ [mfranz#aon:at]
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.
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.
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.
26 #include <simgear/math/SGLimits.hxx>
27 #include <simgear/props/condition.hxx>
30 #include "HUD_private.hxx"
32 #include <Main/globals.hxx>
36 HUD::Item::Item(HUD *hud, const SGPropertyNode *n, float x, float y) :
38 _name(n->getStringValue("name", "[unnamed]")),
41 _digits(n->getIntValue("digits"))
43 const SGPropertyNode *node = n->getNode("condition");
45 _condition = sgReadCondition(globals->get_props(), node);
47 _x = n->getFloatValue("x") + x;
48 _y = n->getFloatValue("y") + y;
49 _w = n->getFloatValue("width");
50 _h = n->getFloatValue("height");
52 vector<SGPropertyNode_ptr> opt = n->getChildren("option");
53 for (unsigned int i = 0; i < opt.size(); i++) {
54 const char *o = opt[i]->getStringValue();
55 if (!strcmp(o, "vertical"))
57 else if (!strcmp(o, "horizontal"))
58 _options |= HORIZONTAL;
59 else if (!strcmp(o, "top"))
61 else if (!strcmp(o, "left"))
63 else if (!strcmp(o, "bottom"))
65 else if (!strcmp(o, "right"))
67 else if (!strcmp(o, "both"))
68 _options |= (LEFT|RIGHT);
69 else if (!strcmp(o, "noticks"))
71 else if (!strcmp(o, "notext"))
74 SG_LOG(SG_INPUT, SG_WARN, "HUD: unsupported option: " << o);
77 // Set up convenience values for centroid of the box and
78 // the span values according to orientation
80 if (_options & VERTICAL) {
86 _center_x = _x + _w / 2.0;
87 _center_y = _y + _h / 2.0;
91 bool HUD::Item::isEnabled()
93 return _condition ? _condition->test() : true;
97 void HUD::Item::draw_line(float x1, float y1, float x2, float y2)
99 _hud->_line_list.add(LineSegment(x1, y1, x2, y2));
103 void HUD::Item::draw_stipple_line(float x1, float y1, float x2, float y2)
105 _hud->_stipple_line_list.add(LineSegment(x1, y1, x2, y2));
109 void HUD::Item::draw_text(float x, float y, const char *msg, int align, int digit)
111 _hud->_text_list.add(x, y, msg, align, digit);
115 void HUD::Item::draw_circle(float xoffs, float yoffs, float r) const
117 glBegin(GL_LINE_LOOP);
118 float step = SG_PI / r;
119 for (float alpha = 0; alpha < SG_PI * 2.0; alpha += step) {
120 float x = r * cos(alpha);
121 float y = r * sin(alpha);
122 glVertex2f(x + xoffs, y + yoffs);
127 void HUD::Item::draw_arc(float xoffs, float yoffs, float t0, float t1, float r) const
129 glBegin(GL_LINE_STRIP);
130 float step = SG_PI / r;
131 t0 = t0 * SG_DEGREES_TO_RADIANS;
132 t1 = t1 * SG_DEGREES_TO_RADIANS;
134 for (float alpha = t0; alpha < t1; alpha += step) {
135 float x = r * cos(alpha);
136 float y = r * sin(alpha);
137 glVertex2f(x + xoffs, y + yoffs);
142 void HUD::Item::draw_bullet(float x, float y, float size)
144 glEnable(GL_POINT_SMOOTH);
152 glDisable(GL_POINT_SMOOTH);
156 // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)'
158 HUD::Item::Format HUD::Item::check_format(const char *f) const
173 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
175 while (*f && isdigit(*f))
179 while (*f && isdigit(*f))
186 fmt = l ? LONG : INT;
188 fmt = l ? DOUBLE : FLOAT;
189 else if (*f == 's') {