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/props/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->getIntValue(); }
178 virtual void setYOffset (int offset);
179 virtual int getYOffset () const { return _y_offset->getIntValue(); }
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 _mouseDown;
194 mutable int _mouseButton, _mouseX, _mouseY;
195 mutable int _mouseDelay;
196 mutable FGPanelInstrument * _mouseInstrument;
197 typedef vector<FGPanelInstrument *> instrument_list_type;
202 SGPropertyNode * _visibility;
203 SGPropertyNode * _x_offset;
204 SGPropertyNode * _y_offset;
205 SGPropertyNode * _jitter;
206 SGPropertyNode * _flipx;
208 const SGPropertyNode * _xsize_node;
209 const SGPropertyNode * _ysize_node;
212 ssgTexture * _mbg[8];
213 // List of instruments in panel.
214 instrument_list_type _instruments;
219 ////////////////////////////////////////////////////////////////////////
221 ////////////////////////////////////////////////////////////////////////
225 * Class for user actions.
227 * The actions are command bindings, like bindings for the keyboard
228 * or joystick, but they are tied to specific mouse actions in
229 * rectangular areas of the panel.
231 class FGPanelAction : public SGConditional
235 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
236 virtual ~FGPanelAction ();
239 virtual int getButton () const { return _button; }
240 virtual int getX () const { return _x; }
241 virtual int getY () const { return _y; }
242 virtual int getWidth () const { return _w; }
243 virtual int getHeight () const { return _h; }
247 // transfer pointer ownership
248 virtual void addBinding (FGBinding * binding, int updown);
249 virtual void setButton (int button) { _button = button; }
250 virtual void setX (int x) { _x = x; }
251 virtual void setY (int y) { _y = y; }
252 virtual void setWidth (int w) { _w = w; }
253 virtual void setHeight (int h) { _h = h; }
255 // Check whether we're in the area.
256 virtual bool inArea (int button, int x, int y)
258 return (button == _button &&
265 // Perform the action.
266 virtual bool doAction (int updown);
269 typedef vector<FGBinding *> binding_list_t;
278 binding_list_t _bindings[2];
283 ////////////////////////////////////////////////////////////////////////
285 ////////////////////////////////////////////////////////////////////////
289 * A transformation for a layer.
291 class FGPanelTransformation : public SGConditional
301 FGPanelTransformation ();
302 virtual ~FGPanelTransformation ();
305 const SGPropertyNode * node;
312 SGInterpTable * table;
318 ////////////////////////////////////////////////////////////////////////
320 ////////////////////////////////////////////////////////////////////////
324 * A single layer of a multi-layered instrument.
326 * Each layer can be subject to a series of transformations based
327 * on current FGFS instrument readings: for example, a texture
328 * representing a needle can rotate to show the airspeed.
330 class FGInstrumentLayer : public SGConditional
334 FGInstrumentLayer (int w = -1, int h = -1);
335 virtual ~FGInstrumentLayer ();
337 virtual void draw () = 0;
338 virtual void transform () const;
340 virtual int getWidth () const { return _w; }
341 virtual int getHeight () const { return _h; }
342 virtual void setWidth (int w) { _w = w; }
343 virtual void setHeight (int h) { _h = h; }
345 // Transfer pointer ownership!!
347 virtual void addTransformation (FGPanelTransformation * transformation);
352 typedef vector<FGPanelTransformation *> transformation_list;
353 transformation_list _transformations;
358 ////////////////////////////////////////////////////////////////////////
360 ////////////////////////////////////////////////////////////////////////
364 * Abstract base class for a panel instrument.
366 * A panel instrument consists of zero or more actions, associated
367 * with mouse clicks in rectangular areas. Currently, the only
368 * concrete class derived from this is FGLayeredInstrument, but others
369 * may show up in the future (some complex instruments could be
370 * entirely hand-coded, for example).
372 class FGPanelInstrument : public SGConditional
375 FGPanelInstrument ();
376 FGPanelInstrument (int x, int y, int w, int h);
377 virtual ~FGPanelInstrument ();
379 virtual void draw () = 0;
380 virtual void drawHotspots();
382 virtual void setPosition(int x, int y);
383 virtual void setSize(int w, int h);
385 virtual int getXPos () const;
386 virtual int getYPos () const;
387 virtual int getWidth () const;
388 virtual int getHeight () const;
390 // Coordinates relative to centre.
391 // Transfer pointer ownership!!
392 virtual void addAction (FGPanelAction * action);
394 // Coordinates relative to centre.
395 virtual bool doMouseAction (int button, int updown, int x, int y);
399 typedef vector<FGPanelAction *> action_list_type;
400 action_list_type _actions;
405 * An instrument constructed of multiple layers.
407 * Each individual layer can be rotated or shifted to correspond
408 * to internal FGFS instrument readings.
410 class FGLayeredInstrument : public FGPanelInstrument
413 FGLayeredInstrument (int x, int y, int w, int h);
414 virtual ~FGLayeredInstrument ();
416 virtual void draw ();
418 // Transfer pointer ownership!!
419 virtual int addLayer (FGInstrumentLayer *layer);
420 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
422 // Transfer pointer ownership!!
423 virtual void addTransformation (FGPanelTransformation * transformation);
426 typedef vector<FGInstrumentLayer *> layer_list;
432 * An instrument layer containing a group of sublayers.
434 * This class is useful for gathering together a group of related
435 * layers, either to hold in an external file or to work under
436 * the same condition.
438 class FGGroupLayer : public FGInstrumentLayer
442 virtual ~FGGroupLayer ();
443 virtual void draw ();
444 // transfer pointer ownership
445 virtual void addLayer (FGInstrumentLayer * layer);
447 vector<FGInstrumentLayer *> _layers;
452 * A textured layer of an instrument.
454 * This is a layer holding a single texture. Normally, the texture's
455 * backgound should be transparent so that lower layers and the panel
456 * background can show through.
458 class FGTexturedLayer : public FGInstrumentLayer
461 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
462 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
463 virtual ~FGTexturedLayer ();
465 virtual void draw ();
467 virtual void setTexture (const FGCroppedTexture &texture) {
470 virtual FGCroppedTexture &getTexture () { return _texture; }
471 virtual const FGCroppedTexture &getTexture () const { return _texture; }
474 mutable FGCroppedTexture _texture;
479 * A text layer of an instrument.
481 * This is a layer holding a string of static and/or generated text.
482 * It is useful for instruments that have text displays, such as
483 * a chronometer, GPS, or NavCom radio.
485 class FGTextLayer : public FGInstrumentLayer
494 class Chunk : public SGConditional
497 Chunk (const string &text, const string &fmt = "%s");
498 Chunk (ChunkType type, const SGPropertyNode * node,
499 const string &fmt = "", float mult = 1.0);
501 const char * getValue () const;
505 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