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>
51 #include <Main/fgfs.hxx>
52 #include <Main/fg_props.hxx>
54 #include <Input/input.hxx>
59 class FGPanelInstrument;
63 ////////////////////////////////////////////////////////////////////////
64 // Texture management.
65 ////////////////////////////////////////////////////////////////////////
69 * Texture manager (should migrate out into FGFS).
71 * This class ensures that no texture is loaded more than once.
73 class FGTextureManager
76 static ssgTexture * createTexture(const string &relativePath);
78 static map<string,ssgTexture *> _textureMap;
83 * Cropped texture (should migrate out into FGFS).
85 * This structure wraps an SSG texture with cropping information.
87 class FGCroppedTexture
92 FGCroppedTexture (const string &path,
93 float _minX = 0.0, float _minY = 0.0,
94 float _maxX = 1.0, float _maxY = 1.0);
95 virtual ~FGCroppedTexture ();
97 virtual void setPath (const string &path) { _path = path; }
99 virtual const string &getPath () const { return _path; }
101 virtual ssgTexture * getTexture ();
103 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
104 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
107 virtual float getMinX () const { return _minX; }
108 virtual float getMinY () const { return _minY; }
109 virtual float getMaxX () const { return _maxX; }
110 virtual float getMaxY () const { return _maxY; }
115 ssgTexture * _texture;
116 float _minX, _minY, _maxX, _maxY;
121 ////////////////////////////////////////////////////////////////////////
123 ////////////////////////////////////////////////////////////////////////
127 * Instrument panel class.
129 * The panel is a container that has a background texture and holds
130 * zero or more instruments. The panel will order the instruments to
131 * redraw themselves when necessary, and will pass mouse clicks on to
132 * the appropriate instruments for processing.
134 class FGPanel : public FGSubsystem
141 // Update the panel (every frame).
142 virtual void init ();
143 virtual void bind ();
144 virtual void unbind ();
145 virtual void draw ();
146 virtual void update (double dt);
147 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
149 virtual void updateMouseDelay();
151 // transfer pointer ownership!!!
152 virtual void addInstrument (FGPanelInstrument * instrument);
154 // Background texture.
155 virtual void setBackground (ssgTexture * texture);
157 // Background multiple textures.
158 virtual void setMultiBackground (ssgTexture * texture, int idx);
160 // Make the panel visible or invisible.
161 virtual bool getVisibility () const;
162 virtual void setVisibility (bool visibility);
164 // Full width of panel.
165 virtual void setWidth (int width) { _width = width; }
166 virtual int getWidth () const { return _width; }
168 // Full height of panel.
169 virtual void setHeight (int height) { _height = height; }
170 virtual int getHeight () const { return _height; }
173 virtual void setXOffset (int offset);
174 virtual int getXOffset () const { return _x_offset; }
177 virtual void setYOffset (int offset);
178 virtual int getYOffset () const { return _y_offset; }
181 virtual void setViewHeight (int height) { _view_height = height; }
182 virtual int getViewHeight () const { return _view_height; }
184 // Handle a mouse click.
185 virtual bool doMouseAction (int button, int updown, int x, int y);
186 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
189 void setupVirtualCockpit();
190 void cleanupVirtualCockpit();
192 mutable bool _visibility;
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;
206 const SGPropertyNode * _xsize_node;
207 const SGPropertyNode * _ysize_node;
210 ssgTexture * _mbg[8];
211 // List of instruments in panel.
212 instrument_list_type _instruments;
217 ////////////////////////////////////////////////////////////////////////
219 ////////////////////////////////////////////////////////////////////////
223 * Class for user actions.
225 * The actions are command bindings, like bindings for the keyboard
226 * or joystick, but they are tied to specific mouse actions in
227 * rectangular areas of the panel.
229 class FGPanelAction : public FGConditional
233 FGPanelAction (int button, int x, int y, int w, int h);
234 virtual ~FGPanelAction ();
237 virtual int getButton () const { return _button; }
238 virtual int getX () const { return _x; }
239 virtual int getY () const { return _y; }
240 virtual int getWidth () const { return _w; }
241 virtual int getHeight () const { return _h; }
245 // transfer pointer ownership
246 virtual void addBinding (FGBinding * binding);
247 virtual void setButton (int button) { _button = button; }
248 virtual void setX (int x) { _x = x; }
249 virtual void setY (int y) { _y = y; }
250 virtual void setWidth (int w) { _w = w; }
251 virtual void setHeight (int h) { _h = h; }
253 // Check whether we're in the area.
254 virtual bool inArea (int button, int x, int y)
256 return (button == _button &&
263 // Perform the action.
264 virtual void doAction ();
267 typedef vector<FGBinding *> binding_list_t;
274 binding_list_t _bindings;
279 ////////////////////////////////////////////////////////////////////////
281 ////////////////////////////////////////////////////////////////////////
285 * A transformation for a layer.
287 class FGPanelTransformation : public FGConditional
297 FGPanelTransformation ();
298 virtual ~FGPanelTransformation ();
301 const SGPropertyNode * node;
306 SGInterpTable * table;
312 ////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////
318 * A single layer of a multi-layered instrument.
320 * Each layer can be subject to a series of transformations based
321 * on current FGFS instrument readings: for example, a texture
322 * representing a needle can rotate to show the airspeed.
324 class FGInstrumentLayer : public FGConditional
328 FGInstrumentLayer (int w = -1, int h = -1);
329 virtual ~FGInstrumentLayer ();
331 virtual void draw () = 0;
332 virtual void transform () const;
334 virtual int getWidth () const { return _w; }
335 virtual int getHeight () const { return _h; }
336 virtual void setWidth (int w) { _w = w; }
337 virtual void setHeight (int h) { _h = h; }
339 // Transfer pointer ownership!!
341 virtual void addTransformation (FGPanelTransformation * transformation);
346 typedef vector<FGPanelTransformation *> transformation_list;
347 transformation_list _transformations;
352 ////////////////////////////////////////////////////////////////////////
354 ////////////////////////////////////////////////////////////////////////
358 * Abstract base class for a panel instrument.
360 * A panel instrument consists of zero or more actions, associated
361 * with mouse clicks in rectangular areas. Currently, the only
362 * concrete class derived from this is FGLayeredInstrument, but others
363 * may show up in the future (some complex instruments could be
364 * entirely hand-coded, for example).
366 class FGPanelInstrument : public FGConditional
369 FGPanelInstrument ();
370 FGPanelInstrument (int x, int y, int w, int h);
371 virtual ~FGPanelInstrument ();
373 virtual void draw () = 0;
374 virtual void drawHotspots();
376 virtual void setPosition(int x, int y);
377 virtual void setSize(int w, int h);
379 virtual int getXPos () const;
380 virtual int getYPos () const;
381 virtual int getWidth () const;
382 virtual int getHeight () const;
384 // Coordinates relative to centre.
385 // Transfer pointer ownership!!
386 virtual void addAction (FGPanelAction * action);
388 // Coordinates relative to centre.
389 virtual bool doMouseAction (int button, int x, int y);
393 typedef vector<FGPanelAction *> action_list_type;
394 action_list_type _actions;
399 * An instrument constructed of multiple layers.
401 * Each individual layer can be rotated or shifted to correspond
402 * to internal FGFS instrument readings.
404 class FGLayeredInstrument : public FGPanelInstrument
407 FGLayeredInstrument (int x, int y, int w, int h);
408 virtual ~FGLayeredInstrument ();
410 virtual void draw ();
412 // Transfer pointer ownership!!
413 virtual int addLayer (FGInstrumentLayer *layer);
414 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
416 // Transfer pointer ownership!!
417 virtual void addTransformation (FGPanelTransformation * transformation);
420 typedef vector<FGInstrumentLayer *> layer_list;
426 * An instrument layer containing a group of sublayers.
428 * This class is useful for gathering together a group of related
429 * layers, either to hold in an external file or to work under
430 * the same condition.
432 class FGGroupLayer : public FGInstrumentLayer
436 virtual ~FGGroupLayer ();
437 virtual void draw ();
438 // transfer pointer ownership
439 virtual void addLayer (FGInstrumentLayer * layer);
441 vector<FGInstrumentLayer *> _layers;
446 * A textured layer of an instrument.
448 * This is a layer holding a single texture. Normally, the texture's
449 * backgound should be transparent so that lower layers and the panel
450 * background can show through.
452 class FGTexturedLayer : public FGInstrumentLayer
455 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
456 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
457 virtual ~FGTexturedLayer ();
459 virtual void draw ();
461 virtual void setTexture (const FGCroppedTexture &texture) {
464 virtual FGCroppedTexture &getTexture () { return _texture; }
465 virtual const FGCroppedTexture &getTexture () const { return _texture; }
468 mutable FGCroppedTexture _texture;
473 * A text layer of an instrument.
475 * This is a layer holding a string of static and/or generated text.
476 * It is useful for instruments that have text displays, such as
477 * a chronometer, GPS, or NavCom radio.
479 class FGTextLayer : public FGInstrumentLayer
488 class Chunk : public FGConditional
491 Chunk (const string &text, const string &fmt = "%s");
492 Chunk (ChunkType type, const SGPropertyNode * node,
493 const string &fmt = "", float mult = 1.0);
495 const char * getValue () const;
499 const SGPropertyNode * _node;
502 mutable char _buf[1024];
505 FGTextLayer (int w = -1, int h = -1);
506 virtual ~FGTextLayer ();
508 virtual void draw ();
510 // Transfer pointer!!
511 virtual void addChunk (Chunk * chunk);
512 virtual void setColor (float r, float g, float b);
513 virtual void setPointSize (float size);
514 virtual void setFontName ( const string &name );
515 virtual void setFont (fntFont * font);
519 void recalc_value () const;
521 typedef vector<Chunk *> chunk_list;
526 mutable string _font_name;
527 mutable string _value;
528 mutable SGTimeStamp _then;
529 mutable SGTimeStamp _now;
534 * A layer that switches between two other layers.
536 * The usefulness of this layer is questionable now that all layers
537 * can have conditions, and it may be deprecated soon.
539 class FGSwitchLayer : public FGInstrumentLayer
542 // Transfer pointers!!
543 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
544 FGInstrumentLayer * layer1,
545 FGInstrumentLayer * layer2);
546 virtual ~FGSwitchLayer ();
548 virtual void draw ();
551 const SGPropertyNode * _node;
552 FGInstrumentLayer * _layer1, * _layer2;
558 ////////////////////////////////////////////////////////////////////////
560 ////////////////////////////////////////////////////////////////////////
563 * Test whether the panel should be visible.
565 bool fgPanelVisible ();
569 ////////////////////////////////////////////////////////////////////////
570 // The current panel, if any.
571 ////////////////////////////////////////////////////////////////////////
573 extern FGPanel * current_panel; // TODO: move to globals
577 #endif // __PANEL_HXX