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