]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_instrument.cxx
- fix unzoomed tapes (TODO: restore tick length)
[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, "autoticks"))
51             _options |= AUTOTICKS;
52         else if (!strcmp(o, "vertical"))
53             _options |= VERT;
54         else if (!strcmp(o, "horizontal"))
55             _options |= HORZ;
56         else if (!strcmp(o, "top"))
57             _options |= TOP;
58         else if (!strcmp(o, "left"))
59             _options |= LEFT;
60         else if (!strcmp(o, "bottom"))
61             _options |= BOTTOM;
62         else if (!strcmp(o, "right"))
63             _options |= RIGHT;
64         else if (!strcmp(o, "both"))
65             _options |= (LEFT|RIGHT);
66         else if (!strcmp(o, "noticks"))
67             _options |= NOTICKS;
68         else if (!strcmp(o, "arithtic"))
69             _options |= ARITHTIC;
70         else if (!strcmp(o, "decitics"))
71             _options |= DECITICS;
72         else if (!strcmp(o, "notext"))
73             _options |= NOTEXT;
74         else
75             SG_LOG(SG_INPUT, SG_WARN, "HUD: unsupported option: " << o);
76     }
77
78     // Set up convenience values for centroid of the box and
79     // the span values according to orientation
80
81     if (_options & VERT) {
82         _scr_span = _h;
83     } else {
84         _scr_span = _w;
85     }
86
87     _mid_span.x = _x + _w / 2.0;
88     _mid_span.y = _y + _h / 2.0;
89 }
90
91
92 bool HUD::Item::isEnabled()
93 {
94     return _condition ? _condition->test() : true;
95 }
96
97
98 void HUD::Item::draw_line(float x1, float y1, float x2, float y2)
99 {
100     _hud->_line_list.add(LineSegment(x1, y1, x2, y2));
101 }
102
103
104 void HUD::Item::draw_stipple_line(float x1, float y1, float x2, float y2)
105 {
106     _hud->_stipple_line_list.add(LineSegment(x1, y1, x2, y2));
107 }
108
109
110 void HUD::Item::draw_text(float x, float y, char *msg, int digit)
111 {
112     _hud->_text_list.add(HUDText(x, y, msg, digit));
113 }
114
115
116 void HUD::Item::draw_circle(float xoffs, float yoffs, float r) const
117 {
118     glBegin(GL_LINE_LOOP);
119     for (int i = 0; i < 25; i++) {
120         float alpha = i * 2.0 * SG_PI / 10.0;
121         float x = r * cos(alpha);
122         float y = r * sin(alpha);
123         glVertex2f(x + xoffs, y + yoffs);
124     }
125     glEnd();
126 }
127
128
129 void HUD::Item::draw_bullet(float x, float y, float size)
130 {
131     glEnable(GL_POINT_SMOOTH);
132     glPointSize(size);
133
134     glBegin(GL_POINTS);
135     glVertex2f(x, y);
136     glEnd();
137
138     glPointSize(1.0);
139     glDisable(GL_POINT_SMOOTH);
140 }
141
142
143 float HUD::Item::text_width(char *str) const
144 {
145     assert(_hud->_font_renderer);
146     float r, l;
147     _hud->_font->getBBox(str, _hud->_font_size, 0, &l, &r, 0, 0);
148     return r - l;
149 }
150
151