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>
43 #include <simgear/math/interpolater.hxx>
44 #include <simgear/misc/props.hxx>
45 #include <simgear/timing/timestamp.hxx>
52 #include <Main/fgfs.hxx>
53 #include <Main/fg_props.hxx>
55 #include <Input/input.hxx>
60 class FGPanelInstrument;
64 ////////////////////////////////////////////////////////////////////////
65 // Texture management.
66 ////////////////////////////////////////////////////////////////////////
70 * Texture manager (should migrate out into FGFS).
72 * This class ensures that no texture is loaded more than once.
74 class FGTextureManager
77 static ssgTexture * createTexture(const string &relativePath);
79 static map<string,ssgTexture *> _textureMap;
84 * Cropped texture (should migrate out into FGFS).
86 * This structure wraps an SSG texture with cropping information.
88 class FGCroppedTexture
93 FGCroppedTexture (const string &path,
94 float _minX = 0.0, float _minY = 0.0,
95 float _maxX = 1.0, float _maxY = 1.0);
96 virtual ~FGCroppedTexture ();
98 virtual void setPath (const string &path) { _path = path; }
100 virtual const string &getPath () const { return _path; }
102 virtual ssgTexture * getTexture ();
104 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
105 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
108 virtual float getMinX () const { return _minX; }
109 virtual float getMinY () const { return _minY; }
110 virtual float getMaxX () const { return _maxX; }
111 virtual float getMaxY () const { return _maxY; }
116 ssgTexture * _texture;
117 float _minX, _minY, _maxX, _maxY;
122 ////////////////////////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////////////////
128 * Instrument panel class.
130 * The panel is a container that has a background texture and holds
131 * zero or more instruments. The panel will order the instruments to
132 * redraw themselves when necessary, and will pass mouse clicks on to
133 * the appropriate instruments for processing.
135 class FGPanel : public FGSubsystem
142 // Update the panel (every frame).
143 virtual void init ();
144 virtual void bind ();
145 virtual void unbind ();
146 virtual void draw ();
147 virtual void update (double dt);
148 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
150 virtual void updateMouseDelay();
152 // transfer pointer ownership!!!
153 virtual void addInstrument (FGPanelInstrument * instrument);
155 // Background texture.
156 virtual void setBackground (ssgTexture * texture);
158 // Background multiple textures.
159 virtual void setMultiBackground (ssgTexture * texture, int idx);
161 // Make the panel visible or invisible.
162 virtual bool getVisibility () const;
163 virtual void setVisibility (bool visibility);
165 // Full width of panel.
166 virtual void setWidth (int width) { _width = width; }
167 virtual int getWidth () const { return _width; }
169 // Full height of panel.
170 virtual void setHeight (int height) { _height = height; }
171 virtual int getHeight () const { return _height; }
174 virtual void setXOffset (int offset);
175 virtual int getXOffset () const { return _x_offset; }
178 virtual void setYOffset (int offset);
179 virtual int getYOffset () const { return _y_offset; }
182 virtual void setViewHeight (int height) { _view_height = height; }
183 virtual int getViewHeight () const { return _view_height; }
185 // Handle a mouse click.
186 virtual bool doMouseAction (int button, int updown, int x, int y);
187 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
190 void setupVirtualCockpit();
191 void cleanupVirtualCockpit();
193 mutable bool _visibility;
194 mutable bool _mouseDown;
195 mutable int _mouseButton, _mouseX, _mouseY;
196 mutable int _mouseDelay;
197 mutable FGPanelInstrument * _mouseInstrument;
198 typedef vector<FGPanelInstrument *> instrument_list_type;
207 const SGPropertyNode * _xsize_node;
208 const SGPropertyNode * _ysize_node;
211 ssgTexture * _mbg[8];
212 // List of instruments in panel.
213 instrument_list_type _instruments;
218 ////////////////////////////////////////////////////////////////////////
220 ////////////////////////////////////////////////////////////////////////
224 * Class for user actions.
226 * The actions are command bindings, like bindings for the keyboard
227 * or joystick, but they are tied to specific mouse actions in
228 * rectangular areas of the panel.
230 class FGPanelAction : public FGConditional
234 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
235 virtual ~FGPanelAction ();
238 virtual int getButton () const { return _button; }
239 virtual int getX () const { return _x; }
240 virtual int getY () const { return _y; }
241 virtual int getWidth () const { return _w; }
242 virtual int getHeight () const { return _h; }
246 // transfer pointer ownership
247 virtual void addBinding (FGBinding * binding, int updown);
248 virtual void setButton (int button) { _button = button; }
249 virtual void setX (int x) { _x = x; }
250 virtual void setY (int y) { _y = y; }
251 virtual void setWidth (int w) { _w = w; }
252 virtual void setHeight (int h) { _h = h; }
254 // Check whether we're in the area.
255 virtual bool inArea (int button, int x, int y)
257 return (button == _button &&
264 // Perform the action.
265 virtual bool doAction (int updown);
268 typedef vector<FGBinding *> binding_list_t;
277 binding_list_t _bindings[2];
282 ////////////////////////////////////////////////////////////////////////
284 ////////////////////////////////////////////////////////////////////////
288 * A transformation for a layer.
290 class FGPanelTransformation : public FGConditional
300 FGPanelTransformation ();
301 virtual ~FGPanelTransformation ();
304 const SGPropertyNode * node;
311 SGInterpTable * table;
317 ////////////////////////////////////////////////////////////////////////
319 ////////////////////////////////////////////////////////////////////////
323 * A single layer of a multi-layered instrument.
325 * Each layer can be subject to a series of transformations based
326 * on current FGFS instrument readings: for example, a texture
327 * representing a needle can rotate to show the airspeed.
329 class FGInstrumentLayer : public FGConditional
333 FGInstrumentLayer (int w = -1, int h = -1);
334 virtual ~FGInstrumentLayer ();
336 virtual void draw () = 0;
337 virtual void transform () const;
339 virtual int getWidth () const { return _w; }
340 virtual int getHeight () const { return _h; }
341 virtual void setWidth (int w) { _w = w; }
342 virtual void setHeight (int h) { _h = h; }
344 // Transfer pointer ownership!!
346 virtual void addTransformation (FGPanelTransformation * transformation);
351 typedef vector<FGPanelTransformation *> transformation_list;
352 transformation_list _transformations;
357 ////////////////////////////////////////////////////////////////////////
359 ////////////////////////////////////////////////////////////////////////
363 * Abstract base class for a panel instrument.
365 * A panel instrument consists of zero or more actions, associated
366 * with mouse clicks in rectangular areas. Currently, the only
367 * concrete class derived from this is FGLayeredInstrument, but others
368 * may show up in the future (some complex instruments could be
369 * entirely hand-coded, for example).
371 class FGPanelInstrument : public FGConditional
374 FGPanelInstrument ();
375 FGPanelInstrument (int x, int y, int w, int h);
376 virtual ~FGPanelInstrument ();
378 virtual void draw () = 0;
379 virtual void drawHotspots();
381 virtual void setPosition(int x, int y);
382 virtual void setSize(int w, int h);
384 virtual int getXPos () const;
385 virtual int getYPos () const;
386 virtual int getWidth () const;
387 virtual int getHeight () const;
389 // Coordinates relative to centre.
390 // Transfer pointer ownership!!
391 virtual void addAction (FGPanelAction * action);
393 // Coordinates relative to centre.
394 virtual bool doMouseAction (int button, int updown, int x, int y);
398 typedef vector<FGPanelAction *> action_list_type;
399 action_list_type _actions;
404 * An instrument constructed of multiple layers.
406 * Each individual layer can be rotated or shifted to correspond
407 * to internal FGFS instrument readings.
409 class FGLayeredInstrument : public FGPanelInstrument
412 FGLayeredInstrument (int x, int y, int w, int h);
413 virtual ~FGLayeredInstrument ();
415 virtual void draw ();
417 // Transfer pointer ownership!!
418 virtual int addLayer (FGInstrumentLayer *layer);
419 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
421 // Transfer pointer ownership!!
422 virtual void addTransformation (FGPanelTransformation * transformation);
425 typedef vector<FGInstrumentLayer *> layer_list;
431 * An instrument layer containing a group of sublayers.
433 * This class is useful for gathering together a group of related
434 * layers, either to hold in an external file or to work under
435 * the same condition.
437 class FGGroupLayer : public FGInstrumentLayer
441 virtual ~FGGroupLayer ();
442 virtual void draw ();
443 // transfer pointer ownership
444 virtual void addLayer (FGInstrumentLayer * layer);
446 vector<FGInstrumentLayer *> _layers;
451 * A textured layer of an instrument.
453 * This is a layer holding a single texture. Normally, the texture's
454 * backgound should be transparent so that lower layers and the panel
455 * background can show through.
457 class FGTexturedLayer : public FGInstrumentLayer
460 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
461 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
462 virtual ~FGTexturedLayer ();
464 virtual void draw ();
466 virtual void setTexture (const FGCroppedTexture &texture) {
469 virtual FGCroppedTexture &getTexture () { return _texture; }
470 virtual const FGCroppedTexture &getTexture () const { return _texture; }
473 mutable FGCroppedTexture _texture;
478 * A text layer of an instrument.
480 * This is a layer holding a string of static and/or generated text.
481 * It is useful for instruments that have text displays, such as
482 * a chronometer, GPS, or NavCom radio.
484 class FGTextLayer : public FGInstrumentLayer
493 class Chunk : public FGConditional
496 Chunk (const string &text, const string &fmt = "%s");
497 Chunk (ChunkType type, const SGPropertyNode * node,
498 const string &fmt = "", float mult = 1.0);
500 const char * getValue () const;
504 const SGPropertyNode * _node;
507 mutable char _buf[1024];
510 FGTextLayer (int w = -1, int h = -1);
511 virtual ~FGTextLayer ();
513 virtual void draw ();
515 // Transfer pointer!!
516 virtual void addChunk (Chunk * chunk);
517 virtual void setColor (float r, float g, float b);
518 virtual void setPointSize (float size);
519 virtual void setFontName ( const string &name );
520 virtual void setFont (fntFont * font);
524 void recalc_value () const;
526 typedef vector<Chunk *> chunk_list;
531 mutable string _font_name;
532 mutable string _value;
533 mutable SGTimeStamp _then;
534 mutable SGTimeStamp _now;
539 * A group layer that switches among its children.
541 * The first layer that passes its condition will be drawn, and
542 * any following layers will be ignored.
544 class FGSwitchLayer : public FGGroupLayer
547 // Transfer pointers!!
549 virtual void draw ();
556 ////////////////////////////////////////////////////////////////////////
558 ////////////////////////////////////////////////////////////////////////
561 * Test whether the panel should be visible.
563 bool fgPanelVisible ();
567 ////////////////////////////////////////////////////////////////////////
568 // The current panel, if any.
569 ////////////////////////////////////////////////////////////////////////
571 extern FGPanel * current_panel; // TODO: move to globals
575 #endif // __PANEL_HXX