]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_instrument.cxx
- collect drawing primitives in the Item base class
[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 #include <simgear/math/SGLimits.hxx>
23 #include <simgear/props/condition.hxx>
24 #include "HUD.hxx"
25
26
27 HUD::Item::Item(HUD *hud, const SGPropertyNode *n, float x, float y) :
28     _hud(hud),
29     _name(n->getStringValue("name", "[unnamed]")),
30     _options(0),
31     _condition(0),
32     _digits(n->getIntValue("digits"))
33 {
34     const SGPropertyNode *node = n->getNode("condition");
35     if (node)
36         _condition = sgReadCondition(globals->get_props(), node);
37
38     _scrn_pos.left = n->getIntValue("x") + x;
39     _scrn_pos.top = n->getIntValue("y") + y;
40     _scrn_pos.right = n->getIntValue("width");
41     _scrn_pos.bottom = n->getIntValue("height");
42
43     vector<SGPropertyNode_ptr> opt = n->getChildren("option");
44     for (unsigned int i = 0; i < opt.size(); i++) {
45         const char *o = opt[i]->getStringValue();
46         if (!strcmp(o, "autoticks"))
47             _options |= AUTOTICKS;
48         else if (!strcmp(o, "vertical"))
49             _options |= VERT;
50         else if (!strcmp(o, "horizontal"))
51             _options |= HORZ;
52         else if (!strcmp(o, "top"))
53             _options |= TOP;
54         else if (!strcmp(o, "left"))
55             _options |= LEFT;
56         else if (!strcmp(o, "bottom"))
57             _options |= BOTTOM;
58         else if (!strcmp(o, "right"))
59             _options |= RIGHT;
60         else if (!strcmp(o, "both"))
61             _options |= (LEFT|RIGHT);
62         else if (!strcmp(o, "noticks"))
63             _options |= NOTICKS;
64         else if (!strcmp(o, "arithtic"))
65             _options |= ARITHTIC;
66         else if (!strcmp(o, "decitics"))
67             _options |= DECITICS;
68         else if (!strcmp(o, "notext"))
69             _options |= NOTEXT;
70         else
71             SG_LOG(SG_INPUT, SG_WARN, "HUD: unsupported option: " << o);
72     }
73
74     // Set up convenience values for centroid of the box and
75     // the span values according to orientation
76
77     if (_options & VERT) {
78         _scr_span = _scrn_pos.bottom;
79     } else {
80         _scr_span = _scrn_pos.right;
81     }
82
83     _mid_span.x = _scrn_pos.left + _scrn_pos.right / 2.0;
84     _mid_span.y = _scrn_pos.top + _scrn_pos.bottom / 2.0;
85 }
86
87
88 bool HUD::Item::isEnabled()
89 {
90     return _condition ? _condition->test() : true;
91 }
92
93
94 void HUD::Item::draw_line( float x1, float y1, float x2, float y2)
95 {
96     _hud->_line_list.add(LineSegment(x1, y1, x2, y2));
97 }
98
99
100 void HUD::Item::draw_stipple_line( float x1, float y1, float x2, float y2)
101 {
102     _hud->_stipple_line_list.add(LineSegment(x1, y1, x2, y2));
103 }
104
105
106 void HUD::Item::draw_text( float x, float y, char *msg, int digit)
107 {
108     _hud->_text_list.add(HUDText(x, y, msg, digit));
109 }
110
111
112 void HUD::Item::draw_circle(float x1, float y1, float r) const
113 {
114     glBegin(GL_LINE_LOOP);
115     for (int count = 0; count < 25; count++) {
116         float cosine = r * cos(count * 2 * SG_PI / 10.0);
117         float sine =   r * sin(count * 2 * SG_PI / 10.0);
118         glVertex2f(cosine + x1, sine + y1);
119     }
120     glEnd();
121 }
122
123
124 void HUD::Item::draw_bullet(float x, float y, float size)
125 {
126     glEnable(GL_POINT_SMOOTH);
127     glPointSize(size);
128
129     glBegin(GL_POINTS);
130     glVertex2f(x, y);
131     glEnd();
132
133     glPointSize(1.0);
134     glDisable(GL_POINT_SMOOTH);
135 }
136
137
138 float HUD::Item::text_width(char *str) const
139 {
140     assert(_hud->_font_renderer);
141     float r, l;
142     _hud->_font->getBBox(str, _hud->_font_size, 0, &l, &r, 0, 0);
143     return r - l;
144 }
145
146