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++
40 #include <simgear/compiler.h>
41 #include <simgear/props/props.hxx>
42 #include <simgear/structure/subsystem_mgr.hxx>
43 #include <simgear/math/interpolater.hxx>
44 #include <simgear/timing/timestamp.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Input/input.hxx>
56 class FGPanelInstrument;
60 ////////////////////////////////////////////////////////////////////////
61 // Texture management.
62 ////////////////////////////////////////////////////////////////////////
66 * Texture manager (should migrate out into FGFS).
68 * This class ensures that no texture is loaded more than once.
70 class FGTextureManager
73 static ssgTexture * createTexture(const string &relativePath);
75 static map<string,ssgTexture *> _textureMap;
80 * Cropped texture (should migrate out into FGFS).
82 * This structure wraps an SSG texture with cropping information.
84 class FGCroppedTexture
89 FGCroppedTexture (const string &path,
90 float _minX = 0.0, float _minY = 0.0,
91 float _maxX = 1.0, float _maxY = 1.0);
92 virtual ~FGCroppedTexture ();
94 virtual void setPath (const string &path) { _path = path; }
96 virtual const string &getPath () const { return _path; }
98 virtual ssgTexture * getTexture ();
100 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
101 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
104 virtual float getMinX () const { return _minX; }
105 virtual float getMinY () const { return _minY; }
106 virtual float getMaxX () const { return _maxX; }
107 virtual float getMaxY () const { return _maxY; }
112 ssgTexture * _texture;
113 float _minX, _minY, _maxX, _maxY;
118 ////////////////////////////////////////////////////////////////////////
120 ////////////////////////////////////////////////////////////////////////
124 * Instrument panel class.
126 * The panel is a container that has a background texture and holds
127 * zero or more instruments. The panel will order the instruments to
128 * redraw themselves when necessary, and will pass mouse clicks on to
129 * the appropriate instruments for processing.
131 class FGPanel : public SGSubsystem
138 // Update the panel (every frame).
139 virtual void init ();
140 virtual void bind ();
141 virtual void unbind ();
142 virtual void draw ();
143 virtual void update (double dt);
144 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
146 virtual void updateMouseDelay();
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->getIntValue(); }
174 virtual void setYOffset (int offset);
175 virtual int getYOffset () const { return _y_offset->getIntValue(); }
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);
183 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
186 void setupVirtualCockpit();
187 void cleanupVirtualCockpit();
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;
198 SGPropertyNode * _visibility;
199 SGPropertyNode * _x_offset;
200 SGPropertyNode * _y_offset;
201 SGPropertyNode * _jitter;
202 SGPropertyNode * _flipx;
204 const SGPropertyNode * _xsize_node;
205 const SGPropertyNode * _ysize_node;
208 ssgTexture * _mbg[8];
209 // List of instruments in panel.
210 instrument_list_type _instruments;
215 ////////////////////////////////////////////////////////////////////////
217 ////////////////////////////////////////////////////////////////////////
221 * Class for user actions.
223 * The actions are command bindings, like bindings for the keyboard
224 * or joystick, but they are tied to specific mouse actions in
225 * rectangular areas of the panel.
227 class FGPanelAction : public SGConditional
231 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
232 virtual ~FGPanelAction ();
235 virtual int getButton () const { return _button; }
236 virtual int getX () const { return _x; }
237 virtual int getY () const { return _y; }
238 virtual int getWidth () const { return _w; }
239 virtual int getHeight () const { return _h; }
243 // transfer pointer ownership
244 virtual void addBinding (FGBinding * binding, int updown);
245 virtual void setButton (int button) { _button = button; }
246 virtual void setX (int x) { _x = x; }
247 virtual void setY (int y) { _y = y; }
248 virtual void setWidth (int w) { _w = w; }
249 virtual void setHeight (int h) { _h = h; }
251 // Check whether we're in the area.
252 virtual bool inArea (int button, int x, int y)
254 return (button == _button &&
261 // Perform the action.
262 virtual bool doAction (int updown);
265 typedef vector<FGBinding *> binding_list_t;
274 binding_list_t _bindings[2];
279 ////////////////////////////////////////////////////////////////////////
281 ////////////////////////////////////////////////////////////////////////
285 * A transformation for a layer.
287 class FGPanelTransformation : public SGConditional
297 FGPanelTransformation ();
298 virtual ~FGPanelTransformation ();
301 const SGPropertyNode * node;
308 SGInterpTable * table;
314 ////////////////////////////////////////////////////////////////////////
316 ////////////////////////////////////////////////////////////////////////
320 * A single layer of a multi-layered instrument.
322 * Each layer can be subject to a series of transformations based
323 * on current FGFS instrument readings: for example, a texture
324 * representing a needle can rotate to show the airspeed.
326 class FGInstrumentLayer : public SGConditional
330 FGInstrumentLayer (int w = -1, int h = -1);
331 virtual ~FGInstrumentLayer ();
333 virtual void draw () = 0;
334 virtual void transform () const;
336 virtual int getWidth () const { return _w; }
337 virtual int getHeight () const { return _h; }
338 virtual void setWidth (int w) { _w = w; }
339 virtual void setHeight (int h) { _h = h; }
341 // Transfer pointer ownership!!
343 virtual void addTransformation (FGPanelTransformation * transformation);
348 typedef vector<FGPanelTransformation *> transformation_list;
349 transformation_list _transformations;
354 ////////////////////////////////////////////////////////////////////////
356 ////////////////////////////////////////////////////////////////////////
360 * Abstract base class for a panel instrument.
362 * A panel instrument consists of zero or more actions, associated
363 * with mouse clicks in rectangular areas. Currently, the only
364 * concrete class derived from this is FGLayeredInstrument, but others
365 * may show up in the future (some complex instruments could be
366 * entirely hand-coded, for example).
368 class FGPanelInstrument : public SGConditional
371 FGPanelInstrument ();
372 FGPanelInstrument (int x, int y, int w, int h);
373 virtual ~FGPanelInstrument ();
375 virtual void draw () = 0;
376 virtual void drawHotspots();
378 virtual void setPosition(int x, int y);
379 virtual void setSize(int w, int h);
381 virtual int getXPos () const;
382 virtual int getYPos () const;
383 virtual int getWidth () const;
384 virtual int getHeight () const;
386 // Coordinates relative to centre.
387 // Transfer pointer ownership!!
388 virtual void addAction (FGPanelAction * action);
390 // Coordinates relative to centre.
391 virtual bool doMouseAction (int button, int updown, int x, int y);
395 typedef vector<FGPanelAction *> action_list_type;
396 action_list_type _actions;
401 * An instrument constructed of multiple layers.
403 * Each individual layer can be rotated or shifted to correspond
404 * to internal FGFS instrument readings.
406 class FGLayeredInstrument : public FGPanelInstrument
409 FGLayeredInstrument (int x, int y, int w, int h);
410 virtual ~FGLayeredInstrument ();
412 virtual void draw ();
414 // Transfer pointer ownership!!
415 virtual int addLayer (FGInstrumentLayer *layer);
416 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
418 // Transfer pointer ownership!!
419 virtual void addTransformation (FGPanelTransformation * transformation);
422 typedef vector<FGInstrumentLayer *> layer_list;
428 * An instrument layer containing a group of sublayers.
430 * This class is useful for gathering together a group of related
431 * layers, either to hold in an external file or to work under
432 * the same condition.
434 class FGGroupLayer : public FGInstrumentLayer
438 virtual ~FGGroupLayer ();
439 virtual void draw ();
440 // transfer pointer ownership
441 virtual void addLayer (FGInstrumentLayer * layer);
443 vector<FGInstrumentLayer *> _layers;
448 * A textured layer of an instrument.
450 * This is a layer holding a single texture. Normally, the texture's
451 * backgound should be transparent so that lower layers and the panel
452 * background can show through.
454 class FGTexturedLayer : public FGInstrumentLayer
457 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
458 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
459 virtual ~FGTexturedLayer ();
461 virtual void draw ();
463 virtual void setTexture (const FGCroppedTexture &texture) {
466 virtual FGCroppedTexture &getTexture () { return _texture; }
467 virtual const FGCroppedTexture &getTexture () const { return _texture; }
470 mutable FGCroppedTexture _texture;
475 * A text layer of an instrument.
477 * This is a layer holding a string of static and/or generated text.
478 * It is useful for instruments that have text displays, such as
479 * a chronometer, GPS, or NavCom radio.
481 class FGTextLayer : public FGInstrumentLayer
490 class Chunk : public SGConditional
493 Chunk (const string &text, const string &fmt = "%s");
494 Chunk (ChunkType type, const SGPropertyNode * node,
495 const string &fmt = "", float mult = 1.0);
497 const char * getValue () const;
501 const SGPropertyNode * _node;
504 mutable char _buf[1024];
507 FGTextLayer (int w = -1, int h = -1);
508 virtual ~FGTextLayer ();
510 virtual void draw ();
512 // Transfer pointer!!
513 virtual void addChunk (Chunk * chunk);
514 virtual void setColor (float r, float g, float b);
515 virtual void setPointSize (float size);
516 virtual void setFontName ( const string &name );
517 virtual void setFont (fntFont * font);
521 void recalc_value () const;
523 typedef vector<Chunk *> chunk_list;
528 mutable string _font_name;
529 mutable string _value;
530 mutable SGTimeStamp _then;
531 mutable SGTimeStamp _now;
536 * A group layer that switches among its children.
538 * The first layer that passes its condition will be drawn, and
539 * any following layers will be ignored.
541 class FGSwitchLayer : public FGGroupLayer
544 // Transfer pointers!!
546 virtual void draw ();
553 ////////////////////////////////////////////////////////////////////////
555 ////////////////////////////////////////////////////////////////////////
558 * Test whether the panel should be visible.
560 bool fgPanelVisible ();
564 #endif // __PANEL_HXX