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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # error This library requires C++
33 #include <simgear/compiler.h>
42 #include <simgear/math/interpolater.hxx>
43 #include <simgear/misc/props.hxx>
44 #include <simgear/timing/timestamp.hxx>
50 #include <Main/fgfs.hxx>
51 #include <Main/fg_props.hxx>
53 #include <Input/input.hxx>
58 class FGPanelInstrument;
62 ////////////////////////////////////////////////////////////////////////
63 // Texture management.
64 ////////////////////////////////////////////////////////////////////////
68 * Texture manager (should migrate out into FGFS).
70 * This class ensures that no texture is loaded more than once.
72 class FGTextureManager
75 static ssgTexture * createTexture(const string &relativePath);
77 static map<string,ssgTexture *> _textureMap;
82 * Cropped texture (should migrate out into FGFS).
84 * This structure wraps an SSG texture with cropping information.
86 class FGCroppedTexture
91 FGCroppedTexture (const string &path,
92 float _minX = 0.0, float _minY = 0.0,
93 float _maxX = 1.0, float _maxY = 1.0);
94 virtual ~FGCroppedTexture ();
96 virtual void setPath (const string &path) { _path = path; }
98 virtual const string &getPath () const { return _path; }
100 virtual ssgTexture * getTexture ();
102 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
103 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
106 virtual float getMinX () const { return _minX; }
107 virtual float getMinY () const { return _minY; }
108 virtual float getMaxX () const { return _maxX; }
109 virtual float getMaxY () const { return _maxY; }
114 ssgTexture * _texture;
115 float _minX, _minY, _maxX, _maxY;
120 ////////////////////////////////////////////////////////////////////////
122 ////////////////////////////////////////////////////////////////////////
126 * Instrument panel class.
128 * The panel is a container that has a background texture and holds
129 * zero or more instruments. The panel will order the instruments to
130 * redraw themselves when necessary, and will pass mouse clicks on to
131 * the appropriate instruments for processing.
133 class FGPanel : public FGSubsystem
140 // Update the panel (every frame).
141 virtual void init ();
142 virtual void bind ();
143 virtual void unbind ();
144 virtual void draw ();
145 virtual void update (double dt);
146 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
148 // transfer pointer ownership!!!
149 virtual void addInstrument (FGPanelInstrument * instrument);
151 // Background texture.
152 virtual void setBackground (ssgTexture * texture);
154 // Background multiple textures.
155 virtual void setMultiBackground (ssgTexture * texture, int idx);
157 // Make the panel visible or invisible.
158 virtual bool getVisibility () const;
159 virtual void setVisibility (bool visibility);
161 // Full width of panel.
162 virtual void setWidth (int width) { _width = width; }
163 virtual int getWidth () const { return _width; }
165 // Full height of panel.
166 virtual void setHeight (int height) { _height = height; }
167 virtual int getHeight () const { return _height; }
170 virtual void setXOffset (int offset);
171 virtual int getXOffset () const { return _x_offset; }
174 virtual void setYOffset (int offset);
175 virtual int getYOffset () const { return _y_offset; }
178 virtual void setViewHeight (int height) { _view_height = height; }
179 virtual int getViewHeight () const { return _view_height; }
181 // Handle a mouse click.
182 virtual bool doMouseAction (int button, int updown, int x, int y);
185 void setupVirtualCockpit();
186 void cleanupVirtualCockpit();
188 mutable bool _visibility;
189 mutable bool _mouseDown;
190 mutable int _mouseButton, _mouseX, _mouseY;
191 mutable int _mouseDelay;
192 mutable FGPanelInstrument * _mouseInstrument;
193 typedef vector<FGPanelInstrument *> instrument_list_type;
201 const SGPropertyNode * _xsize_node;
202 const SGPropertyNode * _ysize_node;
205 ssgTexture * _mbg[8];
206 // List of instruments in panel.
207 instrument_list_type _instruments;
212 ////////////////////////////////////////////////////////////////////////
214 ////////////////////////////////////////////////////////////////////////
218 * Class for user actions.
220 * The actions are command bindings, like bindings for the keyboard
221 * or joystick, but they are tied to specific mouse actions in
222 * rectangular areas of the panel.
224 class FGPanelAction : public FGConditional
228 FGPanelAction (int button, int x, int y, int w, int h);
229 virtual ~FGPanelAction ();
232 virtual int getButton () const { return _button; }
233 virtual int getX () const { return _x; }
234 virtual int getY () const { return _y; }
235 virtual int getWidth () const { return _w; }
236 virtual int getHeight () const { return _h; }
240 // transfer pointer ownership
241 virtual void addBinding (FGBinding * binding);
242 virtual void setButton (int button) { _button = button; }
243 virtual void setX (int x) { _x = x; }
244 virtual void setY (int y) { _y = y; }
245 virtual void setWidth (int w) { _w = w; }
246 virtual void setHeight (int h) { _h = h; }
248 // Check whether we're in the area.
249 virtual bool inArea (int button, int x, int y)
251 return (button == _button &&
258 // Perform the action.
259 virtual void doAction ();
262 typedef vector<FGBinding *> binding_list_t;
269 binding_list_t _bindings;
274 ////////////////////////////////////////////////////////////////////////
276 ////////////////////////////////////////////////////////////////////////
280 * A transformation for a layer.
282 class FGPanelTransformation : public FGConditional
292 FGPanelTransformation ();
293 virtual ~FGPanelTransformation ();
296 const SGPropertyNode * node;
301 SGInterpTable * table;
307 ////////////////////////////////////////////////////////////////////////
309 ////////////////////////////////////////////////////////////////////////
313 * A single layer of a multi-layered instrument.
315 * Each layer can be subject to a series of transformations based
316 * on current FGFS instrument readings: for example, a texture
317 * representing a needle can rotate to show the airspeed.
319 class FGInstrumentLayer : public FGConditional
323 FGInstrumentLayer (int w = -1, int h = -1);
324 virtual ~FGInstrumentLayer ();
326 virtual void draw () = 0;
327 virtual void transform () const;
329 virtual int getWidth () const { return _w; }
330 virtual int getHeight () const { return _h; }
331 virtual void setWidth (int w) { _w = w; }
332 virtual void setHeight (int h) { _h = h; }
334 // Transfer pointer ownership!!
336 virtual void addTransformation (FGPanelTransformation * transformation);
341 typedef vector<FGPanelTransformation *> transformation_list;
342 transformation_list _transformations;
347 ////////////////////////////////////////////////////////////////////////
349 ////////////////////////////////////////////////////////////////////////
353 * Abstract base class for a panel instrument.
355 * A panel instrument consists of zero or more actions, associated
356 * with mouse clicks in rectangular areas. Currently, the only
357 * concrete class derived from this is FGLayeredInstrument, but others
358 * may show up in the future (some complex instruments could be
359 * entirely hand-coded, for example).
361 class FGPanelInstrument : public FGConditional
364 FGPanelInstrument ();
365 FGPanelInstrument (int x, int y, int w, int h);
366 virtual ~FGPanelInstrument ();
368 virtual void draw () = 0;
370 virtual void setPosition(int x, int y);
371 virtual void setSize(int w, int h);
373 virtual int getXPos () const;
374 virtual int getYPos () const;
375 virtual int getWidth () const;
376 virtual int getHeight () const;
378 // Coordinates relative to centre.
379 // Transfer pointer ownership!!
380 virtual void addAction (FGPanelAction * action);
382 // Coordinates relative to centre.
383 virtual bool doMouseAction (int button, int x, int y);
387 typedef vector<FGPanelAction *> action_list_type;
388 action_list_type _actions;
393 * An instrument constructed of multiple layers.
395 * Each individual layer can be rotated or shifted to correspond
396 * to internal FGFS instrument readings.
398 class FGLayeredInstrument : public FGPanelInstrument
401 FGLayeredInstrument (int x, int y, int w, int h);
402 virtual ~FGLayeredInstrument ();
404 virtual void draw ();
406 // Transfer pointer ownership!!
407 virtual int addLayer (FGInstrumentLayer *layer);
408 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
410 // Transfer pointer ownership!!
411 virtual void addTransformation (FGPanelTransformation * transformation);
414 typedef vector<FGInstrumentLayer *> layer_list;
420 * An instrument layer containing a group of sublayers.
422 * This class is useful for gathering together a group of related
423 * layers, either to hold in an external file or to work under
424 * the same condition.
426 class FGGroupLayer : public FGInstrumentLayer
430 virtual ~FGGroupLayer ();
431 virtual void draw ();
432 // transfer pointer ownership
433 virtual void addLayer (FGInstrumentLayer * layer);
435 vector<FGInstrumentLayer *> _layers;
440 * A textured layer of an instrument.
442 * This is a layer holding a single texture. Normally, the texture's
443 * backgound should be transparent so that lower layers and the panel
444 * background can show through.
446 class FGTexturedLayer : public FGInstrumentLayer
449 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
450 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
451 virtual ~FGTexturedLayer ();
453 virtual void draw ();
455 virtual void setTexture (const FGCroppedTexture &texture) {
458 virtual FGCroppedTexture &getTexture () { return _texture; }
459 virtual const FGCroppedTexture &getTexture () const { return _texture; }
462 mutable FGCroppedTexture _texture;
467 * A text layer of an instrument.
469 * This is a layer holding a string of static and/or generated text.
470 * It is useful for instruments that have text displays, such as
471 * a chronometer, GPS, or NavCom radio.
473 class FGTextLayer : public FGInstrumentLayer
482 class Chunk : public FGConditional
485 Chunk (const string &text, const string &fmt = "%s");
486 Chunk (ChunkType type, const SGPropertyNode * node,
487 const string &fmt = "", float mult = 1.0);
489 const char * getValue () const;
493 const SGPropertyNode * _node;
496 mutable char _buf[1024];
499 FGTextLayer (int w = -1, int h = -1);
500 virtual ~FGTextLayer ();
502 virtual void draw ();
504 // Transfer pointer!!
505 virtual void addChunk (Chunk * chunk);
506 virtual void setColor (float r, float g, float b);
507 virtual void setPointSize (float size);
508 virtual void setFontName ( const string &name );
509 virtual void setFont (fntFont * font);
513 void recalc_value () const;
515 typedef vector<Chunk *> chunk_list;
520 mutable string _font_name;
521 mutable string _value;
522 mutable SGTimeStamp _then;
523 mutable SGTimeStamp _now;
528 * A layer that switches between two other layers.
530 * The usefulness of this layer is questionable now that all layers
531 * can have conditions, and it may be deprecated soon.
533 class FGSwitchLayer : public FGInstrumentLayer
536 // Transfer pointers!!
537 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
538 FGInstrumentLayer * layer1,
539 FGInstrumentLayer * layer2);
540 virtual ~FGSwitchLayer ();
542 virtual void draw ();
545 const SGPropertyNode * _node;
546 FGInstrumentLayer * _layer1, * _layer2;
552 ////////////////////////////////////////////////////////////////////////
554 ////////////////////////////////////////////////////////////////////////
557 * Test whether the panel should be visible.
559 bool fgPanelVisible ();
563 ////////////////////////////////////////////////////////////////////////
564 // The current panel, if any.
565 ////////////////////////////////////////////////////////////////////////
567 extern FGPanel * current_panel; // TODO: move to globals
571 #endif // __PANEL_HXX