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