]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.hxx
More property node optimizations.
[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 (int window_x, int window_y, int window_w, int window_h);
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 _winx, _winy, _winw, _winh;
178   int _width;
179   int _height;
180   int _x_offset;
181   int _y_offset;
182   int _view_height;
183   bool _bound;
184   
185   ssgTexture * _bg;
186                                 // List of instruments in panel.
187   instrument_list_type _instruments;
188 };
189
190
191 \f
192 ////////////////////////////////////////////////////////////////////////
193 // Base class for user action types.
194 //
195 // Individual instruments can have actions associated with a mouse
196 // click in a rectangular area.  Current concrete classes include
197 // FGAdjustAction, FGSwapAction, and FGToggleAction.
198 ////////////////////////////////////////////////////////////////////////
199
200 class FGPanelAction
201 {
202 public:
203   FGPanelAction ();
204   FGPanelAction (int button, int x, int y, int w, int h);
205   virtual ~FGPanelAction ();
206
207                                 // Getters.
208   virtual int getButton () const { return _button; }
209   virtual int getX () const { return _x; }
210   virtual int getY () const { return _y; }
211   virtual int getWidth () const { return _w; }
212   virtual int getHeight () const { return _h; }
213
214                                 // Setters.
215   virtual void setButton (int button) { _button = button; }
216   virtual void setX (int x) { _x = x; }
217   virtual void setY (int y) { _y = y; }
218   virtual void setWidth (int w) { _w = w; }
219   virtual void setHeight (int h) { _h = h; }
220
221                                 // Check whether we're in the area.
222   virtual bool inArea (int button, int x, int y)
223   {
224     return (button == _button &&
225             x >= _x &&
226             x < _x + _w &&
227             y >= _y &&
228             y < _y + _h);
229   }
230
231                                 // Perform the action.
232   virtual void doAction () = 0;
233
234 private:
235   int _button;
236   int _x;
237   int _y;
238   int _w;
239   int _h;
240 };
241
242
243 \f
244 ////////////////////////////////////////////////////////////////////////
245 // Adjustment action.
246 //
247 // This is an action to increase or decrease an FGFS value by a certain
248 // increment within a certain range.  If the wrap flag is true, the
249 // value will wrap around if it goes below min or above max; otherwise,
250 // it will simply stop at min or max.
251 ////////////////////////////////////////////////////////////////////////
252
253 class FGAdjustAction : public FGPanelAction
254 {
255 public:
256   FGAdjustAction (int button, int x, int y, int w, int h,
257                   SGValue * value, float increment,
258                   float min, float max, bool wrap=false);
259   virtual ~FGAdjustAction ();
260   virtual void doAction ();
261
262 private:
263   SGValue * _value;
264   float _increment;
265   float _min;
266   float _max;
267   bool _wrap;
268 };
269
270
271 \f
272 ////////////////////////////////////////////////////////////////////////
273 // Swap action.
274 //
275 // This is an action to swap two values.  It's currently used in the
276 // navigation radios.
277 ////////////////////////////////////////////////////////////////////////
278
279 class FGSwapAction : public FGPanelAction
280 {
281 public:
282   FGSwapAction (int button, int x, int y, int w, int h,
283                 SGValue * value1, SGValue * value2);
284   virtual ~FGSwapAction ();
285   virtual void doAction ();
286
287 private:
288   SGValue * _value1;
289   SGValue * _value2;
290 };
291
292
293 \f
294 ////////////////////////////////////////////////////////////////////////
295 // Toggle action.
296 //
297 // This is an action to toggle a boolean value.
298 ////////////////////////////////////////////////////////////////////////
299
300 class FGToggleAction : public FGPanelAction
301 {
302 public:
303   FGToggleAction (int button, int x, int y, int w, int h,
304                   SGValue * value);
305   virtual ~FGToggleAction ();
306   virtual void doAction ();
307
308 private:
309   SGValue * _value;
310 };
311
312
313 \f
314 ////////////////////////////////////////////////////////////////////////
315 // Abstract base class for a panel instrument.
316 //
317 // A panel instrument consists of zero or more actions, associated
318 // with mouse clicks in rectangular areas.  Currently, the only
319 // concrete class derived from this is FGLayeredInstrument, but others
320 // may show up in the future (some complex instruments could be 
321 // entirely hand-coded, for example).
322 ////////////////////////////////////////////////////////////////////////
323
324 class FGPanelInstrument
325 {
326 public:
327   FGPanelInstrument ();
328   FGPanelInstrument (int x, int y, int w, int h);
329   virtual ~FGPanelInstrument ();
330
331   virtual void draw () = 0;
332
333   virtual void setPosition(int x, int y);
334   virtual void setSize(int w, int h);
335
336   virtual int getXPos () const;
337   virtual int getYPos () const;
338   virtual int getWidth () const;
339   virtual int getHeight () const;
340
341                                 // Coordinates relative to centre.
342                                 // Transfer pointer ownership!!
343   virtual void addAction (FGPanelAction * action);
344
345                                 // Coordinates relative to centre.
346   virtual bool doMouseAction (int button, int x, int y);
347
348 protected:
349   int _x, _y, _w, _h;
350   typedef vector<FGPanelAction *> action_list_type;
351   action_list_type _actions;
352 };
353
354
355 \f
356 ////////////////////////////////////////////////////////////////////////
357 // Abstract base class for an instrument layer.
358 //
359 // The FGLayeredInstrument class builds up instruments by using layers
360 // of textures or text.  Each layer can have zero or more
361 // transformations applied to it: for example, a needle layer can
362 // rotate to show the altitude or airspeed.
363 ////////////////////////////////////////////////////////////////////////
364
365
366 /**
367  * A transformation for a layer.
368  */
369 class FGPanelTransformation {
370 public:
371
372   enum Type {
373     XSHIFT,
374     YSHIFT,
375     ROTATION
376   };
377
378   FGPanelTransformation ();
379   virtual ~FGPanelTransformation ();
380
381   Type type;
382   const SGValue * value;
383   float min;
384   float max;
385   float factor;
386   float offset;
387   SGInterpTable * table;
388 };
389
390
391
392 /**
393  * A single layer of a multi-layered instrument.
394  *
395  * Each layer can be subject to a series of transformations based
396  * on current FGFS instrument readings: for example, a texture
397  * representing a needle can rotate to show the airspeed.
398  */
399 class FGInstrumentLayer
400 {
401 public:
402
403   FGInstrumentLayer (int w = -1, int h = -1);
404   virtual ~FGInstrumentLayer ();
405
406   virtual void draw () = 0;
407   virtual void transform () const;
408
409   virtual int getWidth () const { return _w; }
410   virtual int getHeight () const { return _h; }
411   virtual void setWidth (int w) { _w = w; }
412   virtual void setHeight (int h) { _h = h; }
413
414                                 // Transfer pointer ownership!!
415                                 // DEPRECATED
416   virtual void addTransformation (FGPanelTransformation * transformation);
417
418 protected:
419   int _w, _h;
420
421   typedef vector<FGPanelTransformation *> transformation_list;
422   transformation_list _transformations;
423 };
424
425
426 \f
427 ////////////////////////////////////////////////////////////////////////
428 // An instrument composed of layers.
429 //
430 // This class represents an instrument which is simply a series of
431 // layers piled one on top of the other, each one undergoing its own
432 // set of transformations.  For example, one layer can represent
433 // the instrument's face (which doesn't move), while the next layer
434 // can represent a needle that rotates depending on an FGFS variable.
435 ////////////////////////////////////////////////////////////////////////
436
437
438 /**
439  * An instrument constructed of multiple layers.
440  *
441  * Each individual layer can be rotated or shifted to correspond
442  * to internal FGFS instrument readings.
443  */
444 class FGLayeredInstrument : public FGPanelInstrument
445 {
446 public:
447   FGLayeredInstrument (int x, int y, int w, int h);
448   virtual ~FGLayeredInstrument ();
449
450   virtual void draw ();
451
452                                 // Transfer pointer ownership!!
453   virtual int addLayer (FGInstrumentLayer *layer);
454   virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
455
456                                 // Transfer pointer ownership!!
457   virtual void addTransformation (FGPanelTransformation * transformation);
458
459 protected:
460   typedef vector<FGInstrumentLayer *> layer_list;
461   layer_list _layers;
462 };
463
464
465 \f
466 ////////////////////////////////////////////////////////////////////////
467 // A textured layer of an instrument.
468 //
469 // This is a layer holding a single texture.  Normally, the texture's
470 // backgound should be transparent so that lower layers and the panel
471 // background can show through.
472 ////////////////////////////////////////////////////////////////////////
473
474 class FGTexturedLayer : public FGInstrumentLayer
475 {
476 public:
477   FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
478   FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
479   virtual ~FGTexturedLayer ();
480
481   virtual void draw ();
482
483   virtual void setTexture (const FGCroppedTexture &texture) {
484     _texture = texture;
485   }
486   virtual FGCroppedTexture &getTexture () { return _texture; }
487   virtual const FGCroppedTexture &getTexture () const { return _texture; }
488
489 private:
490   mutable FGCroppedTexture _texture;
491 };
492
493
494 \f
495 ////////////////////////////////////////////////////////////////////////
496 // A text layer of an instrument.
497 //
498 // This is a layer holding a string of static and/or generated text.
499 // It is useful for instruments that have text displays, such as
500 // a chronometer, GPS, or NavCom radio.
501 ////////////////////////////////////////////////////////////////////////
502
503 class FGTextLayer : public FGInstrumentLayer
504 {
505 public:
506   typedef enum ChunkType {
507     TEXT,
508     TEXT_VALUE,
509     DOUBLE_VALUE
510   };
511
512   class Chunk {
513   public:
514     Chunk (const string &text, const string &fmt = "%s");
515     Chunk (ChunkType type, const SGValue * value,
516            const string &fmt = "", float mult = 1.0);
517
518     const char * getValue () const;
519   private:
520     ChunkType _type;
521     string _text;
522     const SGValue * _value;
523     string _fmt;
524     float _mult;
525     mutable char _buf[1024];
526   };
527
528   FGTextLayer (int w = -1, int h = -1);
529   virtual ~FGTextLayer ();
530
531   virtual void draw ();
532
533                                 // Transfer pointer!!
534   virtual void addChunk (Chunk * chunk);
535   virtual void setColor (float r, float g, float b);
536   virtual void setPointSize (float size);
537   virtual void setFont (fntFont * font);
538
539 private:
540
541   void recalc_value () const;
542
543   typedef vector<Chunk *> chunk_list;
544   chunk_list _chunks;
545   float _color[4];
546
547   float _pointSize;
548
549   mutable string _value;
550   mutable SGTimeStamp _then;
551   mutable SGTimeStamp _now;
552 };
553
554
555 \f
556 ////////////////////////////////////////////////////////////////////////
557 // A layer that switches between two other layers.
558 ////////////////////////////////////////////////////////////////////////
559
560 class FGSwitchLayer : public FGInstrumentLayer
561 {
562 public:
563                                 // Transfer pointers!!
564   FGSwitchLayer (int w, int h, const SGValue * value,
565                  FGInstrumentLayer * layer1,
566                  FGInstrumentLayer * layer2);
567   virtual ~FGSwitchLayer ();
568
569   virtual void draw ();
570
571 private:
572   const SGValue * _value;
573   FGInstrumentLayer * _layer1, * _layer2;
574 };
575
576
577 \f
578 ////////////////////////////////////////////////////////////////////////
579 // Functions.
580 ////////////////////////////////////////////////////////////////////////
581
582 bool fgPanelVisible ();
583
584
585 \f
586 ////////////////////////////////////////////////////////////////////////
587 // The current panel, if any.
588 ////////////////////////////////////////////////////////////////////////
589
590 extern FGPanel * current_panel;
591
592
593 \f
594 #endif // __PANEL_HXX
595
596 // end of panel.hxx
597
598