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++
39 #include <simgear/compiler.h>
40 #include <simgear/props/props.hxx>
41 #include <simgear/structure/subsystem_mgr.hxx>
42 #include <simgear/math/interpolater.hxx>
43 #include <simgear/timing/timestamp.hxx>
49 #include <Main/fg_props.hxx>
50 #include <Input/input.hxx>
57 class FGPanelInstrument;
61 ////////////////////////////////////////////////////////////////////////
62 // Texture management.
63 ////////////////////////////////////////////////////////////////////////
67 * Texture manager (should migrate out into FGFS).
69 * This class ensures that no texture is loaded more than once.
71 class FGTextureManager
74 static ssgTexture * createTexture(const string &relativePath);
76 static map<string,ssgTexture *> _textureMap;
81 * Cropped texture (should migrate out into FGFS).
83 * This structure wraps an SSG texture with cropping information.
85 class FGCroppedTexture
90 FGCroppedTexture (const string &path,
91 float _minX = 0.0, float _minY = 0.0,
92 float _maxX = 1.0, float _maxY = 1.0);
93 virtual ~FGCroppedTexture ();
95 virtual void setPath (const string &path) { _path = path; }
97 virtual const string &getPath () const { return _path; }
99 virtual ssgTexture * getTexture ();
101 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
102 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
105 virtual float getMinX () const { return _minX; }
106 virtual float getMinY () const { return _minY; }
107 virtual float getMaxX () const { return _maxX; }
108 virtual float getMaxY () const { return _maxY; }
113 ssgTexture * _texture;
114 float _minX, _minY, _maxX, _maxY;
119 ////////////////////////////////////////////////////////////////////////
121 ////////////////////////////////////////////////////////////////////////
125 * Instrument panel class.
127 * The panel is a container that has a background texture and holds
128 * zero or more instruments. The panel will order the instruments to
129 * redraw themselves when necessary, and will pass mouse clicks on to
130 * the appropriate instruments for processing.
132 class FGPanel : public SGSubsystem
139 // Update the panel (every frame).
140 virtual void init ();
141 virtual void bind ();
142 virtual void unbind ();
143 virtual void draw ();
144 virtual void update (double dt);
145 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
147 virtual void updateMouseDelay();
149 // transfer pointer ownership!!!
150 virtual void addInstrument (FGPanelInstrument * instrument);
152 // Background texture.
153 virtual void setBackground (ssgTexture * texture);
155 // Background multiple textures.
156 virtual void setMultiBackground (ssgTexture * texture, int idx);
158 // Make the panel visible or invisible.
159 virtual bool getVisibility () const;
160 virtual void setVisibility (bool visibility);
162 // Full width of panel.
163 virtual void setWidth (int width) { _width = width; }
164 virtual int getWidth () const { return _width; }
166 // Full height of panel.
167 virtual void setHeight (int height) { _height = height; }
168 virtual int getHeight () const { return _height; }
171 virtual void setXOffset (int offset);
172 virtual int getXOffset () const { return _x_offset->getIntValue(); }
175 virtual void setYOffset (int offset);
176 virtual int getYOffset () const { return _y_offset->getIntValue(); }
179 virtual void setViewHeight (int height) { _view_height = height; }
180 virtual int getViewHeight () const { return _view_height; }
182 // Handle a mouse click.
183 virtual bool doMouseAction (int button, int updown, int x, int y);
184 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
187 void setupVirtualCockpit();
188 void cleanupVirtualCockpit();
190 mutable bool _mouseDown;
191 mutable int _mouseButton, _mouseX, _mouseY;
192 mutable int _mouseDelay;
193 mutable FGPanelInstrument * _mouseInstrument;
194 typedef vector<FGPanelInstrument *> instrument_list_type;
199 SGPropertyNode * _visibility;
200 SGPropertyNode * _x_offset;
201 SGPropertyNode * _y_offset;
202 SGPropertyNode * _jitter;
203 SGPropertyNode * _flipx;
205 const SGPropertyNode * _xsize_node;
206 const SGPropertyNode * _ysize_node;
209 ssgTexture * _mbg[8];
210 // List of instruments in panel.
211 instrument_list_type _instruments;
216 ////////////////////////////////////////////////////////////////////////
218 ////////////////////////////////////////////////////////////////////////
222 * Class for user actions.
224 * The actions are command bindings, like bindings for the keyboard
225 * or joystick, but they are tied to specific mouse actions in
226 * rectangular areas of the panel.
228 class FGPanelAction : public SGConditional
232 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
233 virtual ~FGPanelAction ();
236 virtual int getButton () const { return _button; }
237 virtual int getX () const { return _x; }
238 virtual int getY () const { return _y; }
239 virtual int getWidth () const { return _w; }
240 virtual int getHeight () const { return _h; }
244 // transfer pointer ownership
245 virtual void addBinding (FGBinding * binding, int updown);
246 virtual void setButton (int button) { _button = button; }
247 virtual void setX (int x) { _x = x; }
248 virtual void setY (int y) { _y = y; }
249 virtual void setWidth (int w) { _w = w; }
250 virtual void setHeight (int h) { _h = h; }
252 // Check whether we're in the area.
253 virtual bool inArea (int button, int x, int y)
255 return (button == _button &&
262 // Perform the action.
263 virtual bool doAction (int updown);
266 typedef vector<FGBinding *> binding_list_t;
275 binding_list_t _bindings[2];
280 ////////////////////////////////////////////////////////////////////////
282 ////////////////////////////////////////////////////////////////////////
286 * A transformation for a layer.
288 class FGPanelTransformation : public SGConditional
298 FGPanelTransformation ();
299 virtual ~FGPanelTransformation ();
302 const SGPropertyNode * node;
309 SGInterpTable * table;
315 ////////////////////////////////////////////////////////////////////////
317 ////////////////////////////////////////////////////////////////////////
321 * A single layer of a multi-layered instrument.
323 * Each layer can be subject to a series of transformations based
324 * on current FGFS instrument readings: for example, a texture
325 * representing a needle can rotate to show the airspeed.
327 class FGInstrumentLayer : public SGConditional
331 FGInstrumentLayer (int w = -1, int h = -1);
332 virtual ~FGInstrumentLayer ();
334 virtual void draw () = 0;
335 virtual void transform () const;
337 virtual int getWidth () const { return _w; }
338 virtual int getHeight () const { return _h; }
339 virtual void setWidth (int w) { _w = w; }
340 virtual void setHeight (int h) { _h = h; }
342 // Transfer pointer ownership!!
344 virtual void addTransformation (FGPanelTransformation * transformation);
349 typedef vector<FGPanelTransformation *> transformation_list;
350 transformation_list _transformations;
355 ////////////////////////////////////////////////////////////////////////
357 ////////////////////////////////////////////////////////////////////////
361 * Abstract base class for a panel instrument.
363 * A panel instrument consists of zero or more actions, associated
364 * with mouse clicks in rectangular areas. Currently, the only
365 * concrete class derived from this is FGLayeredInstrument, but others
366 * may show up in the future (some complex instruments could be
367 * entirely hand-coded, for example).
369 class FGPanelInstrument : public SGConditional
372 FGPanelInstrument ();
373 FGPanelInstrument (int x, int y, int w, int h);
374 virtual ~FGPanelInstrument ();
376 virtual void draw () = 0;
377 virtual void drawHotspots();
379 virtual void setPosition(int x, int y);
380 virtual void setSize(int w, int h);
382 virtual int getXPos () const;
383 virtual int getYPos () const;
384 virtual int getWidth () const;
385 virtual int getHeight () const;
387 // Coordinates relative to centre.
388 // Transfer pointer ownership!!
389 virtual void addAction (FGPanelAction * action);
391 // Coordinates relative to centre.
392 virtual bool doMouseAction (int button, int updown, int x, int y);
396 typedef vector<FGPanelAction *> action_list_type;
397 action_list_type _actions;
402 * An instrument constructed of multiple layers.
404 * Each individual layer can be rotated or shifted to correspond
405 * to internal FGFS instrument readings.
407 class FGLayeredInstrument : public FGPanelInstrument
410 FGLayeredInstrument (int x, int y, int w, int h);
411 virtual ~FGLayeredInstrument ();
413 virtual void draw ();
415 // Transfer pointer ownership!!
416 virtual int addLayer (FGInstrumentLayer *layer);
417 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
419 // Transfer pointer ownership!!
420 virtual void addTransformation (FGPanelTransformation * transformation);
423 typedef vector<FGInstrumentLayer *> layer_list;
429 * An instrument layer containing a group of sublayers.
431 * This class is useful for gathering together a group of related
432 * layers, either to hold in an external file or to work under
433 * the same condition.
435 class FGGroupLayer : public FGInstrumentLayer
439 virtual ~FGGroupLayer ();
440 virtual void draw ();
441 // transfer pointer ownership
442 virtual void addLayer (FGInstrumentLayer * layer);
444 vector<FGInstrumentLayer *> _layers;
449 * A textured layer of an instrument.
451 * This is a layer holding a single texture. Normally, the texture's
452 * backgound should be transparent so that lower layers and the panel
453 * background can show through.
455 class FGTexturedLayer : public FGInstrumentLayer
458 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
459 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
460 virtual ~FGTexturedLayer ();
462 virtual void draw ();
464 virtual void setTexture (const FGCroppedTexture &texture) {
467 virtual FGCroppedTexture &getTexture () { return _texture; }
468 virtual const FGCroppedTexture &getTexture () const { return _texture; }
471 mutable FGCroppedTexture _texture;
476 * A text layer of an instrument.
478 * This is a layer holding a string of static and/or generated text.
479 * It is useful for instruments that have text displays, such as
480 * a chronometer, GPS, or NavCom radio.
482 class FGTextLayer : public FGInstrumentLayer
491 class Chunk : public SGConditional
494 Chunk (const string &text, const string &fmt = "%s");
495 Chunk (ChunkType type, const SGPropertyNode * node,
496 const string &fmt = "", float mult = 1.0, float offs = 0.0,
497 bool truncation = false);
499 const char * getValue () const;
503 const SGPropertyNode * _node;
508 mutable char _buf[1024];
511 FGTextLayer (int w = -1, int h = -1);
512 virtual ~FGTextLayer ();
514 virtual void draw ();
516 // Transfer pointer!!
517 virtual void addChunk (Chunk * chunk);
518 virtual void setColor (float r, float g, float b);
519 virtual void setPointSize (float size);
520 virtual void setFontName ( const string &name );
521 virtual void setFont (fntFont * font);
525 void recalc_value () const;
527 typedef vector<Chunk *> chunk_list;
532 mutable string _font_name;
533 mutable string _value;
534 mutable SGTimeStamp _then;
535 mutable SGTimeStamp _now;
540 * A group layer that switches among its children.
542 * The first layer that passes its condition will be drawn, and
543 * any following layers will be ignored.
545 class FGSwitchLayer : public FGGroupLayer
548 // Transfer pointers!!
550 virtual void draw ();
557 ////////////////////////////////////////////////////////////////////////
559 ////////////////////////////////////////////////////////////////////////
562 * Test whether the panel should be visible.
564 bool fgPanelVisible ();
568 #endif // __PANEL_HXX