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