1 // panel.hxx - generic support classes for a 2D panel.
3 // Written by David Megginson, started January 2000.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <osg/ref_ptr>
25 #include <osg/StateSet>
26 #include <osg/Texture2D>
28 #include <simgear/compiler.h>
29 #include <simgear/props/props.hxx>
30 #include <simgear/structure/subsystem_mgr.hxx>
31 #include <simgear/structure/SGBinding.hxx>
32 #include <simgear/math/interpolater.hxx>
33 #include <simgear/timing/timestamp.hxx>
39 #include <Main/fg_props.hxx>
41 class FGPanelInstrument;
45 ////////////////////////////////////////////////////////////////////////
46 // Texture management.
47 ////////////////////////////////////////////////////////////////////////
51 * Texture manager (should migrate out into FGFS).
53 * This class ensures that no texture is loaded more than once.
55 class FGTextureManager
58 static osg::Texture2D* createTexture(const std::string &relativePath,
59 bool staticTexture = true);
60 static void addTexture(const std::string &relativePath, osg::Texture2D* texture);
62 static std::map<std::string,osg::ref_ptr<osg::Texture2D> > _textureMap;
67 * Cropped texture (should migrate out into FGFS).
69 * This structure wraps an SSG texture with cropping information.
71 class FGCroppedTexture
76 FGCroppedTexture (const std::string &path,
77 float _minX = 0.0, float _minY = 0.0,
78 float _maxX = 1.0, float _maxY = 1.0);
79 virtual ~FGCroppedTexture ();
81 virtual void setPath (const std::string &path) { _path = path; }
83 virtual const std::string &getPath () const { return _path; }
85 virtual osg::StateSet* getTexture ();
87 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
88 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
91 virtual float getMinX () const { return _minX; }
92 virtual float getMinY () const { return _minY; }
93 virtual float getMaxX () const { return _maxX; }
94 virtual float getMaxY () const { return _maxY; }
99 osg::ref_ptr<osg::StateSet> _texture;
100 float _minX, _minY, _maxX, _maxY;
105 ////////////////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////////
111 * Instrument panel class.
113 * The panel is a container that has a background texture and holds
114 * zero or more instruments. The panel will order the instruments to
115 * redraw themselves when necessary, and will pass mouse clicks on to
116 * the appropriate instruments for processing.
118 class FGPanel : public SGSubsystem
125 // Update the panel (every frame).
126 virtual void init ();
127 virtual void bind ();
128 virtual void unbind ();
129 virtual void draw (osg::State& state);
130 virtual void update (double);
131 void update (osg::State& state);
132 virtual void update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
134 virtual void updateMouseDelay();
136 // transfer pointer ownership!!!
137 virtual void addInstrument (FGPanelInstrument * instrument);
139 // Background texture.
140 virtual void setBackground (osg::Texture2D* texture);
142 // Background multiple textures.
143 virtual void setMultiBackground (osg::Texture2D* texture, int idx);
145 // Make the panel visible or invisible.
146 virtual bool getVisibility () const;
147 virtual void setVisibility (bool visibility);
149 // Full width of panel.
150 virtual void setWidth (int width) { _width = width; }
151 virtual int getWidth () const { return _width; }
153 // Full height of panel.
154 virtual void setHeight (int height) { _height = height; }
155 virtual int getHeight () const { return _height; }
158 virtual void setXOffset (int offset);
159 virtual int getXOffset () const { return _x_offset->getIntValue(); }
162 virtual void setYOffset (int offset);
163 virtual int getYOffset () const { return _y_offset->getIntValue(); }
166 virtual void setViewHeight (int height) { _view_height = height; }
167 virtual int getViewHeight () const { return _view_height; }
169 // Handle a mouse click.
170 virtual bool doMouseAction (int button, int updown, int x, int y);
171 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
173 virtual void setDepthTest (bool enable);
175 bool getAutohide(void) const { return _autohide; };
176 void setAutohide(bool enable) { _autohide = enable; };
179 void setupVirtualCockpit();
180 void cleanupVirtualCockpit();
182 mutable bool _mouseDown;
183 mutable int _mouseButton, _mouseX, _mouseY;
184 mutable int _mouseDelay;
185 mutable FGPanelInstrument * _mouseInstrument;
186 typedef std::vector<FGPanelInstrument *> instrument_list_type;
191 SGPropertyNode_ptr _visibility;
192 SGPropertyNode_ptr _x_offset;
193 SGPropertyNode_ptr _y_offset;
194 SGPropertyNode_ptr _jitter;
195 SGPropertyNode_ptr _flipx;
197 SGConstPropertyNode_ptr _xsize_node;
198 SGConstPropertyNode_ptr _ysize_node;
200 osg::ref_ptr<osg::StateSet> _bg;
201 osg::ref_ptr<osg::StateSet> _mbg[8];
202 // List of instruments in panel.
203 instrument_list_type _instruments;
204 bool _enable_depth_test;
210 ////////////////////////////////////////////////////////////////////////
212 ////////////////////////////////////////////////////////////////////////
216 * Class for user actions.
218 * The actions are command bindings, like bindings for the keyboard
219 * or joystick, but they are tied to specific mouse actions in
220 * rectangular areas of the panel.
222 class FGPanelAction : public SGConditional
226 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
227 virtual ~FGPanelAction ();
230 virtual int getButton () const { return _button; }
231 virtual int getX () const { return _x; }
232 virtual int getY () const { return _y; }
233 virtual int getWidth () const { return _w; }
234 virtual int getHeight () const { return _h; }
238 // transfer pointer ownership
239 virtual void addBinding (SGBinding * binding, int updown);
240 virtual void setButton (int button) { _button = button; }
241 virtual void setX (int x) { _x = x; }
242 virtual void setY (int y) { _y = y; }
243 virtual void setWidth (int w) { _w = w; }
244 virtual void setHeight (int h) { _h = h; }
246 // Check whether we're in the area.
247 virtual bool inArea (int button, int x, int y)
249 return (button == _button &&
256 // Perform the action.
257 virtual bool doAction (int updown);
260 typedef std::vector<SGBinding *> binding_list_t;
269 binding_list_t _bindings[2];
274 ////////////////////////////////////////////////////////////////////////
276 ////////////////////////////////////////////////////////////////////////
280 * A transformation for a layer.
282 class FGPanelTransformation : public SGConditional
292 FGPanelTransformation ();
293 virtual ~FGPanelTransformation ();
296 SGConstPropertyNode_ptr node;
303 SGInterpTable * table;
309 ////////////////////////////////////////////////////////////////////////
311 ////////////////////////////////////////////////////////////////////////
315 * A single layer of a multi-layered instrument.
317 * Each layer can be subject to a series of transformations based
318 * on current FGFS instrument readings: for example, a texture
319 * representing a needle can rotate to show the airspeed.
321 class FGInstrumentLayer : public SGConditional
325 FGInstrumentLayer (int w = -1, int h = -1);
326 virtual ~FGInstrumentLayer ();
328 virtual void draw (osg::State& state) = 0;
329 virtual void transform () const;
331 virtual int getWidth () const { return _w; }
332 virtual int getHeight () const { return _h; }
333 virtual void setWidth (int w) { _w = w; }
334 virtual void setHeight (int h) { _h = h; }
336 // Transfer pointer ownership!!
338 virtual void addTransformation (FGPanelTransformation * transformation);
343 typedef std::vector<FGPanelTransformation *> transformation_list;
344 transformation_list _transformations;
349 ////////////////////////////////////////////////////////////////////////
351 ////////////////////////////////////////////////////////////////////////
355 * Abstract base class for a panel instrument.
357 * A panel instrument consists of zero or more actions, associated
358 * with mouse clicks in rectangular areas. Currently, the only
359 * concrete class derived from this is FGLayeredInstrument, but others
360 * may show up in the future (some complex instruments could be
361 * entirely hand-coded, for example).
363 class FGPanelInstrument : public SGConditional
366 FGPanelInstrument ();
367 FGPanelInstrument (int x, int y, int w, int h);
368 virtual ~FGPanelInstrument ();
370 virtual void draw (osg::State& state) = 0;
371 virtual void drawHotspots(osg::State& state);
373 virtual void setPosition(int x, int y);
374 virtual void setSize(int w, int h);
376 virtual int getXPos () const;
377 virtual int getYPos () const;
378 virtual int getWidth () const;
379 virtual int getHeight () const;
381 // Coordinates relative to centre.
382 // Transfer pointer ownership!!
383 virtual void addAction (FGPanelAction * action);
385 // Coordinates relative to centre.
386 virtual bool doMouseAction (int button, int updown, int x, int y);
390 typedef std::vector<FGPanelAction *> action_list_type;
391 action_list_type _actions;
396 * An instrument constructed of multiple layers.
398 * Each individual layer can be rotated or shifted to correspond
399 * to internal FGFS instrument readings.
401 class FGLayeredInstrument : public FGPanelInstrument
404 FGLayeredInstrument (int x, int y, int w, int h);
405 virtual ~FGLayeredInstrument ();
407 virtual void draw (osg::State& state);
409 // Transfer pointer ownership!!
410 virtual int addLayer (FGInstrumentLayer *layer);
411 virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
413 // Transfer pointer ownership!!
414 virtual void addTransformation (FGPanelTransformation * transformation);
417 typedef std::vector<FGInstrumentLayer *> layer_list;
423 * An empty-shell instrument that exists soley in
424 * order to redirect commands from the panel to a
425 * complex instrument inherited from SGSubsystem.
427 * Currently the only complex instrument is the KLN89,
428 * which we've hardwired this to for now.
430 class FGSpecialInstrument : public FGPanelInstrument
433 FGSpecialInstrument(DCLGPS* sb);
434 //FGSpecialInstrument (int x, int y, int w, int h);
435 virtual ~FGSpecialInstrument ();
437 virtual void draw (osg::State& state);
445 * An instrument layer containing a group of sublayers.
447 * This class is useful for gathering together a group of related
448 * layers, either to hold in an external file or to work under
449 * the same condition.
451 class FGGroupLayer : public FGInstrumentLayer
455 virtual ~FGGroupLayer ();
456 virtual void draw (osg::State& state);
457 // transfer pointer ownership
458 virtual void addLayer (FGInstrumentLayer * layer);
460 std::vector<FGInstrumentLayer *> _layers;
465 * A textured layer of an instrument.
467 * This is a layer holding a single texture. Normally, the texture's
468 * backgound should be transparent so that lower layers and the panel
469 * background can show through.
471 class FGTexturedLayer : public FGInstrumentLayer
474 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
475 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
476 virtual ~FGTexturedLayer ();
478 virtual void draw (osg::State& state);
480 virtual void setTexture (const FGCroppedTexture &texture) {
483 virtual const FGCroppedTexture &getTexture () const { return _texture; }
484 virtual FGCroppedTexture *getTexture() { return &_texture; }
486 void setEmissive(bool e) { _emissive = e; }
489 FGCroppedTexture _texture;
495 * A text layer of an instrument.
497 * This is a layer holding a string of static and/or generated text.
498 * It is useful for instruments that have text displays, such as
499 * a chronometer, GPS, or NavCom radio.
501 class FGTextLayer : public FGInstrumentLayer
510 class Chunk : public SGConditional
513 Chunk (const std::string &text, const std::string &fmt = "%s");
514 Chunk (ChunkType type, const SGPropertyNode * node,
515 const std::string &fmt = "", float mult = 1.0, float offs = 0.0,
516 bool truncation = false);
518 const char * getValue () const;
522 SGConstPropertyNode_ptr _node;
527 mutable char _buf[1024];
530 FGTextLayer (int w = -1, int h = -1);
531 virtual ~FGTextLayer ();
533 virtual void draw (osg::State& state);
535 // Transfer pointer!!
536 virtual void addChunk (Chunk * chunk);
537 virtual void setColor (float r, float g, float b);
538 virtual void setPointSize (float size);
539 virtual void setFontName ( const std::string &name );
540 virtual void setFont (fntFont * font);
544 void recalc_value () const;
546 typedef std::vector<Chunk *> chunk_list;
551 mutable std::string _font_name;
552 mutable std::string _value;
553 mutable SGTimeStamp _then;
554 mutable SGTimeStamp _now;
559 * A group layer that switches among its children.
561 * The first layer that passes its condition will be drawn, and
562 * any following layers will be ignored.
564 class FGSwitchLayer : public FGGroupLayer
567 // Transfer pointers!!
569 virtual void draw (osg::State& state);
576 ////////////////////////////////////////////////////////////////////////
578 ////////////////////////////////////////////////////////////////////////
581 * Test whether the panel should be visible.
583 bool fgPanelVisible ();
587 #endif // __PANEL_HXX