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/props/PropertyObject.hxx>
32 #include <simgear/structure/subsystem_mgr.hxx>
33 #include <simgear/structure/SGBinding.hxx>
34 #include <simgear/math/interpolater.hxx>
35 #include <simgear/timing/timestamp.hxx>
41 #include <Main/fg_props.hxx>
43 class FGPanelInstrument;
47 ////////////////////////////////////////////////////////////////////////
48 // Texture management.
49 ////////////////////////////////////////////////////////////////////////
53 * Texture manager (should migrate out into FGFS).
55 * This class ensures that no texture is loaded more than once.
57 class FGTextureManager
60 static osg::Texture2D* createTexture(const std::string &relativePath,
61 bool staticTexture = true);
62 static void addTexture(const std::string &relativePath, osg::Texture2D* texture);
64 static std::map<std::string,osg::ref_ptr<osg::Texture2D> > _textureMap;
69 * Cropped texture (should migrate out into FGFS).
71 * This structure wraps an SSG texture with cropping information.
73 class FGCroppedTexture
78 FGCroppedTexture (const std::string &path,
79 float _minX = 0.0, float _minY = 0.0,
80 float _maxX = 1.0, float _maxY = 1.0);
81 virtual ~FGCroppedTexture ();
83 virtual void setPath (const std::string &path) { _path = path; }
85 virtual const std::string &getPath () const { return _path; }
87 virtual osg::StateSet* getTexture ();
89 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
90 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
93 virtual float getMinX () const { return _minX; }
94 virtual float getMinY () const { return _minY; }
95 virtual float getMaxX () const { return _maxX; }
96 virtual float getMaxY () const { return _maxY; }
101 osg::ref_ptr<osg::StateSet> _texture;
102 float _minX, _minY, _maxX, _maxY;
107 ////////////////////////////////////////////////////////////////////////
109 ////////////////////////////////////////////////////////////////////////
113 * Instrument panel class.
115 * The panel is a container that has a background texture and holds
116 * zero or more instruments. The panel will order the instruments to
117 * redraw themselves when necessary, and will pass mouse clicks on to
118 * the appropriate instruments for processing.
120 class FGPanel : public SGSubsystem
127 // Update the panel (every frame).
128 virtual void init ();
129 virtual void bind ();
130 virtual void unbind ();
131 virtual void draw (osg::State& state);
132 virtual void update (double);
134 //void update (osg::State& state);
135 //virtual void update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
137 virtual void updateMouseDelay();
139 // transfer pointer ownership!!!
140 virtual void addInstrument (FGPanelInstrument * instrument);
142 // Background texture.
143 virtual void setBackground (osg::Texture2D* texture);
145 // Background multiple textures.
146 virtual void setMultiBackground (osg::Texture2D* texture, int idx);
148 // Make the panel visible or invisible.
149 virtual bool getVisibility () const;
150 virtual void setVisibility (bool visibility);
152 // Full width of panel.
153 virtual void setWidth (int width) { _width = width; }
154 virtual int getWidth () const { return _width; }
156 // Full height of panel.
157 virtual void setHeight (int height) { _height = height; }
158 virtual int getHeight () const { return _height; }
161 virtual void setXOffset (int offset);
162 virtual int getXOffset () const { return _x_offset->getIntValue(); }
165 virtual void setYOffset (int offset);
166 virtual int getYOffset () const { return _y_offset->getIntValue(); }
169 virtual void setViewHeight (int height) { _view_height = height; }
170 virtual int getViewHeight () const { return _view_height; }
172 // Handle a mouse click.
173 virtual bool doMouseAction (int button, int updown, int x, int y);
174 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
176 virtual void setDepthTest (bool enable);
178 bool getAutohide(void) const { return _autohide; };
179 void setAutohide(bool enable) { _autohide = enable; };
181 double getAspectScale() const;
184 void setupVirtualCockpit();
185 void cleanupVirtualCockpit();
187 mutable bool _mouseDown;
188 mutable int _mouseButton, _mouseX, _mouseY;
189 mutable int _mouseDelay;
190 mutable FGPanelInstrument * _mouseInstrument;
191 typedef std::vector<FGPanelInstrument *> instrument_list_type;
196 SGPropertyNode_ptr _visibility;
197 SGPropertyNode_ptr _x_offset;
198 SGPropertyNode_ptr _y_offset;
199 SGPropertyNode_ptr _jitter;
200 SGPropertyNode_ptr _flipx;
202 SGConstPropertyNode_ptr _xsize_node;
203 SGConstPropertyNode_ptr _ysize_node;
205 osg::ref_ptr<osg::StateSet> _bg;
206 osg::ref_ptr<osg::StateSet> _mbg[8];
207 // List of instruments in panel.
208 instrument_list_type _instruments;
209 bool _enable_depth_test;
212 SGPropObjBool _drawPanelHotspots;
217 ////////////////////////////////////////////////////////////////////////
219 ////////////////////////////////////////////////////////////////////////
223 * Class for user actions.
225 * The actions are command bindings, like bindings for the keyboard
226 * or joystick, but they are tied to specific mouse actions in
227 * rectangular areas of the panel.
229 class FGPanelAction : public SGConditional
233 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
234 virtual ~FGPanelAction ();
237 virtual int getButton () const { return _button; }
238 virtual int getX () const { return _x; }
239 virtual int getY () const { return _y; }
240 virtual int getWidth () const { return _w; }
241 virtual int getHeight () const { return _h; }
245 // transfer pointer ownership
246 virtual void addBinding (SGBinding * binding, int updown);
247 virtual void setButton (int button) { _button = button; }
248 virtual void setX (int x) { _x = x; }
249 virtual void setY (int y) { _y = y; }
250 virtual void setWidth (int w) { _w = w; }
251 virtual void setHeight (int h) { _h = h; }
253 // Check whether we're in the area.
254 virtual bool inArea (int button, int x, int y)
256 return (button == _button &&
263 // Perform the action.
264 virtual bool doAction (int updown);
267 typedef std::vector<SGBinding *> binding_list_t;
276 binding_list_t _bindings[2];
281 ////////////////////////////////////////////////////////////////////////
283 ////////////////////////////////////////////////////////////////////////
287 * A transformation for a layer.
289 class FGPanelTransformation : public SGConditional
299 FGPanelTransformation ();
300 virtual ~FGPanelTransformation ();
303 SGConstPropertyNode_ptr node;
310 SGInterpTable * table;
316 ////////////////////////////////////////////////////////////////////////
318 ////////////////////////////////////////////////////////////////////////
322 * A single layer of a multi-layered instrument.
324 * Each layer can be subject to a series of transformations based
325 * on current FGFS instrument readings: for example, a texture
326 * representing a needle can rotate to show the airspeed.
328 class FGInstrumentLayer : public SGConditional
332 FGInstrumentLayer (int w = -1, int h = -1);
333 virtual ~FGInstrumentLayer ();
335 virtual void draw (osg::State& state) = 0;
336 virtual void transform () const;
338 virtual int getWidth () const { return _w; }
339 virtual int getHeight () const { return _h; }
340 virtual void setWidth (int w) { _w = w; }
341 virtual void setHeight (int h) { _h = h; }
343 // Transfer pointer ownership!!
345 virtual void addTransformation (FGPanelTransformation * transformation);
350 typedef std::vector<FGPanelTransformation *> transformation_list;
351 transformation_list _transformations;
356 ////////////////////////////////////////////////////////////////////////
358 ////////////////////////////////////////////////////////////////////////
362 * Abstract base class for a panel instrument.
364 * A panel instrument consists of zero or more actions, associated
365 * with mouse clicks in rectangular areas. Currently, the only
366 * concrete class derived from this is FGLayeredInstrument, but others
367 * may show up in the future (some complex instruments could be
368 * entirely hand-coded, for example).
370 class FGPanelInstrument : public SGConditional
373 FGPanelInstrument ();
374 FGPanelInstrument (int x, int y, int w, int h);
375 virtual ~FGPanelInstrument ();
377 virtual void draw (osg::State& state) = 0;
378 virtual void drawHotspots(osg::State& state);
380 virtual void setPosition(int x, int y);
381 virtual void setSize(int w, int h);
383 virtual int getXPos () const;
384 virtual int getYPos () const;
385 virtual int getWidth () const;
386 virtual int getHeight () const;
388 // Coordinates relative to centre.
389 // Transfer pointer ownership!!
390 virtual void addAction (FGPanelAction * action);
392 // Coordinates relative to centre.
393 virtual bool doMouseAction (int button, int updown, int x, int y);
397 typedef std::vector<FGPanelAction *> action_list_type;
398 action_list_type _actions;
403 * An instrument constructed of multiple layers.
405 * Each individual layer can be rotated or shifted to correspond
406 * to internal FGFS instrument readings.
408 class FGLayeredInstrument : public FGPanelInstrument
411 FGLayeredInstrument (int x, int y, int w, int h);
412 virtual ~FGLayeredInstrument ();
414 virtual void draw (osg::State& state);
416 // Transfer pointer ownership!!
417 virtual int addLayer (FGInstrumentLayer *layer);
418 virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
420 // Transfer pointer ownership!!
421 virtual void addTransformation (FGPanelTransformation * transformation);
424 typedef std::vector<FGInstrumentLayer *> layer_list;
430 * An empty-shell instrument that exists soley in
431 * order to redirect commands from the panel to a
432 * complex instrument inherited from SGSubsystem.
434 * Currently the only complex instrument is the KLN89,
435 * which we've hardwired this to for now.
437 class FGSpecialInstrument : public FGPanelInstrument
440 FGSpecialInstrument(DCLGPS* sb);
441 //FGSpecialInstrument (int x, int y, int w, int h);
442 virtual ~FGSpecialInstrument ();
444 virtual void draw (osg::State& state);
452 * An instrument layer containing a group of sublayers.
454 * This class is useful for gathering together a group of related
455 * layers, either to hold in an external file or to work under
456 * the same condition.
458 class FGGroupLayer : public FGInstrumentLayer
462 virtual ~FGGroupLayer ();
463 virtual void draw (osg::State& state);
464 // transfer pointer ownership
465 virtual void addLayer (FGInstrumentLayer * layer);
467 std::vector<FGInstrumentLayer *> _layers;
472 * A textured layer of an instrument.
474 * This is a layer holding a single texture. Normally, the texture's
475 * backgound should be transparent so that lower layers and the panel
476 * background can show through.
478 class FGTexturedLayer : public FGInstrumentLayer
481 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
482 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
483 virtual ~FGTexturedLayer ();
485 virtual void draw (osg::State& state);
487 virtual void setTexture (const FGCroppedTexture &texture) {
490 virtual const FGCroppedTexture &getTexture () const { return _texture; }
491 virtual FGCroppedTexture *getTexture() { return &_texture; }
493 void setEmissive(bool e) { _emissive = e; }
496 FGCroppedTexture _texture;
502 * A text layer of an instrument.
504 * This is a layer holding a string of static and/or generated text.
505 * It is useful for instruments that have text displays, such as
506 * a chronometer, GPS, or NavCom radio.
508 class FGTextLayer : public FGInstrumentLayer
517 class Chunk : public SGConditional
520 Chunk (const std::string &text, const std::string &fmt = "%s");
521 Chunk (ChunkType type, const SGPropertyNode * node,
522 const std::string &fmt = "", float mult = 1.0, float offs = 0.0,
523 bool truncation = false);
525 const char * getValue () const;
529 SGConstPropertyNode_ptr _node;
534 mutable char _buf[1024];
537 FGTextLayer (int w = -1, int h = -1);
538 virtual ~FGTextLayer ();
540 virtual void draw (osg::State& state);
542 // Transfer pointer!!
543 virtual void addChunk (Chunk * chunk);
544 virtual void setColor (float r, float g, float b);
545 virtual void setPointSize (float size);
546 virtual void setFontName ( const std::string &name );
547 virtual void setFont (fntFont * font);
551 void recalc_value () const;
553 typedef std::vector<Chunk *> chunk_list;
558 mutable std::string _font_name;
559 mutable std::string _value;
560 mutable SGTimeStamp _then;
561 mutable SGTimeStamp _now;
566 * A group layer that switches among its children.
568 * The first layer that passes its condition will be drawn, and
569 * any following layers will be ignored.
571 class FGSwitchLayer : public FGGroupLayer
574 // Transfer pointers!!
576 virtual void draw (osg::State& state);
583 ////////////////////////////////////////////////////////////////////////
585 ////////////////////////////////////////////////////////////////////////
588 * Test whether the panel should be visible.
590 bool fgPanelVisible ();
594 #endif // __PANEL_HXX