]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_gauge.cxx
get rid of struct Point and get_centroid(). Instruments may access *their*
[flightgear.git] / src / Instrumentation / HUD / HUD_gauge.cxx
1 // HUD_gauge.cxx -- HUD Gauge Instrument
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 "HUD.hxx"
23
24
25 HUD::Gauge::Gauge(HUD *hud, const SGPropertyNode *n, float x, float y) :
26     Scale(hud, n, x, y)
27 {
28 }
29
30
31 // As implemented, draw only correctly draws a horizontal or vertical
32 // scale. It should contain a variation that permits clock type displays.
33 // Now is supports "tickless" displays such as control surface indicators.
34 // This routine should be worked over before using. Current value would be
35 // fetched and not used if not commented out. Clearly that is intollerable.
36
37 void HUD::Gauge::draw(void)
38 {
39     if (!_input.isValid())
40         return;
41
42     float marker_xs, marker_xe;
43     float marker_ys, marker_ye;
44     float text_x, text_y;
45     float width, height, bottom_4;
46     float lenstr;
47     int i;
48     const int BUFSIZE = 80;
49     char buf[BUFSIZE];
50     bool condition;
51     int disp_val = 0;
52     float vmin = _input.min();
53     float vmax = _input.max();
54     float cur_value = _input.getFloatValue();
55
56     width = _x + _w;                                            // FIXME huh?
57     height = _y + _h;
58     bottom_4 = _h / 4.0;
59
60     // Draw the basic markings for the scale...
61     if (option_vert()) { // Vertical scale
62         // Bottom tick bar
63         draw_line(_x, _y, width, _y);
64
65         // Top tick bar
66         draw_line(_x, height, width, height);
67
68         marker_xs = _x;
69         marker_xe = width;
70
71         if (option_left()) { // Read left, so line down right side
72             draw_line(width, _y, width, height);
73             marker_xs  = marker_xe - _w / 3.0;   // Adjust tick
74         }
75
76         if (option_right()) {     // Read  right, so down left sides
77             draw_line(_x, _y, _x, height);
78             marker_xe = _x + _w / 3.0;   // Adjust tick
79         }
80
81         // At this point marker x_start and x_end values are transposed.
82         // To keep this from confusing things they are now interchanged.
83         if (option_both()) {
84             marker_ye = marker_xs;
85             marker_xs = marker_xe;
86             marker_xe = marker_ye;
87         }
88
89         // Work through from bottom to top of scale. Calculating where to put
90         // minor and major ticks.
91
92         if (!option_noticks()) {    // If not no ticks...:)
93             // Calculate x marker offsets
94             int last = (int)vmax + 1;
95             i = (int)vmin;
96
97             for (; i < last; i++) {
98                 // Calculate the location of this tick
99                 marker_ys = _y + (i - vmin) * factor()/* +.5f*/;
100
101                 // We compute marker_ys even though we don't know if we will use
102                 // either major or minor divisions. Simpler.
103
104                 if (_minor_divs) {                  // Minor tick marks
105                     if (!(i % (int)_minor_divs)) {
106                         if (option_left() && option_right()) {
107                             draw_line(_x, marker_ys, marker_xs - 3, marker_ys);
108                             draw_line(marker_xe + 3, marker_ys, width, marker_ys);
109
110                         } else if (option_left()) {
111                             draw_line(marker_xs + 3, marker_ys, marker_xe, marker_ys);
112                         } else {
113                             draw_line(marker_xs, marker_ys, marker_xe - 3, marker_ys);
114                         }
115                     }
116                 }
117
118                 // Now we work on the major divisions. Since these are also labeled
119                 // and no labels are drawn otherwise, we label inside this if
120                 // statement.
121
122                 if (_major_divs) {                  // Major tick mark
123                     if (!(i % (int)_major_divs)) {
124                         if (option_left() && option_right()) {
125                             draw_line(_x, marker_ys, marker_xs, marker_ys);
126                             draw_line(marker_xe, marker_ys, width, marker_ys);
127                         } else {
128                             draw_line(marker_xs, marker_ys, marker_xe, marker_ys);
129                         }
130
131                         if (!option_notext()) {
132                             disp_val = i;
133                             snprintf(buf, BUFSIZE, "%d",
134                                     int(disp_val * _input.factor()/*+.5*/));  /// was data_scaling(), which makes no sense
135
136                             lenstr = text_width(buf);
137
138                             if (option_left() && option_right()) {
139                                 text_x = _center_x - lenstr/2 ;
140
141                             } else if (option_left()) {
142                                 text_x = marker_xs - lenstr;
143                             } else {
144                                 text_x = marker_xe - lenstr;
145                             }
146                             // Now we know where to put the text.
147                             text_y = marker_ys;
148                             draw_text(text_x, text_y, buf, 0);
149                         }
150                     }
151                 }
152             }
153         }
154
155         // Now that the scale is drawn, we draw in the pointer(s). Since labels
156         // have been drawn, text_x and text_y may be recycled. This is used
157         // with the marker start stops to produce a pointer for each side reading
158
159         text_y = _y + ((cur_value - vmin) * factor() /*+.5f*/);
160         //    text_x = marker_xs - _x;
161
162         if (option_right()) {
163             glBegin(GL_LINE_STRIP);
164             glVertex2f(_x, text_y + 5);
165             glVertex2f(marker_xe, text_y);
166             glVertex2f(_x, text_y - 5);
167             glEnd();
168         }
169         if (option_left()) {
170             glBegin(GL_LINE_STRIP);
171             glVertex2f(width, text_y + 5);
172             glVertex2f(marker_xs, text_y);
173             glVertex2f(width, text_y - 5);
174             glEnd();
175         }
176         // End if VERTICAL SCALE TYPE
177
178     } else {                             // Horizontal scale by default
179         // left tick bar
180         draw_line(_x, _y, _x, height);
181
182         // right tick bar
183         draw_line(width, _y, width, height );
184
185         marker_ys = _y;                       // Starting point for
186         marker_ye = height;                              // tick y location calcs
187         marker_xs = _x + (cur_value - vmin) * factor() /*+ .5f*/;
188
189         if (option_top()) {
190             // Bottom box line
191             draw_line(_x, _y, width, _y);
192
193             marker_ye = _y + _h / 2.0;   // Tick point adjust
194             // Bottom arrow
195             glBegin(GL_LINE_STRIP);
196             glVertex2f(marker_xs - bottom_4, _y);
197             glVertex2f(marker_xs, marker_ye);
198             glVertex2f(marker_xs + bottom_4, _y);
199             glEnd();
200         }
201
202         if (option_bottom()) {
203             // Top box line
204             draw_line(_x, height, width, height);
205             // Tick point adjust
206             marker_ys = height - _h / 2.0;
207
208             // Top arrow
209             glBegin(GL_LINE_STRIP);
210             glVertex2f(marker_xs + bottom_4, height);
211             glVertex2f(marker_xs, marker_ys );
212             glVertex2f(marker_xs - bottom_4, height);
213             glEnd();
214         }
215
216
217         int last = (int)vmax + 1;
218         i = (int)vmin;
219         for (; i <last ; i++) {
220             condition = true;
221             if (!_modulo && i < _input.min())
222                     condition = false;
223
224             if (condition) {
225                 marker_xs = _x + (i - vmin) * factor()/* +.5f*/;
226                 //        marker_xs = _x + (int)((i - vmin) * factor() + .5f);
227                 if (_minor_divs) {
228                     if (!(i % (int)_minor_divs)) {
229                         // draw in ticks only if they aren't too close to the edge.
230                         if (((marker_xs + 5) > _x)
231                                || ((marker_xs - 5) < (width))) {
232
233                             if (option_both()) {
234                                 draw_line(marker_xs, _y, marker_xs, marker_ys - 4);
235                                 draw_line(marker_xs, marker_ye + 4, marker_xs, height);
236
237                             } else if (option_top()) {
238                                 draw_line(marker_xs, marker_ys, marker_xs, marker_ye - 4);
239                             } else {
240                                 draw_line(marker_xs, marker_ys + 4, marker_xs, marker_ye);
241                             }
242                         }
243                     }
244                 }
245
246                 if (_major_divs) {
247                     if (!(i % (int)_major_divs)) {
248                         if (_modulo) {
249                             if (disp_val < 0) {
250                                 while (disp_val < 0)
251                                     disp_val += _modulo;
252                             }
253                             disp_val = i % (int)_modulo;
254                         } else {
255                             disp_val = i;
256                         }
257                         snprintf(buf, BUFSIZE, "%d",
258                                 int(disp_val * _input.factor()/* +.5*/)); // was data_scaling(), which makes no sense
259                         lenstr = text_width(buf);
260
261                         // Draw major ticks and text only if far enough from the edge.
262                         if (((marker_xs - 10) > _x)
263                                 && ((marker_xs + 10) < width)) {
264                             if (option_both()) {
265                                 draw_line(marker_xs, _y, marker_xs, marker_ys);
266                                 draw_line(marker_xs, marker_ye, marker_xs, height);
267
268                                 if (!option_notext())
269                                     draw_text(marker_xs - lenstr, marker_ys + 4, buf, 0);
270
271                             } else {
272                                 draw_line(marker_xs, marker_ys, marker_xs, marker_ye);
273
274                                 if (!option_notext()) {
275                                     if (option_top())
276                                         draw_text(marker_xs - lenstr, height - 10, buf, 0);
277                                     else
278                                         draw_text(marker_xs - lenstr, _y, buf, 0);
279                                 }
280                             }
281                         }
282                     }
283                 }
284             }
285         }
286     }
287 }
288
289