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")),
36 _label_gap(n->getFloatValue("label-gap-width") / 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
68 if (_input.max() + .5f < float(SGLimits<long>::max()))
69 _odd_type = long(floorf(_input.max() + 0.5f)) & 1 ? true : false;
73 void HUD::Tape::draw(void) // (HUD_scale * pscale)
75 if (!_input.isValid())
78 float value = _input.getFloatValue();
83 draw_horizontal(value);
87 void HUD::Tape::draw_vertical(float value)
89 float vmin = 0.0, vmax = 0.0;
96 float right = _x + _w;
100 vmin = value - _half_width_units; // width units == needle travel
101 vmax = value + _half_width_units; // or picture unit span.
104 } else if (_pointer_type == MOVING) {
109 vmin = value - _half_width_units; // width units == needle travel
110 vmax = value + _half_width_units; // or picture unit span.
115 if (_draw_tick_bottom)
116 draw_line(_x, _y, right, _y);
120 draw_line(_x, top, right, top);
122 marker_xs = _x; // x start
123 marker_xe = right; // x extent
126 // We do not use else in the following so that combining the
127 // two options produces a "caged" display with double
128 // carrots. The same is done for horizontal card indicators.
130 // draw capping lines and pointers
131 if (option_left()) { // Calculate x marker offset
134 draw_line(marker_xe, _y, marker_xe, marker_ye);
136 marker_xs = marker_xe - _w / 3.0;
138 // draw_line(marker_xs, _center_y, marker_xe, _center_y + _w / 6);
139 // draw_line(marker_xs, _center_y, marker_xe, _center_y - _w / 6);
142 if (_pointer_type == MOVING) {
143 float ycentre, ypoint, xpoint;
146 if (_input.min() >= 0.0)
148 else if (_input.max() + _input.min() == 0.0)
151 ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min());
153 ycentre = _y + _input.min() * _h / (_input.max() - _input.min());
159 ypoint = ycentre + ((value - 1.0) * range / _val_span);
161 ypoint = ycentre + (value * range / _val_span);
163 xpoint = right + _marker_offset;
164 draw_line(xpoint, ycentre, xpoint, ypoint);
165 draw_line(xpoint, ypoint, xpoint - _marker_offset, ypoint);
166 draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint + 5.0);
167 draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint - 5.0);
170 draw_fixed_pointer(_marker_offset + marker_xe, text_y + _w / 6,
171 _marker_offset + marker_xs, text_y, _marker_offset + marker_xe,
175 } // if (option_left())
178 // draw capping lines and pointers
179 if (option_right()) {
182 draw_line(_x, _y, _x, marker_ye);
184 marker_xe = _x + _w / 3.0;
186 // draw_line(_x, _center_y + _w / 6, marker_xe, _center_y);
187 // draw_line(_x, _center_y - _w / 6, marker_xe, _center_y);
190 if (_pointer_type == MOVING) {
191 float ycentre, ypoint, xpoint;
194 if (_input.min() >= 0.0)
196 else if (_input.max() + _input.min() == 0.0)
199 ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min());
201 ycentre = _y + _input.min() * _h / (_input.max() - _input.min());
206 ypoint = ycentre + ((value - 1.0) * range / _val_span);
208 ypoint = ycentre + (value * range / _val_span);
210 xpoint = _x - _marker_offset;
211 draw_line(xpoint, ycentre, xpoint, ypoint);
212 draw_line(xpoint, ypoint, xpoint + _marker_offset, ypoint);
213 draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint + 5.0);
214 draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint - 5.0);
217 draw_fixed_pointer(-_marker_offset + _x, text_y + _w / 6,
218 -_marker_offset + marker_xe, text_y, -_marker_offset + _x,
222 } // if (option_right())
225 // At this point marker x_start and x_end values are transposed.
226 // To keep this from confusing things they are now swapped.
228 marker_ye = marker_xs, marker_xs = marker_xe, marker_xe = marker_ye;
232 // Work through from bottom to top of scale. Calculating where to put
233 // minor and major ticks.
235 // draw scale or tape
236 float vstart = floorf(vmin / _major_divs) * _major_divs;
237 float min_diff = _w / 6.0; // length difference between major & minor tick
239 // FIXME consider oddtype
240 for (int i = 0; ; i++) {
241 float v = vstart + i * _minor_divs;
244 if (v < _input.min())
246 else if (v > _input.max())
249 float y = _y + (v - vmin) * factor();
256 if (_div_ratio && i % _div_ratio) { // minor div
258 if (_tick_type == LINE) {
259 if (_tick_length == VARIABLE) {
260 draw_line(_x, y, marker_xs, y);
261 draw_line(marker_xe, y, right, y);
263 draw_line(_x, y, marker_xs, y);
264 draw_line(marker_xe, y, right, y);
267 } else { // _tick_type == CIRCLE
268 draw_bullet(_x, y, 3.0);
271 } else if (option_left()) {
272 if (_tick_type == LINE) {
273 if (_tick_length == VARIABLE) {
274 draw_line(marker_xs + min_diff, y, marker_xe, y);
276 draw_line(marker_xs, y, marker_xe, y);
278 } else { // _tick_type == CIRCLE
279 draw_bullet(marker_xs + 4, y, 3.0);
282 } else { // if (option_right())
283 if (_tick_type == LINE) {
284 if (_tick_length == VARIABLE) {
285 draw_line(marker_xs, y, marker_xe - min_diff, y);
287 draw_line(marker_xs, y, marker_xe, y);
290 } else { // _tick_type == CIRCLE
291 draw_bullet(marker_xe - 4, y, 3.0);
295 } else { // major div
297 v = fmodf(v + _modulo, _modulo);
303 if (_tick_type == LINE) {
304 draw_line(_x, y, marker_xs, y);
305 draw_line(marker_xs, y, right, y);
307 } else { // _tick_type == CIRCLE
308 draw_bullet(_x, y, 5.0);
311 x = marker_xs, align = CENTER;
314 if (_tick_type == LINE)
315 draw_line(marker_xs, y, marker_xe, y);
316 else // _tick_type == CIRCLE
317 draw_bullet(marker_xs + 4, y, 5.0);
320 x = marker_xs, align = RIGHT|VCENTER;
322 x = marker_xe, align = LEFT|VCENTER;
325 if (!option_notext()) {
326 char *s = format_value(v);
329 _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
331 if (b < _y || t > top)
334 if (_label_gap == 0.0
335 || b < _center_y - _label_gap && t < _center_y - _label_gap
336 || b > _center_y + _label_gap && t > _center_y + _label_gap) {
345 void HUD::Tape::draw_horizontal(float value)
347 float vmin = 0.0, vmax = 0.0;
355 float right = _x + _w;
359 vmin = value - _half_width_units; // width units == needle travel
360 vmax = value + _half_width_units; // or picture unit span.
363 } else if (_pointer_type == MOVING) {
368 vmin = value - _half_width_units; // width units == needle travel
369 vmax = value + _half_width_units; // or picture unit span.
375 draw_line(_x, _y, _x, top);
378 if (_draw_tick_right)
379 draw_line(right, _y, right, top);
381 marker_ys = _y; // Starting point for
382 marker_ye = top; // tick y location calcs
384 marker_xs = _x + ((value - vmin) * factor());
387 if (_draw_cap_bottom)
388 draw_line(_x, _y, right, _y);
391 marker_ye = _y + _h / 2;
393 // draw_line(_center_x, marker_ye, _center_x - _h / 4, _y);
394 // draw_line(_center_x, marker_ye, _center_x + _h / 4, _y);
397 if (_pointer_type == MOVING) {
398 float xcentre = _center_x;
400 float xpoint = xcentre + (value * range / _val_span);
401 float ypoint = _y - _marker_offset;
402 draw_line(xcentre, ypoint, xpoint, ypoint);
403 draw_line(xpoint, ypoint, xpoint, ypoint + _marker_offset);
404 draw_line(xpoint, ypoint + _marker_offset, xpoint + 5.0, ypoint + 5.0);
405 draw_line(xpoint, ypoint + _marker_offset, xpoint - 5.0, ypoint + 5.0);
408 draw_fixed_pointer(marker_xs - _h / 4, _y, marker_xs,
409 marker_ye, marker_xs + _h / 4, _y);
412 } // if (option_top())
414 if (option_bottom()) {
416 draw_line(_x, top, right, top);
419 marker_ys = top - _h / 2;
421 // draw_line(_center_x + _h / 4, _y + _h, _center_x, marker_ys);
422 // draw_line(_center_x - _h / 4, _y + _h, _center_x , marker_ys);
425 if (_pointer_type == MOVING) {
426 float xcentre = _center_x;
429 float xpoint = xcentre + (value * range / _val_span);
430 float ypoint = hgt + _marker_offset;
431 draw_line(xcentre, ypoint, xpoint, ypoint);
432 draw_line(xpoint, ypoint, xpoint, ypoint - _marker_offset);
433 draw_line(xpoint, ypoint - _marker_offset, xpoint + 5.0, ypoint - 5.0);
434 draw_line(xpoint, ypoint - _marker_offset, xpoint - 5.0, ypoint - 5.0);
437 draw_fixed_pointer(marker_xs + _h / 4, top, marker_xs, marker_ys,
438 marker_xs - _h / 4, top);
441 } // if (option_bottom())
444 float vstart = floorf(vmin / _major_divs) * _major_divs;
445 float min_diff = _h / 6.0; // length difference between major & minor tick
447 // FIXME consider oddtype
448 for (int i = 0; ; i++) {
449 float v = vstart + i * _minor_divs;
452 if (v < _input.min())
454 else if (v > _input.max())
457 float x = _x + (v - vmin) * factor();
464 if (_div_ratio && i % _div_ratio) { // minor div
466 if (_tick_length == VARIABLE) {
467 draw_line(x, _y, x, marker_ys - 4);
468 draw_line(x, marker_ye + 4, x, top);
470 draw_line(x, _y, x, marker_ys);
471 draw_line(x, marker_ye, x, top);
477 if (_tick_length == VARIABLE)
478 draw_line(x, marker_ys, x, marker_ye - min_diff);
480 draw_line(x, marker_ys, x, marker_ye);
482 } else if (_tick_length == VARIABLE) {
483 draw_line(x, marker_ys + 4, x, marker_ye);
485 draw_line(x, marker_ys, x, marker_ye);
489 } else { // major divs
491 v = fmodf(v + _modulo, _modulo);
497 draw_line(x, _y, x, marker_ye);
498 draw_line(x, marker_ye, x, _y + _h);
499 y = marker_ys, align = CENTER;
502 draw_line(x, marker_ys, x, marker_ye);
505 y = top, align = TOP|HCENTER;
507 y = _y, align = BOTTOM|HCENTER;
510 if (!option_notext()) {
511 char *s = format_value(v);
514 _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t);
516 if (l < _x || r > right)
519 if (_label_gap == 0.0
520 || l < _center_x - _label_gap && r < _center_x - _label_gap
521 || l > _center_x + _label_gap && r > _center_x + _label_gap) {
530 char *HUD::Tape::format_value(float v)
532 if (fabsf(v) < 1e-8) // avoid -0.0
535 if (_label_fmt == INT)
536 snprintf(_buf, BUFSIZE, _format.c_str(), int(v));
537 else if (_label_fmt == LONG)
538 snprintf(_buf, BUFSIZE, _format.c_str(), long(v));
539 else if (_label_fmt == FLOAT)
540 snprintf(_buf, BUFSIZE, _format.c_str(), v);
541 else // _label_fmt == DOUBLE
542 snprintf(_buf, BUFSIZE, _format.c_str(), double(v));
547 void HUD::Tape::draw_fixed_pointer(float x1, float y1, float x2, float y2, float x3, float y3)
549 glBegin(GL_LINE_STRIP);