]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud_gaug.cxx
remove readCard and let the gauge_instr class read its own properties
[flightgear.git] / src / Cockpit / hud_gaug.cxx
1 #include "hud.hxx"
2
3 #ifdef USE_HUD_TextList
4 #define textString(x, y, text, digit)  TextString(text, x , y ,digit)
5 #else
6 #define textString(x, y, text, digit)  puDrawString(guiFnt, text, x, y)
7 #endif
8
9
10 gauge_instr::gauge_instr(const SGPropertyNode *node) :
11     instr_scale(
12             node->getIntValue("x"),
13             node->getIntValue("y"),
14             node->getIntValue("width"),
15             node->getIntValue("height"),
16             0 /*load_fn*/,
17             node->getIntValue("options"),
18             node->getFloatValue("maxValue") - node->getFloatValue("minValue"), // Always shows span?
19             node->getFloatValue("maxValue"),
20             node->getFloatValue("minValue"),
21             node->getFloatValue("disp_scaling"),
22             node->getIntValue("major_divs"),
23             node->getIntValue("minor_divs"),
24             node->getIntValue("modulator"), // "rollover"
25             0, /* hud.cxx: static int dp_shoing = 0; */    // FIXME
26             node->getBoolValue("working"))
27 {
28     SG_LOG(SG_INPUT, SG_INFO, "Done reading gauge instrument "
29             << node->getStringValue("name", "[unnamed]"));
30
31     string loadfn = node->getStringValue("loadfn");             // FIXME
32     float (*load_fn)(void);
33     if (loadfn=="anzg") {
34         load_fn = get_anzg;
35     } else if (loadfn=="heading") {
36         load_fn = get_heading;
37     } else if (loadfn=="aoa") {
38         load_fn = get_aoa;
39     } else if (loadfn=="climb") {
40         load_fn = get_climb_rate;
41     } else if (loadfn=="altitude") {
42         load_fn = get_altitude;
43     } else if (loadfn=="agl") {
44         load_fn = get_agl;
45     } else if (loadfn=="speed") {
46         load_fn = get_speed;
47     } else if (loadfn=="view_direction") {
48         load_fn = get_view_direction;
49     } else if (loadfn=="aileronval") {
50         load_fn = get_aileronval;
51     } else if (loadfn=="elevatorval") {
52         load_fn = get_elevatorval;
53     } else if (loadfn=="elevatortrimval") {
54         load_fn = get_elev_trimval;
55     } else if (loadfn=="rudderval") {
56         load_fn = get_rudderval;
57     } else if (loadfn=="throttleval") {
58         load_fn = get_throttleval;
59     } else
60         load_fn = 0;
61
62     set_data_source(load_fn);
63 }
64
65
66 // As implemented, draw only correctly draws a horizontal or vertical
67 // scale. It should contain a variation that permits clock type displays.
68 // Now is supports "tickless" displays such as control surface indicators.
69 // This routine should be worked over before using. Current value would be
70 // fetched and not used if not commented out. Clearly that is intollerable.
71
72 void gauge_instr::draw(void)
73 {
74     float marker_xs, marker_xe;
75     float marker_ys, marker_ye;
76     int text_x, text_y;
77     int width, height, bottom_4;
78     int lenstr;
79     int i;
80     char TextScale[80];
81     bool condition;
82     int disp_val = 0;
83     float vmin = min_val();
84     float vmax = max_val();
85     POINT mid_scr = get_centroid();
86     float cur_value = get_value();
87     RECT  scrn_rect = get_location();
88     UINT options = get_options();
89
90     width = scrn_rect.left + scrn_rect.right;
91     height = scrn_rect.top + scrn_rect.bottom;
92     bottom_4 = scrn_rect.bottom / 4;
93
94     // Draw the basic markings for the scale...
95     if (huds_vert(options)) { // Vertical scale
96         // Bottom tick bar
97         drawOneLine(scrn_rect.left, scrn_rect.top, width, scrn_rect.top);
98
99         // Top tick bar
100         drawOneLine( scrn_rect.left, height, width, height);
101
102         marker_xs = scrn_rect.left;
103         marker_xe = width;
104
105         if (huds_left(options)) { // Read left, so line down right side
106             drawOneLine(width, scrn_rect.top, width, height);
107             marker_xs  = marker_xe - scrn_rect.right / 3.0;   // Adjust tick
108         }
109
110         if (huds_right(options)) {     // Read  right, so down left sides
111             drawOneLine(scrn_rect.left, scrn_rect.top, scrn_rect.left, height);
112             marker_xe = scrn_rect.left + scrn_rect.right / 3.0;   // Adjust tick
113         }
114
115         // At this point marker x_start and x_end values are transposed.
116         // To keep this from confusing things they are now interchanged.
117         if (huds_both(options)) {
118             marker_ye = marker_xs;
119             marker_xs = marker_xe;
120             marker_xe = marker_ye;
121         }
122
123         // Work through from bottom to top of scale. Calculating where to put
124         // minor and major ticks.
125
126         if (!huds_noticks(options)) {    // If not no ticks...:)
127             // Calculate x marker offsets
128             int last = (int)vmax + 1;    // FloatToInt(vmax)+1;
129             i = (int)vmin; //FloatToInt(vmin);
130
131             for (; i < last; i++) {
132                 // Calculate the location of this tick
133                 marker_ys = scrn_rect.top + (i - vmin) * factor()/* +.5f*/;
134
135                 // We compute marker_ys even though we don't know if we will use
136                 // either major or minor divisions. Simpler.
137
138                 if (div_min()) {                  // Minor tick marks
139                     if (!(i % (int)div_min())) {
140                         if (huds_left(options) && huds_right(options)) {
141                             drawOneLine(scrn_rect.left, marker_ys, marker_xs - 3, marker_ys);
142                             drawOneLine(marker_xe + 3, marker_ys, width, marker_ys);
143
144                         } else if (huds_left(options)) {
145                             drawOneLine(marker_xs + 3, marker_ys, marker_xe, marker_ys);
146                         } else {
147                             drawOneLine(marker_xs, marker_ys, marker_xe - 3, marker_ys);
148                         }
149                     }
150                 }
151
152                 // Now we work on the major divisions. Since these are also labeled
153                 // and no labels are drawn otherwise, we label inside this if
154                 // statement.
155
156                 if (div_max()) {                  // Major tick mark
157                     if (!(i % (int)div_max())) {
158                         if (huds_left(options) && huds_right(options)) {
159                             drawOneLine(scrn_rect.left, marker_ys, marker_xs, marker_ys);
160                             drawOneLine(marker_xe, marker_ys, width, marker_ys);
161                         } else {
162                             drawOneLine(marker_xs, marker_ys, marker_xe, marker_ys);
163                         }
164
165                         if (!huds_notext(options)) {
166                             disp_val = i;
167                             sprintf(TextScale, "%d",
168                                     FloatToInt(disp_val * data_scaling()/*+.5*/));
169
170                             lenstr = getStringWidth(TextScale);
171
172                             if (huds_left(options) && huds_right(options)) {
173                                 text_x = mid_scr.x -  lenstr/2 ;
174
175                             } else if (huds_left(options)) {
176                                 text_x = FloatToInt(marker_xs - lenstr);
177                             } else {
178                                 text_x = FloatToInt(marker_xe - lenstr);
179                             }
180                             // Now we know where to put the text.
181                             text_y = FloatToInt(marker_ys);
182                             textString(text_x, text_y, TextScale, 0);
183                         }
184                     }
185                 }
186             }
187         }
188
189         // Now that the scale is drawn, we draw in the pointer(s). Since labels
190         // have been drawn, text_x and text_y may be recycled. This is used
191         // with the marker start stops to produce a pointer for each side reading
192
193         text_y = scrn_rect.top + FloatToInt((cur_value - vmin) * factor() /*+.5f*/);
194         //    text_x = marker_xs - scrn_rect.left;
195
196         if (huds_right(options)) {
197             glBegin(GL_LINE_STRIP);
198             glVertex2f(scrn_rect.left, text_y + 5);
199             glVertex2f(FloatToInt(marker_xe), text_y);
200             glVertex2f(scrn_rect.left, text_y - 5);
201             glEnd();
202         }
203         if (huds_left(options)) {
204             glBegin(GL_LINE_STRIP);
205             glVertex2f(width, text_y + 5);
206             glVertex2f(FloatToInt(marker_xs), text_y);
207             glVertex2f(width, text_y - 5);
208             glEnd();
209         }
210         // End if VERTICAL SCALE TYPE
211
212     } else {                             // Horizontal scale by default
213         // left tick bar
214         drawOneLine(scrn_rect.left, scrn_rect.top, scrn_rect.left, height);
215
216         // right tick bar
217         drawOneLine(width, scrn_rect.top, width, height );
218
219         marker_ys = scrn_rect.top;                       // Starting point for
220         marker_ye = height;                              // tick y location calcs
221         marker_xs = scrn_rect.left + (cur_value - vmin) * factor() /*+ .5f*/;
222
223         if (huds_top(options)) {
224             // Bottom box line
225             drawOneLine(scrn_rect.left, scrn_rect.top, width, scrn_rect.top);
226
227             marker_ye = scrn_rect.top + scrn_rect.bottom / 2.0;   // Tick point adjust
228             // Bottom arrow
229             glBegin(GL_LINE_STRIP);
230             glVertex2f(marker_xs - bottom_4, scrn_rect.top);
231             glVertex2f(marker_xs, marker_ye);
232             glVertex2f(marker_xs + bottom_4, scrn_rect.top);
233             glEnd();
234         }
235
236         if (huds_bottom(options)) {
237             // Top box line
238             drawOneLine(scrn_rect.left, height, width, height);
239             // Tick point adjust
240             marker_ys = height - scrn_rect.bottom  / 2.0;
241
242             // Top arrow
243             glBegin(GL_LINE_STRIP);
244             glVertex2f(marker_xs + bottom_4, height);
245             glVertex2f(marker_xs, marker_ys );
246             glVertex2f(marker_xs - bottom_4, height);
247             glEnd();
248         }
249
250
251         int last = (int)vmax + 1; //FloatToInt(vmax)+1;
252         i = (int)vmin; //FloatToInt(vmin);
253         for (; i <last ; i++) {
254             condition = true;
255             if (!modulo() && i < min_val())
256                     condition = false;
257
258             if (condition) {
259                 marker_xs = scrn_rect.left + (i - vmin) * factor()/* +.5f*/;
260                 //        marker_xs = scrn_rect.left + (int)((i - vmin) * factor() + .5f);
261                 if (div_min()) {
262                     if (!(i % (int)div_min())) {
263                         // draw in ticks only if they aren't too close to the edge.
264                         if (((marker_xs + 5) > scrn_rect.left)
265                                || ((marker_xs - 5) < (width))) {
266
267                             if (huds_both(options)) {
268                                 drawOneLine(marker_xs, scrn_rect.top, marker_xs, marker_ys - 4);
269                                 drawOneLine(marker_xs, marker_ye + 4, marker_xs, height);
270
271                             } else if (huds_top(options)) {
272                                 drawOneLine(marker_xs, marker_ys, marker_xs, marker_ye - 4);
273                             } else {
274                                 drawOneLine(marker_xs, marker_ys + 4, marker_xs, marker_ye);
275                             }
276                         }
277                     }
278                 }
279
280                 if (div_max()) {
281                     if (!(i % (int)div_max())) {
282                         if (modulo()) {
283                             if (disp_val < 0) {
284                                 while (disp_val < 0)
285                                     disp_val += modulo();
286                             }
287                             disp_val = i % (int)modulo();
288                         } else {
289                             disp_val = i;
290                         }
291                         sprintf(TextScale, "%d",
292                                 FloatToInt(disp_val * data_scaling()/* +.5*/));
293                         lenstr = getStringWidth(TextScale);
294
295                         // Draw major ticks and text only if far enough from the edge.
296                         if (((marker_xs - 10) > scrn_rect.left)
297                                 && ((marker_xs + 10) < width)) {
298                             if (huds_both(options)) {
299                                 drawOneLine(marker_xs, scrn_rect.top, marker_xs, marker_ys);
300                                 drawOneLine(marker_xs, marker_ye, marker_xs, height);
301
302                                 if (!huds_notext(options))
303                                     textString(marker_xs - lenstr, marker_ys + 4, TextScale, 0);
304
305                             } else {
306                                 drawOneLine(marker_xs, marker_ys, marker_xs, marker_ye);
307
308                                 if (!huds_notext(options)) {
309                                     if (huds_top(options))
310                                         textString(marker_xs - lenstr, height - 10, TextScale, 0);
311                                     else
312                                         textString(marker_xs - lenstr, scrn_rect.top, TextScale, 0);
313                                 }
314                             }
315                         }
316                     }
317                 }
318             }
319         }
320     }
321 }
322
323