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>
43 #include <simgear/math/SGLimits.hxx>
44 #include <simgear/constants.h>
45 #include <simgear/structure/subsystem_mgr.hxx>
47 #include <Airports/runways.hxx> // FGRunway
48 #include <GUI/gui.h> // fntRenderer ? guiErrorMessage()
49 #include <GUI/new_gui.hxx> // FGFontCache, FGColor
50 #include <Include/fg_typedefs.h>
51 #include <Main/fg_props.hxx>
59 LineSegment(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
60 : _x0(x0), _y0(y0), _x1(x1), _y1(y1) {}
68 GLfloat _x0, _y0, _x1, _y1;
75 void add(const LineSegment& seg) { _list.push_back(seg); }
76 void erase() { _list.erase(_list.begin(), _list.end()); }
77 inline unsigned int size() const { return _list.size(); }
80 vector<LineSegment>::const_iterator it, end = _list.end();
81 for (it = _list.begin(); it != end; ++it)
87 vector<LineSegment> _list;
94 HUDText(fntRenderer *f, float x, float y, const char *s, int align = 0, int digits = 0);
101 static const int BUFSIZE = 64;
109 TextList() { _font = 0; }
111 void setFont(fntRenderer *Renderer) { _font = Renderer; }
112 void add(float x, float y, const char *s, int align = 0, int digit = 0) {
113 _list.push_back(HUDText(_font, x, y, s, align, digit));
115 void erase() { _list.erase(_list.begin(), _list.end()); }
116 void align(const char *s, int align, float *x, float *y,
117 float *l, float *r, float *b, float *t) const;
122 vector<HUDText> _list;
130 class HUD : public SGSubsystem, public SGPropertyChangeListener {
137 // called from Main/renderer.cxx to draw 2D and 3D HUD
138 void draw(osg::State&);
140 // listener callback to read various HUD related properties
141 void valueChanged(SGPropertyNode *);
142 // set current glColor
143 void setColor() const;
144 inline bool isVisible() const { return _visible; }
145 inline bool isAntialiased() const { return _antialiased; }
146 inline bool isTransparent() const { return _transparent; }
147 inline bool is3D() const { return _3Denabled; }
148 inline float alphaClamp() const { return _cl; }
149 inline double timer() const { return _timer; }
150 static void textAlign(fntRenderer *rend, const char *s, int align, float *x, float *y,
151 float *l, float *r, float *b, float *t);
153 enum Units { FEET, METER };
154 Units getUnits() const { return _units; }
157 HORIZONTAL = 0x0000, // keep that at zero?
167 // for alignment (with LEFT, RIGHT, TOP, BOTTOM)
170 CENTER = (HCENTER|VCENTER),
175 int load(const char *, float x = 320.0f, float y = 240.0f,
176 int level = 0, const std::string& indent = "");
180 void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
189 class TurnBankIndicator;
194 deque<Item *> _items;
196 SGPropertyNode_ptr _current;
197 SGPropertyNode_ptr _visibility;
198 SGPropertyNode_ptr _3DenabledN;
199 SGPropertyNode_ptr _antialiasing;
200 SGPropertyNode_ptr _transparency;
201 SGPropertyNode_ptr _red, _green, _blue, _alpha;
202 SGPropertyNode_ptr _alpha_clamp;
203 SGPropertyNode_ptr _brightness;
208 float _r, _g, _b, _a, _cl;
210 SGPropertyNode_ptr _scr_widthN, _scr_heightN;
211 int _scr_width, _scr_height;
212 SGPropertyNode_ptr _unitsN;
216 fntRenderer *_font_renderer;
217 FGFontCache *_font_cache;
224 LineList _stipple_line_list;
231 Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
232 float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
235 _damped(SGLimitsf::max())
239 _factor = n->getFloatValue("factor", factor);
240 _offset = n->getFloatValue("offset", offset);
241 _min = n->getFloatValue("min", min);
242 _max = n->getFloatValue("max", max);
243 _coeff = 1.0 - 1.0 / powf(10, fabsf(n->getFloatValue("damp", 0.0)));
244 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
246 const char *path = p->getStringValue();
247 if (path && path[0]) {
248 _property = fgGetNode(path, true);
254 bool getBoolValue() const {
256 return _property->getBoolValue();
259 const char *getStringValue() const {
261 return _property->getStringValue();
264 float getFloatValue() {
266 float f = _property->getFloatValue() * _factor + _offset;
267 if (_damped == SGLimitsf::max())
270 f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
274 inline float isValid() const { return _valid; }
275 inline float min() const { return _min; }
276 inline float max() const { return _max; }
277 inline float factor() const { return _factor; }
278 float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
280 void set_min(float m, bool force = true) {
281 if (force || _min == -SGLimitsf::max())
284 void set_max(float m, bool force = true) {
285 if (force || _max == SGLimitsf::max())
291 SGConstPropertyNode_ptr _property;
304 Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
306 virtual void draw() = 0;
307 virtual bool isEnabled();
320 Format check_format(const char *) const;
321 inline float get_span() const { return _scr_span; }
322 inline int get_digits() const { return _digits; }
324 inline bool option_vert() const { return (_options & VERTICAL) == VERTICAL; }
325 inline bool option_left() const { return (_options & LEFT) == LEFT; }
326 inline bool option_right() const { return (_options & RIGHT) == RIGHT; }
327 inline bool option_both() const { return (_options & BOTH) == BOTH; }
328 inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
329 inline bool option_notext() const { return (_options & NOTEXT) == NOTEXT; }
330 inline bool option_top() const { return (_options & TOP) == TOP; }
331 inline bool option_bottom() const { return (_options & BOTTOM) == BOTTOM; }
333 void draw_line(float x1, float y1, float x2, float y2);
334 void draw_stipple_line(float x1, float y1, float x2, float y2);
335 void draw_text(float x, float y, char *msg, int align = 0, int digit = 0);
336 void draw_circle(float x1, float y1, float r) const;
337 void draw_bullet(float, float, float);
342 float _x, _y, _w, _h;
343 float _center_x, _center_y;
346 SGSharedPtr<SGCondition> _condition;
347 float _disp_factor; // Multiply by to get numbers shown on scale.
348 float _scr_span; // Working values for draw;
354 class HUD::Label : public Item {
356 Label(HUD *parent, const SGPropertyNode *, float x, float y);
365 int _halign; // HUDText alignment
369 float _pointer_width;
370 float _pointer_length;
372 SGSharedPtr<SGCondition> _blink_condition;
373 double _blink_interval;
374 double _blink_target; // time for next blink state change
380 // abstract base class for both moving scale and moving needle (fixed scale)
383 class HUD::Scale : public Item {
385 Scale(HUD *parent, const SGPropertyNode *, float x, float y);
386 virtual void draw ( void ) {} // No-op here. Defined in derived classes.
389 inline float factor() const { return _display_factor; }
390 inline float range_to_show() const { return _range_shown; }
393 float _major_divs; // major division marker units
394 float _minor_divs; // minor division marker units
395 unsigned int _modulo; // Roll over point
398 float _range_shown; // Width Units.
399 float _display_factor; // factor => screen units/range values.
403 class HUD::Gauge : public Scale {
405 Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
411 // displays the indicated quantity on a scale that moves past the
412 // pointer. It may be horizontal or vertical.
414 class HUD::Tape : public Scale {
416 Tape(HUD *parent, const SGPropertyNode *, float x, float y);
420 void draw_vertical(float);
421 void draw_horizontal(float);
422 void draw_fixed_pointer(float, float, float, float, float, float);
423 char *format_value(float);
427 float _half_width_units;
428 bool _draw_tick_bottom;
430 bool _draw_tick_right;
431 bool _draw_tick_left;
432 bool _draw_cap_bottom;
434 bool _draw_cap_right;
436 float _marker_offset;
441 int _div_ratio; // _major_divs/_minor_divs
442 bool _odd_type; // whether to put numbers at 0/2/4 or 1/3/5
444 enum { BUFSIZE = 64 };
447 enum PointerType { FIXED, MOVING } _pointer_type;
448 enum TickType { LINE, CIRCLE } _tick_type;
449 enum TickLength { VARIABLE, CONSTANT } _tick_length;
454 class HUD::Dial : public Scale {
456 Dial(HUD *parent, const SGPropertyNode *, float x, float y);
466 class HUD::TurnBankIndicator : public Item {
468 TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
474 void draw_line(float, float, float, float);
475 void draw_tick(float angle, float r1, float r2, int side);
486 class HUD::Ladder : public Item {
488 Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
492 void draw_zenith(float, float);
493 void draw_nadir(float, float);
495 void draw_text(float x, float y, char *s, int align = 0) {
496 _locTextList.add(x, y, s, align, 0);
499 void draw_line(float x1, float y1, float x2, float y2) {
500 _locLineList.add(LineSegment(x1, y1, x2, y2));
503 void draw_stipple_line(float x1, float y1, float x2, float y2) {
504 _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
507 enum Type { PITCH, CLIMB_DIVE } _type;
512 unsigned int _scr_hole;
516 bool _dynamic_origin;
518 bool _frl; // fuselage reference line
520 bool _target_markers;
521 bool _velocity_vector;
525 bool _climb_dive_marker;
526 bool _glide_slope_marker;
529 bool _waypoint_marker;
534 // The Ladder has its own temporary display lists
535 TextList _locTextList;
536 LineList _locLineList;
537 LineList _locStippleLineList;
542 // responsible for rendering the active runway in the hud (if visible).
544 class HUD::Runway : public Item {
546 Runway(HUD *parent, const SGPropertyNode *, float x, float y);
550 void boundPoint(const sgdVec3& v, sgdVec3& m);
551 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
552 bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
554 bool get_active_runway(FGRunway& rwy);
555 void get_rwy_points(sgdVec3 *points);
558 SGPropertyNode_ptr _agl;
559 sgdVec3 _points3d[6], _points2d[6];
562 double _arrow_scale; // scales of runway indication arrow
563 double _arrow_radius;
564 double _line_scale; // maximum line scale
565 double _scale_dist; // distance where to start scaling the lines
566 double _default_pitch;
567 double _default_heading;
570 FGViewer* _cockpit_view;
571 unsigned short _stipple_out; // stipple pattern of the outline of the runway
572 unsigned short _stipple_center; // stipple pattern of the center line of the runway
573 bool _draw_arrow; // draw arrow when runway is not visible in HUD
574 bool _draw_arrow_always; // always draws arrow
575 float _left, _right, _top, _bottom;
579 class HUD::AimingReticle : public Item {
581 AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
585 SGSharedPtr<SGCondition> _active_condition; // stadiametric (true) or standby (false)
586 Input _diameter; // inner/outer radius relation