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