]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/panel.hxx
bvh: Adapt to upstream bvh changes in simgear.
[flightgear.git] / utils / fgpanel / panel.hxx
1 //  panel.hxx - generic support classes for a 2D panel.
2 //
3 //  Written by David Megginson, started January 2000.
4 //  Adopted for standalone fgpanel application by Torsten Dreyer, August 2009
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License as
8 //  published by the Free Software Foundation; either version 2 of the
9 //  License, or (at your option) any later version.
10 // 
11 //  This program is distributed in the hope that it will be useful, but
12 //  WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 //
20 //  $Id$
21
22 #ifndef __PANEL_HXX
23 #define __PANEL_HXX
24
25 #ifndef __cplusplus
26 # error This library requires C++
27 #endif
28
29
30 #ifdef HAVE_CONFIG_H
31 #  include <config.h>
32 #endif
33
34 #include <plib/fnt.h>
35
36 #include <simgear/props/propsfwd.hxx>
37 #include <simgear/props/condition.hxx>
38 #include <simgear/structure/subsystem_mgr.hxx>
39 #include <simgear/math/interpolater.hxx>
40 #include <simgear/sg_inlines.h>
41 #include "FGTextureLoaderInterface.hxx"
42
43 class FGPanelInstrument;
44 using namespace std;
45 \f
46 ////////////////////////////////////////////////////////////////////////
47 // Texture management.
48 ////////////////////////////////////////////////////////////////////////
49
50 class FGCroppedTexture;
51 typedef SGSharedPtr<FGCroppedTexture> FGCroppedTexture_ptr;
52 /**
53  * Cropped texture (should migrate out into FGFS).
54  *
55  * This structure wraps an SSG texture with cropping information.
56  */
57 class FGCroppedTexture : public SGReferenced
58 {
59 public:
60   FGCroppedTexture (const string &path,
61                   float _minX = 0.0, float _minY = 0.0,
62                   float _maxX = 1.0, float _maxY = 1.0);
63
64   virtual ~FGCroppedTexture ();
65
66   virtual void setPath (const string &path) { _path = path; }
67
68   virtual const string &getPath () const { return _path; }
69
70   virtual void setCrop (float minX, float minY, float maxX, float maxY) {
71     _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
72   }
73
74   static void registerTextureLoader( const string & extension, FGTextureLoaderInterface * loader ) {
75     if( textureLoader.count( extension ) == 0 )
76       textureLoader[extension] = loader;
77   }
78
79   virtual float getMinX () const { return _minX; }
80   virtual float getMinY () const { return _minY; }
81   virtual float getMaxX () const { return _maxX; }
82   virtual float getMaxY () const { return _maxY; }
83   GLuint getTexture() const { return _texture; }
84
85   virtual void bind( bool doGLBind = true );
86
87 private:
88   string _path;
89   float _minX, _minY, _maxX, _maxY;
90
91   GLuint _texture;
92   static GLuint current_bound_texture;
93   static map<string,GLuint> cache;
94   static map<string,FGTextureLoaderInterface*> textureLoader;
95 };
96
97
98 \f
99 ////////////////////////////////////////////////////////////////////////
100 // Top-level panel.
101 ////////////////////////////////////////////////////////////////////////
102
103
104 /**
105  * Instrument panel class.
106  *
107  * The panel is a container that has a background texture and holds
108  * zero or more instruments.  The panel will order the instruments to
109  * redraw themselves when necessary, and will pass mouse clicks on to
110  * the appropriate instruments for processing.
111  */
112 class FGPanel : public SGSubsystem
113 {
114 public:
115
116   FGPanel ( SGPropertyNode_ptr root );
117   virtual ~FGPanel ();
118
119                                 // Update the panel (every frame).
120   virtual void init ();
121   virtual void bind ();
122   virtual void unbind ();
123 //  virtual void draw ();
124   virtual void update (double dt);
125
126                                 // transfer pointer ownership!!!
127   virtual void addInstrument (FGPanelInstrument * instrument);
128
129                                 // Background texture.
130   virtual void setBackground (FGCroppedTexture_ptr texture);
131   inline void setBackgroundWidth( double d ) {
132     _bg_width = d;
133   }
134
135   inline void setBackgroundHeight( double d ) {
136     _bg_height = d;
137   }
138
139                                 // Background multiple textures.
140   virtual void setMultiBackground (FGCroppedTexture_ptr texture , int idx);
141
142                                 // Full width of panel.
143   virtual void setWidth (int width) { _width = width; }
144   virtual int getWidth () const { return _width; }
145
146                                 // Full height of panel.
147   virtual void setHeight (int height) { _height = height; }
148   virtual int getHeight () const { return _height; }
149
150 private:
151
152   typedef vector<FGPanelInstrument *> instrument_list_type;
153   int _width;
154   int _height;
155
156   SGPropertyNode_ptr _root;
157   SGPropertyNode_ptr _flipx;
158   SGPropertyNode_ptr _rotate;
159
160   FGCroppedTexture_ptr _bg;
161   double _bg_width;
162   double _bg_height;
163   FGCroppedTexture_ptr _mbg[8];
164                                 // List of instruments in panel.
165   instrument_list_type _instruments;
166
167   GLuint initDisplayList;
168
169   GLuint getInitDisplayList();
170 };
171
172
173 \f
174 ////////////////////////////////////////////////////////////////////////
175 // Transformations.
176 ////////////////////////////////////////////////////////////////////////
177
178
179 /**
180  * A transformation for a layer.
181  */
182 class FGPanelTransformation : public SGConditional
183 {
184 public:
185
186   enum Type {
187     XSHIFT,
188     YSHIFT,
189     ROTATION
190   };
191
192   FGPanelTransformation ();
193   virtual ~FGPanelTransformation ();
194
195   Type type;
196   SGConstPropertyNode_ptr node;
197   float min;
198   float max;
199   bool has_mod;
200   float mod;
201   float factor;
202   float offset;
203   SGInterpTable * table;
204 };
205
206
207
208 \f
209 ////////////////////////////////////////////////////////////////////////
210 // Layers
211 ////////////////////////////////////////////////////////////////////////
212
213
214 /**
215  * A single layer of a multi-layered instrument.
216  *
217  * Each layer can be subject to a series of transformations based
218  * on current FGFS instrument readings: for example, a texture
219  * representing a needle can rotate to show the airspeed.
220  */
221 class FGInstrumentLayer : public SGConditional
222 {
223 public:
224
225   FGInstrumentLayer (int w = -1, int h = -1);
226   virtual ~FGInstrumentLayer ();
227
228   virtual void draw () = 0;
229   virtual void transform () const;
230
231   virtual int getWidth () const { return _w; }
232   virtual int getHeight () const { return _h; }
233   virtual void setWidth (int w) { _w = w; }
234   virtual void setHeight (int h) { _h = h; }
235
236                                 // Transfer pointer ownership!!
237                                 // DEPRECATED
238   virtual void addTransformation (FGPanelTransformation * transformation);
239
240 protected:
241   int _w, _h;
242
243   typedef vector<FGPanelTransformation *> transformation_list;
244   transformation_list _transformations;
245 };
246
247
248 \f
249 ////////////////////////////////////////////////////////////////////////
250 // Instruments.
251 ////////////////////////////////////////////////////////////////////////
252
253
254 /**
255  * Abstract base class for a panel instrument.
256  *
257  * A panel instrument consists of zero or more actions, associated
258  * with mouse clicks in rectangular areas.  Currently, the only
259  * concrete class derived from this is FGLayeredInstrument, but others
260  * may show up in the future (some complex instruments could be 
261  * entirely hand-coded, for example).
262  */
263 class FGPanelInstrument : public SGConditional
264 {
265 public:
266   FGPanelInstrument ();
267   FGPanelInstrument (int x, int y, int w, int h);
268   virtual ~FGPanelInstrument ();
269
270   virtual void draw () = 0;
271
272   virtual void setPosition(int x, int y);
273   virtual void setSize(int w, int h);
274
275   virtual int getXPos () const;
276   virtual int getYPos () const;
277   virtual int getWidth () const;
278   virtual int getHeight () const;
279
280 protected:
281   int _x, _y, _w, _h;
282 };
283
284
285 /**
286  * An instrument constructed of multiple layers.
287  *
288  * Each individual layer can be rotated or shifted to correspond
289  * to internal FGFS instrument readings.
290  */
291 class FGLayeredInstrument : public FGPanelInstrument
292 {
293 public:
294   FGLayeredInstrument (int x, int y, int w, int h);
295   virtual ~FGLayeredInstrument ();
296
297   virtual void draw ();
298
299                                 // Transfer pointer ownership!!
300   virtual int addLayer (FGInstrumentLayer *layer);
301   virtual int addLayer (FGCroppedTexture_ptr texture, int w = -1, int h = -1);
302
303                                 // Transfer pointer ownership!!
304   virtual void addTransformation (FGPanelTransformation * transformation);
305
306 protected:
307   typedef vector<FGInstrumentLayer *> layer_list;
308   layer_list _layers;
309 };
310
311
312 /**
313  * An instrument layer containing a group of sublayers.
314  *
315  * This class is useful for gathering together a group of related
316  * layers, either to hold in an external file or to work under
317  * the same condition.
318  */
319 class FGGroupLayer : public FGInstrumentLayer
320 {
321 public:
322   FGGroupLayer ();
323   virtual ~FGGroupLayer ();
324   virtual void draw ();
325                                 // transfer pointer ownership
326   virtual void addLayer (FGInstrumentLayer * layer);
327 protected:
328   vector<FGInstrumentLayer *> _layers;
329 };
330
331
332 /**
333  * A textured layer of an instrument.
334  *
335  * This is a layer holding a single texture.  Normally, the texture's
336  * backgound should be transparent so that lower layers and the panel
337  * background can show through.
338  */
339 class FGTexturedLayer : public FGInstrumentLayer
340 {
341 public:
342   FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
343   FGTexturedLayer (FGCroppedTexture_ptr texture, int w = -1, int h = -1);
344   virtual ~FGTexturedLayer ();
345
346   virtual void draw ();
347
348   virtual void setTexture (FGCroppedTexture_ptr texture) {
349     _texture = texture;
350   }
351   FGCroppedTexture_ptr getTexture() { return _texture; }
352
353   void setEmissive(bool e) { _emissive = e; }
354
355 private:
356   GLuint getDisplayList();
357
358   FGCroppedTexture_ptr _texture;
359   bool _emissive;
360   GLuint displayList;
361 };
362
363
364 /**
365  * A text layer of an instrument.
366  *
367  * This is a layer holding a string of static and/or generated text.
368  * It is useful for instruments that have text displays, such as
369  * a chronometer, GPS, or NavCom radio.
370  */
371 class FGTextLayer : public FGInstrumentLayer
372 {
373 public:
374   enum ChunkType {
375     TEXT,
376     TEXT_VALUE,
377     DOUBLE_VALUE
378   };
379
380   class Chunk : public SGConditional
381   {
382   public:
383     Chunk (const string &text, const string &fmt = "%s");
384     Chunk (ChunkType type, const SGPropertyNode * node,
385            const string &fmt = "", float mult = 1.0, float offs = 0.0,
386            bool truncation = false);
387
388     const char * getValue () const;
389   private:
390     ChunkType _type;
391     string _text;
392     SGConstPropertyNode_ptr _node;
393     string _fmt;
394     float _mult;
395     float _offs;
396     bool _trunc;
397     mutable char _buf[1024];
398     
399   };
400
401   FGTextLayer (int w = -1, int h = -1);
402   virtual ~FGTextLayer ();
403
404   virtual void draw ();
405
406                                 // Transfer pointer!!
407   virtual void addChunk (Chunk * chunk);
408   virtual void setColor (float r, float g, float b);
409   virtual void setPointSize (float size);
410   virtual void setFontName ( const string &name );
411   virtual void setFont (fntFont * font);
412
413 private:
414
415   void recalc_value () const;
416
417   typedef vector<Chunk *> chunk_list;
418   chunk_list _chunks;
419   float _color[4];
420
421   float _pointSize;
422   mutable string _font_name;
423   mutable string _value;
424   mutable SGTimeStamp _then;
425   mutable SGTimeStamp _now;
426
427   static fntRenderer text_renderer;
428 };
429
430
431 /**
432  * A group layer that switches among its children.
433  *
434  * The first layer that passes its condition will be drawn, and
435  * any following layers will be ignored.
436  */
437 class FGSwitchLayer : public FGGroupLayer
438 {
439 public:
440                                 // Transfer pointers!!
441   FGSwitchLayer ();
442   virtual void draw ();
443
444 };
445
446 \f
447 #endif // __PANEL_HXX
448
449 // end of panel.hxx
450
451
452