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 update (int dt);
145 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
147 // transfer pointer ownership!!!
148 virtual void addInstrument (FGPanelInstrument * instrument);
150 // Background texture.
151 virtual void setBackground (ssgTexture * texture);
153 // Background multiple textures.
154 virtual void setMultiBackground (ssgTexture * texture, int idx);
156 // Make the panel visible or invisible.
157 virtual bool getVisibility () const;
158 virtual void setVisibility (bool visibility);
160 // Full width of panel.
161 virtual void setWidth (int width) { _width = width; }
162 virtual int getWidth () const { return _width; }
164 // Full height of panel.
165 virtual void setHeight (int height) { _height = height; }
166 virtual int getHeight () const { return _height; }
169 virtual void setXOffset (int offset);
170 virtual int getXOffset () const { return _x_offset; }
173 virtual void setYOffset (int offset);
174 virtual int getYOffset () const { return _y_offset; }
177 virtual void setViewHeight (int height) { _view_height = height; }
178 virtual int getViewHeight () const { return _view_height; }
180 // Handle a mouse click.
181 virtual bool doMouseAction (int button, int updown, int x, int y);
184 void setupVirtualCockpit();
185 void cleanupVirtualCockpit();
187 mutable bool _visibility;
188 mutable bool _mouseDown;
189 mutable int _mouseButton, _mouseX, _mouseY;
190 mutable int _mouseDelay;
191 mutable FGPanelInstrument * _mouseInstrument;
192 typedef vector<FGPanelInstrument *> instrument_list_type;
200 const SGPropertyNode * _xsize_node;
201 const SGPropertyNode * _ysize_node;
204 ssgTexture * _mbg[8];
205 // List of instruments in panel.
206 instrument_list_type _instruments;
211 ////////////////////////////////////////////////////////////////////////
213 ////////////////////////////////////////////////////////////////////////
217 * Class for user actions.
219 * The actions are command bindings, like bindings for the keyboard
220 * or joystick, but they are tied to specific mouse actions in
221 * rectangular areas of the panel.
223 class FGPanelAction : public FGConditional
227 FGPanelAction (int button, int x, int y, int w, int h);
228 virtual ~FGPanelAction ();
231 virtual int getButton () const { return _button; }
232 virtual int getX () const { return _x; }
233 virtual int getY () const { return _y; }
234 virtual int getWidth () const { return _w; }
235 virtual int getHeight () const { return _h; }
239 // transfer pointer ownership
240 virtual void addBinding (FGBinding * binding);
241 virtual void setButton (int button) { _button = button; }
242 virtual void setX (int x) { _x = x; }
243 virtual void setY (int y) { _y = y; }
244 virtual void setWidth (int w) { _w = w; }
245 virtual void setHeight (int h) { _h = h; }
247 // Check whether we're in the area.
248 virtual bool inArea (int button, int x, int y)
250 return (button == _button &&
257 // Perform the action.
258 virtual void doAction ();
261 typedef vector<FGBinding *> binding_list_t;
268 binding_list_t _bindings;
273 ////////////////////////////////////////////////////////////////////////
275 ////////////////////////////////////////////////////////////////////////
279 * A transformation for a layer.
281 class FGPanelTransformation : public FGConditional
291 FGPanelTransformation ();
292 virtual ~FGPanelTransformation ();
295 const SGPropertyNode * node;
300 SGInterpTable * table;
306 ////////////////////////////////////////////////////////////////////////
308 ////////////////////////////////////////////////////////////////////////
312 * A single layer of a multi-layered instrument.
314 * Each layer can be subject to a series of transformations based
315 * on current FGFS instrument readings: for example, a texture
316 * representing a needle can rotate to show the airspeed.
318 class FGInstrumentLayer : public FGConditional
322 FGInstrumentLayer (int w = -1, int h = -1);
323 virtual ~FGInstrumentLayer ();
325 virtual void draw () = 0;
326 virtual void transform () const;
328 virtual int getWidth () const { return _w; }
329 virtual int getHeight () const { return _h; }
330 virtual void setWidth (int w) { _w = w; }
331 virtual void setHeight (int h) { _h = h; }
333 // Transfer pointer ownership!!
335 virtual void addTransformation (FGPanelTransformation * transformation);
340 typedef vector<FGPanelTransformation *> transformation_list;
341 transformation_list _transformations;
346 ////////////////////////////////////////////////////////////////////////
348 ////////////////////////////////////////////////////////////////////////
352 * Abstract base class for a panel instrument.
354 * A panel instrument consists of zero or more actions, associated
355 * with mouse clicks in rectangular areas. Currently, the only
356 * concrete class derived from this is FGLayeredInstrument, but others
357 * may show up in the future (some complex instruments could be
358 * entirely hand-coded, for example).
360 class FGPanelInstrument : public FGConditional
363 FGPanelInstrument ();
364 FGPanelInstrument (int x, int y, int w, int h);
365 virtual ~FGPanelInstrument ();
367 virtual void draw () = 0;
369 virtual void setPosition(int x, int y);
370 virtual void setSize(int w, int h);
372 virtual int getXPos () const;
373 virtual int getYPos () const;
374 virtual int getWidth () const;
375 virtual int getHeight () const;
377 // Coordinates relative to centre.
378 // Transfer pointer ownership!!
379 virtual void addAction (FGPanelAction * action);
381 // Coordinates relative to centre.
382 virtual bool doMouseAction (int button, int x, int y);
386 typedef vector<FGPanelAction *> action_list_type;
387 action_list_type _actions;
392 * An instrument constructed of multiple layers.
394 * Each individual layer can be rotated or shifted to correspond
395 * to internal FGFS instrument readings.
397 class FGLayeredInstrument : public FGPanelInstrument
400 FGLayeredInstrument (int x, int y, int w, int h);
401 virtual ~FGLayeredInstrument ();
403 virtual void draw ();
405 // Transfer pointer ownership!!
406 virtual int addLayer (FGInstrumentLayer *layer);
407 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
409 // Transfer pointer ownership!!
410 virtual void addTransformation (FGPanelTransformation * transformation);
413 typedef vector<FGInstrumentLayer *> layer_list;
419 * An instrument layer containing a group of sublayers.
421 * This class is useful for gathering together a group of related
422 * layers, either to hold in an external file or to work under
423 * the same condition.
425 class FGGroupLayer : public FGInstrumentLayer
429 virtual ~FGGroupLayer ();
430 virtual void draw ();
431 // transfer pointer ownership
432 virtual void addLayer (FGInstrumentLayer * layer);
434 vector<FGInstrumentLayer *> _layers;
439 * A textured layer of an instrument.
441 * This is a layer holding a single texture. Normally, the texture's
442 * backgound should be transparent so that lower layers and the panel
443 * background can show through.
445 class FGTexturedLayer : public FGInstrumentLayer
448 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
449 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
450 virtual ~FGTexturedLayer ();
452 virtual void draw ();
454 virtual void setTexture (const FGCroppedTexture &texture) {
457 virtual FGCroppedTexture &getTexture () { return _texture; }
458 virtual const FGCroppedTexture &getTexture () const { return _texture; }
461 mutable FGCroppedTexture _texture;
466 * A text layer of an instrument.
468 * This is a layer holding a string of static and/or generated text.
469 * It is useful for instruments that have text displays, such as
470 * a chronometer, GPS, or NavCom radio.
472 class FGTextLayer : public FGInstrumentLayer
481 class Chunk : public FGConditional
484 Chunk (const string &text, const string &fmt = "%s");
485 Chunk (ChunkType type, const SGPropertyNode * node,
486 const string &fmt = "", float mult = 1.0);
488 const char * getValue () const;
492 const SGPropertyNode * _node;
495 mutable char _buf[1024];
498 FGTextLayer (int w = -1, int h = -1);
499 virtual ~FGTextLayer ();
501 virtual void draw ();
503 // Transfer pointer!!
504 virtual void addChunk (Chunk * chunk);
505 virtual void setColor (float r, float g, float b);
506 virtual void setPointSize (float size);
507 virtual void setFontName ( const string &name );
508 virtual void setFont (fntFont * font);
512 void recalc_value () const;
514 typedef vector<Chunk *> chunk_list;
519 mutable string _font_name;
520 mutable string _value;
521 mutable SGTimeStamp _then;
522 mutable SGTimeStamp _now;
527 * A layer that switches between two other layers.
529 * The usefulness of this layer is questionable now that all layers
530 * can have conditions, and it may be deprecated soon.
532 class FGSwitchLayer : public FGInstrumentLayer
535 // Transfer pointers!!
536 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
537 FGInstrumentLayer * layer1,
538 FGInstrumentLayer * layer2);
539 virtual ~FGSwitchLayer ();
541 virtual void draw ();
544 const SGPropertyNode * _node;
545 FGInstrumentLayer * _layer1, * _layer2;
551 ////////////////////////////////////////////////////////////////////////
553 ////////////////////////////////////////////////////////////////////////
556 * Test whether the panel should be visible.
558 bool fgPanelVisible ();
562 ////////////////////////////////////////////////////////////////////////
563 // The current panel, if any.
564 ////////////////////////////////////////////////////////////////////////
566 extern FGPanel * current_panel; // TODO: move to globals
570 #endif // __PANEL_HXX