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>
42 #include <simgear/math/interpolater.hxx>
43 #include <simgear/misc/props.hxx>
44 #include <simgear/timing/timestamp.hxx>
50 #include <Main/fgfs.hxx>
52 #include <Input/input.hxx>
57 class FGPanelInstrument;
61 ////////////////////////////////////////////////////////////////////////
62 // Texture manager (should migrate out into FGFS).
64 // This class ensures that no texture is loaded more than once.
65 ////////////////////////////////////////////////////////////////////////
67 class FGTextureManager
70 static ssgTexture * createTexture(const string &relativePath);
72 static map<string,ssgTexture *> _textureMap;
77 ////////////////////////////////////////////////////////////////////////
78 // Cropped texture (should migrate out into FGFS).
80 // This structure wraps an SSG texture with cropping information.
81 ////////////////////////////////////////////////////////////////////////
83 class FGCroppedTexture
88 FGCroppedTexture (const string &path,
89 float _minX = 0.0, float _minY = 0.0,
90 float _maxX = 1.0, float _maxY = 1.0);
91 virtual ~FGCroppedTexture ();
93 virtual void setPath (const string &path) { _path = path; }
95 virtual const string &getPath () const { return _path; }
97 virtual ssgTexture * getTexture ();
99 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
100 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
103 virtual float getMinX () const { return _minX; }
104 virtual float getMinY () const { return _minY; }
105 virtual float getMaxX () const { return _maxX; }
106 virtual float getMaxY () const { return _maxY; }
111 ssgTexture * _texture;
112 float _minX, _minY, _maxX, _maxY;
117 ////////////////////////////////////////////////////////////////////////
118 // Instrument panel class.
120 // The panel is a container that has a background texture and holds
121 // zero or more instruments. The panel will order the instruments to
122 // redraw themselves when necessary, and will pass mouse clicks on to
123 // the appropriate instruments for processing.
124 ////////////////////////////////////////////////////////////////////////
126 class FGPanel : public FGSubsystem
133 // Update the panel (every frame).
134 virtual void init ();
135 virtual void bind ();
136 virtual void unbind ();
137 virtual void update ();
138 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
140 // transfer pointer ownership!!!
141 virtual void addInstrument (FGPanelInstrument * instrument);
143 // Background texture.
144 virtual void setBackground (ssgTexture * texture);
146 // Make the panel visible or invisible.
147 virtual bool getVisibility () const;
148 virtual void setVisibility (bool visibility);
150 // Full width of panel.
151 virtual void setWidth (int width) { _width = width; }
152 virtual int getWidth () const { return _width; }
154 // Full height of panel.
155 virtual void setHeight (int height) { _height = height; }
156 virtual int getHeight () const { return _height; }
159 virtual void setXOffset (int offset);
160 virtual int getXOffset () const { return _x_offset; }
163 virtual void setYOffset (int offset);
164 virtual int getYOffset () const { return _y_offset; }
167 virtual void setViewHeight (int height) { _view_height = height; }
168 virtual int getViewHeight () const { return _view_height; }
170 // Handle a mouse click.
171 virtual bool doMouseAction (int button, int updown, int x, int y);
174 mutable bool _visibility;
175 mutable bool _mouseDown;
176 mutable int _mouseButton, _mouseX, _mouseY;
177 mutable int _mouseDelay;
178 mutable FGPanelInstrument * _mouseInstrument;
179 typedef vector<FGPanelInstrument *> instrument_list_type;
187 const SGPropertyNode * _xsize_node;
188 const SGPropertyNode * _ysize_node;
191 // List of instruments in panel.
192 instrument_list_type _instruments;
197 ////////////////////////////////////////////////////////////////////////
198 // Base class for user action types.
200 // Individual instruments can have actions associated with a mouse
201 // click in a rectangular area. Current concrete classes include
202 // FGAdjustAction, FGSwapAction, and FGToggleAction.
203 ////////////////////////////////////////////////////////////////////////
209 FGPanelAction (int button, int x, int y, int w, int h);
210 virtual ~FGPanelAction ();
213 virtual int getButton () const { return _button; }
214 virtual int getX () const { return _x; }
215 virtual int getY () const { return _y; }
216 virtual int getWidth () const { return _w; }
217 virtual int getHeight () const { return _h; }
220 virtual void addBinding (const FGBinding &binding);
221 virtual void setButton (int button) { _button = button; }
222 virtual void setX (int x) { _x = x; }
223 virtual void setY (int y) { _y = y; }
224 virtual void setWidth (int w) { _w = w; }
225 virtual void setHeight (int h) { _h = h; }
227 // Check whether we're in the area.
228 virtual bool inArea (int button, int x, int y)
230 return (button == _button &&
237 // Perform the action.
238 virtual void doAction ();
241 typedef vector<FGBinding> binding_list_t;
248 binding_list_t _bindings;
253 ////////////////////////////////////////////////////////////////////////
254 // Abstract base class for a panel instrument.
256 // A panel instrument consists of zero or more actions, associated
257 // with mouse clicks in rectangular areas. Currently, the only
258 // concrete class derived from this is FGLayeredInstrument, but others
259 // may show up in the future (some complex instruments could be
260 // entirely hand-coded, for example).
261 ////////////////////////////////////////////////////////////////////////
263 class FGPanelInstrument
266 FGPanelInstrument ();
267 FGPanelInstrument (int x, int y, int w, int h);
268 virtual ~FGPanelInstrument ();
270 virtual void draw () = 0;
272 virtual void setPosition(int x, int y);
273 virtual void setSize(int w, int h);
275 virtual int getXPos () const;
276 virtual int getYPos () const;
277 virtual int getWidth () const;
278 virtual int getHeight () const;
280 // Coordinates relative to centre.
281 // Transfer pointer ownership!!
282 virtual void addAction (FGPanelAction * action);
284 // Coordinates relative to centre.
285 virtual bool doMouseAction (int button, int x, int y);
289 typedef vector<FGPanelAction *> action_list_type;
290 action_list_type _actions;
295 ////////////////////////////////////////////////////////////////////////
296 // Abstract base class for an instrument layer.
298 // The FGLayeredInstrument class builds up instruments by using layers
299 // of textures or text. Each layer can have zero or more
300 // transformations applied to it: for example, a needle layer can
301 // rotate to show the altitude or airspeed.
302 ////////////////////////////////////////////////////////////////////////
306 * A transformation for a layer.
308 class FGPanelTransformation {
317 FGPanelTransformation ();
318 virtual ~FGPanelTransformation ();
321 const SGPropertyNode * node;
326 SGInterpTable * table;
332 * A single layer of a multi-layered instrument.
334 * Each layer can be subject to a series of transformations based
335 * on current FGFS instrument readings: for example, a texture
336 * representing a needle can rotate to show the airspeed.
338 class FGInstrumentLayer
342 FGInstrumentLayer (int w = -1, int h = -1);
343 virtual ~FGInstrumentLayer ();
345 virtual void draw () = 0;
346 virtual void transform () const;
348 virtual int getWidth () const { return _w; }
349 virtual int getHeight () const { return _h; }
350 virtual void setWidth (int w) { _w = w; }
351 virtual void setHeight (int h) { _h = h; }
353 // Transfer pointer ownership!!
355 virtual void addTransformation (FGPanelTransformation * transformation);
360 typedef vector<FGPanelTransformation *> transformation_list;
361 transformation_list _transformations;
366 ////////////////////////////////////////////////////////////////////////
367 // An instrument composed of layers.
369 // This class represents an instrument which is simply a series of
370 // layers piled one on top of the other, each one undergoing its own
371 // set of transformations. For example, one layer can represent
372 // the instrument's face (which doesn't move), while the next layer
373 // can represent a needle that rotates depending on an FGFS variable.
374 ////////////////////////////////////////////////////////////////////////
378 * An instrument constructed of multiple layers.
380 * Each individual layer can be rotated or shifted to correspond
381 * to internal FGFS instrument readings.
383 class FGLayeredInstrument : public FGPanelInstrument
386 FGLayeredInstrument (int x, int y, int w, int h);
387 virtual ~FGLayeredInstrument ();
389 virtual void draw ();
391 // Transfer pointer ownership!!
392 virtual int addLayer (FGInstrumentLayer *layer);
393 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
395 // Transfer pointer ownership!!
396 virtual void addTransformation (FGPanelTransformation * transformation);
399 typedef vector<FGInstrumentLayer *> layer_list;
405 ////////////////////////////////////////////////////////////////////////
406 // A textured layer of an instrument.
408 // This is a layer holding a single texture. Normally, the texture's
409 // backgound should be transparent so that lower layers and the panel
410 // background can show through.
411 ////////////////////////////////////////////////////////////////////////
413 class FGTexturedLayer : public FGInstrumentLayer
416 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
417 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
418 virtual ~FGTexturedLayer ();
420 virtual void draw ();
422 virtual void setTexture (const FGCroppedTexture &texture) {
425 virtual FGCroppedTexture &getTexture () { return _texture; }
426 virtual const FGCroppedTexture &getTexture () const { return _texture; }
429 mutable FGCroppedTexture _texture;
434 ////////////////////////////////////////////////////////////////////////
435 // A text layer of an instrument.
437 // This is a layer holding a string of static and/or generated text.
438 // It is useful for instruments that have text displays, such as
439 // a chronometer, GPS, or NavCom radio.
440 ////////////////////////////////////////////////////////////////////////
442 class FGTextLayer : public FGInstrumentLayer
445 typedef enum ChunkType {
453 Chunk (const string &text, const string &fmt = "%s");
454 Chunk (ChunkType type, const SGPropertyNode * node,
455 const string &fmt = "", float mult = 1.0);
457 const char * getValue () const;
461 const SGPropertyNode * _node;
464 mutable char _buf[1024];
467 FGTextLayer (int w = -1, int h = -1);
468 virtual ~FGTextLayer ();
470 virtual void draw ();
472 // Transfer pointer!!
473 virtual void addChunk (Chunk * chunk);
474 virtual void setColor (float r, float g, float b);
475 virtual void setPointSize (float size);
476 virtual void setFont (fntFont * font);
480 void recalc_value () const;
482 typedef vector<Chunk *> chunk_list;
488 mutable string _value;
489 mutable SGTimeStamp _then;
490 mutable SGTimeStamp _now;
495 ////////////////////////////////////////////////////////////////////////
496 // A layer that switches between two other layers.
497 ////////////////////////////////////////////////////////////////////////
499 class FGSwitchLayer : public FGInstrumentLayer
502 // Transfer pointers!!
503 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
504 FGInstrumentLayer * layer1,
505 FGInstrumentLayer * layer2);
506 virtual ~FGSwitchLayer ();
508 virtual void draw ();
511 const SGPropertyNode * _node;
512 FGInstrumentLayer * _layer1, * _layer2;
517 ////////////////////////////////////////////////////////////////////////
519 ////////////////////////////////////////////////////////////////////////
521 bool fgPanelVisible ();
525 ////////////////////////////////////////////////////////////////////////
526 // The current panel, if any.
527 ////////////////////////////////////////////////////////////////////////
529 extern FGPanel * current_panel;
533 #endif // __PANEL_HXX