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.
24 static const float TICK_OFFSET = 2.f;
27 HUD::Tape::Tape(HUD *hud, const SGPropertyNode *n, float x, float y) :
29 _draw_tick_bottom(n->getBoolValue("tick-bottom", false)),
30 _draw_tick_top(n->getBoolValue("tick-top", false)),
31 _draw_tick_right(n->getBoolValue("tick-right", false)),
32 _draw_tick_left(n->getBoolValue("tick-left", false)),
33 _draw_cap_bottom(n->getBoolValue("cap-bottom", false)),
34 _draw_cap_top(n->getBoolValue("cap-top", false)),
35 _draw_cap_right(n->getBoolValue("cap-right", false)),
36 _draw_cap_left(n->getBoolValue("cap-left", false)),
37 _marker_offset(n->getFloatValue("marker-offset")),
38 _label_offset(n->getFloatValue("label-offset", 3.0)),
39 _label_gap(n->getFloatValue("label-gap-width") / 2.0),
40 _pointer(n->getBoolValue("enable-pointer", true)),
41 _format(n->getStringValue("format", "%d"))
43 _half_width_units = range_to_show() / 2.0;
46 s = n->getStringValue("pointer-type");
47 _pointer_type = strcmp(s, "moving") ? FIXED : MOVING; // "fixed", "moving"
49 s = n->getStringValue("tick-type");
50 _tick_type = strcmp(s, "bullet") ? LINE : CIRCLE; // "bullet", "line"
52 s = n->getStringValue("tick-length"); // "variable", "constant"
53 _tick_length = strcmp(s, "constant") ? VARIABLE : CONSTANT;
55 _label_fmt = check_format(_format.c_str());
56 if (_label_fmt != INT && _label_fmt != LONG
57 && _label_fmt != FLOAT && _label_fmt != DOUBLE) {
58 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid <format> '" << _format.c_str()
59 << "' in <tape> '" << _name << "' (must be number format)");
64 if (_minor_divs != 0.0f)
65 _div_ratio = int(_major_divs / _minor_divs + 0.5f);
67 _div_ratio = 0, _minor_divs = _major_divs;
69 // int k; //odd or even values for ticks // FIXME odd scale
71 if (_input.max() + .5f < float(SGLimits<long>::max()))
72 _odd_type = long(floorf(_input.max() + 0.5f)) & 1 ? true : false;
76 void HUD::Tape::draw(void) // (HUD_scale * pscale)
78 if (!_input.isValid())
81 float value = _input.getFloatValue();
86 draw_horizontal(value);
90 void HUD::Tape::draw_vertical(float value)
92 float vmin = 0.0, vmax = 0.0;
99 float right = _x + _w;
103 vmin = value - _half_width_units; // width units == needle travel
104 vmax = value + _half_width_units; // or picture unit span.
107 } else if (_pointer_type == MOVING) {
112 vmin = value - _half_width_units; // width units == needle travel
113 vmax = value + _half_width_units; // or picture unit span.
118 if (_draw_tick_bottom)
119 draw_line(_x, _y, right, _y);
123 draw_line(_x, top, right, top);
125 marker_xs = _x; // x start
126 marker_xe = right; // x extent
129 // We do not use else in the following so that combining the
130 // two options produces a "caged" display with double
131 // carrots. The same is done for horizontal card indicators.
133 // draw capping lines and pointers
134 if (option_left()) { // Calculate x marker offset
137 draw_line(marker_xe, _y, marker_xe, marker_ye);
139 marker_xs = marker_xe - _w / 3.0;
141 // draw_line(marker_xs, _center_y, marker_xe, _center_y + _w / 6);
142 // draw_line(marker_xs, _center_y, marker_xe, _center_y - _w / 6);
145 if (_pointer_type == MOVING) {
146 float ycentre, ypoint, xpoint;
149 if (_input.min() >= 0.0)
151 else if (_input.max() + _input.min() == 0.0)
154 ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min());
156 ycentre = _y + _input.min() * _h / (_input.max() - _input.min());
162 ypoint = ycentre + ((value - 1.0) * range / _val_span);
164 ypoint = ycentre + (value * range / _val_span);
166 xpoint = right + _marker_offset;
167 draw_line(xpoint, ycentre, xpoint, ypoint);
168 draw_line(xpoint, ypoint, xpoint - _marker_offset, ypoint);
169 draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint + 5.0);
170 draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint - 5.0);
173 draw_fixed_pointer(_marker_offset + marker_xe, text_y + _w / 6,
174 _marker_offset + marker_xs, text_y, _marker_offset + marker_xe,
178 } // if (option_left())
181 // draw capping lines and pointers
182 if (option_right()) {
185 draw_line(_x, _y, _x, marker_ye);
187 marker_xe = _x + _w / 3.0;
189 // draw_line(_x, _center_y + _w / 6, marker_xe, _center_y);
190 // draw_line(_x, _center_y - _w / 6, marker_xe, _center_y);
193 if (_pointer_type == MOVING) {
194 float ycentre, ypoint, xpoint;
197 if (_input.min() >= 0.0)
199 else if (_input.max() + _input.min() == 0.0)
202 ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min());
204 ycentre = _y + _input.min() * _h / (_input.max() - _input.min());
209 ypoint = ycentre + ((value - 1.0) * range / _val_span);
211 ypoint = ycentre + (value * range / _val_span);
213 xpoint = _x - _marker_offset;
214 draw_line(xpoint, ycentre, xpoint, ypoint);
215 draw_line(xpoint, ypoint, xpoint + _marker_offset, ypoint);
216 draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint + 5.0);
217 draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint - 5.0);
220 draw_fixed_pointer(-_marker_offset + _x, text_y + _w / 6,
221 -_marker_offset + marker_xe, text_y, -_marker_offset + _x,
225 } // if (option_right())
228 // At this point marker x_start and x_end values are transposed.
229 // To keep this from confusing things they are now swapped.
231 marker_ye = marker_xs, marker_xs = marker_xe, marker_xe = marker_ye;
235 // Work through from bottom to top of scale. Calculating where to put
236 // minor and major ticks.
238 // draw scale or tape
239 float vstart = floorf(vmin / _major_divs) * _major_divs;
240 float min_diff = _w / 6.0; // length difference between major & minor tick
242 // FIXME consider oddtype
243 for (int i = 0; ; i++) {
244 float v = vstart + i * _minor_divs;
247 if (v < _input.min())
249 else if (v > _input.max())
253 float y = _y + (v - vmin) * factor();
255 if (y < _y + TICK_OFFSET)
257 if (y > top - TICK_OFFSET)
260 if (_div_ratio && i % _div_ratio) { // minor div
262 if (_tick_type == LINE) {
263 if (_tick_length == VARIABLE) {
264 draw_line(_x, y, marker_xs, y);
265 draw_line(marker_xe, y, right, y);
267 draw_line(_x, y, marker_xs, y);
268 draw_line(marker_xe, y, right, y);
271 } else { // _tick_type == CIRCLE
272 draw_bullet(_x, y, 3.0);
275 } else if (option_left()) {
276 if (_tick_type == LINE) {
277 if (_tick_length == VARIABLE) {
278 draw_line(marker_xs + min_diff, y, marker_xe, y);
280 draw_line(marker_xs, y, marker_xe, y);
282 } else { // _tick_type == CIRCLE
283 draw_bullet(marker_xs + 4, y, 3.0);
286 } else { // if (option_right())
287 if (_tick_type == LINE) {
288 if (_tick_length == VARIABLE) {
289 draw_line(marker_xs, y, marker_xe - min_diff, y);
291 draw_line(marker_xs, y, marker_xe, y);
294 } else { // _tick_type == CIRCLE
295 draw_bullet(marker_xe - 4, y, 3.0);
299 } else { // major div
301 v = fmodf(v + _modulo, _modulo);
307 if (_tick_type == LINE) {
308 draw_line(_x, y, marker_xs, y);
309 draw_line(marker_xs, y, right, y);
311 } else { // _tick_type == CIRCLE
312 draw_bullet(_x, y, 5.0);
315 x = marker_xs, align = CENTER;
318 if (_tick_type == LINE)
319 draw_line(marker_xs, y, marker_xe, y);
320 else // _tick_type == CIRCLE
321 draw_bullet(marker_xs + 4, y, 5.0);
324 x = marker_xs - _label_offset, align = RIGHT|VCENTER;
326 x = marker_xe + _label_offset, align = LEFT|VCENTER;
329 if (!option_notext()) {
330 char *s = format_value(v);
333 _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
335 if (b < _y || t > top)
338 if (_label_gap == 0.0
339 || (b < _center_y - _label_gap && t < _center_y - _label_gap)
340 || (b > _center_y + _label_gap && t > _center_y + _label_gap)) {
349 void HUD::Tape::draw_horizontal(float value)
351 float vmin = 0.0, vmax = 0.0;
359 float right = _x + _w;
363 vmin = value - _half_width_units; // width units == needle travel
364 vmax = value + _half_width_units; // or picture unit span.
367 } else if (_pointer_type == MOVING) {
372 vmin = value - _half_width_units; // width units == needle travel
373 vmax = value + _half_width_units; // or picture unit span.
379 draw_line(_x, _y, _x, top);
382 if (_draw_tick_right)
383 draw_line(right, _y, right, top);
385 marker_ys = _y; // Starting point for
386 marker_ye = top; // tick y location calcs
388 marker_xs = _x + ((value - vmin) * factor());
391 if (_draw_cap_bottom)
392 draw_line(_x, _y, right, _y);
395 marker_ye = _y + _h / 2;
397 // draw_line(_center_x, marker_ye, _center_x - _h / 4, _y);
398 // draw_line(_center_x, marker_ye, _center_x + _h / 4, _y);
401 if (_pointer_type == MOVING) {
402 float xcentre = _center_x;
404 float xpoint = xcentre + (value * range / _val_span);
405 float ypoint = _y - _marker_offset;
406 draw_line(xcentre, ypoint, xpoint, ypoint);
407 draw_line(xpoint, ypoint, xpoint, ypoint + _marker_offset);
408 draw_line(xpoint, ypoint + _marker_offset, xpoint + 5.0, ypoint + 5.0);
409 draw_line(xpoint, ypoint + _marker_offset, xpoint - 5.0, ypoint + 5.0);
412 draw_fixed_pointer(marker_xs - _h / 4, _y, marker_xs,
413 marker_ye, marker_xs + _h / 4, _y);
416 } // if (option_top())
418 if (option_bottom()) {
420 draw_line(_x, top, right, top);
423 marker_ys = top - _h / 2;
425 // draw_line(_center_x + _h / 4, _y + _h, _center_x, marker_ys);
426 // draw_line(_center_x - _h / 4, _y + _h, _center_x , marker_ys);
429 if (_pointer_type == MOVING) {
430 float xcentre = _center_x;
433 float xpoint = xcentre + (value * range / _val_span);
434 float ypoint = hgt + _marker_offset;
435 draw_line(xcentre, ypoint, xpoint, ypoint);
436 draw_line(xpoint, ypoint, xpoint, ypoint - _marker_offset);
437 draw_line(xpoint, ypoint - _marker_offset, xpoint + 5.0, ypoint - 5.0);
438 draw_line(xpoint, ypoint - _marker_offset, xpoint - 5.0, ypoint - 5.0);
441 draw_fixed_pointer(marker_xs + _h / 4, top, marker_xs, marker_ys,
442 marker_xs - _h / 4, top);
445 } // if (option_bottom())
448 float vstart = floorf(vmin / _major_divs) * _major_divs;
449 float min_diff = _h / 6.0; // length difference between major & minor tick
451 // FIXME consider oddtype
452 for (int i = 0; ; i++) {
453 float v = vstart + i * _minor_divs;
456 if (v < _input.min())
458 else if (v > _input.max())
462 float x = _x + (v - vmin) * factor();
464 if (x < _x + TICK_OFFSET)
466 if (x > right - TICK_OFFSET)
469 if (_div_ratio && i % _div_ratio) { // minor div
471 if (_tick_length == VARIABLE) {
472 draw_line(x, _y, x, marker_ys - 4);
473 draw_line(x, marker_ye + 4, x, top);
475 draw_line(x, _y, x, marker_ys);
476 draw_line(x, marker_ye, x, top);
482 if (_tick_length == VARIABLE)
483 draw_line(x, marker_ys, x, marker_ye - min_diff);
485 draw_line(x, marker_ys, x, marker_ye);
487 } else if (_tick_length == VARIABLE) {
488 draw_line(x, marker_ys + 4, x, marker_ye);
490 draw_line(x, marker_ys, x, marker_ye);
494 } else { // major divs
496 v = fmodf(v + _modulo, _modulo);
502 draw_line(x, _y, x, marker_ye);
503 draw_line(x, marker_ye, x, _y + _h);
504 y = marker_ys, align = CENTER;
507 draw_line(x, marker_ys, x, marker_ye);
510 y = top - _label_offset, align = TOP|HCENTER;
512 y = _y + _label_offset, align = BOTTOM|HCENTER;
515 if (!option_notext()) {
516 char *s = format_value(v);
519 _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
521 if (l < _x || r > right)
524 if (_label_gap == 0.0
525 || (l < _center_x - _label_gap && r < _center_x - _label_gap)
526 || (l > _center_x + _label_gap && r > _center_x + _label_gap)) {
535 char *HUD::Tape::format_value(float v)
537 if (fabsf(v) < 1e-8) // avoid -0.0
540 if (_label_fmt == INT)
541 snprintf(_buf, BUFSIZE, _format.c_str(), int(v));
542 else if (_label_fmt == LONG)
543 snprintf(_buf, BUFSIZE, _format.c_str(), long(v));
544 else if (_label_fmt == FLOAT)
545 snprintf(_buf, BUFSIZE, _format.c_str(), v);
546 else // _label_fmt == DOUBLE
547 snprintf(_buf, BUFSIZE, _format.c_str(), double(v));
552 void HUD::Tape::draw_fixed_pointer(float x1, float y1, float x2, float y2, float x3, float y3)
554 glBegin(GL_LINE_STRIP);