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