1 // panel.hxx - generic support classes for a 2D panel.
3 // Written by David Megginson, started January 2000.
4 // Adopted for standalone fgpanel application by Torsten Dreyer, August 2009
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.
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.
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.
26 # error This library requires C++
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"
43 class FGPanelInstrument;
46 ////////////////////////////////////////////////////////////////////////
47 // Texture management.
48 ////////////////////////////////////////////////////////////////////////
50 class FGCroppedTexture;
51 typedef SGSharedPtr<FGCroppedTexture> FGCroppedTexture_ptr;
53 * Cropped texture (should migrate out into FGFS).
55 * This structure wraps an SSG texture with cropping information.
57 class FGCroppedTexture : public SGReferenced
60 FGCroppedTexture (const string &path,
61 float _minX = 0.0, float _minY = 0.0,
62 float _maxX = 1.0, float _maxY = 1.0);
64 virtual ~FGCroppedTexture ();
66 virtual void setPath (const string &path) { _path = path; }
68 virtual const string &getPath () const { return _path; }
70 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
71 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
74 static void registerTextureLoader( const string & extension, FGTextureLoaderInterface * loader ) {
75 if( textureLoader.count( extension ) == 0 )
76 textureLoader[extension] = loader;
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; }
85 virtual void bind( bool doGLBind = true );
89 float _minX, _minY, _maxX, _maxY;
92 static GLuint current_bound_texture;
93 static map<string,GLuint> cache;
94 static map<string,FGTextureLoaderInterface*> textureLoader;
99 ////////////////////////////////////////////////////////////////////////
101 ////////////////////////////////////////////////////////////////////////
105 * Instrument panel class.
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.
112 class FGPanel : public SGSubsystem
116 FGPanel ( SGPropertyNode_ptr root );
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);
126 // transfer pointer ownership!!!
127 virtual void addInstrument (FGPanelInstrument * instrument);
129 // Background texture.
130 virtual void setBackground (FGCroppedTexture_ptr texture);
131 inline void setBackgroundWidth( double d ) {
135 inline void setBackgroundHeight( double d ) {
139 // Background multiple textures.
140 virtual void setMultiBackground (FGCroppedTexture_ptr texture , int idx);
142 // Full width of panel.
143 virtual void setWidth (int width) { _width = width; }
144 virtual int getWidth () const { return _width; }
146 // Full height of panel.
147 virtual void setHeight (int height) { _height = height; }
148 virtual int getHeight () const { return _height; }
152 typedef vector<FGPanelInstrument *> instrument_list_type;
156 SGPropertyNode_ptr _root;
157 SGPropertyNode_ptr _flipx;
158 SGPropertyNode_ptr _rotate;
160 FGCroppedTexture_ptr _bg;
163 FGCroppedTexture_ptr _mbg[8];
164 // List of instruments in panel.
165 instrument_list_type _instruments;
167 GLuint initDisplayList;
169 GLuint getInitDisplayList();
174 ////////////////////////////////////////////////////////////////////////
176 ////////////////////////////////////////////////////////////////////////
180 * A transformation for a layer.
182 class FGPanelTransformation : public SGConditional
192 FGPanelTransformation ();
193 virtual ~FGPanelTransformation ();
196 SGConstPropertyNode_ptr node;
203 SGInterpTable * table;
209 ////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////
215 * A single layer of a multi-layered instrument.
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.
221 class FGInstrumentLayer : public SGConditional
225 FGInstrumentLayer (int w = -1, int h = -1);
226 virtual ~FGInstrumentLayer ();
228 virtual void draw () = 0;
229 virtual void transform () const;
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; }
236 // Transfer pointer ownership!!
238 virtual void addTransformation (FGPanelTransformation * transformation);
243 typedef vector<FGPanelTransformation *> transformation_list;
244 transformation_list _transformations;
249 ////////////////////////////////////////////////////////////////////////
251 ////////////////////////////////////////////////////////////////////////
255 * Abstract base class for a panel instrument.
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).
263 class FGPanelInstrument : public SGConditional
266 FGPanelInstrument ();
267 FGPanelInstrument (int x, int y, int w, int h);
268 virtual ~FGPanelInstrument ();
270 virtual void draw () = 0;
272 virtual void setPosition(int x, int y);
273 virtual void setSize(int w, int h);
275 virtual int getXPos () const;
276 virtual int getYPos () const;
277 virtual int getWidth () const;
278 virtual int getHeight () const;
286 * An instrument constructed of multiple layers.
288 * Each individual layer can be rotated or shifted to correspond
289 * to internal FGFS instrument readings.
291 class FGLayeredInstrument : public FGPanelInstrument
294 FGLayeredInstrument (int x, int y, int w, int h);
295 virtual ~FGLayeredInstrument ();
297 virtual void draw ();
299 // Transfer pointer ownership!!
300 virtual int addLayer (FGInstrumentLayer *layer);
301 virtual int addLayer (FGCroppedTexture_ptr texture, int w = -1, int h = -1);
303 // Transfer pointer ownership!!
304 virtual void addTransformation (FGPanelTransformation * transformation);
307 typedef vector<FGInstrumentLayer *> layer_list;
313 * An instrument layer containing a group of sublayers.
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.
319 class FGGroupLayer : public FGInstrumentLayer
323 virtual ~FGGroupLayer ();
324 virtual void draw ();
325 // transfer pointer ownership
326 virtual void addLayer (FGInstrumentLayer * layer);
328 vector<FGInstrumentLayer *> _layers;
333 * A textured layer of an instrument.
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.
339 class FGTexturedLayer : public FGInstrumentLayer
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 ();
346 virtual void draw ();
348 virtual void setTexture (FGCroppedTexture_ptr texture) {
351 FGCroppedTexture_ptr getTexture() { return _texture; }
353 void setEmissive(bool e) { _emissive = e; }
356 GLuint getDisplayList();
358 FGCroppedTexture_ptr _texture;
365 * A text layer of an instrument.
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.
371 class FGTextLayer : public FGInstrumentLayer
380 class Chunk : public SGConditional
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);
388 const char * getValue () const;
392 SGConstPropertyNode_ptr _node;
397 mutable char _buf[1024];
401 FGTextLayer (int w = -1, int h = -1);
402 virtual ~FGTextLayer ();
404 virtual void draw ();
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);
415 void recalc_value () const;
417 typedef vector<Chunk *> chunk_list;
422 mutable string _font_name;
423 mutable string _value;
424 mutable SGTimeStamp _then;
425 mutable SGTimeStamp _now;
427 static fntRenderer text_renderer;
432 * A group layer that switches among its children.
434 * The first layer that passes its condition will be drawn, and
435 * any following layers will be ignored.
437 class FGSwitchLayer : public FGGroupLayer
440 // Transfer pointers!!
442 virtual void draw ();
447 #endif // __PANEL_HXX