]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.hxx
Vivian MEAZZA & Tim MOORE:
[flightgear.git] / src / Cockpit / panel.hxx
1 //  panel.hxx - generic support classes for a 2D panel.
2 //
3 //  Written by David Megginson, started January 2000.
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License as
7 //  published by the Free Software Foundation; either version 2 of the
8 //  License, or (at your option) any later version.
9 // 
10 //  This program is distributed in the hope that it will be useful, but
11 //  WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 //  $Id$
20
21 #ifndef __PANEL_HXX
22 #define __PANEL_HXX
23
24 #ifndef __cplusplus
25 # error This library requires C++
26 #endif
27
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #ifdef HAVE_WINDOWS_H
34 #  include <windows.h>
35 #endif
36
37 #include <osg/ref_ptr>
38 #include <osg/StateSet>
39 #include <osg/Texture2D>
40
41 #include <plib/fnt.h>
42
43 #include <simgear/compiler.h>
44 #include <simgear/props/props.hxx>
45 #include <simgear/structure/subsystem_mgr.hxx>
46 #include <simgear/math/interpolater.hxx>
47 #include <simgear/timing/timestamp.hxx>
48
49 #include <cmath>
50 #include <vector>
51 #include <map>
52
53 #include <Main/fg_props.hxx>
54 #include <Input/input.hxx>
55 #include <Instrumentation/dclgps.hxx>
56
57 SG_USING_STD(vector);
58 SG_USING_STD(map);
59
60
61 class FGPanelInstrument;
62
63
64 \f
65 ////////////////////////////////////////////////////////////////////////
66 // Texture management.
67 ////////////////////////////////////////////////////////////////////////
68
69
70 /**
71  * Texture manager (should migrate out into FGFS).
72  *
73  * This class ensures that no texture is loaded more than once.
74  */
75 class FGTextureManager
76 {
77 public:
78   static osg::Texture2D* createTexture(const string &relativePath);
79   static void addTexture(const string &relativePath, osg::Texture2D* texture);
80 private:
81   static map<string,osg::ref_ptr<osg::Texture2D> > _textureMap;
82 };
83
84
85 /**
86  * Cropped texture (should migrate out into FGFS).
87  *
88  * This structure wraps an SSG texture with cropping information.
89  */
90 class FGCroppedTexture
91 {
92 public:
93
94   FGCroppedTexture ();
95   FGCroppedTexture (const string &path,
96                   float _minX = 0.0, float _minY = 0.0,
97                   float _maxX = 1.0, float _maxY = 1.0);
98   virtual ~FGCroppedTexture ();
99
100   virtual void setPath (const string &path) { _path = path; }
101
102   virtual const string &getPath () const { return _path; }
103
104   virtual osg::StateSet* getTexture ();
105
106   virtual void setCrop (float minX, float minY, float maxX, float maxY) {
107     _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
108   }
109
110   virtual float getMinX () const { return _minX; }
111   virtual float getMinY () const { return _minY; }
112   virtual float getMaxX () const { return _maxX; }
113   virtual float getMaxY () const { return _maxY; }
114
115
116 private:
117   string _path;
118   osg::ref_ptr<osg::StateSet> _texture;
119   float _minX, _minY, _maxX, _maxY;
120 };
121
122
123 \f
124 ////////////////////////////////////////////////////////////////////////
125 // Top-level panel.
126 ////////////////////////////////////////////////////////////////////////
127
128
129 /**
130  * Instrument panel class.
131  *
132  * The panel is a container that has a background texture and holds
133  * zero or more instruments.  The panel will order the instruments to
134  * redraw themselves when necessary, and will pass mouse clicks on to
135  * the appropriate instruments for processing.
136  */
137 class FGPanel : public SGSubsystem
138 {
139 public:
140
141   FGPanel ();
142   virtual ~FGPanel ();
143
144                                 // Update the panel (every frame).
145   virtual void init ();
146   virtual void bind ();
147   virtual void unbind ();
148   virtual void draw (osg::State& state);
149   virtual void update (double);
150   void update (osg::State& state);
151   virtual void update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
152
153   virtual void updateMouseDelay();
154
155                                 // transfer pointer ownership!!!
156   virtual void addInstrument (FGPanelInstrument * instrument);
157
158                                 // Background texture.
159   virtual void setBackground (osg::Texture2D* texture);
160
161                                 // Background multiple textures.
162   virtual void setMultiBackground (osg::Texture2D* texture, int idx);
163
164                                 // Make the panel visible or invisible.
165   virtual bool getVisibility () const;
166   virtual void setVisibility (bool visibility);
167
168                                 // Full width of panel.
169   virtual void setWidth (int width) { _width = width; }
170   virtual int getWidth () const { return _width; }
171
172                                 // Full height of panel.
173   virtual void setHeight (int height) { _height = height; }
174   virtual int getHeight () const { return _height; }
175
176                                 // X-offset
177   virtual void setXOffset (int offset);
178   virtual int getXOffset () const { return _x_offset->getIntValue(); }
179
180                                 // Y-offset.
181   virtual void setYOffset (int offset);
182   virtual int getYOffset () const { return _y_offset->getIntValue(); }
183
184                                 // View height.
185   virtual void setViewHeight (int height) { _view_height = height; }
186   virtual int getViewHeight () const { return _view_height; }
187
188                                 // Handle a mouse click.
189   virtual bool doMouseAction (int button, int updown, int x, int y);
190   virtual bool doLocalMouseAction(int button, int updown, int x, int y);
191
192   virtual void setDepthTest (bool enable);
193
194 private:
195   void setupVirtualCockpit();
196   void cleanupVirtualCockpit();
197
198   mutable bool _mouseDown;
199   mutable int _mouseButton, _mouseX, _mouseY;
200   mutable int _mouseDelay;
201   mutable FGPanelInstrument * _mouseInstrument;
202   typedef vector<FGPanelInstrument *> instrument_list_type;
203   int _width;
204   int _height;
205   int _view_height;
206
207   SGPropertyNode_ptr _visibility;
208   SGPropertyNode_ptr _x_offset;
209   SGPropertyNode_ptr _y_offset;
210   SGPropertyNode_ptr _jitter;
211   SGPropertyNode_ptr _flipx;
212
213   SGConstPropertyNode_ptr _xsize_node;
214   SGConstPropertyNode_ptr _ysize_node;
215   
216   osg::ref_ptr<osg::StateSet> _bg;
217   osg::ref_ptr<osg::StateSet> _mbg[8];
218                                 // List of instruments in panel.
219   instrument_list_type _instruments;
220   bool _enable_depth_test;
221 };
222
223
224 \f
225 ////////////////////////////////////////////////////////////////////////
226 // Actions
227 ////////////////////////////////////////////////////////////////////////
228
229
230 /**
231  * Class for user actions.
232  *
233  * The actions are command bindings, like bindings for the keyboard
234  * or joystick, but they are tied to specific mouse actions in
235  * rectangular areas of the panel.
236  */
237 class FGPanelAction : public SGConditional
238 {
239 public:
240   FGPanelAction ();
241   FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
242   virtual ~FGPanelAction ();
243
244                                 // Getters.
245   virtual int getButton () const { return _button; }
246   virtual int getX () const { return _x; }
247   virtual int getY () const { return _y; }
248   virtual int getWidth () const { return _w; }
249   virtual int getHeight () const { return _h; }
250
251                                 // Setters.
252
253                                 // transfer pointer ownership
254   virtual void addBinding (SGBinding * binding, int updown);
255   virtual void setButton (int button) { _button = button; }
256   virtual void setX (int x) { _x = x; }
257   virtual void setY (int y) { _y = y; }
258   virtual void setWidth (int w) { _w = w; }
259   virtual void setHeight (int h) { _h = h; }
260
261                                 // Check whether we're in the area.
262   virtual bool inArea (int button, int x, int y)
263   {
264     return (button == _button &&
265             x >= _x &&
266             x < _x + _w &&
267             y >= _y &&
268             y < _y + _h);
269   }
270
271                                 // Perform the action.
272   virtual bool doAction (int updown);
273
274 private:
275   typedef vector<SGBinding *> binding_list_t;
276
277   int _button;
278   int _x;
279   int _y;
280   int _w;
281   int _h;
282   bool _repeatable;
283   int _last_state;
284   binding_list_t _bindings[2];
285 };
286
287
288 \f
289 ////////////////////////////////////////////////////////////////////////
290 // Transformations.
291 ////////////////////////////////////////////////////////////////////////
292
293
294 /**
295  * A transformation for a layer.
296  */
297 class FGPanelTransformation : public SGConditional
298 {
299 public:
300
301   enum Type {
302     XSHIFT,
303     YSHIFT,
304     ROTATION
305   };
306
307   FGPanelTransformation ();
308   virtual ~FGPanelTransformation ();
309
310   Type type;
311   SGConstPropertyNode_ptr node;
312   float min;
313   float max;
314   bool has_mod;
315   float mod;
316   float factor;
317   float offset;
318   SGInterpTable * table;
319 };
320
321
322
323 \f
324 ////////////////////////////////////////////////////////////////////////
325 // Layers
326 ////////////////////////////////////////////////////////////////////////
327
328
329 /**
330  * A single layer of a multi-layered instrument.
331  *
332  * Each layer can be subject to a series of transformations based
333  * on current FGFS instrument readings: for example, a texture
334  * representing a needle can rotate to show the airspeed.
335  */
336 class FGInstrumentLayer : public SGConditional
337 {
338 public:
339
340   FGInstrumentLayer (int w = -1, int h = -1);
341   virtual ~FGInstrumentLayer ();
342
343   virtual void draw (osg::State& state) = 0;
344   virtual void transform () const;
345
346   virtual int getWidth () const { return _w; }
347   virtual int getHeight () const { return _h; }
348   virtual void setWidth (int w) { _w = w; }
349   virtual void setHeight (int h) { _h = h; }
350
351                                 // Transfer pointer ownership!!
352                                 // DEPRECATED
353   virtual void addTransformation (FGPanelTransformation * transformation);
354
355 protected:
356   int _w, _h;
357
358   typedef vector<FGPanelTransformation *> transformation_list;
359   transformation_list _transformations;
360 };
361
362
363 \f
364 ////////////////////////////////////////////////////////////////////////
365 // Instruments.
366 ////////////////////////////////////////////////////////////////////////
367
368
369 /**
370  * Abstract base class for a panel instrument.
371  *
372  * A panel instrument consists of zero or more actions, associated
373  * with mouse clicks in rectangular areas.  Currently, the only
374  * concrete class derived from this is FGLayeredInstrument, but others
375  * may show up in the future (some complex instruments could be 
376  * entirely hand-coded, for example).
377  */
378 class FGPanelInstrument : public SGConditional
379 {
380 public:
381   FGPanelInstrument ();
382   FGPanelInstrument (int x, int y, int w, int h);
383   virtual ~FGPanelInstrument ();
384
385   virtual void draw (osg::State& state) = 0;
386   virtual void drawHotspots(osg::State& state);
387
388   virtual void setPosition(int x, int y);
389   virtual void setSize(int w, int h);
390
391   virtual int getXPos () const;
392   virtual int getYPos () const;
393   virtual int getWidth () const;
394   virtual int getHeight () const;
395
396                                 // Coordinates relative to centre.
397                                 // Transfer pointer ownership!!
398   virtual void addAction (FGPanelAction * action);
399
400                                 // Coordinates relative to centre.
401   virtual bool doMouseAction (int button, int updown, int x, int y);
402
403 protected:
404   int _x, _y, _w, _h;
405   typedef vector<FGPanelAction *> action_list_type;
406   action_list_type _actions;
407 };
408
409
410 /**
411  * An instrument constructed of multiple layers.
412  *
413  * Each individual layer can be rotated or shifted to correspond
414  * to internal FGFS instrument readings.
415  */
416 class FGLayeredInstrument : public FGPanelInstrument
417 {
418 public:
419   FGLayeredInstrument (int x, int y, int w, int h);
420   virtual ~FGLayeredInstrument ();
421
422   virtual void draw (osg::State& state);
423
424                                 // Transfer pointer ownership!!
425   virtual int addLayer (FGInstrumentLayer *layer);
426   virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
427
428                                 // Transfer pointer ownership!!
429   virtual void addTransformation (FGPanelTransformation * transformation);
430
431 protected:
432   typedef vector<FGInstrumentLayer *> layer_list;
433   layer_list _layers;
434 };
435
436
437 /**
438  * An empty-shell instrument that exists soley in
439  * order to redirect commands from the panel to a
440  * complex instrument inherited from SGSubsystem.
441  *
442  * Currently the only complex instrument is the KLN89,
443  * which we've hardwired this to for now.
444  */
445 class FGSpecialInstrument : public FGPanelInstrument
446 {
447 public:
448   FGSpecialInstrument(DCLGPS* sb);
449   //FGSpecialInstrument (int x, int y, int w, int h);
450   virtual ~FGSpecialInstrument ();
451
452   virtual void draw (osg::State& state);
453   
454 protected:
455   DCLGPS* complex;
456 };
457
458
459 /**
460  * An instrument layer containing a group of sublayers.
461  *
462  * This class is useful for gathering together a group of related
463  * layers, either to hold in an external file or to work under
464  * the same condition.
465  */
466 class FGGroupLayer : public FGInstrumentLayer
467 {
468 public:
469   FGGroupLayer ();
470   virtual ~FGGroupLayer ();
471   virtual void draw (osg::State& state);
472                                 // transfer pointer ownership
473   virtual void addLayer (FGInstrumentLayer * layer);
474 protected:
475   vector<FGInstrumentLayer *> _layers;
476 };
477
478
479 /**
480  * A textured layer of an instrument.
481  *
482  * This is a layer holding a single texture.  Normally, the texture's
483  * backgound should be transparent so that lower layers and the panel
484  * background can show through.
485  */
486 class FGTexturedLayer : public FGInstrumentLayer
487 {
488 public:
489   FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
490   FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
491   virtual ~FGTexturedLayer ();
492
493   virtual void draw (osg::State& state);
494
495   virtual void setTexture (const FGCroppedTexture &texture) {
496     _texture = texture;
497   }
498   virtual const FGCroppedTexture &getTexture () const { return _texture; }
499   virtual FGCroppedTexture *getTexture() { return &_texture; }
500
501   void setEmissive(bool e) { _emissive = e; }
502
503 private:
504   FGCroppedTexture _texture;
505   bool _emissive;
506 };
507
508
509 /**
510  * A text layer of an instrument.
511  *
512  * This is a layer holding a string of static and/or generated text.
513  * It is useful for instruments that have text displays, such as
514  * a chronometer, GPS, or NavCom radio.
515  */
516 class FGTextLayer : public FGInstrumentLayer
517 {
518 public:
519   enum ChunkType {
520     TEXT,
521     TEXT_VALUE,
522     DOUBLE_VALUE
523   };
524
525   class Chunk : public SGConditional
526   {
527   public:
528     Chunk (const string &text, const string &fmt = "%s");
529     Chunk (ChunkType type, const SGPropertyNode * node,
530            const string &fmt = "", float mult = 1.0, float offs = 0.0,
531            bool truncation = false);
532
533     const char * getValue () const;
534   private:
535     ChunkType _type;
536     string _text;
537     SGConstPropertyNode_ptr _node;
538     string _fmt;
539     float _mult;
540     float _offs;
541     bool _trunc;
542     mutable char _buf[1024];
543   };
544
545   FGTextLayer (int w = -1, int h = -1);
546   virtual ~FGTextLayer ();
547
548   virtual void draw (osg::State& state);
549
550                                 // Transfer pointer!!
551   virtual void addChunk (Chunk * chunk);
552   virtual void setColor (float r, float g, float b);
553   virtual void setPointSize (float size);
554   virtual void setFontName ( const string &name );
555   virtual void setFont (fntFont * font);
556
557 private:
558
559   void recalc_value () const;
560
561   typedef vector<Chunk *> chunk_list;
562   chunk_list _chunks;
563   float _color[4];
564
565   float _pointSize;
566   mutable string _font_name;
567   mutable string _value;
568   mutable SGTimeStamp _then;
569   mutable SGTimeStamp _now;
570 };
571
572
573 /**
574  * A group layer that switches among its children.
575  *
576  * The first layer that passes its condition will be drawn, and
577  * any following layers will be ignored.
578  */
579 class FGSwitchLayer : public FGGroupLayer
580 {
581 public:
582                                 // Transfer pointers!!
583   FGSwitchLayer ();
584   virtual void draw (osg::State& state);
585
586 };
587
588
589
590 \f
591 ////////////////////////////////////////////////////////////////////////
592 // Functions.
593 ////////////////////////////////////////////////////////////////////////
594
595 /**
596  * Test whether the panel should be visible.
597  */
598 bool fgPanelVisible ();
599
600
601 \f
602 #endif // __PANEL_HXX
603
604 // end of panel.hxx
605
606
607