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.
25 # error This library requires C++
37 #include <osg/ref_ptr>
38 #include <osg/StateSet>
39 #include <osg/Texture2D>
43 #include <simgear/compiler.h>
44 #include <simgear/props/props.hxx>
45 #include <simgear/structure/subsystem_mgr.hxx>
46 #include <simgear/math/interpolater.hxx>
47 #include <simgear/timing/timestamp.hxx>
53 #include <Main/fg_props.hxx>
54 #include <Input/input.hxx>
55 #include <Instrumentation/dclgps.hxx>
61 class FGPanelInstrument;
65 ////////////////////////////////////////////////////////////////////////
66 // Texture management.
67 ////////////////////////////////////////////////////////////////////////
71 * Texture manager (should migrate out into FGFS).
73 * This class ensures that no texture is loaded more than once.
75 class FGTextureManager
78 static osg::Texture2D* createTexture(const string &relativePath);
79 static void addTexture(const string &relativePath, osg::Texture2D* texture);
81 static map<string,osg::ref_ptr<osg::Texture2D> > _textureMap;
86 * Cropped texture (should migrate out into FGFS).
88 * This structure wraps an SSG texture with cropping information.
90 class FGCroppedTexture
95 FGCroppedTexture (const string &path,
96 float _minX = 0.0, float _minY = 0.0,
97 float _maxX = 1.0, float _maxY = 1.0);
98 virtual ~FGCroppedTexture ();
100 virtual void setPath (const string &path) { _path = path; }
102 virtual const string &getPath () const { return _path; }
104 virtual osg::StateSet* getTexture ();
106 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
107 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
110 virtual float getMinX () const { return _minX; }
111 virtual float getMinY () const { return _minY; }
112 virtual float getMaxX () const { return _maxX; }
113 virtual float getMaxY () const { return _maxY; }
118 osg::ref_ptr<osg::StateSet> _texture;
119 float _minX, _minY, _maxX, _maxY;
124 ////////////////////////////////////////////////////////////////////////
126 ////////////////////////////////////////////////////////////////////////
130 * Instrument panel class.
132 * The panel is a container that has a background texture and holds
133 * zero or more instruments. The panel will order the instruments to
134 * redraw themselves when necessary, and will pass mouse clicks on to
135 * the appropriate instruments for processing.
137 class FGPanel : public SGSubsystem
144 // Update the panel (every frame).
145 virtual void init ();
146 virtual void bind ();
147 virtual void unbind ();
148 virtual void draw (osg::State& state);
149 virtual void update (double);
150 void update (osg::State& state);
151 virtual void update (osg::State& state, GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
153 virtual void updateMouseDelay();
155 // transfer pointer ownership!!!
156 virtual void addInstrument (FGPanelInstrument * instrument);
158 // Background texture.
159 virtual void setBackground (osg::Texture2D* texture);
161 // Background multiple textures.
162 virtual void setMultiBackground (osg::Texture2D* texture, int idx);
164 // Make the panel visible or invisible.
165 virtual bool getVisibility () const;
166 virtual void setVisibility (bool visibility);
168 // Full width of panel.
169 virtual void setWidth (int width) { _width = width; }
170 virtual int getWidth () const { return _width; }
172 // Full height of panel.
173 virtual void setHeight (int height) { _height = height; }
174 virtual int getHeight () const { return _height; }
177 virtual void setXOffset (int offset);
178 virtual int getXOffset () const { return _x_offset->getIntValue(); }
181 virtual void setYOffset (int offset);
182 virtual int getYOffset () const { return _y_offset->getIntValue(); }
185 virtual void setViewHeight (int height) { _view_height = height; }
186 virtual int getViewHeight () const { return _view_height; }
188 // Handle a mouse click.
189 virtual bool doMouseAction (int button, int updown, int x, int y);
190 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
192 virtual void setDepthTest (bool enable);
194 bool getAutohide(void) const { return _autohide; };
195 void setAutohide(bool enable) { _autohide = enable; };
198 void setupVirtualCockpit();
199 void cleanupVirtualCockpit();
201 mutable bool _mouseDown;
202 mutable int _mouseButton, _mouseX, _mouseY;
203 mutable int _mouseDelay;
204 mutable FGPanelInstrument * _mouseInstrument;
205 typedef vector<FGPanelInstrument *> instrument_list_type;
210 SGPropertyNode_ptr _visibility;
211 SGPropertyNode_ptr _x_offset;
212 SGPropertyNode_ptr _y_offset;
213 SGPropertyNode_ptr _jitter;
214 SGPropertyNode_ptr _flipx;
216 SGConstPropertyNode_ptr _xsize_node;
217 SGConstPropertyNode_ptr _ysize_node;
219 osg::ref_ptr<osg::StateSet> _bg;
220 osg::ref_ptr<osg::StateSet> _mbg[8];
221 // List of instruments in panel.
222 instrument_list_type _instruments;
223 bool _enable_depth_test;
229 ////////////////////////////////////////////////////////////////////////
231 ////////////////////////////////////////////////////////////////////////
235 * Class for user actions.
237 * The actions are command bindings, like bindings for the keyboard
238 * or joystick, but they are tied to specific mouse actions in
239 * rectangular areas of the panel.
241 class FGPanelAction : public SGConditional
245 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
246 virtual ~FGPanelAction ();
249 virtual int getButton () const { return _button; }
250 virtual int getX () const { return _x; }
251 virtual int getY () const { return _y; }
252 virtual int getWidth () const { return _w; }
253 virtual int getHeight () const { return _h; }
257 // transfer pointer ownership
258 virtual void addBinding (SGBinding * binding, int updown);
259 virtual void setButton (int button) { _button = button; }
260 virtual void setX (int x) { _x = x; }
261 virtual void setY (int y) { _y = y; }
262 virtual void setWidth (int w) { _w = w; }
263 virtual void setHeight (int h) { _h = h; }
265 // Check whether we're in the area.
266 virtual bool inArea (int button, int x, int y)
268 return (button == _button &&
275 // Perform the action.
276 virtual bool doAction (int updown);
279 typedef vector<SGBinding *> binding_list_t;
288 binding_list_t _bindings[2];
293 ////////////////////////////////////////////////////////////////////////
295 ////////////////////////////////////////////////////////////////////////
299 * A transformation for a layer.
301 class FGPanelTransformation : public SGConditional
311 FGPanelTransformation ();
312 virtual ~FGPanelTransformation ();
315 SGConstPropertyNode_ptr node;
322 SGInterpTable * table;
328 ////////////////////////////////////////////////////////////////////////
330 ////////////////////////////////////////////////////////////////////////
334 * A single layer of a multi-layered instrument.
336 * Each layer can be subject to a series of transformations based
337 * on current FGFS instrument readings: for example, a texture
338 * representing a needle can rotate to show the airspeed.
340 class FGInstrumentLayer : public SGConditional
344 FGInstrumentLayer (int w = -1, int h = -1);
345 virtual ~FGInstrumentLayer ();
347 virtual void draw (osg::State& state) = 0;
348 virtual void transform () const;
350 virtual int getWidth () const { return _w; }
351 virtual int getHeight () const { return _h; }
352 virtual void setWidth (int w) { _w = w; }
353 virtual void setHeight (int h) { _h = h; }
355 // Transfer pointer ownership!!
357 virtual void addTransformation (FGPanelTransformation * transformation);
362 typedef vector<FGPanelTransformation *> transformation_list;
363 transformation_list _transformations;
368 ////////////////////////////////////////////////////////////////////////
370 ////////////////////////////////////////////////////////////////////////
374 * Abstract base class for a panel instrument.
376 * A panel instrument consists of zero or more actions, associated
377 * with mouse clicks in rectangular areas. Currently, the only
378 * concrete class derived from this is FGLayeredInstrument, but others
379 * may show up in the future (some complex instruments could be
380 * entirely hand-coded, for example).
382 class FGPanelInstrument : public SGConditional
385 FGPanelInstrument ();
386 FGPanelInstrument (int x, int y, int w, int h);
387 virtual ~FGPanelInstrument ();
389 virtual void draw (osg::State& state) = 0;
390 virtual void drawHotspots(osg::State& state);
392 virtual void setPosition(int x, int y);
393 virtual void setSize(int w, int h);
395 virtual int getXPos () const;
396 virtual int getYPos () const;
397 virtual int getWidth () const;
398 virtual int getHeight () const;
400 // Coordinates relative to centre.
401 // Transfer pointer ownership!!
402 virtual void addAction (FGPanelAction * action);
404 // Coordinates relative to centre.
405 virtual bool doMouseAction (int button, int updown, int x, int y);
409 typedef vector<FGPanelAction *> action_list_type;
410 action_list_type _actions;
415 * An instrument constructed of multiple layers.
417 * Each individual layer can be rotated or shifted to correspond
418 * to internal FGFS instrument readings.
420 class FGLayeredInstrument : public FGPanelInstrument
423 FGLayeredInstrument (int x, int y, int w, int h);
424 virtual ~FGLayeredInstrument ();
426 virtual void draw (osg::State& state);
428 // Transfer pointer ownership!!
429 virtual int addLayer (FGInstrumentLayer *layer);
430 virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
432 // Transfer pointer ownership!!
433 virtual void addTransformation (FGPanelTransformation * transformation);
436 typedef vector<FGInstrumentLayer *> layer_list;
442 * An empty-shell instrument that exists soley in
443 * order to redirect commands from the panel to a
444 * complex instrument inherited from SGSubsystem.
446 * Currently the only complex instrument is the KLN89,
447 * which we've hardwired this to for now.
449 class FGSpecialInstrument : public FGPanelInstrument
452 FGSpecialInstrument(DCLGPS* sb);
453 //FGSpecialInstrument (int x, int y, int w, int h);
454 virtual ~FGSpecialInstrument ();
456 virtual void draw (osg::State& state);
464 * An instrument layer containing a group of sublayers.
466 * This class is useful for gathering together a group of related
467 * layers, either to hold in an external file or to work under
468 * the same condition.
470 class FGGroupLayer : public FGInstrumentLayer
474 virtual ~FGGroupLayer ();
475 virtual void draw (osg::State& state);
476 // transfer pointer ownership
477 virtual void addLayer (FGInstrumentLayer * layer);
479 vector<FGInstrumentLayer *> _layers;
484 * A textured layer of an instrument.
486 * This is a layer holding a single texture. Normally, the texture's
487 * backgound should be transparent so that lower layers and the panel
488 * background can show through.
490 class FGTexturedLayer : public FGInstrumentLayer
493 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
494 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
495 virtual ~FGTexturedLayer ();
497 virtual void draw (osg::State& state);
499 virtual void setTexture (const FGCroppedTexture &texture) {
502 virtual const FGCroppedTexture &getTexture () const { return _texture; }
503 virtual FGCroppedTexture *getTexture() { return &_texture; }
505 void setEmissive(bool e) { _emissive = e; }
508 FGCroppedTexture _texture;
514 * A text layer of an instrument.
516 * This is a layer holding a string of static and/or generated text.
517 * It is useful for instruments that have text displays, such as
518 * a chronometer, GPS, or NavCom radio.
520 class FGTextLayer : public FGInstrumentLayer
529 class Chunk : public SGConditional
532 Chunk (const string &text, const string &fmt = "%s");
533 Chunk (ChunkType type, const SGPropertyNode * node,
534 const string &fmt = "", float mult = 1.0, float offs = 0.0,
535 bool truncation = false);
537 const char * getValue () const;
541 SGConstPropertyNode_ptr _node;
546 mutable char _buf[1024];
549 FGTextLayer (int w = -1, int h = -1);
550 virtual ~FGTextLayer ();
552 virtual void draw (osg::State& state);
554 // Transfer pointer!!
555 virtual void addChunk (Chunk * chunk);
556 virtual void setColor (float r, float g, float b);
557 virtual void setPointSize (float size);
558 virtual void setFontName ( const string &name );
559 virtual void setFont (fntFont * font);
563 void recalc_value () const;
565 typedef vector<Chunk *> chunk_list;
570 mutable string _font_name;
571 mutable string _value;
572 mutable SGTimeStamp _then;
573 mutable SGTimeStamp _now;
578 * A group layer that switches among its children.
580 * The first layer that passes its condition will be drawn, and
581 * any following layers will be ignored.
583 class FGSwitchLayer : public FGGroupLayer
586 // Transfer pointers!!
588 virtual void draw (osg::State& state);
595 ////////////////////////////////////////////////////////////////////////
597 ////////////////////////////////////////////////////////////////////////
600 * Test whether the panel should be visible.
602 bool fgPanelVisible ();
606 #endif // __PANEL_HXX