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