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>
51 class FGPanelInstrument;
55 ////////////////////////////////////////////////////////////////////////
56 // Texture manager (should migrate out into FGFS).
58 // This class ensures that no texture is loaded more than once.
59 ////////////////////////////////////////////////////////////////////////
61 class FGTextureManager
64 static ssgTexture * createTexture(const string &relativePath);
66 static map<string,ssgTexture *> _textureMap;
71 ////////////////////////////////////////////////////////////////////////
72 // Cropped texture (should migrate out into FGFS).
74 // This structure wraps an SSG texture with cropping information.
75 ////////////////////////////////////////////////////////////////////////
77 class FGCroppedTexture
82 FGCroppedTexture (const string &path,
83 float _minX = 0.0, float _minY = 0.0,
84 float _maxX = 1.0, float _maxY = 1.0);
85 virtual ~FGCroppedTexture ();
87 virtual void setPath (const string &path) { _path = path; }
89 virtual const string &getPath () const { return _path; }
91 virtual ssgTexture * getTexture ();
93 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
94 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
97 virtual float getMinX () const { return _minX; }
98 virtual float getMinY () const { return _minY; }
99 virtual float getMaxX () const { return _maxX; }
100 virtual float getMaxY () const { return _maxY; }
105 ssgTexture * _texture;
106 float _minX, _minY, _maxX, _maxY;
111 ////////////////////////////////////////////////////////////////////////
112 // Instrument panel class.
114 // The panel is a container that has a background texture and holds
115 // zero or more instruments. The panel will order the instruments to
116 // redraw themselves when necessary, and will pass mouse clicks on to
117 // the appropriate instruments for processing.
118 ////////////////////////////////////////////////////////////////////////
124 FGPanel (int x, int y, int w, int h);
127 // transfer pointer ownership!!!
128 virtual void addInstrument (FGPanelInstrument * instrument);
130 // Update the panel (every frame).
131 virtual void update () const;
133 // Background texture.
134 virtual void setBackground (ssgTexture * texture);
136 // Make the panel visible or invisible.
137 virtual bool getVisibility () const;
138 virtual void setVisibility (bool visibility);
140 // Handle a mouse click.
141 virtual bool doMouseAction (int button, int updown, int x, int y);
144 mutable bool _visibility;
145 mutable bool _mouseDown;
146 mutable int _mouseButton, _mouseX, _mouseY;
147 mutable int _mouseDelay;
148 mutable FGPanelInstrument * _mouseInstrument;
149 typedef vector<FGPanelInstrument *> instrument_list_type;
153 // List of instruments in panel.
154 instrument_list_type _instruments;
159 ////////////////////////////////////////////////////////////////////////
160 // Base class for user action types.
162 // Individual instruments can have actions associated with a mouse
163 // click in a rectangular area. Current concrete classes include
164 // FGAdjustAction, FGSwapAction, and FGToggleAction.
165 ////////////////////////////////////////////////////////////////////////
171 FGPanelAction (int button, int x, int y, int w, int h);
172 virtual ~FGPanelAction ();
175 virtual int getButton () const { return _button; }
176 virtual int getX () const { return _x; }
177 virtual int getY () const { return _y; }
178 virtual int getWidth () const { return _w; }
179 virtual int getHeight () const { return _h; }
182 virtual void setButton (int button) { _button = button; }
183 virtual void setX (int x) { _x = x; }
184 virtual void setY (int y) { _y = y; }
185 virtual void setWidth (int w) { _w = w; }
186 virtual void setHeight (int h) { _h = h; }
188 // Check whether we're in the area.
189 virtual bool inArea (int button, int x, int y)
191 return (button == _button &&
198 // Perform the action.
199 virtual void doAction () = 0;
211 ////////////////////////////////////////////////////////////////////////
212 // Adjustment action.
214 // This is an action to increase or decrease an FGFS value by a certain
215 // increment within a certain range. If the wrap flag is true, the
216 // value will wrap around if it goes below min or above max; otherwise,
217 // it will simply stop at min or max.
218 ////////////////////////////////////////////////////////////////////////
220 class FGAdjustAction : public FGPanelAction
223 FGAdjustAction (int button, int x, int y, int w, int h,
224 SGValue * value, float increment,
225 float min, float max, bool wrap=false);
226 virtual ~FGAdjustAction ();
227 virtual void doAction ();
239 ////////////////////////////////////////////////////////////////////////
242 // This is an action to swap two values. It's currently used in the
243 // navigation radios.
244 ////////////////////////////////////////////////////////////////////////
246 class FGSwapAction : public FGPanelAction
249 FGSwapAction (int button, int x, int y, int w, int h,
250 SGValue * value1, SGValue * value2);
251 virtual ~FGSwapAction ();
252 virtual void doAction ();
261 ////////////////////////////////////////////////////////////////////////
264 // This is an action to toggle a boolean value.
265 ////////////////////////////////////////////////////////////////////////
267 class FGToggleAction : public FGPanelAction
270 FGToggleAction (int button, int x, int y, int w, int h,
272 virtual ~FGToggleAction ();
273 virtual void doAction ();
281 ////////////////////////////////////////////////////////////////////////
282 // Abstract base class for a panel instrument.
284 // A panel instrument consists of zero or more actions, associated
285 // with mouse clicks in rectangular areas. Currently, the only
286 // concrete class derived from this is FGLayeredInstrument, but others
287 // may show up in the future (some complex instruments could be
288 // entirely hand-coded, for example).
289 ////////////////////////////////////////////////////////////////////////
291 class FGPanelInstrument
294 FGPanelInstrument ();
295 FGPanelInstrument (int x, int y, int w, int h);
296 virtual ~FGPanelInstrument ();
298 virtual void draw () = 0;
300 virtual void setPosition(int x, int y);
301 virtual void setSize(int w, int h);
303 virtual int getXPos () const;
304 virtual int getYPos () const;
305 virtual int getWidth () const;
306 virtual int getHeight () const;
308 // Coordinates relative to centre.
309 // Transfer pointer ownership!!
310 virtual void addAction (FGPanelAction * action);
312 // Coordinates relative to centre.
313 virtual bool doMouseAction (int button, int x, int y);
317 typedef vector<FGPanelAction *> action_list_type;
318 action_list_type _actions;
323 ////////////////////////////////////////////////////////////////////////
324 // Abstract base class for an instrument layer.
326 // The FGLayeredInstrument class builds up instruments by using layers
327 // of textures or text. Each layer can have zero or more
328 // transformations applied to it: for example, a needle layer can
329 // rotate to show the altitude or airspeed.
330 ////////////////////////////////////////////////////////////////////////
334 * A transformation for a layer.
336 class FGPanelTransformation {
345 FGPanelTransformation ();
346 FGPanelTransformation (Type type, const SGValue * value,
347 float min, float max,
348 float factor, float offset);
349 virtual ~FGPanelTransformation ();
352 const SGValue * value;
362 * A single layer of a multi-layered instrument.
364 * Each layer can be subject to a series of transformations based
365 * on current FGFS instrument readings: for example, a texture
366 * representing a needle can rotate to show the airspeed.
368 class FGInstrumentLayer
372 FGInstrumentLayer (int w = -1, int h = -1);
373 virtual ~FGInstrumentLayer ();
375 virtual void draw () = 0;
376 virtual void transform () const;
378 virtual int getWidth () const { return _w; }
379 virtual int getHeight () const { return _h; }
380 virtual void setWidth (int w) { _w = w; }
381 virtual void setHeight (int h) { _h = h; }
383 // Transfer pointer ownership!!
385 virtual void addTransformation (FGPanelTransformation * transformation);
390 typedef vector<FGPanelTransformation *> transformation_list;
391 transformation_list _transformations;
396 ////////////////////////////////////////////////////////////////////////
397 // An instrument composed of layers.
399 // This class represents an instrument which is simply a series of
400 // layers piled one on top of the other, each one undergoing its own
401 // set of transformations. For example, one layer can represent
402 // the instrument's face (which doesn't move), while the next layer
403 // can represent a needle that rotates depending on an FGFS variable.
404 ////////////////////////////////////////////////////////////////////////
408 * An instrument constructed of multiple layers.
410 * Each individual layer can be rotated or shifted to correspond
411 * to internal FGFS instrument readings.
413 class FGLayeredInstrument : public FGPanelInstrument
416 typedef vector<FGInstrumentLayer *> layer_list;
417 FGLayeredInstrument (int x, int y, int w, int h);
418 virtual ~FGLayeredInstrument ();
420 virtual void draw ();
422 // Transfer pointer ownership!!
423 virtual int addLayer (FGInstrumentLayer *layer);
424 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
426 // Transfer pointer ownership!!
427 virtual void addTransformation (FGPanelTransformation * transformation);
435 ////////////////////////////////////////////////////////////////////////
436 // A textured layer of an instrument.
438 // This is a layer holding a single texture. Normally, the texture's
439 // backgound should be transparent so that lower layers and the panel
440 // background can show through.
441 ////////////////////////////////////////////////////////////////////////
443 class FGTexturedLayer : public FGInstrumentLayer
446 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
447 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
448 virtual ~FGTexturedLayer ();
450 virtual void draw ();
452 virtual void setTexture (const FGCroppedTexture &texture) {
455 virtual FGCroppedTexture &getTexture () { return _texture; }
456 virtual const FGCroppedTexture &getTexture () const { return _texture; }
459 mutable FGCroppedTexture _texture;
464 ////////////////////////////////////////////////////////////////////////
465 // A moving window on a texture.
467 // This layer automatically recrops a cropped texture based on
468 // property values, creating a moving window over the texture.
469 ////////////////////////////////////////////////////////////////////////
471 class FGWindowLayer : public FGTexturedLayer
474 FGWindowLayer (int w = -1, int h = -1);
475 FGWindowLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
476 virtual ~FGWindowLayer ();
478 virtual void draw ();
480 virtual const SGValue * getXValue () const { return _xValue; }
481 virtual void setXValue (const SGValue * value) { _xValue = value; }
482 virtual const SGValue * getYValue () const { return _yValue; }
483 virtual void setYValue (const SGValue * value) { _yValue = value; }
486 const SGValue * _xValue;
487 const SGValue * _yValue;
492 ////////////////////////////////////////////////////////////////////////
493 // A text layer of an instrument.
495 // This is a layer holding a string of static and/or generated text.
496 // It is useful for instruments that have text displays, such as
497 // a chronometer, GPS, or NavCom radio.
498 ////////////////////////////////////////////////////////////////////////
500 class FGTextLayer : public FGInstrumentLayer
503 typedef enum ChunkType {
511 Chunk (const string &text, const string &fmt = "%s");
512 Chunk (ChunkType type, const SGValue * value,
513 const string &fmt = "", float mult = 1.0);
515 const char * getValue () const;
519 const SGValue * _value;
522 mutable char _buf[1024];
525 FGTextLayer (int w = -1, int h = -1, Chunk * chunk1 = 0, Chunk * chunk2 = 0,
527 virtual ~FGTextLayer ();
529 virtual void draw ();
531 // Transfer pointer!!
532 virtual void addChunk (Chunk * chunk);
533 virtual void setColor (float r, float g, float b);
534 virtual void setPointSize (float size);
535 virtual void setFont (fntFont * font);
538 typedef vector<Chunk *> chunk_list;
543 // FIXME: need only one globally
544 mutable fntRenderer _renderer;
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 ////////////////////////////////////////////////////////////////////////
572 // The current panel, if any.
573 ////////////////////////////////////////////////////////////////////////
575 extern FGPanel * current_panel;
579 #endif // __PANEL_HXX