| 1 | /* |
| 2 | * SPDX-FileName: HUD_instrument.cxx |
| 3 | * SPDX-FileComment: HUD Common Instrument Base |
| 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 | #ifdef HAVE_CONFIG_H |
| 10 | # include <config.h> |
| 11 | #endif |
| 12 | |
| 13 | #include <simgear/math/SGLimits.hxx> |
| 14 | #include <simgear/props/condition.hxx> |
| 15 | |
| 16 | #include "HUD.hxx" |
| 17 | #include "HUD_private.hxx" |
| 18 | |
| 19 | #include <Main/globals.hxx> |
| 20 | |
| 21 | using std::vector; |
| 22 | |
| 23 | HUD::Item::Item(HUD *hud, const SGPropertyNode *n, float x, float y) : |
| 24 | _hud(hud), |
| 25 | _name(n->getStringValue("name", "[unnamed]")), |
| 26 | _options(0), |
| 27 | _condition(0), |
| 28 | _digits(n->getIntValue("digits")) |
| 29 | { |
| 30 | const SGPropertyNode *node = n->getNode("condition"); |
| 31 | if (node) |
| 32 | _condition = sgReadCondition(globals->get_props(), node); |
| 33 | |
| 34 | _x = n->getFloatValue("x") + x; |
| 35 | _y = n->getFloatValue("y") + y; |
| 36 | _w = n->getFloatValue("width"); |
| 37 | _h = n->getFloatValue("height"); |
| 38 | |
| 39 | vector<SGPropertyNode_ptr> opt = n->getChildren("option"); |
| 40 | for (unsigned int i = 0; i < opt.size(); i++) { |
| 41 | std::string o = opt[i]->getStringValue(); |
| 42 | if (o == "vertical") |
| 43 | _options |= VERTICAL; |
| 44 | else if (o == "horizontal") |
| 45 | _options |= HORIZONTAL; |
| 46 | else if (o == "top") |
| 47 | _options |= TOP; |
| 48 | else if (o == "left") |
| 49 | _options |= LEFT; |
| 50 | else if (o == "bottom") |
| 51 | _options |= BOTTOM; |
| 52 | else if (o == "right") |
| 53 | _options |= RIGHT; |
| 54 | else if (o == "both") |
| 55 | _options |= (LEFT|RIGHT); |
| 56 | else if (o == "noticks") |
| 57 | _options |= NOTICKS; |
| 58 | else if (o == "notext") |
| 59 | _options |= NOTEXT; |
| 60 | else |
| 61 | SG_LOG(SG_INPUT, SG_WARN, "HUD: unsupported option: " << o); |
| 62 | } |
| 63 | |
| 64 | // Set up convenience values for centroid of the box and |
| 65 | // the span values according to orientation |
| 66 | |
| 67 | if (_options & VERTICAL) { |
| 68 | _scr_span = _h; |
| 69 | } else { |
| 70 | _scr_span = _w; |
| 71 | } |
| 72 | |
| 73 | _center_x = _x + _w / 2.0; |
| 74 | _center_y = _y + _h / 2.0; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | bool HUD::Item::isEnabled() |
| 79 | { |
| 80 | return _condition ? _condition->test() : true; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void HUD::Item::draw_line(float x1, float y1, float x2, float y2) |
| 85 | { |
| 86 | _hud->_line_list.add(LineSegment(x1, y1, x2, y2)); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void HUD::Item::draw_stipple_line(float x1, float y1, float x2, float y2) |
| 91 | { |
| 92 | _hud->_stipple_line_list.add(LineSegment(x1, y1, x2, y2)); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void HUD::Item::draw_text(float x, float y, const char *msg, int align, int digit) |
| 97 | { |
| 98 | _hud->_text_list.add(x, y, msg, align, digit); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void HUD::Item::draw_circle(float xoffs, float yoffs, float r) const |
| 103 | { |
| 104 | float step = SG_PI / r; |
| 105 | double prevX = r; |
| 106 | double prevY = 0.0; |
| 107 | for (float alpha = step; alpha < SG_PI * 2.0; alpha += step) { |
| 108 | float x = r * cos(alpha); |
| 109 | float y = r * sin(alpha); |
| 110 | _hud->_line_list.add(LineSegment(prevX + xoffs, prevY + yoffs, |
| 111 | x + xoffs, y + yoffs)); |
| 112 | prevX = x; |
| 113 | prevY = y; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void HUD::Item::draw_arc(float xoffs, float yoffs, float t0, float t1, float r) const |
| 118 | { |
| 119 | float step = SG_PI / r; |
| 120 | t0 = t0 * SG_DEGREES_TO_RADIANS; |
| 121 | t1 = t1 * SG_DEGREES_TO_RADIANS; |
| 122 | |
| 123 | double prevX = r * cos(t0); |
| 124 | double prevY = r * sin(t0); |
| 125 | for (float alpha = t0 + step; alpha < t1; alpha += step) { |
| 126 | float x = r * cos(alpha); |
| 127 | float y = r * sin(alpha); |
| 128 | _hud->_line_list.add(LineSegment(prevX + xoffs, prevY + yoffs, |
| 129 | x + xoffs, y + yoffs)); |
| 130 | prevX = x; |
| 131 | prevY = y; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void HUD::Item::draw_bullet(float x, float y, float size) |
| 136 | { |
| 137 | glEnable(GL_POINT_SMOOTH); |
| 138 | glPointSize(size); |
| 139 | |
| 140 | glBegin(GL_POINTS); |
| 141 | glVertex2f(x, y); |
| 142 | glEnd(); |
| 143 | |
| 144 | glPointSize(1.0); |
| 145 | glDisable(GL_POINT_SMOOTH); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | // make sure the format matches '[ -+#]?\d*(\.\d*)?(l?[df]|s)' |
| 150 | // |
| 151 | HUD::Item::Format HUD::Item::check_format(const char *f) const |
| 152 | { |
| 153 | bool l = false; |
| 154 | Format fmt = STRING; |
| 155 | |
| 156 | for (; *f; f++) { |
| 157 | if (*f == '%') { |
| 158 | if (f[1] == '%') |
| 159 | f++; |
| 160 | else |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | if (*f++ != '%') |
| 165 | return NONE; |
| 166 | if (*f == ' ' || *f == '+' || *f == '-' || *f == '#') |
| 167 | f++; |
| 168 | while (*f && isdigit(*f)) |
| 169 | f++; |
| 170 | if (*f == '.') { |
| 171 | f++; |
| 172 | while (*f && isdigit(*f)) |
| 173 | f++; |
| 174 | } |
| 175 | if (*f == 'l') |
| 176 | l = true, f++; |
| 177 | |
| 178 | if (*f == 'd') |
| 179 | fmt = l ? LONG : INT; |
| 180 | else if (*f == 'f') |
| 181 | fmt = l ? DOUBLE : FLOAT; |
| 182 | else if (*f == 's') { |
| 183 | if (l) |
| 184 | return INVALID; |
| 185 | fmt = STRING; |
| 186 | } else |
| 187 | return INVALID; |
| 188 | |
| 189 | for (++f; *f; f++) { |
| 190 | if (*f == '%') { |
| 191 | if (f[1] == '%') |
| 192 | f++; |
| 193 | else |
| 194 | return INVALID; |
| 195 | } |
| 196 | } |
| 197 | return fmt; |
| 198 | } |
| 199 | |
| 200 |