1 // HUD.hxx -- Head Up Display
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 #include <simgear/compiler.h>
26 #include <simgear/props/condition.hxx>
41 #include <simgear/math/SGLimits.hxx>
42 #include <simgear/constants.h>
43 #include <simgear/structure/subsystem_mgr.hxx>
45 #include <Airports/runways.hxx> // FGRunway
46 #include <GUI/gui.h> // fntRenderer ? guiErrorMessage()
47 #include <GUI/new_gui.hxx> // FGFontCache, FGColor
48 #include <Main/fg_props.hxx>
56 ClipBox(const SGPropertyNode *, float xoffset = 0, float yoffset = 0);
63 SGConstPropertyNode_ptr _top_node;
64 SGConstPropertyNode_ptr _bot_node;
65 SGConstPropertyNode_ptr _left_node;
66 SGConstPropertyNode_ptr _right_node;
77 LineSegment(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
78 : _x0(x0), _y0(y0), _x1(x1), _y1(y1) {}
86 GLfloat _x0, _y0, _x1, _y1;
93 void add(const LineSegment& seg) { _list.push_back(seg); }
94 void erase() { _list.erase(_list.begin(), _list.end()); }
95 inline unsigned int size() const { return _list.size(); }
98 vector<LineSegment>::const_iterator it, end = _list.end();
99 for (it = _list.begin(); it != end; ++it)
105 vector<LineSegment> _list;
112 HUDText(fntRenderer *f, float x, float y, const char *s, int align = 0, int digits = 0);
119 static const int BUFSIZE = 64;
127 TextList() { _font = 0; }
129 void setFont(fntRenderer *Renderer) { _font = Renderer; }
130 void add(float x, float y, const char *s, int align = 0, int digit = 0) {
131 _list.push_back(HUDText(_font, x, y, s, align, digit));
133 void erase() { _list.erase(_list.begin(), _list.end()); }
134 void align(const char *s, int align, float *x, float *y,
135 float *l, float *r, float *b, float *t) const;
140 vector<HUDText> _list;
148 class HUD : public SGSubsystem, public SGPropertyChangeListener {
157 // called from Main/renderer.cxx to draw 2D and 3D HUD
158 void draw(osg::State&);
160 // listener callback to read various HUD related properties
161 void valueChanged(SGPropertyNode *);
162 // set current glColor
163 void setColor() const;
164 inline bool isVisible() const { return _visible; }
165 inline bool isAntialiased() const { return _antialiased; }
166 inline bool isTransparent() const { return _transparent; }
167 inline bool is3D() const { return _3Denabled; }
168 inline float alphaClamp() const { return _cl; }
169 inline double timer() const { return _timer; }
170 static void textAlign(fntRenderer *rend, const char *s, int align, float *x, float *y,
171 float *l, float *r, float *b, float *t);
173 enum Units { FEET, METER };
174 Units getUnits() const { return _units; }
177 HORIZONTAL = 0x0000, // keep that at zero?
187 // for alignment (with LEFT, RIGHT, TOP, BOTTOM)
190 CENTER = (HCENTER|VCENTER),
195 int load(const char *, float x = 320.0f, float y = 240.0f,
196 int level = 0, const std::string& indent = "");
202 void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
204 void currentColorChanged();
213 class TurnBankIndicator;
218 deque<Item *> _items;
219 deque<Item *> _ladders;
221 SGPropertyNode_ptr _currentPath;
222 SGPropertyNode_ptr _currentColor;
223 SGPropertyNode_ptr _visibility;
224 SGPropertyNode_ptr _3DenabledN;
225 SGPropertyNode_ptr _antialiasing;
226 SGPropertyNode_ptr _transparency;
227 SGPropertyNode_ptr _red, _green, _blue, _alpha;
228 SGPropertyNode_ptr _alpha_clamp;
229 SGPropertyNode_ptr _brightness;
234 float _r, _g, _b, _a, _cl;
236 SGPropertyNode_ptr _scr_widthN, _scr_heightN;
237 int _scr_width, _scr_height;
238 SGPropertyNode_ptr _unitsN;
242 fntRenderer *_font_renderer;
243 FGFontCache *_font_cache;
247 bool _listener_active;
252 LineList _stipple_line_list;
259 Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
260 float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
263 _damped(SGLimitsf::max())
267 _factor = n->getFloatValue("factor", factor);
268 _offset = n->getFloatValue("offset", offset);
269 _min = n->getFloatValue("min", min);
270 _max = n->getFloatValue("max", max);
271 _coeff = 1.0 - 1.0 / powf(10, fabs(n->getFloatValue("damp", 0.0)));
272 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
274 const char *path = p->getStringValue();
275 if (path && path[0]) {
276 _property = fgGetNode(path, true);
282 bool getBoolValue() const {
284 return _property->getBoolValue();
287 const char *getStringValue() const {
289 return _property->getStringValue();
292 float getFloatValue() {
294 float f = _property->getFloatValue() * _factor + _offset;
295 if (_damped == SGLimitsf::max())
298 f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
302 inline float isValid() const { return _valid; }
303 inline float min() const { return _min; }
304 inline float max() const { return _max; }
305 inline float factor() const { return _factor; }
306 float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
308 void set_min(float m, bool force = true) {
309 if (force || _min == -SGLimitsf::max())
312 void set_max(float m, bool force = true) {
313 if (force || _max == SGLimitsf::max())
319 SGConstPropertyNode_ptr _property;
332 Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
334 virtual void draw() = 0;
335 virtual bool isEnabled();
348 Format check_format(const char *) const;
349 inline float get_span() const { return _scr_span; }
350 inline int get_digits() const { return _digits; }
352 inline bool option_vert() const { return (_options & VERTICAL) == VERTICAL; }
353 inline bool option_left() const { return (_options & LEFT) == LEFT; }
354 inline bool option_right() const { return (_options & RIGHT) == RIGHT; }
355 inline bool option_both() const { return (_options & BOTH) == BOTH; }
356 inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
357 inline bool option_notext() const { return (_options & NOTEXT) == NOTEXT; }
358 inline bool option_top() const { return (_options & TOP) == TOP; }
359 inline bool option_bottom() const { return (_options & BOTTOM) == BOTTOM; }
361 void draw_line(float x1, float y1, float x2, float y2);
362 void draw_stipple_line(float x1, float y1, float x2, float y2);
363 void draw_text(float x, float y, const char *msg, int align = 0, int digit = 0);
364 void draw_circle(float x1, float y1, float r) const;
365 void draw_arc(float x1, float y1, float t0, float t1, float r) const;
366 void draw_bullet(float, float, float);
371 float _x, _y, _w, _h;
372 float _center_x, _center_y;
375 SGSharedPtr<SGCondition> _condition;
376 float _disp_factor; // Multiply by to get numbers shown on scale.
377 float _scr_span; // Working values for draw;
383 class HUD::Label : public Item {
385 Label(HUD *parent, const SGPropertyNode *, float x, float y);
394 int _halign; // HUDText alignment
398 float _pointer_width;
399 float _pointer_length;
401 SGSharedPtr<SGCondition> _blink_condition;
402 double _blink_interval;
403 double _blink_target; // time for next blink state change
409 // abstract base class for both moving scale and moving needle (fixed scale)
412 class HUD::Scale : public Item {
414 Scale(HUD *parent, const SGPropertyNode *, float x, float y);
415 virtual void draw ( void ) {} // No-op here. Defined in derived classes.
418 inline float factor() const { return _display_factor; }
419 inline float range_to_show() const { return _range_shown; }
422 float _major_divs; // major division marker units
423 float _minor_divs; // minor division marker units
424 unsigned int _modulo; // Roll over point
427 float _range_shown; // Width Units.
428 float _display_factor; // factor => screen units/range values.
432 class HUD::Gauge : public Scale {
434 Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
440 // displays the indicated quantity on a scale that moves past the
441 // pointer. It may be horizontal or vertical.
443 class HUD::Tape : public Scale {
445 Tape(HUD *parent, const SGPropertyNode *, float x, float y);
449 void draw_vertical(float);
450 void draw_horizontal(float);
451 void draw_fixed_pointer(float, float, float, float, float, float);
452 char *format_value(float);
456 float _half_width_units;
457 bool _draw_tick_bottom;
459 bool _draw_tick_right;
460 bool _draw_tick_left;
461 bool _draw_cap_bottom;
463 bool _draw_cap_right;
465 float _marker_offset;
471 int _div_ratio; // _major_divs/_minor_divs
472 bool _odd_type; // whether to put numbers at 0/2/4 or 1/3/5
474 enum { BUFSIZE = 64 };
477 enum PointerType { FIXED, MOVING } _pointer_type;
478 enum TickType { LINE, CIRCLE } _tick_type;
479 enum TickLength { VARIABLE, CONSTANT } _tick_length;
484 class HUD::Dial : public Scale {
486 Dial(HUD *parent, const SGPropertyNode *, float x, float y);
496 class HUD::TurnBankIndicator : public Item {
498 TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
504 void draw_line(float, float, float, float);
505 void draw_tick(float angle, float r1, float r2, int side);
516 class HUD::Ladder : public Item {
518 Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
523 void draw_zenith(float, float);
524 void draw_nadir(float, float);
526 void draw_text(float x, float y, const char *s, int align = 0) {
527 _locTextList.add(x, y, s, align, 0);
530 void draw_line(float x1, float y1, float x2, float y2, bool stipple = false) {
532 _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
534 _locLineList.add(LineSegment(x1, y1, x2, y2));
537 enum Type { PITCH, CLIMB_DIVE } _type;
543 float _zero_bar_overlength;
544 bool _dive_bar_angle;
549 bool _dynamic_origin;
550 bool _frl; // fuselage reference line
552 bool _target_markers;
553 bool _velocity_vector;
557 bool _climb_dive_marker;
558 bool _glide_slope_marker;
561 bool _waypoint_marker;
567 // The Ladder has its own temporary display lists
568 TextList _locTextList;
569 LineList _locLineList;
570 LineList _locStippleLineList;
575 // responsible for rendering the active runway in the hud (if visible).
577 class HUD::Runway : public Item {
579 Runway(HUD *parent, const SGPropertyNode *, float x, float y);
583 void boundPoint(const sgdVec3& v, sgdVec3& m);
584 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
585 bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
587 FGRunway* get_active_runway();
588 void get_rwy_points(sgdVec3 *points);
591 SGPropertyNode_ptr _agl;
592 sgdVec3 _points3d[6], _points2d[6];
595 double _arrow_scale; // scales of runway indication arrow
596 double _arrow_radius;
597 double _line_scale; // maximum line scale
598 double _scale_dist; // distance where to start scaling the lines
599 double _default_pitch;
600 double _default_heading;
603 unsigned short _stipple_out; // stipple pattern of the outline of the runway
604 unsigned short _stipple_center; // stipple pattern of the center line of the runway
605 bool _draw_arrow; // draw arrow when runway is not visible in HUD
606 bool _draw_arrow_always; // always draws arrow
607 float _left, _right, _top, _bottom;
611 class HUD::AimingReticle : public Item {
613 AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
617 SGSharedPtr<SGCondition> _active_condition; // stadiametric (true) or standby (false)
618 SGSharedPtr<SGCondition> _tachy_condition; // tachymetric (true) or standby (false)
619 SGSharedPtr<SGCondition> _align_condition; // tachymetric (true) or standby (false)
621 Input _diameter; // inner/outer radius relation