| 1 | /* |
| 2 | * SPDX-FileName: HUD_tape.cxx |
| 3 | * SPDX-FileComment: HUD Tape Instrument |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 1997 Michele F. America [micheleamerica#geocities:com] |
| 5 | * SPDX-FileContributor: Copyright (C) 2006 Melchior FRANZ [mfranz#aon:at] |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "HUD.hxx" |
| 10 | #include "HUD_private.hxx" |
| 11 | |
| 12 | static const float TICK_OFFSET = 2.f; |
| 13 | |
| 14 | |
| 15 | HUD::Tape::Tape(HUD *hud, const SGPropertyNode *n, float x, float y) : |
| 16 | Scale(hud, n, x, y), |
| 17 | _draw_tick_bottom(n->getBoolValue("tick-bottom", false)), |
| 18 | _draw_tick_top(n->getBoolValue("tick-top", false)), |
| 19 | _draw_tick_right(n->getBoolValue("tick-right", false)), |
| 20 | _draw_tick_left(n->getBoolValue("tick-left", false)), |
| 21 | _draw_cap_bottom(n->getBoolValue("cap-bottom", false)), |
| 22 | _draw_cap_top(n->getBoolValue("cap-top", false)), |
| 23 | _draw_cap_right(n->getBoolValue("cap-right", false)), |
| 24 | _draw_cap_left(n->getBoolValue("cap-left", false)), |
| 25 | _marker_offset(n->getFloatValue("marker-offset")), |
| 26 | _label_offset(n->getFloatValue("label-offset", 3.0)), |
| 27 | _label_gap(n->getFloatValue("label-gap-width") / 2.0), |
| 28 | _pointer(n->getBoolValue("enable-pointer", true)), |
| 29 | _format(n->getStringValue("format", "%d")) |
| 30 | { |
| 31 | _half_width_units = range_to_show() / 2.0; |
| 32 | |
| 33 | std::string s = n->getStringValue("pointer-type"); |
| 34 | _pointer_type = s != "moving" ? FIXED : MOVING; // "fixed", "moving" |
| 35 | |
| 36 | s = n->getStringValue("tick-type"); |
| 37 | _tick_type = s != "bullet" ? LINE : CIRCLE; // "bullet", "line" |
| 38 | |
| 39 | s = n->getStringValue("tick-length"); // "variable", "constant" |
| 40 | _tick_length = s != "constant" ? VARIABLE : CONSTANT; |
| 41 | |
| 42 | _label_fmt = check_format(_format.c_str()); |
| 43 | if (_label_fmt != INT && _label_fmt != LONG |
| 44 | && _label_fmt != FLOAT && _label_fmt != DOUBLE) { |
| 45 | SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid <format> '" << _format.c_str() |
| 46 | << "' in <tape> '" << _name << "' (must be number format)"); |
| 47 | _label_fmt = INT; |
| 48 | _format = "%d"; |
| 49 | } |
| 50 | |
| 51 | if (_minor_divs != 0.0f) |
| 52 | _div_ratio = int(_major_divs / _minor_divs + 0.5f); |
| 53 | else |
| 54 | _div_ratio = 0, _minor_divs = _major_divs; |
| 55 | |
| 56 | // int k; //odd or even values for ticks // FIXME odd scale |
| 57 | _odd_type = false; |
| 58 | if (_input.max() + .5f < float(SGLimits<long>::max())) |
| 59 | _odd_type = long(floorf(_input.max() + 0.5f)) & 1 ? true : false; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void HUD::Tape::draw(void) // (HUD_scale * pscale) |
| 64 | { |
| 65 | if (!_input.isValid()) |
| 66 | return; |
| 67 | |
| 68 | float value = _input.getFloatValue(); |
| 69 | |
| 70 | if (option_vert()) |
| 71 | draw_vertical(value); |
| 72 | else |
| 73 | draw_horizontal(value); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void HUD::Tape::draw_vertical(float value) |
| 78 | { |
| 79 | float vmin = 0.0;//, vmax = 0.0; |
| 80 | float marker_xs; |
| 81 | float marker_xe; |
| 82 | float marker_ye; |
| 83 | float text_y = 0.0; |
| 84 | |
| 85 | float top = _y + _h; |
| 86 | float right = _x + _w; |
| 87 | |
| 88 | |
| 89 | if (!_pointer) { |
| 90 | vmin = value - _half_width_units; // width units == needle travel |
| 91 | // vmax = value + _half_width_units; // or picture unit span. |
| 92 | text_y = _center_y; |
| 93 | |
| 94 | } else if (_pointer_type == MOVING) { |
| 95 | vmin = _input.min(); |
| 96 | // vmax = _input.max(); |
| 97 | |
| 98 | } else { // FIXED |
| 99 | vmin = value - _half_width_units; // width units == needle travel |
| 100 | // vmax = value + _half_width_units; // or picture unit span. |
| 101 | text_y = _center_y; |
| 102 | } |
| 103 | |
| 104 | // Bottom tick bar |
| 105 | if (_draw_tick_bottom) |
| 106 | draw_line(_x, _y, right, _y); |
| 107 | |
| 108 | // Top tick bar |
| 109 | if (_draw_tick_top) |
| 110 | draw_line(_x, top, right, top); |
| 111 | |
| 112 | marker_xs = _x; // x start |
| 113 | marker_xe = right; // x extent |
| 114 | marker_ye = top; |
| 115 | |
| 116 | // We do not use else in the following so that combining the |
| 117 | // two options produces a "caged" display with double |
| 118 | // carrots. The same is done for horizontal card indicators. |
| 119 | |
| 120 | // draw capping lines and pointers |
| 121 | if (option_left()) { // Calculate x marker offset |
| 122 | |
| 123 | if (_draw_cap_right) |
| 124 | draw_line(marker_xe, _y, marker_xe, marker_ye); |
| 125 | |
| 126 | marker_xs = marker_xe - _w / 3.0; |
| 127 | |
| 128 | // draw_line(marker_xs, _center_y, marker_xe, _center_y + _w / 6); |
| 129 | // draw_line(marker_xs, _center_y, marker_xe, _center_y - _w / 6); |
| 130 | |
| 131 | if (_pointer) { |
| 132 | if (_pointer_type == MOVING) { |
| 133 | float ycentre, ypoint, xpoint; |
| 134 | float range, right; |
| 135 | |
| 136 | if (_input.min() >= 0.0) |
| 137 | ycentre = _y; |
| 138 | else if (_input.max() + _input.min() == 0.0) |
| 139 | ycentre = _center_y; |
| 140 | else if (_odd_type) |
| 141 | ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min()); |
| 142 | else |
| 143 | ycentre = _y + _input.min() * _h / (_input.max() - _input.min()); |
| 144 | |
| 145 | range = _h; |
| 146 | right = _x + _w; |
| 147 | |
| 148 | if (_odd_type) |
| 149 | ypoint = ycentre + ((value - 1.0) * range / _val_span); |
| 150 | else |
| 151 | ypoint = ycentre + (value * range / _val_span); |
| 152 | |
| 153 | xpoint = right + _marker_offset; |
| 154 | draw_line(xpoint, ycentre, xpoint, ypoint); |
| 155 | draw_line(xpoint, ypoint, xpoint - _marker_offset, ypoint); |
| 156 | draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint + 5.0); |
| 157 | draw_line(xpoint - _marker_offset, ypoint, xpoint - 5.0, ypoint - 5.0); |
| 158 | |
| 159 | } else { // FIXED |
| 160 | draw_fixed_pointer(_marker_offset + marker_xe, text_y + _w / 6, |
| 161 | _marker_offset + marker_xs, text_y, _marker_offset + marker_xe, |
| 162 | text_y - _w / 6); |
| 163 | } |
| 164 | } |
| 165 | } // if (option_left()) |
| 166 | |
| 167 | |
| 168 | // draw capping lines and pointers |
| 169 | if (option_right()) { |
| 170 | |
| 171 | if (_draw_cap_left) |
| 172 | draw_line(_x, _y, _x, marker_ye); |
| 173 | |
| 174 | marker_xe = _x + _w / 3.0; |
| 175 | // Indicator carrot |
| 176 | // draw_line(_x, _center_y + _w / 6, marker_xe, _center_y); |
| 177 | // draw_line(_x, _center_y - _w / 6, marker_xe, _center_y); |
| 178 | |
| 179 | if (_pointer) { |
| 180 | if (_pointer_type == MOVING) { |
| 181 | float ycentre, ypoint, xpoint; |
| 182 | float range; |
| 183 | |
| 184 | if (_input.min() >= 0.0) |
| 185 | ycentre = _y; |
| 186 | else if (_input.max() + _input.min() == 0.0) |
| 187 | ycentre = _center_y; |
| 188 | else if (_odd_type) |
| 189 | ycentre = _y + (1.0 - _input.min()) * _h / (_input.max() - _input.min()); |
| 190 | else |
| 191 | ycentre = _y + _input.min() * _h / (_input.max() - _input.min()); |
| 192 | |
| 193 | range = _h; |
| 194 | |
| 195 | if (_odd_type) |
| 196 | ypoint = ycentre + ((value - 1.0) * range / _val_span); |
| 197 | else |
| 198 | ypoint = ycentre + (value * range / _val_span); |
| 199 | |
| 200 | xpoint = _x - _marker_offset; |
| 201 | draw_line(xpoint, ycentre, xpoint, ypoint); |
| 202 | draw_line(xpoint, ypoint, xpoint + _marker_offset, ypoint); |
| 203 | draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint + 5.0); |
| 204 | draw_line(xpoint + _marker_offset, ypoint, xpoint + 5.0, ypoint - 5.0); |
| 205 | |
| 206 | } else { // FIXED |
| 207 | draw_fixed_pointer(-_marker_offset + _x, text_y + _w / 6, |
| 208 | -_marker_offset + marker_xe, text_y, -_marker_offset + _x, |
| 209 | text_y - _w / 6); |
| 210 | } |
| 211 | } // if (_pointer) |
| 212 | } // if (option_right()) |
| 213 | |
| 214 | |
| 215 | // At this point marker x_start and x_end values are transposed. |
| 216 | // To keep this from confusing things they are now swapped. |
| 217 | if (option_both()) |
| 218 | marker_ye = marker_xs, marker_xs = marker_xe, marker_xe = marker_ye; |
| 219 | |
| 220 | |
| 221 | |
| 222 | // Work through from bottom to top of scale. Calculating where to put |
| 223 | // minor and major ticks. |
| 224 | |
| 225 | // draw scale or tape |
| 226 | float vstart = floorf(vmin / _major_divs) * _major_divs; |
| 227 | float min_diff = _w / 6.0; // length difference between major & minor tick |
| 228 | |
| 229 | // FIXME consider oddtype |
| 230 | for (int i = 0; ; i++) { |
| 231 | float v = vstart + i * _minor_divs; |
| 232 | |
| 233 | if (!_modulo) { |
| 234 | if (v < _input.min()) |
| 235 | continue; |
| 236 | else if (v > _input.max()) |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | float y = _y + (v - vmin) * factor(); |
| 241 | |
| 242 | if (y < _y + TICK_OFFSET) |
| 243 | continue; |
| 244 | if (y > top - TICK_OFFSET) |
| 245 | break; |
| 246 | |
| 247 | if (_div_ratio && i % _div_ratio) { // minor div |
| 248 | if (option_both()) { |
| 249 | if (_tick_type == LINE) { |
| 250 | if (_tick_length == VARIABLE) { |
| 251 | draw_line(_x, y, marker_xs, y); |
| 252 | draw_line(marker_xe, y, right, y); |
| 253 | } else { |
| 254 | draw_line(_x, y, marker_xs, y); |
| 255 | draw_line(marker_xe, y, right, y); |
| 256 | } |
| 257 | |
| 258 | } else { // _tick_type == CIRCLE |
| 259 | draw_bullet(_x, y, 3.0); |
| 260 | } |
| 261 | |
| 262 | } else if (option_left()) { |
| 263 | if (_tick_type == LINE) { |
| 264 | if (_tick_length == VARIABLE) { |
| 265 | draw_line(marker_xs + min_diff, y, marker_xe, y); |
| 266 | } else { |
| 267 | draw_line(marker_xs, y, marker_xe, y); |
| 268 | } |
| 269 | } else { // _tick_type == CIRCLE |
| 270 | draw_bullet(marker_xs + 4, y, 3.0); |
| 271 | } |
| 272 | |
| 273 | } else { // if (option_right()) |
| 274 | if (_tick_type == LINE) { |
| 275 | if (_tick_length == VARIABLE) { |
| 276 | draw_line(marker_xs, y, marker_xe - min_diff, y); |
| 277 | } else { |
| 278 | draw_line(marker_xs, y, marker_xe, y); |
| 279 | } |
| 280 | |
| 281 | } else { // _tick_type == CIRCLE |
| 282 | draw_bullet(marker_xe - 4, y, 3.0); |
| 283 | } |
| 284 | } // end huds both |
| 285 | |
| 286 | } else { // major div |
| 287 | if (_modulo) |
| 288 | v = fmodf(v + _modulo, _modulo); |
| 289 | |
| 290 | float x; |
| 291 | int align; |
| 292 | |
| 293 | if (option_both()) { |
| 294 | if (_tick_type == LINE) { |
| 295 | draw_line(_x, y, marker_xs, y); |
| 296 | draw_line(marker_xs, y, right, y); |
| 297 | |
| 298 | } else { // _tick_type == CIRCLE |
| 299 | draw_bullet(_x, y, 5.0); |
| 300 | } |
| 301 | |
| 302 | x = marker_xs, align = CENTER; |
| 303 | |
| 304 | } else { |
| 305 | if (_tick_type == LINE) |
| 306 | draw_line(marker_xs, y, marker_xe, y); |
| 307 | else // _tick_type == CIRCLE |
| 308 | draw_bullet(marker_xs + 4, y, 5.0); |
| 309 | |
| 310 | if (option_left()) |
| 311 | x = marker_xs - _label_offset, align = RIGHT|VCENTER; |
| 312 | else |
| 313 | x = marker_xe + _label_offset, align = LEFT|VCENTER; |
| 314 | } |
| 315 | |
| 316 | if (!option_notext()) { |
| 317 | char *s = format_value(v); |
| 318 | |
| 319 | float l, r, b, t; |
| 320 | _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t); |
| 321 | |
| 322 | if (b < _y || t > top) |
| 323 | continue; |
| 324 | |
| 325 | if (_label_gap == 0.0 |
| 326 | || (b < _center_y - _label_gap && t < _center_y - _label_gap) |
| 327 | || (b > _center_y + _label_gap && t > _center_y + _label_gap)) { |
| 328 | draw_text(x, y, s); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } // for |
| 333 | } |
| 334 | |
| 335 | |
| 336 | void HUD::Tape::draw_horizontal(float value) |
| 337 | { |
| 338 | float vmin = 0.0;//, vmax = 0.0; |
| 339 | float marker_xs; |
| 340 | // float marker_xe; |
| 341 | float marker_ys; |
| 342 | float marker_ye; |
| 343 | // float text_y = 0.0; |
| 344 | |
| 345 | float top = _y + _h; |
| 346 | float right = _x + _w; |
| 347 | |
| 348 | |
| 349 | if (!_pointer) { |
| 350 | vmin = value - _half_width_units; // width units == needle travel |
| 351 | // vmax = value + _half_width_units; // or picture unit span. |
| 352 | // text_y = _center_y; |
| 353 | |
| 354 | } else if (_pointer_type == MOVING) { |
| 355 | vmin = _input.min(); |
| 356 | // vmax = _input.max(); |
| 357 | |
| 358 | } else { // FIXED |
| 359 | vmin = value - _half_width_units; // width units == needle travel |
| 360 | // vmax = value + _half_width_units; // or picture unit span. |
| 361 | // text_y = _center_y; |
| 362 | } |
| 363 | |
| 364 | // left tick bar |
| 365 | if (_draw_tick_left) |
| 366 | draw_line(_x, _y, _x, top); |
| 367 | |
| 368 | // right tick bar |
| 369 | if (_draw_tick_right) |
| 370 | draw_line(right, _y, right, top); |
| 371 | |
| 372 | marker_ys = _y; // Starting point for |
| 373 | marker_ye = top; // tick y location calcs |
| 374 | // marker_xe = right; |
| 375 | marker_xs = _x + ((value - vmin) * factor()); |
| 376 | |
| 377 | if (option_top()) { |
| 378 | if (_draw_cap_bottom) |
| 379 | draw_line(_x, _y, right, _y); |
| 380 | |
| 381 | // Tick point adjust |
| 382 | marker_ye = _y + _h / 2; |
| 383 | // Bottom arrow |
| 384 | // draw_line(_center_x, marker_ye, _center_x - _h / 4, _y); |
| 385 | // draw_line(_center_x, marker_ye, _center_x + _h / 4, _y); |
| 386 | // draw pointer |
| 387 | if (_pointer) { |
| 388 | if (_pointer_type == MOVING) { |
| 389 | float xcentre = _center_x; |
| 390 | float range = _w; |
| 391 | float xpoint = xcentre + (value * range / _val_span); |
| 392 | float ypoint = _y - _marker_offset; |
| 393 | draw_line(xcentre, ypoint, xpoint, ypoint); |
| 394 | draw_line(xpoint, ypoint, xpoint, ypoint + _marker_offset); |
| 395 | draw_line(xpoint, ypoint + _marker_offset, xpoint + 5.0, ypoint + 5.0); |
| 396 | draw_line(xpoint, ypoint + _marker_offset, xpoint - 5.0, ypoint + 5.0); |
| 397 | |
| 398 | } else { // FIXED |
| 399 | draw_fixed_pointer(marker_xs - _h / 4, _y, marker_xs, |
| 400 | marker_ye, marker_xs + _h / 4, _y); |
| 401 | } |
| 402 | } |
| 403 | } // if (option_top()) |
| 404 | |
| 405 | if (option_bottom()) { |
| 406 | if (_draw_cap_top) |
| 407 | draw_line(_x, top, right, top); |
| 408 | |
| 409 | // Tick point adjust |
| 410 | marker_ys = top - _h / 2; |
| 411 | // Top arrow |
| 412 | // draw_line(_center_x + _h / 4, _y + _h, _center_x, marker_ys); |
| 413 | // draw_line(_center_x - _h / 4, _y + _h, _center_x , marker_ys); |
| 414 | |
| 415 | if (_pointer) { |
| 416 | if (_pointer_type == MOVING) { |
| 417 | float xcentre = _center_x; |
| 418 | float range = _w; |
| 419 | float hgt = _y + _h; |
| 420 | float xpoint = xcentre + (value * range / _val_span); |
| 421 | float ypoint = hgt + _marker_offset; |
| 422 | draw_line(xcentre, ypoint, xpoint, ypoint); |
| 423 | draw_line(xpoint, ypoint, xpoint, ypoint - _marker_offset); |
| 424 | draw_line(xpoint, ypoint - _marker_offset, xpoint + 5.0, ypoint - 5.0); |
| 425 | draw_line(xpoint, ypoint - _marker_offset, xpoint - 5.0, ypoint - 5.0); |
| 426 | |
| 427 | } else { // FIXED |
| 428 | draw_fixed_pointer(marker_xs + _h / 4, top, marker_xs, marker_ys, |
| 429 | marker_xs - _h / 4, top); |
| 430 | } |
| 431 | } |
| 432 | } // if (option_bottom()) |
| 433 | |
| 434 | |
| 435 | float vstart = floorf(vmin / _major_divs) * _major_divs; |
| 436 | float min_diff = _h / 6.0; // length difference between major & minor tick |
| 437 | |
| 438 | // FIXME consider oddtype |
| 439 | for (int i = 0; ; i++) { |
| 440 | float v = vstart + i * _minor_divs; |
| 441 | |
| 442 | if (!_modulo) { |
| 443 | if (v < _input.min()) |
| 444 | continue; |
| 445 | else if (v > _input.max()) |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | float x = _x + (v - vmin) * factor(); |
| 450 | |
| 451 | if (x < _x + TICK_OFFSET) |
| 452 | continue; |
| 453 | if (x > right - TICK_OFFSET) |
| 454 | break; |
| 455 | |
| 456 | if (_div_ratio && i % _div_ratio) { // minor div |
| 457 | if (option_both()) { |
| 458 | if (_tick_length == VARIABLE) { |
| 459 | draw_line(x, _y, x, marker_ys - 4); |
| 460 | draw_line(x, marker_ye + 4, x, top); |
| 461 | } else { |
| 462 | draw_line(x, _y, x, marker_ys); |
| 463 | draw_line(x, marker_ye, x, top); |
| 464 | } |
| 465 | |
| 466 | } else { |
| 467 | if (option_top()) { |
| 468 | // draw minor ticks |
| 469 | if (_tick_length == VARIABLE) |
| 470 | draw_line(x, marker_ys, x, marker_ye - min_diff); |
| 471 | else |
| 472 | draw_line(x, marker_ys, x, marker_ye); |
| 473 | |
| 474 | } else if (_tick_length == VARIABLE) { |
| 475 | draw_line(x, marker_ys + 4, x, marker_ye); |
| 476 | } else { |
| 477 | draw_line(x, marker_ys, x, marker_ye); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | } else { // major divs |
| 482 | if (_modulo) |
| 483 | v = fmodf(v + _modulo, _modulo); |
| 484 | |
| 485 | float y; |
| 486 | int align; |
| 487 | |
| 488 | if (option_both()) { |
| 489 | draw_line(x, _y, x, marker_ye); |
| 490 | draw_line(x, marker_ye, x, _y + _h); |
| 491 | y = marker_ys, align = CENTER; |
| 492 | |
| 493 | } else { |
| 494 | draw_line(x, marker_ys, x, marker_ye); |
| 495 | |
| 496 | if (option_top()) |
| 497 | y = top - _label_offset, align = TOP|HCENTER; |
| 498 | else |
| 499 | y = _y + _label_offset, align = BOTTOM|HCENTER; |
| 500 | } |
| 501 | |
| 502 | if (!option_notext()) { |
| 503 | char *s = format_value(v); |
| 504 | |
| 505 | float l, r, b, t; |
| 506 | _hud->_text_list.align(s, align, &x, &y, &l, &r, &b, &t); |
| 507 | |
| 508 | if (l < _x || r > right) |
| 509 | continue; |
| 510 | |
| 511 | if (_label_gap == 0.0 |
| 512 | || (l < _center_x - _label_gap && r < _center_x - _label_gap) |
| 513 | || (l > _center_x + _label_gap && r > _center_x + _label_gap)) { |
| 514 | draw_text(x, y, s); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } // for |
| 519 | } |
| 520 | |
| 521 | |
| 522 | char *HUD::Tape::format_value(float v) |
| 523 | { |
| 524 | if (fabs(v) < 1e-8) // avoid -0.0 |
| 525 | v = 0.0f; |
| 526 | |
| 527 | if (_label_fmt == INT) |
| 528 | snprintf(_buf, BUFSIZE, _format.c_str(), int(v)); |
| 529 | else if (_label_fmt == LONG) |
| 530 | snprintf(_buf, BUFSIZE, _format.c_str(), long(v)); |
| 531 | else if (_label_fmt == FLOAT) |
| 532 | snprintf(_buf, BUFSIZE, _format.c_str(), v); |
| 533 | else // _label_fmt == DOUBLE |
| 534 | snprintf(_buf, BUFSIZE, _format.c_str(), double(v)); |
| 535 | return _buf; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | void HUD::Tape::draw_fixed_pointer(float x1, float y1, float x2, float y2, float x3, float y3) |
| 540 | { |
| 541 | glBegin(GL_LINE_STRIP); |
| 542 | glVertex2f(x1, y1); |
| 543 | glVertex2f(x2, y2); |
| 544 | glVertex2f(x3, y3); |
| 545 | glEnd(); |
| 546 | } |
| 547 | |
| 548 |