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/SGBinding.hxx>
33 #include <simgear/math/interpolater.hxx>
34 #include <simgear/timing/timestamp.hxx>
40 #include <Main/fg_props.hxx>
42 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 SGReferenced
127 virtual void draw (osg::State& state);
129 virtual void updateMouseDelay(double dt);
131 // transfer pointer ownership!!!
132 virtual void addInstrument (FGPanelInstrument * instrument);
134 // Background texture.
135 virtual void setBackground (osg::Texture2D* texture);
137 // Background multiple textures.
138 virtual void setMultiBackground (osg::Texture2D* texture, int idx);
140 virtual void setWidth (int width) { _width = width; }
141 virtual int getWidth () const { return _width; }
143 // Full height of panel.
144 virtual void setHeight (int height) { _height = height; }
145 virtual int getHeight () const { return _height; }
148 virtual void setXOffset (int offset);
149 virtual int getXOffset () const { return _x_offset->getIntValue(); }
152 virtual void setYOffset (int offset);
153 virtual int getYOffset () const { return _y_offset->getIntValue(); }
156 // virtual void setViewHeight (int height) { _view_height = height; }
157 // virtual int getViewHeight () const { return _view_height; }
160 * find the actual logical extend of the panel, including all instruments
163 void getLogicalExtent(int &x0, int& y0, int& x1, int &y1);
165 // Handle a mouse click.
166 virtual bool doMouseAction (int button, int updown, int x, int y);
167 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
169 virtual void setDepthTest (bool enable);
171 bool getAutohide(void) const { return _autohide; };
172 void setAutohide(bool enable) { _autohide = enable; };
174 double getAspectScale() const;
177 void setupVirtualCockpit();
178 void cleanupVirtualCockpit();
180 mutable bool _mouseDown;
181 mutable int _mouseButton, _mouseX, _mouseY;
182 double _mouseActionRepeat;
184 mutable FGPanelInstrument * _mouseInstrument;
185 typedef std::vector<FGPanelInstrument *> instrument_list_type;
190 SGPropertyNode_ptr _x_offset;
191 SGPropertyNode_ptr _y_offset;
192 SGPropertyNode_ptr _jitter;
193 SGPropertyNode_ptr _flipx;
195 SGConstPropertyNode_ptr _xsize_node;
196 SGConstPropertyNode_ptr _ysize_node;
198 osg::ref_ptr<osg::StateSet> _bg;
199 osg::ref_ptr<osg::StateSet> _mbg[8];
200 // List of instruments in panel.
201 instrument_list_type _instruments;
202 bool _enable_depth_test;
205 SGPropObjBool _drawPanelHotspots;
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);
388 void extendRect(IntRect& r) const;
391 typedef std::vector<FGPanelAction *> action_list_type;
392 action_list_type _actions;
397 * An instrument constructed of multiple layers.
399 * Each individual layer can be rotated or shifted to correspond
400 * to internal FGFS instrument readings.
402 class FGLayeredInstrument : public FGPanelInstrument
405 FGLayeredInstrument (int x, int y, int w, int h);
406 virtual ~FGLayeredInstrument ();
408 virtual void draw (osg::State& state);
410 // Transfer pointer ownership!!
411 virtual int addLayer (FGInstrumentLayer *layer);
412 virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
414 // Transfer pointer ownership!!
415 virtual void addTransformation (FGPanelTransformation * transformation);
418 typedef std::vector<FGInstrumentLayer *> layer_list;
424 * An empty-shell instrument that exists soley in
425 * order to redirect commands from the panel to a
426 * complex instrument inherited from SGSubsystem.
428 * Currently the only complex instrument is the KLN89,
429 * which we've hardwired this to for now.
431 class FGSpecialInstrument : public FGPanelInstrument
434 FGSpecialInstrument(DCLGPS* sb);
435 //FGSpecialInstrument (int x, int y, int w, int h);
436 virtual ~FGSpecialInstrument ();
438 virtual void draw (osg::State& state);
446 * An instrument layer containing a group of sublayers.
448 * This class is useful for gathering together a group of related
449 * layers, either to hold in an external file or to work under
450 * the same condition.
452 class FGGroupLayer : public FGInstrumentLayer
456 virtual ~FGGroupLayer ();
457 virtual void draw (osg::State& state);
458 // transfer pointer ownership
459 virtual void addLayer (FGInstrumentLayer * layer);
461 std::vector<FGInstrumentLayer *> _layers;
466 * A textured layer of an instrument.
468 * This is a layer holding a single texture. Normally, the texture's
469 * backgound should be transparent so that lower layers and the panel
470 * background can show through.
472 class FGTexturedLayer : public FGInstrumentLayer
475 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
476 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
477 virtual ~FGTexturedLayer ();
479 virtual void draw (osg::State& state);
481 virtual void setTexture (const FGCroppedTexture &texture) {
484 virtual const FGCroppedTexture &getTexture () const { return _texture; }
485 virtual FGCroppedTexture *getTexture() { return &_texture; }
487 void setEmissive(bool e) { _emissive = e; }
490 FGCroppedTexture _texture;
496 * A text layer of an instrument.
498 * This is a layer holding a string of static and/or generated text.
499 * It is useful for instruments that have text displays, such as
500 * a chronometer, GPS, or NavCom radio.
502 class FGTextLayer : public FGInstrumentLayer
511 class Chunk : public SGConditional
514 Chunk (const std::string &text, const std::string &fmt = "%s");
515 Chunk (ChunkType type, const SGPropertyNode * node,
516 const std::string &fmt = "", float mult = 1.0, float offs = 0.0,
517 bool truncation = false);
519 const char * getValue () const;
523 SGConstPropertyNode_ptr _node;
528 mutable char _buf[1024];
531 FGTextLayer (int w = -1, int h = -1);
532 virtual ~FGTextLayer ();
534 virtual void draw (osg::State& state);
536 // Transfer pointer!!
537 virtual void addChunk (Chunk * chunk);
538 virtual void setColor (float r, float g, float b);
539 virtual void setPointSize (float size);
540 virtual void setFontName ( const std::string &name );
541 virtual void setFont (fntFont * font);
545 void recalc_value () const;
547 typedef std::vector<Chunk *> chunk_list;
552 mutable std::string _font_name;
553 mutable std::string _value;
554 mutable SGTimeStamp _then;
555 mutable SGTimeStamp _now;
560 * A group layer that switches among its children.
562 * The first layer that passes its condition will be drawn, and
563 * any following layers will be ignored.
565 class FGSwitchLayer : public FGGroupLayer
568 // Transfer pointers!!
570 virtual void draw (osg::State& state);
573 #endif // __PANEL_HXX