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