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/misc/props.hxx>
48 #include <Time/timestamp.hxx>
53 class FGPanelInstrument;
57 ////////////////////////////////////////////////////////////////////////
58 // Texture manager (should migrate out into FGFS).
60 // This class ensures that no texture is loaded more than once.
61 ////////////////////////////////////////////////////////////////////////
63 class FGTextureManager
66 static ssgTexture * createTexture(const string &relativePath);
68 static map<string,ssgTexture *> _textureMap;
73 ////////////////////////////////////////////////////////////////////////
74 // Cropped texture (should migrate out into FGFS).
76 // This structure wraps an SSG texture with cropping information.
77 ////////////////////////////////////////////////////////////////////////
79 class FGCroppedTexture
84 FGCroppedTexture (const string &path,
85 float _minX = 0.0, float _minY = 0.0,
86 float _maxX = 1.0, float _maxY = 1.0);
87 virtual ~FGCroppedTexture ();
89 virtual void setPath (const string &path) { _path = path; }
91 virtual const string &getPath () const { return _path; }
93 virtual ssgTexture * getTexture ();
95 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
96 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
99 virtual float getMinX () const { return _minX; }
100 virtual float getMinY () const { return _minY; }
101 virtual float getMaxX () const { return _maxX; }
102 virtual float getMaxY () const { return _maxY; }
107 ssgTexture * _texture;
108 float _minX, _minY, _maxX, _maxY;
113 ////////////////////////////////////////////////////////////////////////
114 // Instrument panel class.
116 // The panel is a container that has a background texture and holds
117 // zero or more instruments. The panel will order the instruments to
118 // redraw themselves when necessary, and will pass mouse clicks on to
119 // the appropriate instruments for processing.
120 ////////////////////////////////////////////////////////////////////////
126 FGPanel (int window_x, int window_y, int window_w, int window_h);
129 // transfer pointer ownership!!!
130 virtual void addInstrument (FGPanelInstrument * instrument);
132 // Update the panel (every frame).
133 virtual void update () const;
135 // Background texture.
136 virtual void setBackground (ssgTexture * texture);
138 // Make the panel visible or invisible.
139 virtual bool getVisibility () const;
140 virtual void setVisibility (bool visibility);
142 // Full width of panel.
143 virtual void setWidth (int width) { _width = width; }
144 virtual int getWidth () const { return _width; }
146 // Full height of panel.
147 virtual void setHeight (int height) { _height = height; }
148 virtual int getHeight () const { return _height; }
151 virtual void setXOffset (int offset);
152 virtual int getXOffset () const { return _x_offset; }
155 virtual void setYOffset (int offset);
156 virtual int getYOffset () const { return _y_offset; }
159 virtual void setViewHeight (int height) { _view_height = height; }
160 virtual int getViewHeight () const { return _view_height; }
162 // Handle a mouse click.
163 virtual bool doMouseAction (int button, int updown, int x, int y);
166 mutable bool _visibility;
167 mutable bool _mouseDown;
168 mutable int _mouseButton, _mouseX, _mouseY;
169 mutable int _mouseDelay;
170 mutable FGPanelInstrument * _mouseInstrument;
171 typedef vector<FGPanelInstrument *> instrument_list_type;
172 int _winx, _winy, _winw, _winh;
180 // List of instruments in panel.
181 instrument_list_type _instruments;
186 ////////////////////////////////////////////////////////////////////////
187 // Base class for user action types.
189 // Individual instruments can have actions associated with a mouse
190 // click in a rectangular area. Current concrete classes include
191 // FGAdjustAction, FGSwapAction, and FGToggleAction.
192 ////////////////////////////////////////////////////////////////////////
198 FGPanelAction (int button, int x, int y, int w, int h);
199 virtual ~FGPanelAction ();
202 virtual int getButton () const { return _button; }
203 virtual int getX () const { return _x; }
204 virtual int getY () const { return _y; }
205 virtual int getWidth () const { return _w; }
206 virtual int getHeight () const { return _h; }
209 virtual void setButton (int button) { _button = button; }
210 virtual void setX (int x) { _x = x; }
211 virtual void setY (int y) { _y = y; }
212 virtual void setWidth (int w) { _w = w; }
213 virtual void setHeight (int h) { _h = h; }
215 // Check whether we're in the area.
216 virtual bool inArea (int button, int x, int y)
218 return (button == _button &&
225 // Perform the action.
226 virtual void doAction () = 0;
238 ////////////////////////////////////////////////////////////////////////
239 // Adjustment action.
241 // This is an action to increase or decrease an FGFS value by a certain
242 // increment within a certain range. If the wrap flag is true, the
243 // value will wrap around if it goes below min or above max; otherwise,
244 // it will simply stop at min or max.
245 ////////////////////////////////////////////////////////////////////////
247 class FGAdjustAction : public FGPanelAction
250 FGAdjustAction (int button, int x, int y, int w, int h,
251 SGValue * value, float increment,
252 float min, float max, bool wrap=false);
253 virtual ~FGAdjustAction ();
254 virtual void doAction ();
266 ////////////////////////////////////////////////////////////////////////
269 // This is an action to swap two values. It's currently used in the
270 // navigation radios.
271 ////////////////////////////////////////////////////////////////////////
273 class FGSwapAction : public FGPanelAction
276 FGSwapAction (int button, int x, int y, int w, int h,
277 SGValue * value1, SGValue * value2);
278 virtual ~FGSwapAction ();
279 virtual void doAction ();
288 ////////////////////////////////////////////////////////////////////////
291 // This is an action to toggle a boolean value.
292 ////////////////////////////////////////////////////////////////////////
294 class FGToggleAction : public FGPanelAction
297 FGToggleAction (int button, int x, int y, int w, int h,
299 virtual ~FGToggleAction ();
300 virtual void doAction ();
308 ////////////////////////////////////////////////////////////////////////
309 // Abstract base class for a panel instrument.
311 // A panel instrument consists of zero or more actions, associated
312 // with mouse clicks in rectangular areas. Currently, the only
313 // concrete class derived from this is FGLayeredInstrument, but others
314 // may show up in the future (some complex instruments could be
315 // entirely hand-coded, for example).
316 ////////////////////////////////////////////////////////////////////////
318 class FGPanelInstrument
321 FGPanelInstrument ();
322 FGPanelInstrument (int x, int y, int w, int h);
323 virtual ~FGPanelInstrument ();
325 virtual void draw () = 0;
327 virtual void setPosition(int x, int y);
328 virtual void setSize(int w, int h);
330 virtual int getXPos () const;
331 virtual int getYPos () const;
332 virtual int getWidth () const;
333 virtual int getHeight () const;
335 // Coordinates relative to centre.
336 // Transfer pointer ownership!!
337 virtual void addAction (FGPanelAction * action);
339 // Coordinates relative to centre.
340 virtual bool doMouseAction (int button, int x, int y);
344 typedef vector<FGPanelAction *> action_list_type;
345 action_list_type _actions;
350 ////////////////////////////////////////////////////////////////////////
351 // Abstract base class for an instrument layer.
353 // The FGLayeredInstrument class builds up instruments by using layers
354 // of textures or text. Each layer can have zero or more
355 // transformations applied to it: for example, a needle layer can
356 // rotate to show the altitude or airspeed.
357 ////////////////////////////////////////////////////////////////////////
361 * A transformation for a layer.
363 class FGPanelTransformation {
372 FGPanelTransformation ();
373 virtual ~FGPanelTransformation ();
376 const SGValue * value;
386 * A single layer of a multi-layered instrument.
388 * Each layer can be subject to a series of transformations based
389 * on current FGFS instrument readings: for example, a texture
390 * representing a needle can rotate to show the airspeed.
392 class FGInstrumentLayer
396 FGInstrumentLayer (int w = -1, int h = -1);
397 virtual ~FGInstrumentLayer ();
399 virtual void draw () = 0;
400 virtual void transform () const;
402 virtual int getWidth () const { return _w; }
403 virtual int getHeight () const { return _h; }
404 virtual void setWidth (int w) { _w = w; }
405 virtual void setHeight (int h) { _h = h; }
407 // Transfer pointer ownership!!
409 virtual void addTransformation (FGPanelTransformation * transformation);
414 typedef vector<FGPanelTransformation *> transformation_list;
415 transformation_list _transformations;
420 ////////////////////////////////////////////////////////////////////////
421 // An instrument composed of layers.
423 // This class represents an instrument which is simply a series of
424 // layers piled one on top of the other, each one undergoing its own
425 // set of transformations. For example, one layer can represent
426 // the instrument's face (which doesn't move), while the next layer
427 // can represent a needle that rotates depending on an FGFS variable.
428 ////////////////////////////////////////////////////////////////////////
432 * An instrument constructed of multiple layers.
434 * Each individual layer can be rotated or shifted to correspond
435 * to internal FGFS instrument readings.
437 class FGLayeredInstrument : public FGPanelInstrument
440 FGLayeredInstrument (int x, int y, int w, int h);
441 virtual ~FGLayeredInstrument ();
443 virtual void draw ();
445 // Transfer pointer ownership!!
446 virtual int addLayer (FGInstrumentLayer *layer);
447 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
449 // Transfer pointer ownership!!
450 virtual void addTransformation (FGPanelTransformation * transformation);
453 typedef vector<FGInstrumentLayer *> layer_list;
459 ////////////////////////////////////////////////////////////////////////
460 // A textured layer of an instrument.
462 // This is a layer holding a single texture. Normally, the texture's
463 // backgound should be transparent so that lower layers and the panel
464 // background can show through.
465 ////////////////////////////////////////////////////////////////////////
467 class FGTexturedLayer : public FGInstrumentLayer
470 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
471 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
472 virtual ~FGTexturedLayer ();
474 virtual void draw ();
476 virtual void setTexture (const FGCroppedTexture &texture) {
479 virtual FGCroppedTexture &getTexture () { return _texture; }
480 virtual const FGCroppedTexture &getTexture () const { return _texture; }
483 mutable FGCroppedTexture _texture;
488 ////////////////////////////////////////////////////////////////////////
489 // A text layer of an instrument.
491 // This is a layer holding a string of static and/or generated text.
492 // It is useful for instruments that have text displays, such as
493 // a chronometer, GPS, or NavCom radio.
494 ////////////////////////////////////////////////////////////////////////
496 class FGTextLayer : public FGInstrumentLayer
499 typedef enum ChunkType {
507 Chunk (const string &text, const string &fmt = "%s");
508 Chunk (ChunkType type, const SGValue * value,
509 const string &fmt = "", float mult = 1.0);
511 const char * getValue () const;
515 const SGValue * _value;
518 mutable char _buf[1024];
521 FGTextLayer (int w = -1, int h = -1);
522 virtual ~FGTextLayer ();
524 virtual void draw ();
526 // Transfer pointer!!
527 virtual void addChunk (Chunk * chunk);
528 virtual void setColor (float r, float g, float b);
529 virtual void setPointSize (float size);
530 virtual void setFont (fntFont * font);
534 void recalc_value () const;
536 typedef vector<Chunk *> chunk_list;
542 mutable string _value;
543 mutable FGTimeStamp _then;
544 mutable FGTimeStamp _now;
549 ////////////////////////////////////////////////////////////////////////
550 // A layer that switches between two other layers.
551 ////////////////////////////////////////////////////////////////////////
553 class FGSwitchLayer : public FGInstrumentLayer
556 // Transfer pointers!!
557 FGSwitchLayer (int w, int h, const SGValue * value,
558 FGInstrumentLayer * layer1,
559 FGInstrumentLayer * layer2);
560 virtual ~FGSwitchLayer ();
562 virtual void draw ();
565 const SGValue * _value;
566 FGInstrumentLayer * _layer1, * _layer2;
571 ////////////////////////////////////////////////////////////////////////
573 ////////////////////////////////////////////////////////////////////////
575 bool fgPanelVisible ();
579 ////////////////////////////////////////////////////////////////////////
580 // The current panel, if any.
581 ////////////////////////////////////////////////////////////////////////
583 extern FGPanel * current_panel;
587 #endif // __PANEL_HXX