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>
37 SG_USING_NAMESPACE(std);
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 <Include/fg_typedefs.h>
49 #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(float x, float y, char *s, int d = 0) : _x(x), _y(y), _digits(d) {
95 strncpy(_msg, s, BUFSIZE);
98 // this code is changed to display Numbers with big/small digits
99 // according to MIL Standards for example Altitude above 10000 ft
100 // is shown as 10ooo.
102 void draw(fntRenderer *fnt) {
103 float orig_size = fnt->getPointSize();
104 if (!_digits) { // show all digits in same size
105 fnt->setPointSize(orig_size * 0.8);
106 fnt->start2f(_x, _y);
109 fnt->setPointSize(orig_size);
118 //if negative value then increase the c and p values
120 c++; // was moved to the comment. Unintentionally? TODO
124 while (tmp[i] != '\0') {
125 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
130 fnt->setPointSize(orig_size * 0.8);
132 char *tmp1 = _msg + p1;
135 fnt->start2f(_x + p2, _y);
138 fnt->setPointSize(orig_size * 1.2);
140 strncpy(tmp2, _msg, p1);
143 fnt->start2f(_x, _y);
146 fnt->setPointSize(orig_size * 1.2);
147 fnt->start2f(_x, _y);
150 fnt->setPointSize(orig_size);
156 static const int BUFSIZE = 64;
164 TextList() { _font = 0; }
166 void setFont(fntRenderer *Renderer) { _font = Renderer; }
167 void add(const HUDText& String) { _list.push_back(String); }
168 void erase() { _list.erase(_list.begin(), _list.end()); }
173 glPushAttrib(GL_COLOR_BUFFER_BIT);
177 vector<HUDText>::iterator it, end = _list.end();
178 for (it = _list.begin(); it != end; ++it)
182 glDisable(GL_TEXTURE_2D);
188 vector<HUDText> _list;
196 class HUD : public SGSubsystem, public SGPropertyChangeListener {
208 float top, bottom, left, right;
211 // called from Main/renderer.cxx to draw 2D and 3D HUD
214 // listener callback to read various HUD related properties
215 void valueChanged(SGPropertyNode *);
216 // set current glColor
217 void setColor() const;
218 inline bool isVisible() const { return _visible; }
219 inline bool isAntialiased() const { return _antialiased; }
220 inline bool isTransparent() const { return _transparent; }
221 inline bool is3D() const { return _3Denabled; }
222 inline float alphaClamp() const { return _cl; }
223 inline double timer() const { return _timer; }
225 enum Units { FEET, METER };
226 Units getUnits() const { return _units; }
251 int load(const char *, float x = 320.0f, float y = 240.0f,
252 int level = 0, const string& indent = "");
256 void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
265 class TurnBankIndicator;
269 deque<Item *> _items;
271 SGPropertyNode_ptr _current;
272 SGPropertyNode_ptr _visibility;
273 SGPropertyNode_ptr _3DenabledN;
274 SGPropertyNode_ptr _antialiasing;
275 SGPropertyNode_ptr _transparency;
276 SGPropertyNode_ptr _red, _green, _blue, _alpha;
277 SGPropertyNode_ptr _alpha_clamp;
278 SGPropertyNode_ptr _brightness;
283 float _r, _g, _b, _a, _cl;
285 SGPropertyNode_ptr _scr_widthN, _scr_heightN;
286 int _scr_width, _scr_height;
287 SGPropertyNode_ptr _unitsN;
291 fntRenderer *_font_renderer;
292 FGFontCache *_font_cache;
299 LineList _stipple_line_list;
306 Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
307 float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
310 _damped(SGLimitsf::max())
314 _factor = n->getFloatValue("factor", factor);
315 _offset = n->getFloatValue("offset", offset);
316 _min = n->getFloatValue("min", min);
317 _max = n->getFloatValue("max", max);
318 _coeff = n->getFloatValue("damp", 0.0);
319 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
321 const char *path = p->getStringValue();
322 if (path && path[0]) {
323 _property = fgGetNode(path, true);
329 const char *getStringValue() const {
331 return _property->getStringValue();
334 float getFloatValue() {
336 float f = _property->getFloatValue() * _factor + _offset;
337 if (_damped == SGLimitsf::max())
340 f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
341 return f < _min ? _min : f > _max ? _max : f;
344 inline float isValid() const { return _valid; }
345 inline float min() const { return _min; }
346 inline float max() const { return _max; }
347 inline float factor() const { return _factor; }
349 void set_min(float m, bool force = true) {
350 if (force || _min == -SGLimitsf::max())
353 void set_max(float m, bool force = true) {
354 if (force || _max == SGLimitsf::max())
360 SGConstPropertyNode_ptr _property;
371 class HUD::Item { // An Abstract Base Class (ABC)
373 Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
375 virtual void draw() = 0;
376 virtual bool isEnabled();
380 inline Rect get_location() const { return _scrn_pos; }
381 inline float get_span() const { return _scr_span; }
382 inline Point get_centroid() const { return _mid_span; }
383 inline int get_digits() const { return _digits; }
385 inline float get_x() const { return _scrn_pos.left; }
386 inline float get_y() const { return _scrn_pos.top; }
387 inline float get_width() const { return _scrn_pos.right; }
388 inline float get_height() const { return _scrn_pos.bottom; }
390 inline bool option_vert() const { return _options & VERT; }
391 inline bool option_left() const { return _options & LEFT; }
392 inline bool option_right() const { return _options & RIGHT; }
393 inline bool option_both() const { return (_options & BOTH) == BOTH; }
394 inline bool option_noticks() const { return _options & NOTICKS; }
395 inline bool option_notext() const { return _options & NOTEXT; }
396 inline bool option_top() const { return _options & TOP; }
397 inline bool option_bottom() const { return _options & BOTTOM; }
399 void draw_line( float x1, float y1, float x2, float y2) {
400 _hud->_line_list.add(LineSegment(x1, y1, x2, y2));
403 void draw_stipple_line( float x1, float y1, float x2, float y2) {
404 _hud->_stipple_line_list.add(LineSegment(x1, y1, x2, y2));
407 void draw_text( float x, float y, char *msg, int digit) {
408 _hud->_text_list.add(HUDText(x, y, msg, digit));
411 float text_width(char *str) const {
412 assert(_hud->_font_renderer);
414 _hud->_font->getBBox(str, _hud->_font_size, 0, &l, &r, 0, 0);
418 void draw_circle(float x1, float y1, float r) const {
419 glBegin(GL_LINE_LOOP);
420 for (int count = 0; count < 25; count++) {
421 float cosine = r * cos(count * 2 * SG_PI / 10.0);
422 float sine = r * sin(count * 2 * SG_PI / 10.0);
423 glVertex2f(cosine + x1, sine + y1);
433 SGCondition *_condition;
434 Rect _scrn_pos; // Framing - affects scale dimensions
435 // and orientation. Vert vs Horz, etc.
436 float _disp_factor; // Multiply by to get numbers shown on scale.
437 float _scr_span; // Working values for draw;
444 class HUD::Label : public Item {
446 Label(HUD *parent, const SGPropertyNode *, float x, float y);
465 Format check_format(const char *) const;
476 SGCondition *_blink_condition;
477 double _blink_interval;
478 double _blink_target; // time for next blink state change
484 // abstract base class for both moving scale and moving needle (fixed scale)
487 class HUD::Scale : public Item {
489 Scale(HUD *parent, const SGPropertyNode *, float x, float y);
490 virtual void draw ( void ) {} // No-op here. Defined in derived classes.
493 inline unsigned int modulo() const { return _modulo; }
494 inline float factor() const { return scale_factor; }
495 inline float range_to_show() const { return _range_shown; }
498 unsigned int _major_divs; // major division marker units
499 unsigned int _minor_divs; // minor division marker units
502 float _range_shown; // Width Units.
503 float scale_factor; // factor => screen units/range values.
504 unsigned int _modulo; // Roll over point
508 class HUD::Gauge : public Scale {
510 Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
516 // displays the indicated quantity on a scale that moves past the
517 // pointer. It may be horizontal or vertical.
519 class HUD::Tape : public Scale {
521 Tape(HUD *parent, const SGPropertyNode *, float x, float y);
525 void circle(float, float, float);
526 void fixed(float, float, float, float, float, float);
527 void zoomed_scale(int, int);
531 float half_width_units;
532 bool draw_tick_bottom;
534 bool draw_tick_right;
536 bool draw_cap_bottom;
550 class HUD::Dial : public Scale {
552 Dial(HUD *parent, const SGPropertyNode *, float x, float y);
562 class HUD::TurnBankIndicator : public Item {
564 TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
573 float _bank_scale_radius;
578 class HUD::Ladder : public Item {
580 Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
584 void draw_zenith(float, float, float);
585 void draw_nadir(float, float, float);
587 void draw_text(float x, float y, char *s) {
588 _locTextList.add(HUDText(x, y, s));
591 void draw_line(float x1, float y1, float x2, float y2) {
592 _locLineList.add(LineSegment(x1, y1, x2, y2));
595 void draw_stipple_line(float x1, float y1, float x2, float y2) {
596 _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
601 enum Type { PITCH, CLIMB_DIVE } _type;
602 unsigned int width_units;
604 unsigned int minor_div;
605 unsigned int label_pos;
606 unsigned int _scr_hole;
612 bool _velocity_vector;
616 bool _climb_dive_marker;
617 bool _glide_slope_marker;
620 bool _waypoint_marker;
625 // The Ladder has its own temporary display lists
626 TextList _locTextList;
627 LineList _locLineList;
628 LineList _locStippleLineList;
633 // responsible for rendering the active runway in the hud (if visible).
635 class HUD::Runway : public Item {
637 Runway(HUD *parent, const SGPropertyNode *, float x, float y);
641 void boundPoint(const sgdVec3& v, sgdVec3& m);
642 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
643 bool drawLine(const sgdVec3& a1, const sgdVec3& a2,
644 const sgdVec3& p1, const sgdVec3& p2);
646 bool get_active_runway(FGRunway& rwy);
647 void get_rwy_points(sgdVec3 *points);
650 SGPropertyNode_ptr _agl;
651 sgdVec3 _points3d[6], _points2d[6];
654 double _arrow_scale; // scales of runway indication arrow
655 double _arrow_radius;
656 double _line_scale; // maximum line scale
657 double _scale_dist; // distance where to start scaling the lines
658 double _default_pitch;
659 double _default_heading;
662 FGViewer* _cockpit_view;
663 unsigned short _stipple_out; // stipple pattern of the outline of the runway
664 unsigned short _stipple_center; // stipple pattern of the center line of the runway
665 bool _draw_arrow; // draw arrow when runway is not visible in HUD
666 bool _draw_arrow_always; // always draws arrow