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