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