1 // HUD_tape.cxx -- HUD Tape Instrument
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.
25 HUD::Tape::Tape(HUD *hud, const SGPropertyNode *n, float x, float y) :
27 _draw_tick_bottom(n->getBoolValue("tick-bottom", false)),
28 _draw_tick_top(n->getBoolValue("tick-top", false)),
29 _draw_tick_right(n->getBoolValue("tick-right", false)),
30 _draw_tick_left(n->getBoolValue("tick-left", false)),
31 _draw_cap_bottom(n->getBoolValue("cap-bottom", false)),
32 _draw_cap_top(n->getBoolValue("cap-top", false)),
33 _draw_cap_right(n->getBoolValue("cap-right", false)),
34 _draw_cap_left(n->getBoolValue("cap-left", false)),
35 _marker_offset(n->getFloatValue("marker-offset", 0.0)),
36 _label_gap(n->getFloatValue("label-gap-width", 0.0) / 2.0),
37 _pointer(n->getBoolValue("enable-pointer", true)),
38 _format(n->getStringValue("format", "%d"))
40 _half_width_units = range_to_show() / 2.0;
43 s = n->getStringValue("pointer-type");
44 _pointer_type = strcmp(s, "moving") ? FIXED : MOVING; // "fixed", "moving"
46 s = n->getStringValue("tick-type");
47 _tick_type = strcmp(s, "bullet") ? LINE : CIRCLE; // "bullet", "line"
49 s = n->getStringValue("tick-length"); // "variable", "constant"
50 _tick_length = strcmp(s, "constant") ? VARIABLE : CONSTANT;
52 _label_fmt = check_format(_format.c_str());
53 if (_label_fmt != INT && _label_fmt != LONG
54 && _label_fmt != FLOAT && _label_fmt != DOUBLE) {
55 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid <format> '" << _format.c_str()
56 << "' in <tape> '" << _name << "' (must be number format)");
61 if (_minor_divs != 0.0f)
62 _div_ratio = int(_major_divs / _minor_divs + 0.5f);
64 _div_ratio = 0, _minor_divs = _major_divs;
66 // int k; //odd or even values for ticks // FIXME odd scale
67 _odd_type = int(floorf(_input.max() + 0.5)) & 1 ? true : false;
71 void HUD::Tape::draw(void) // (HUD_scale * pscale)
73 if (!_input.isValid())
76 float value = _input.getFloatValue();
81 draw_horizontal(value);
85 void HUD::Tape::draw_vertical(float value)
87 float vmin = 0.0, vmax = 0.0;
94 float right = _x + _w;
98 vmin = value - _half_width_units; // width units == needle travel
99 vmax = value + _half_width_units; // or picture unit span.
102 } else if (_pointer_type == MOVING) {
107 vmin = value - _half_width_units; // width units == needle travel
108 vmax = value + _half_width_units; // or picture unit span.
113 if (_draw_tick_bottom)
114 draw_line(_x, _y, right, _y);
118 draw_line(_x, top, right, top);
120 marker_xs = _x; // x start
121 marker_xe = right; // x extent
124 // We do not use else in the following so that combining the
125 // two options produces a "caged" display with double
126 // carrots. The same is done for horizontal card indicators.
128 // draw capping lines and pointers
129 if (option_left()) { // Calculate x marker offset
132 draw_line(marker_xe, _y, marker_xe, marker_ye);
134 marker_xs = marker_xe - _w / 3.0;
136 // draw_line(marker_xs, _center_y, marker_xe, _center_y + _w / 6);
137 // draw_line(marker_xs, _center_y, marker_xe, _center_y - _w / 6);
140 if (_pointer_type == MOVING) {
141 float ycentre, ypoint, xpoint;
144 if (_input.min() >= 0.0)
146 else if (_input.max() + _input.min() == 0.0)
149 ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min());
151 ycentre = _y + _input.min() * _h / (_input.max() - _input.min());
157 ypoint = ycentre + ((value - 1.0) * range / _val_span);
159 ypoint = ycentre + (value * range / _val_span);
161 xpoint = right + _marker_offset;
162 draw_line(xpoint, ycentre, xpoint, ypoint);
163 draw_line(xpoint, ypoint, xpoint - _marker_offset, ypoint);
164 draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint + 5.0);
165 draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint - 5.0);
168 draw_fixed_pointer(_marker_offset + marker_xe, text_y + _w / 6,
169 _marker_offset + marker_xs, text_y, _marker_offset + marker_xe,
173 } // if (option_left())
176 // draw capping lines and pointers
177 if (option_right()) {
180 draw_line(_x, _y, _x, marker_ye);
182 marker_xe = _x + _w / 3.0;
184 // draw_line(_x, _center_y + _w / 6, marker_xe, _center_y);
185 // draw_line(_x, _center_y - _w / 6, marker_xe, _center_y);
188 if (_pointer_type == MOVING) {
189 float ycentre, ypoint, xpoint;
192 if (_input.min() >= 0.0)
194 else if (_input.max() + _input.min() == 0.0)
197 ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min());
199 ycentre = _y + _input.min() * _h / (_input.max() - _input.min());
204 ypoint = ycentre + ((value - 1.0) * range / _val_span);
206 ypoint = ycentre + (value * range / _val_span);
208 xpoint = _x - _marker_offset;
209 draw_line(xpoint, ycentre, xpoint, ypoint);
210 draw_line(xpoint, ypoint, xpoint + _marker_offset, ypoint);
211 draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint + 5.0);
212 draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint - 5.0);
215 draw_fixed_pointer(-_marker_offset + _x, text_y + _w / 6,
216 -_marker_offset + marker_xe, text_y, -_marker_offset + _x,
220 } // if (option_right())
223 // At this point marker x_start and x_end values are transposed.
224 // To keep this from confusing things they are now swapped.
226 marker_ye = marker_xs, marker_xs = marker_xe, marker_xe = marker_ye;
230 // Work through from bottom to top of scale. Calculating where to put
231 // minor and major ticks.
233 // draw scale or tape
234 float vstart = floorf(vmin / _major_divs) * _major_divs;
235 float min_diff = _w / 6.0; // length difference between major & minor tick
237 // FIXME consider oddtype
238 for (int i = 0; ; i++) {
239 float v = vstart + i * _minor_divs;
242 if (v < _input.min())
244 else if (v > _input.max())
247 float y = _y + (v - vmin) * factor();
254 if (_div_ratio && i % _div_ratio) { // minor div
256 if (_tick_type == LINE) {
257 if (_tick_length == VARIABLE) {
258 draw_line(_x, y, marker_xs, y);
259 draw_line(marker_xe, y, right, y);
261 draw_line(_x, y, marker_xs, y);
262 draw_line(marker_xe, y, right, y);
265 } else { // _tick_type == CIRCLE
266 draw_bullet(_x, y, 3.0);
269 } else if (option_left()) {
270 if (_tick_type == LINE) {
271 if (_tick_length == VARIABLE) {
272 draw_line(marker_xs + min_diff, y, marker_xe, y);
274 draw_line(marker_xs, y, marker_xe, y);
276 } else { // _tick_type == CIRCLE
277 draw_bullet(marker_xs + 4, y, 3.0);
280 } else { // if (option_right())
281 if (_tick_type == LINE) {
282 if (_tick_length == VARIABLE) {
283 draw_line(marker_xs, y, marker_xe - min_diff, y);
285 draw_line(marker_xs, y, marker_xe, y);
288 } else { // _tick_type == CIRCLE
289 draw_bullet(marker_xe - 4, y, 3.0);
293 } else { // major div
295 v = fmodf(v + _modulo, _modulo);
301 if (_tick_type == LINE) {
302 draw_line(_x, y, marker_xs, y);
303 draw_line(marker_xs, y, right, y);
305 } else { // _tick_type == CIRCLE
306 draw_bullet(_x, y, 5.0);
309 x = marker_xs, align = CENTER;
312 if (_tick_type == LINE)
313 draw_line(marker_xs, y, marker_xe, y);
314 else // _tick_type == CIRCLE
315 draw_bullet(marker_xs + 4, y, 5.0);
318 x = marker_xs, align = RIGHT|VCENTER;
320 x = marker_xe, align = LEFT|VCENTER;
323 if (!option_notext()) {
324 char *s = format_value(v);
327 _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
329 if (b < _y || t > top)
332 if (_label_gap == 0.0
333 || b < _center_y - _label_gap && t < _center_y - _label_gap
334 || b > _center_y + _label_gap && t > _center_y + _label_gap) {
343 void HUD::Tape::draw_horizontal(float value)
345 float vmin = 0.0, vmax = 0.0;
353 float right = _x + _w;
357 vmin = value - _half_width_units; // width units == needle travel
358 vmax = value + _half_width_units; // or picture unit span.
361 } else if (_pointer_type == MOVING) {
366 vmin = value - _half_width_units; // width units == needle travel
367 vmax = value + _half_width_units; // or picture unit span.
373 draw_line(_x, _y, _x, top);
376 if (_draw_tick_right)
377 draw_line(right, _y, right, top);
379 marker_ys = _y; // Starting point for
380 marker_ye = top; // tick y location calcs
382 marker_xs = _x + ((value - vmin) * factor());
385 if (_draw_cap_bottom)
386 draw_line(_x, _y, right, _y);
389 marker_ye = _y + _h / 2;
391 // draw_line(_center_x, marker_ye, _center_x - _h / 4, _y);
392 // draw_line(_center_x, marker_ye, _center_x + _h / 4, _y);
395 if (_pointer_type == MOVING) {
396 float xcentre = _center_x;
398 float xpoint = xcentre + (value * range / _val_span);
399 float ypoint = _y - _marker_offset;
400 draw_line(xcentre, ypoint, xpoint, ypoint);
401 draw_line(xpoint, ypoint, xpoint, ypoint + _marker_offset);
402 draw_line(xpoint, ypoint + _marker_offset, xpoint + 5.0, ypoint + 5.0);
403 draw_line(xpoint, ypoint + _marker_offset, xpoint - 5.0, ypoint + 5.0);
406 draw_fixed_pointer(marker_xs - _h / 4, _y, marker_xs,
407 marker_ye, marker_xs + _h / 4, _y);
410 } // if (option_top())
412 if (option_bottom()) {
414 draw_line(_x, top, right, top);
417 marker_ys = top - _h / 2;
419 // draw_line(_center_x + _h / 4, _y + _h, _center_x, marker_ys);
420 // draw_line(_center_x - _h / 4, _y + _h, _center_x , marker_ys);
423 if (_pointer_type == MOVING) {
424 float xcentre = _center_x;
427 float xpoint = xcentre + (value * range / _val_span);
428 float ypoint = hgt + _marker_offset;
429 draw_line(xcentre, ypoint, xpoint, ypoint);
430 draw_line(xpoint, ypoint, xpoint, ypoint - _marker_offset);
431 draw_line(xpoint, ypoint - _marker_offset, xpoint + 5.0, ypoint - 5.0);
432 draw_line(xpoint, ypoint - _marker_offset, xpoint - 5.0, ypoint - 5.0);
435 draw_fixed_pointer(marker_xs + _h / 4, top, marker_xs, marker_ys,
436 marker_xs - _h / 4, top);
439 } // if (option_bottom())
442 float vstart = floorf(vmin / _major_divs) * _major_divs;
443 float min_diff = _h / 6.0; // length difference between major & minor tick
445 // FIXME consider oddtype
446 for (int i = 0; ; i++) {
447 float v = vstart + i * _minor_divs;
450 if (v < _input.min())
452 else if (v > _input.max())
455 float x = _x + (v - vmin) * factor();
462 if (_div_ratio && i % _div_ratio) { // minor div
464 if (_tick_length == VARIABLE) {
465 draw_line(x, _y, x, marker_ys - 4);
466 draw_line(x, marker_ye + 4, x, top);
468 draw_line(x, _y, x, marker_ys);
469 draw_line(x, marker_ye, x, top);
475 if (_tick_length == VARIABLE)
476 draw_line(x, marker_ys, x, marker_ye - min_diff);
478 draw_line(x, marker_ys, x, marker_ye);
480 } else if (_tick_length == VARIABLE) {
481 draw_line(x, marker_ys + 4, x, marker_ye);
483 draw_line(x, marker_ys, x, marker_ye);
487 } else { // major divs
489 v = fmodf(v + _modulo, _modulo);
495 draw_line(x, _y, x, marker_ye);
496 draw_line(x, marker_ye, x, _y + _h);
497 y = marker_ys, align = CENTER;
500 draw_line(x, marker_ys, x, marker_ye);
503 y = top, align = TOP|HCENTER;
505 y = _y, align = BOTTOM|HCENTER;
508 if (!option_notext()) {
509 char *s = format_value(v);
512 _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
514 if (l < _x || r > right)
517 if (_label_gap == 0.0
518 || l < _center_x - _label_gap && r < _center_x - _label_gap
519 || l > _center_x + _label_gap && r > _center_x + _label_gap) {
528 char *HUD::Tape::format_value(float v)
530 if (fabsf(v) < 1e-8) // avoid -0.0
533 if (_label_fmt == INT)
534 snprintf(_buf, BUFSIZE, _format.c_str(), int(v + 0.5f));
535 else if (_label_fmt == LONG)
536 snprintf(_buf, BUFSIZE, _format.c_str(), long(v + 0.5f));
537 else if (_label_fmt == FLOAT)
538 snprintf(_buf, BUFSIZE, _format.c_str(), v);
539 else // _label_fmt == DOUBLE
540 snprintf(_buf, BUFSIZE, _format.c_str(), double(v));
545 void HUD::Tape::draw_fixed_pointer(float x1, float y1, float x2, float y2, float x3, float y3)
547 glBegin(GL_LINE_STRIP);