]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.hxx
Fix two problems:
[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 update (double dt);
145   virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
146
147                                 // transfer pointer ownership!!!
148   virtual void addInstrument (FGPanelInstrument * instrument);
149
150                                 // Background texture.
151   virtual void setBackground (ssgTexture * texture);
152
153                                 // Background multiple textures.
154   virtual void setMultiBackground (ssgTexture * texture, int idx);
155
156                                 // Make the panel visible or invisible.
157   virtual bool getVisibility () const;
158   virtual void setVisibility (bool visibility);
159
160                                 // Full width of panel.
161   virtual void setWidth (int width) { _width = width; }
162   virtual int getWidth () const { return _width; }
163
164                                 // Full height of panel.
165   virtual void setHeight (int height) { _height = height; }
166   virtual int getHeight () const { return _height; }
167
168                                 // X-offset
169   virtual void setXOffset (int offset);
170   virtual int getXOffset () const { return _x_offset; }
171
172                                 // Y-offset.
173   virtual void setYOffset (int offset);
174   virtual int getYOffset () const { return _y_offset; }
175
176                                 // View height.
177   virtual void setViewHeight (int height) { _view_height = height; }
178   virtual int getViewHeight () const { return _view_height; }
179
180                                 // Handle a mouse click.
181   virtual bool doMouseAction (int button, int updown, int x, int y);
182
183 private:
184   void setupVirtualCockpit();
185   void cleanupVirtualCockpit();
186
187   mutable bool _visibility;
188   mutable bool _mouseDown;
189   mutable int _mouseButton, _mouseX, _mouseY;
190   mutable int _mouseDelay;
191   mutable FGPanelInstrument * _mouseInstrument;
192   typedef vector<FGPanelInstrument *> instrument_list_type;
193   int _width;
194   int _height;
195   int _x_offset;
196   int _y_offset;
197   int _view_height;
198   float _jitter;
199
200   const SGPropertyNode * _xsize_node;
201   const SGPropertyNode * _ysize_node;
202   
203   ssgTexture * _bg;
204   ssgTexture * _mbg[8];
205                                 // List of instruments in panel.
206   instrument_list_type _instruments;
207 };
208
209
210 \f
211 ////////////////////////////////////////////////////////////////////////
212 // Actions
213 ////////////////////////////////////////////////////////////////////////
214
215
216 /**
217  * Class for user actions.
218  *
219  * The actions are command bindings, like bindings for the keyboard
220  * or joystick, but they are tied to specific mouse actions in
221  * rectangular areas of the panel.
222  */
223 class FGPanelAction : public FGConditional
224 {
225 public:
226   FGPanelAction ();
227   FGPanelAction (int button, int x, int y, int w, int h);
228   virtual ~FGPanelAction ();
229
230                                 // Getters.
231   virtual int getButton () const { return _button; }
232   virtual int getX () const { return _x; }
233   virtual int getY () const { return _y; }
234   virtual int getWidth () const { return _w; }
235   virtual int getHeight () const { return _h; }
236
237                                 // Setters.
238
239                                 // transfer pointer ownership
240   virtual void addBinding (FGBinding * binding);
241   virtual void setButton (int button) { _button = button; }
242   virtual void setX (int x) { _x = x; }
243   virtual void setY (int y) { _y = y; }
244   virtual void setWidth (int w) { _w = w; }
245   virtual void setHeight (int h) { _h = h; }
246
247                                 // Check whether we're in the area.
248   virtual bool inArea (int button, int x, int y)
249   {
250     return (button == _button &&
251             x >= _x &&
252             x < _x + _w &&
253             y >= _y &&
254             y < _y + _h);
255   }
256
257                                 // Perform the action.
258   virtual void doAction ();
259
260 private:
261   typedef vector<FGBinding *> binding_list_t;
262
263   int _button;
264   int _x;
265   int _y;
266   int _w;
267   int _h;
268   binding_list_t _bindings;
269 };
270
271
272 \f
273 ////////////////////////////////////////////////////////////////////////
274 // Transformations.
275 ////////////////////////////////////////////////////////////////////////
276
277
278 /**
279  * A transformation for a layer.
280  */
281 class FGPanelTransformation : public FGConditional
282 {
283 public:
284
285   enum Type {
286     XSHIFT,
287     YSHIFT,
288     ROTATION
289   };
290
291   FGPanelTransformation ();
292   virtual ~FGPanelTransformation ();
293
294   Type type;
295   const SGPropertyNode * node;
296   float min;
297   float max;
298   float factor;
299   float offset;
300   SGInterpTable * table;
301 };
302
303
304
305 \f
306 ////////////////////////////////////////////////////////////////////////
307 // Layers
308 ////////////////////////////////////////////////////////////////////////
309
310
311 /**
312  * A single layer of a multi-layered instrument.
313  *
314  * Each layer can be subject to a series of transformations based
315  * on current FGFS instrument readings: for example, a texture
316  * representing a needle can rotate to show the airspeed.
317  */
318 class FGInstrumentLayer : public FGConditional
319 {
320 public:
321
322   FGInstrumentLayer (int w = -1, int h = -1);
323   virtual ~FGInstrumentLayer ();
324
325   virtual void draw () = 0;
326   virtual void transform () const;
327
328   virtual int getWidth () const { return _w; }
329   virtual int getHeight () const { return _h; }
330   virtual void setWidth (int w) { _w = w; }
331   virtual void setHeight (int h) { _h = h; }
332
333                                 // Transfer pointer ownership!!
334                                 // DEPRECATED
335   virtual void addTransformation (FGPanelTransformation * transformation);
336
337 protected:
338   int _w, _h;
339
340   typedef vector<FGPanelTransformation *> transformation_list;
341   transformation_list _transformations;
342 };
343
344
345 \f
346 ////////////////////////////////////////////////////////////////////////
347 // Instruments.
348 ////////////////////////////////////////////////////////////////////////
349
350
351 /**
352  * Abstract base class for a panel instrument.
353  *
354  * A panel instrument consists of zero or more actions, associated
355  * with mouse clicks in rectangular areas.  Currently, the only
356  * concrete class derived from this is FGLayeredInstrument, but others
357  * may show up in the future (some complex instruments could be 
358  * entirely hand-coded, for example).
359  */
360 class FGPanelInstrument : public FGConditional
361 {
362 public:
363   FGPanelInstrument ();
364   FGPanelInstrument (int x, int y, int w, int h);
365   virtual ~FGPanelInstrument ();
366
367   virtual void draw () = 0;
368
369   virtual void setPosition(int x, int y);
370   virtual void setSize(int w, int h);
371
372   virtual int getXPos () const;
373   virtual int getYPos () const;
374   virtual int getWidth () const;
375   virtual int getHeight () const;
376
377                                 // Coordinates relative to centre.
378                                 // Transfer pointer ownership!!
379   virtual void addAction (FGPanelAction * action);
380
381                                 // Coordinates relative to centre.
382   virtual bool doMouseAction (int button, int x, int y);
383
384 protected:
385   int _x, _y, _w, _h;
386   typedef vector<FGPanelAction *> action_list_type;
387   action_list_type _actions;
388 };
389
390
391 /**
392  * An instrument constructed of multiple layers.
393  *
394  * Each individual layer can be rotated or shifted to correspond
395  * to internal FGFS instrument readings.
396  */
397 class FGLayeredInstrument : public FGPanelInstrument
398 {
399 public:
400   FGLayeredInstrument (int x, int y, int w, int h);
401   virtual ~FGLayeredInstrument ();
402
403   virtual void draw ();
404
405                                 // Transfer pointer ownership!!
406   virtual int addLayer (FGInstrumentLayer *layer);
407   virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
408
409                                 // Transfer pointer ownership!!
410   virtual void addTransformation (FGPanelTransformation * transformation);
411
412 protected:
413   typedef vector<FGInstrumentLayer *> layer_list;
414   layer_list _layers;
415 };
416
417
418 /**
419  * An instrument layer containing a group of sublayers.
420  *
421  * This class is useful for gathering together a group of related
422  * layers, either to hold in an external file or to work under
423  * the same condition.
424  */
425 class FGGroupLayer : public FGInstrumentLayer
426 {
427 public:
428   FGGroupLayer ();
429   virtual ~FGGroupLayer ();
430   virtual void draw ();
431                                 // transfer pointer ownership
432   virtual void addLayer (FGInstrumentLayer * layer);
433 private:
434   vector<FGInstrumentLayer *> _layers;
435 };
436
437
438 /**
439  * A textured layer of an instrument.
440  *
441  * This is a layer holding a single texture.  Normally, the texture's
442  * backgound should be transparent so that lower layers and the panel
443  * background can show through.
444  */
445 class FGTexturedLayer : public FGInstrumentLayer
446 {
447 public:
448   FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
449   FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
450   virtual ~FGTexturedLayer ();
451
452   virtual void draw ();
453
454   virtual void setTexture (const FGCroppedTexture &texture) {
455     _texture = texture;
456   }
457   virtual FGCroppedTexture &getTexture () { return _texture; }
458   virtual const FGCroppedTexture &getTexture () const { return _texture; }
459
460 private:
461   mutable FGCroppedTexture _texture;
462 };
463
464
465 /**
466  * A text layer of an instrument.
467  *
468  * This is a layer holding a string of static and/or generated text.
469  * It is useful for instruments that have text displays, such as
470  * a chronometer, GPS, or NavCom radio.
471  */
472 class FGTextLayer : public FGInstrumentLayer
473 {
474 public:
475   enum ChunkType {
476     TEXT,
477     TEXT_VALUE,
478     DOUBLE_VALUE
479   };
480
481   class Chunk : public FGConditional
482   {
483   public:
484     Chunk (const string &text, const string &fmt = "%s");
485     Chunk (ChunkType type, const SGPropertyNode * node,
486            const string &fmt = "", float mult = 1.0);
487
488     const char * getValue () const;
489   private:
490     ChunkType _type;
491     string _text;
492     const SGPropertyNode * _node;
493     string _fmt;
494     float _mult;
495     mutable char _buf[1024];
496   };
497
498   FGTextLayer (int w = -1, int h = -1);
499   virtual ~FGTextLayer ();
500
501   virtual void draw ();
502
503                                 // Transfer pointer!!
504   virtual void addChunk (Chunk * chunk);
505   virtual void setColor (float r, float g, float b);
506   virtual void setPointSize (float size);
507   virtual void setFontName ( const string &name );
508   virtual void setFont (fntFont * font);
509
510 private:
511
512   void recalc_value () const;
513
514   typedef vector<Chunk *> chunk_list;
515   chunk_list _chunks;
516   float _color[4];
517
518   float _pointSize;
519   mutable string _font_name;
520   mutable string _value;
521   mutable SGTimeStamp _then;
522   mutable SGTimeStamp _now;
523 };
524
525
526 /**
527  * A layer that switches between two other layers.
528  *
529  * The usefulness of this layer is questionable now that all layers
530  * can have conditions, and it may be deprecated soon.
531  */
532 class FGSwitchLayer : public FGInstrumentLayer
533 {
534 public:
535                                 // Transfer pointers!!
536   FGSwitchLayer (int w, int h, const SGPropertyNode * node,
537                  FGInstrumentLayer * layer1,
538                  FGInstrumentLayer * layer2);
539   virtual ~FGSwitchLayer ();
540
541   virtual void draw ();
542
543 private:
544   const SGPropertyNode * _node;
545   FGInstrumentLayer * _layer1, * _layer2;
546 };
547
548
549
550 \f
551 ////////////////////////////////////////////////////////////////////////
552 // Functions.
553 ////////////////////////////////////////////////////////////////////////
554
555 /**
556  * Test whether the panel should be visible.
557  */
558 bool fgPanelVisible ();
559
560
561 \f
562 ////////////////////////////////////////////////////////////////////////
563 // The current panel, if any.
564 ////////////////////////////////////////////////////////////////////////
565
566 extern FGPanel * current_panel; // TODO: move to globals
567
568
569 \f
570 #endif // __PANEL_HXX
571
572 // end of panel.hxx
573
574
575