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>
58 LineSegment(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
59 : _x0(x0), _y0(y0), _x1(x1), _y1(y1) {}
67 GLfloat _x0, _y0, _x1, _y1;
74 void add(const LineSegment& seg) { _list.push_back(seg); }
75 void erase() { _list.erase(_list.begin(), _list.end()); }
76 inline unsigned int size() const { return _list.size(); }
79 vector<LineSegment>::const_iterator it, end = _list.end();
80 for (it = _list.begin(); it != end; ++it)
86 vector<LineSegment> _list;
93 HUDText(fntRenderer *f, float x, float y, const char *s, int align = 0, int digits = 0);
100 static const int BUFSIZE = 64;
108 TextList() { _font = 0; }
110 void setFont(fntRenderer *Renderer) { _font = Renderer; }
111 void add(float x, float y, const char *s, int align = 0, int digit = 0) {
112 _list.push_back(HUDText(_font, x, y, s, align, digit));
114 void erase() { _list.erase(_list.begin(), _list.end()); }
115 void align(const char *s, int align, float *x, float *y,
116 float *l, float *r, float *b, float *t) const;
121 vector<HUDText> _list;
129 class HUD : public SGSubsystem, public SGPropertyChangeListener {
136 // called from Main/renderer.cxx to draw 2D and 3D HUD
139 // listener callback to read various HUD related properties
140 void valueChanged(SGPropertyNode *);
141 // set current glColor
142 void setColor() const;
143 inline bool isVisible() const { return _visible; }
144 inline bool isAntialiased() const { return _antialiased; }
145 inline bool isTransparent() const { return _transparent; }
146 inline bool is3D() const { return _3Denabled; }
147 inline float alphaClamp() const { return _cl; }
148 inline double timer() const { return _timer; }
149 static void textAlign(fntRenderer *r, const char *s, int align, float *x, float *y,
150 float *l, float *r, float *b, float *t);
152 enum Units { FEET, METER };
153 Units getUnits() const { return _units; }
156 HORIZONTAL = 0x0000, // keep that at zero?
166 // for alignment (with LEFT, RIGHT, TOP, BOTTOM)
169 CENTER = (HCENTER|VCENTER),
174 int load(const char *, float x = 320.0f, float y = 240.0f,
175 int level = 0, const string& indent = "");
179 void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
188 class TurnBankIndicator;
193 deque<Item *> _items;
195 SGPropertyNode_ptr _current;
196 SGPropertyNode_ptr _visibility;
197 SGPropertyNode_ptr _3DenabledN;
198 SGPropertyNode_ptr _antialiasing;
199 SGPropertyNode_ptr _transparency;
200 SGPropertyNode_ptr _red, _green, _blue, _alpha;
201 SGPropertyNode_ptr _alpha_clamp;
202 SGPropertyNode_ptr _brightness;
207 float _r, _g, _b, _a, _cl;
209 SGPropertyNode_ptr _scr_widthN, _scr_heightN;
210 int _scr_width, _scr_height;
211 SGPropertyNode_ptr _unitsN;
215 fntRenderer *_font_renderer;
216 FGFontCache *_font_cache;
223 LineList _stipple_line_list;
230 Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
231 float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
234 _damped(SGLimitsf::max())
238 _factor = n->getFloatValue("factor", factor);
239 _offset = n->getFloatValue("offset", offset);
240 _min = n->getFloatValue("min", min);
241 _max = n->getFloatValue("max", max);
242 _coeff = 1.0 - 1.0 / powf(10, fabsf(n->getFloatValue("damp", 0.0)));
243 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
245 const char *path = p->getStringValue();
246 if (path && path[0]) {
247 _property = fgGetNode(path, true);
253 bool getBoolValue() const {
255 return _property->getBoolValue();
258 const char *getStringValue() const {
260 return _property->getStringValue();
263 float getFloatValue() {
265 float f = _property->getFloatValue() * _factor + _offset;
266 if (_damped == SGLimitsf::max())
269 f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
273 inline float isValid() const { return _valid; }
274 inline float min() const { return _min; }
275 inline float max() const { return _max; }
276 inline float factor() const { return _factor; }
277 float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
279 void set_min(float m, bool force = true) {
280 if (force || _min == -SGLimitsf::max())
283 void set_max(float m, bool force = true) {
284 if (force || _max == SGLimitsf::max())
290 SGConstPropertyNode_ptr _property;
303 Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
305 virtual void draw() = 0;
306 virtual bool isEnabled();
309 inline float get_span() const { return _scr_span; }
310 inline int get_digits() const { return _digits; }
312 inline bool option_vert() const { return (_options & VERTICAL) == VERTICAL; }
313 inline bool option_left() const { return (_options & LEFT) == LEFT; }
314 inline bool option_right() const { return (_options & RIGHT) == RIGHT; }
315 inline bool option_both() const { return (_options & BOTH) == BOTH; }
316 inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
317 inline bool option_notext() const { return (_options & NOTEXT) == NOTEXT; }
318 inline bool option_top() const { return (_options & TOP) == TOP; }
319 inline bool option_bottom() const { return (_options & BOTTOM) == BOTTOM; }
321 void draw_line(float x1, float y1, float x2, float y2);
322 void draw_stipple_line(float x1, float y1, float x2, float y2);
323 void draw_text(float x, float y, char *msg, int align = 0, int digit = 0);
324 void draw_circle(float x1, float y1, float r) const;
325 void draw_bullet(float, float, float);
330 float _x, _y, _w, _h;
331 float _center_x, _center_y;
334 SGCondition *_condition;
335 float _disp_factor; // Multiply by to get numbers shown on scale.
336 float _scr_span; // Working values for draw;
342 class HUD::Label : public Item {
344 Label(HUD *parent, const SGPropertyNode *, float x, float y);
358 Format check_format(const char *) const;
364 int _halign; // HUDText alignment
368 float _pointer_width;
369 float _pointer_length;
371 SGCondition *_blink_condition;
372 double _blink_interval;
373 double _blink_target; // time for next blink state change
379 // abstract base class for both moving scale and moving needle (fixed scale)
382 class HUD::Scale : public Item {
384 Scale(HUD *parent, const SGPropertyNode *, float x, float y);
385 virtual void draw ( void ) {} // No-op here. Defined in derived classes.
388 inline float factor() const { return _display_factor; }
389 inline float range_to_show() const { return _range_shown; }
392 float _major_divs; // major division marker units
393 float _minor_divs; // minor division marker units
394 unsigned int _modulo; // Roll over point
397 float _range_shown; // Width Units.
398 float _display_factor; // factor => screen units/range values.
402 class HUD::Gauge : public Scale {
404 Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
410 // displays the indicated quantity on a scale that moves past the
411 // pointer. It may be horizontal or vertical.
413 class HUD::Tape : public Scale {
415 Tape(HUD *parent, const SGPropertyNode *, float x, float y);
419 void draw_fixed_pointer(float, float, float, float, float, float);
420 void zoomed_scale(int, int);
424 float _half_width_units;
425 bool _draw_tick_bottom;
427 bool _draw_tick_right;
428 bool _draw_tick_left;
429 bool _draw_cap_bottom;
431 bool _draw_cap_right;
433 float _marker_offset;
438 enum PointerType { FIXED, MOVING } _pointer_type;
439 enum TickType { LINE, CIRCLE } _tick_type;
440 enum TickLength { VARIABLE, CONSTANT } _tick_length;
445 class HUD::Dial : public Scale {
447 Dial(HUD *parent, const SGPropertyNode *, float x, float y);
457 class HUD::TurnBankIndicator : public Item {
459 TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
465 void draw_line(float, float, float, float);
466 void draw_tick(float angle, float r1, float r2, int side);
477 class HUD::Ladder : public Item {
479 Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
483 void draw_zenith(float, float);
484 void draw_nadir(float, float);
486 void draw_text(float x, float y, char *s, int align = 0) {
487 _locTextList.add(x, y, s, align, 0);
490 void draw_line(float x1, float y1, float x2, float y2) {
491 _locLineList.add(LineSegment(x1, y1, x2, y2));
494 void draw_stipple_line(float x1, float y1, float x2, float y2) {
495 _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
498 enum Type { PITCH, CLIMB_DIVE } _type;
503 unsigned int _scr_hole;
507 bool _dynamic_origin;
509 bool _frl; // fuselage reference line
511 bool _target_markers;
512 bool _velocity_vector;
516 bool _climb_dive_marker;
517 bool _glide_slope_marker;
520 bool _waypoint_marker;
525 // The Ladder has its own temporary display lists
526 TextList _locTextList;
527 LineList _locLineList;
528 LineList _locStippleLineList;
533 // responsible for rendering the active runway in the hud (if visible).
535 class HUD::Runway : public Item {
537 Runway(HUD *parent, const SGPropertyNode *, float x, float y);
541 void boundPoint(const sgdVec3& v, sgdVec3& m);
542 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
543 bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
545 bool get_active_runway(FGRunway& rwy);
546 void get_rwy_points(sgdVec3 *points);
549 SGPropertyNode_ptr _agl;
550 sgdVec3 _points3d[6], _points2d[6];
553 double _arrow_scale; // scales of runway indication arrow
554 double _arrow_radius;
555 double _line_scale; // maximum line scale
556 double _scale_dist; // distance where to start scaling the lines
557 double _default_pitch;
558 double _default_heading;
561 FGViewer* _cockpit_view;
562 unsigned short _stipple_out; // stipple pattern of the outline of the runway
563 unsigned short _stipple_center; // stipple pattern of the center line of the runway
564 bool _draw_arrow; // draw arrow when runway is not visible in HUD
565 bool _draw_arrow_always; // always draws arrow
566 float _left, _right, _top, _bottom;
570 class HUD::AimingReticle : public Item {
572 AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
576 SGCondition *_active_condition; // stadiametric (true) or standby (false)
577 Input _diameter; // inner/outer radius relation