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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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>
51 #include <Instrumentation/dclgps.hxx>
58 class FGPanelInstrument;
62 ////////////////////////////////////////////////////////////////////////
63 // Texture management.
64 ////////////////////////////////////////////////////////////////////////
68 * Texture manager (should migrate out into FGFS).
70 * This class ensures that no texture is loaded more than once.
72 class FGTextureManager
75 static ssgTexture * createTexture(const string &relativePath);
77 static map<string,ssgTexture *> _textureMap;
82 * Cropped texture (should migrate out into FGFS).
84 * This structure wraps an SSG texture with cropping information.
86 class FGCroppedTexture
91 FGCroppedTexture (const string &path,
92 float _minX = 0.0, float _minY = 0.0,
93 float _maxX = 1.0, float _maxY = 1.0);
94 virtual ~FGCroppedTexture ();
96 virtual void setPath (const string &path) { _path = path; }
98 virtual const string &getPath () const { return _path; }
100 virtual ssgTexture * getTexture ();
102 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
103 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
106 virtual float getMinX () const { return _minX; }
107 virtual float getMinY () const { return _minY; }
108 virtual float getMaxX () const { return _maxX; }
109 virtual float getMaxY () const { return _maxY; }
114 ssgTexture * _texture;
115 float _minX, _minY, _maxX, _maxY;
120 ////////////////////////////////////////////////////////////////////////
122 ////////////////////////////////////////////////////////////////////////
126 * Instrument panel class.
128 * The panel is a container that has a background texture and holds
129 * zero or more instruments. The panel will order the instruments to
130 * redraw themselves when necessary, and will pass mouse clicks on to
131 * the appropriate instruments for processing.
133 class FGPanel : public SGSubsystem
140 // Update the panel (every frame).
141 virtual void init ();
142 virtual void bind ();
143 virtual void unbind ();
144 virtual void draw ();
145 virtual void update (double dt);
146 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
148 virtual void updateMouseDelay();
150 // transfer pointer ownership!!!
151 virtual void addInstrument (FGPanelInstrument * instrument);
153 // Background texture.
154 virtual void setBackground (ssgTexture * texture);
156 // Background multiple textures.
157 virtual void setMultiBackground (ssgTexture * texture, int idx);
159 // Make the panel visible or invisible.
160 virtual bool getVisibility () const;
161 virtual void setVisibility (bool visibility);
163 // Full width of panel.
164 virtual void setWidth (int width) { _width = width; }
165 virtual int getWidth () const { return _width; }
167 // Full height of panel.
168 virtual void setHeight (int height) { _height = height; }
169 virtual int getHeight () const { return _height; }
172 virtual void setXOffset (int offset);
173 virtual int getXOffset () const { return _x_offset->getIntValue(); }
176 virtual void setYOffset (int offset);
177 virtual int getYOffset () const { return _y_offset->getIntValue(); }
180 virtual void setViewHeight (int height) { _view_height = height; }
181 virtual int getViewHeight () const { return _view_height; }
183 // Handle a mouse click.
184 virtual bool doMouseAction (int button, int updown, int x, int y);
185 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
187 virtual void setDepthTest (bool enable);
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_ptr _visibility;
203 SGPropertyNode_ptr _x_offset;
204 SGPropertyNode_ptr _y_offset;
205 SGPropertyNode_ptr _jitter;
206 SGPropertyNode_ptr _flipx;
208 SGConstPropertyNode_ptr _xsize_node;
209 SGConstPropertyNode_ptr _ysize_node;
212 ssgTexture * _mbg[8];
213 // List of instruments in panel.
214 instrument_list_type _instruments;
215 bool _enable_depth_test;
220 ////////////////////////////////////////////////////////////////////////
222 ////////////////////////////////////////////////////////////////////////
226 * Class for user actions.
228 * The actions are command bindings, like bindings for the keyboard
229 * or joystick, but they are tied to specific mouse actions in
230 * rectangular areas of the panel.
232 class FGPanelAction : public SGConditional
236 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
237 virtual ~FGPanelAction ();
240 virtual int getButton () const { return _button; }
241 virtual int getX () const { return _x; }
242 virtual int getY () const { return _y; }
243 virtual int getWidth () const { return _w; }
244 virtual int getHeight () const { return _h; }
248 // transfer pointer ownership
249 virtual void addBinding (FGBinding * binding, int updown);
250 virtual void setButton (int button) { _button = button; }
251 virtual void setX (int x) { _x = x; }
252 virtual void setY (int y) { _y = y; }
253 virtual void setWidth (int w) { _w = w; }
254 virtual void setHeight (int h) { _h = h; }
256 // Check whether we're in the area.
257 virtual bool inArea (int button, int x, int y)
259 return (button == _button &&
266 // Perform the action.
267 virtual bool doAction (int updown);
270 typedef vector<FGBinding *> binding_list_t;
279 binding_list_t _bindings[2];
284 ////////////////////////////////////////////////////////////////////////
286 ////////////////////////////////////////////////////////////////////////
290 * A transformation for a layer.
292 class FGPanelTransformation : public SGConditional
302 FGPanelTransformation ();
303 virtual ~FGPanelTransformation ();
306 SGConstPropertyNode_ptr node;
313 SGInterpTable * table;
319 ////////////////////////////////////////////////////////////////////////
321 ////////////////////////////////////////////////////////////////////////
325 * A single layer of a multi-layered instrument.
327 * Each layer can be subject to a series of transformations based
328 * on current FGFS instrument readings: for example, a texture
329 * representing a needle can rotate to show the airspeed.
331 class FGInstrumentLayer : public SGConditional
335 FGInstrumentLayer (int w = -1, int h = -1);
336 virtual ~FGInstrumentLayer ();
338 virtual void draw () = 0;
339 virtual void transform () const;
341 virtual int getWidth () const { return _w; }
342 virtual int getHeight () const { return _h; }
343 virtual void setWidth (int w) { _w = w; }
344 virtual void setHeight (int h) { _h = h; }
346 // Transfer pointer ownership!!
348 virtual void addTransformation (FGPanelTransformation * transformation);
353 typedef vector<FGPanelTransformation *> transformation_list;
354 transformation_list _transformations;
359 ////////////////////////////////////////////////////////////////////////
361 ////////////////////////////////////////////////////////////////////////
365 * Abstract base class for a panel instrument.
367 * A panel instrument consists of zero or more actions, associated
368 * with mouse clicks in rectangular areas. Currently, the only
369 * concrete class derived from this is FGLayeredInstrument, but others
370 * may show up in the future (some complex instruments could be
371 * entirely hand-coded, for example).
373 class FGPanelInstrument : public SGConditional
376 FGPanelInstrument ();
377 FGPanelInstrument (int x, int y, int w, int h);
378 virtual ~FGPanelInstrument ();
380 virtual void draw () = 0;
381 virtual void drawHotspots();
383 virtual void setPosition(int x, int y);
384 virtual void setSize(int w, int h);
386 virtual int getXPos () const;
387 virtual int getYPos () const;
388 virtual int getWidth () const;
389 virtual int getHeight () const;
391 // Coordinates relative to centre.
392 // Transfer pointer ownership!!
393 virtual void addAction (FGPanelAction * action);
395 // Coordinates relative to centre.
396 virtual bool doMouseAction (int button, int updown, int x, int y);
400 typedef vector<FGPanelAction *> action_list_type;
401 action_list_type _actions;
406 * An instrument constructed of multiple layers.
408 * Each individual layer can be rotated or shifted to correspond
409 * to internal FGFS instrument readings.
411 class FGLayeredInstrument : public FGPanelInstrument
414 FGLayeredInstrument (int x, int y, int w, int h);
415 virtual ~FGLayeredInstrument ();
417 virtual void draw ();
419 // Transfer pointer ownership!!
420 virtual int addLayer (FGInstrumentLayer *layer);
421 virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
423 // Transfer pointer ownership!!
424 virtual void addTransformation (FGPanelTransformation * transformation);
427 typedef vector<FGInstrumentLayer *> layer_list;
433 * An empty-shell instrument that exists soley in
434 * order to redirect commands from the panel to a
435 * complex instrument inherited from SGSubsystem.
437 * Currently the only complex instrument is the KLN89,
438 * which we've hardwired this to for now.
440 class FGSpecialInstrument : public FGPanelInstrument
443 FGSpecialInstrument(DCLGPS* sb);
444 //FGSpecialInstrument (int x, int y, int w, int h);
445 virtual ~FGSpecialInstrument ();
447 virtual void draw ();
455 * An instrument layer containing a group of sublayers.
457 * This class is useful for gathering together a group of related
458 * layers, either to hold in an external file or to work under
459 * the same condition.
461 class FGGroupLayer : public FGInstrumentLayer
465 virtual ~FGGroupLayer ();
466 virtual void draw ();
467 // transfer pointer ownership
468 virtual void addLayer (FGInstrumentLayer * layer);
470 vector<FGInstrumentLayer *> _layers;
475 * A textured layer of an instrument.
477 * This is a layer holding a single texture. Normally, the texture's
478 * backgound should be transparent so that lower layers and the panel
479 * background can show through.
481 class FGTexturedLayer : public FGInstrumentLayer
484 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
485 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
486 virtual ~FGTexturedLayer ();
488 virtual void draw ();
490 virtual void setTexture (const FGCroppedTexture &texture) {
493 virtual const FGCroppedTexture &getTexture () const { return _texture; }
494 virtual FGCroppedTexture *getTexture() { return &_texture; }
496 void setEmissive(bool e) { _emissive = e; }
499 FGCroppedTexture _texture;
505 * A text layer of an instrument.
507 * This is a layer holding a string of static and/or generated text.
508 * It is useful for instruments that have text displays, such as
509 * a chronometer, GPS, or NavCom radio.
511 class FGTextLayer : public FGInstrumentLayer
520 class Chunk : public SGConditional
523 Chunk (const string &text, const string &fmt = "%s");
524 Chunk (ChunkType type, const SGPropertyNode * node,
525 const string &fmt = "", float mult = 1.0, float offs = 0.0,
526 bool truncation = false);
528 const char * getValue () const;
532 SGConstPropertyNode_ptr _node;
537 mutable char _buf[1024];
540 FGTextLayer (int w = -1, int h = -1);
541 virtual ~FGTextLayer ();
543 virtual void draw ();
545 // Transfer pointer!!
546 virtual void addChunk (Chunk * chunk);
547 virtual void setColor (float r, float g, float b);
548 virtual void setPointSize (float size);
549 virtual void setFontName ( const string &name );
550 virtual void setFont (fntFont * font);
554 void recalc_value () const;
556 typedef vector<Chunk *> chunk_list;
561 mutable string _font_name;
562 mutable string _value;
563 mutable SGTimeStamp _then;
564 mutable SGTimeStamp _now;
569 * A group layer that switches among its children.
571 * The first layer that passes its condition will be drawn, and
572 * any following layers will be ignored.
574 class FGSwitchLayer : public FGGroupLayer
577 // Transfer pointers!!
579 virtual void draw ();
586 ////////////////////////////////////////////////////////////////////////
588 ////////////////////////////////////////////////////////////////////////
591 * Test whether the panel should be visible.
593 bool fgPanelVisible ();
597 #endif // __PANEL_HXX