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