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>
55 class FGPanelInstrument;
59 ////////////////////////////////////////////////////////////////////////
60 // Texture manager (should migrate out into FGFS).
62 // This class ensures that no texture is loaded more than once.
63 ////////////////////////////////////////////////////////////////////////
65 class FGTextureManager
68 static ssgTexture * createTexture(const string &relativePath);
70 static map<string,ssgTexture *> _textureMap;
75 ////////////////////////////////////////////////////////////////////////
76 // Cropped texture (should migrate out into FGFS).
78 // This structure wraps an SSG texture with cropping information.
79 ////////////////////////////////////////////////////////////////////////
81 class FGCroppedTexture
86 FGCroppedTexture (const string &path,
87 float _minX = 0.0, float _minY = 0.0,
88 float _maxX = 1.0, float _maxY = 1.0);
89 virtual ~FGCroppedTexture ();
91 virtual void setPath (const string &path) { _path = path; }
93 virtual const string &getPath () const { return _path; }
95 virtual ssgTexture * getTexture ();
97 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
98 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
101 virtual float getMinX () const { return _minX; }
102 virtual float getMinY () const { return _minY; }
103 virtual float getMaxX () const { return _maxX; }
104 virtual float getMaxY () const { return _maxY; }
109 ssgTexture * _texture;
110 float _minX, _minY, _maxX, _maxY;
115 ////////////////////////////////////////////////////////////////////////
116 // Instrument panel class.
118 // The panel is a container that has a background texture and holds
119 // zero or more instruments. The panel will order the instruments to
120 // redraw themselves when necessary, and will pass mouse clicks on to
121 // the appropriate instruments for processing.
122 ////////////////////////////////////////////////////////////////////////
124 class FGPanel : public FGSubsystem
131 // Update the panel (every frame).
132 virtual void init ();
133 virtual void bind ();
134 virtual void unbind ();
135 virtual void update ();
137 // transfer pointer ownership!!!
138 virtual void addInstrument (FGPanelInstrument * instrument);
140 // Background texture.
141 virtual void setBackground (ssgTexture * texture);
143 // Make the panel visible or invisible.
144 virtual bool getVisibility () const;
145 virtual void setVisibility (bool visibility);
147 // Full width of panel.
148 virtual void setWidth (int width) { _width = width; }
149 virtual int getWidth () const { return _width; }
151 // Full height of panel.
152 virtual void setHeight (int height) { _height = height; }
153 virtual int getHeight () const { return _height; }
156 virtual void setXOffset (int offset);
157 virtual int getXOffset () const { return _x_offset; }
160 virtual void setYOffset (int offset);
161 virtual int getYOffset () const { return _y_offset; }
164 virtual void setViewHeight (int height) { _view_height = height; }
165 virtual int getViewHeight () const { return _view_height; }
167 // Handle a mouse click.
168 virtual bool doMouseAction (int button, int updown, int x, int y);
171 mutable bool _visibility;
172 mutable bool _mouseDown;
173 mutable int _mouseButton, _mouseX, _mouseY;
174 mutable int _mouseDelay;
175 mutable FGPanelInstrument * _mouseInstrument;
176 typedef vector<FGPanelInstrument *> instrument_list_type;
184 const SGPropertyNode * _xsize_node;
185 const SGPropertyNode * _ysize_node;
188 // List of instruments in panel.
189 instrument_list_type _instruments;
194 ////////////////////////////////////////////////////////////////////////
195 // Base class for user action types.
197 // Individual instruments can have actions associated with a mouse
198 // click in a rectangular area. Current concrete classes include
199 // FGAdjustAction, FGSwapAction, and FGToggleAction.
200 ////////////////////////////////////////////////////////////////////////
206 FGPanelAction (int button, int x, int y, int w, int h);
207 virtual ~FGPanelAction ();
210 virtual int getButton () const { return _button; }
211 virtual int getX () const { return _x; }
212 virtual int getY () const { return _y; }
213 virtual int getWidth () const { return _w; }
214 virtual int getHeight () const { return _h; }
217 virtual void setButton (int button) { _button = button; }
218 virtual void setX (int x) { _x = x; }
219 virtual void setY (int y) { _y = y; }
220 virtual void setWidth (int w) { _w = w; }
221 virtual void setHeight (int h) { _h = h; }
223 // Check whether we're in the area.
224 virtual bool inArea (int button, int x, int y)
226 return (button == _button &&
233 // Perform the action.
234 virtual void doAction () = 0;
246 ////////////////////////////////////////////////////////////////////////
247 // Adjustment action.
249 // This is an action to increase or decrease an FGFS value by a certain
250 // increment within a certain range. If the wrap flag is true, the
251 // value will wrap around if it goes below min or above max; otherwise,
252 // it will simply stop at min or max.
253 ////////////////////////////////////////////////////////////////////////
255 class FGAdjustAction : public FGPanelAction
258 FGAdjustAction (int button, int x, int y, int w, int h,
259 SGPropertyNode * node, float increment,
260 float min, float max, bool wrap=false);
261 virtual ~FGAdjustAction ();
262 virtual void doAction ();
265 SGPropertyNode * _node;
274 ////////////////////////////////////////////////////////////////////////
277 // This is an action to swap two values. It's currently used in the
278 // navigation radios.
279 ////////////////////////////////////////////////////////////////////////
281 class FGSwapAction : public FGPanelAction
284 FGSwapAction (int button, int x, int y, int w, int h,
285 SGPropertyNode * node1, SGPropertyNode * node2);
286 virtual ~FGSwapAction ();
287 virtual void doAction ();
290 SGPropertyNode * _node1;
291 SGPropertyNode * _node2;
296 ////////////////////////////////////////////////////////////////////////
299 // This is an action to toggle a boolean value.
300 ////////////////////////////////////////////////////////////////////////
302 class FGToggleAction : public FGPanelAction
305 FGToggleAction (int button, int x, int y, int w, int h,
306 SGPropertyNode * node);
307 virtual ~FGToggleAction ();
308 virtual void doAction ();
311 SGPropertyNode * _node;
316 ////////////////////////////////////////////////////////////////////////
317 // Abstract base class for a panel instrument.
319 // A panel instrument consists of zero or more actions, associated
320 // with mouse clicks in rectangular areas. Currently, the only
321 // concrete class derived from this is FGLayeredInstrument, but others
322 // may show up in the future (some complex instruments could be
323 // entirely hand-coded, for example).
324 ////////////////////////////////////////////////////////////////////////
326 class FGPanelInstrument
329 FGPanelInstrument ();
330 FGPanelInstrument (int x, int y, int w, int h);
331 virtual ~FGPanelInstrument ();
333 virtual void draw () = 0;
335 virtual void setPosition(int x, int y);
336 virtual void setSize(int w, int h);
338 virtual int getXPos () const;
339 virtual int getYPos () const;
340 virtual int getWidth () const;
341 virtual int getHeight () const;
343 // Coordinates relative to centre.
344 // Transfer pointer ownership!!
345 virtual void addAction (FGPanelAction * action);
347 // Coordinates relative to centre.
348 virtual bool doMouseAction (int button, int x, int y);
352 typedef vector<FGPanelAction *> action_list_type;
353 action_list_type _actions;
358 ////////////////////////////////////////////////////////////////////////
359 // Abstract base class for an instrument layer.
361 // The FGLayeredInstrument class builds up instruments by using layers
362 // of textures or text. Each layer can have zero or more
363 // transformations applied to it: for example, a needle layer can
364 // rotate to show the altitude or airspeed.
365 ////////////////////////////////////////////////////////////////////////
369 * A transformation for a layer.
371 class FGPanelTransformation {
380 FGPanelTransformation ();
381 virtual ~FGPanelTransformation ();
384 const SGPropertyNode * node;
389 SGInterpTable * table;
395 * A single layer of a multi-layered instrument.
397 * Each layer can be subject to a series of transformations based
398 * on current FGFS instrument readings: for example, a texture
399 * representing a needle can rotate to show the airspeed.
401 class FGInstrumentLayer
405 FGInstrumentLayer (int w = -1, int h = -1);
406 virtual ~FGInstrumentLayer ();
408 virtual void draw () = 0;
409 virtual void transform () const;
411 virtual int getWidth () const { return _w; }
412 virtual int getHeight () const { return _h; }
413 virtual void setWidth (int w) { _w = w; }
414 virtual void setHeight (int h) { _h = h; }
416 // Transfer pointer ownership!!
418 virtual void addTransformation (FGPanelTransformation * transformation);
423 typedef vector<FGPanelTransformation *> transformation_list;
424 transformation_list _transformations;
429 ////////////////////////////////////////////////////////////////////////
430 // An instrument composed of layers.
432 // This class represents an instrument which is simply a series of
433 // layers piled one on top of the other, each one undergoing its own
434 // set of transformations. For example, one layer can represent
435 // the instrument's face (which doesn't move), while the next layer
436 // can represent a needle that rotates depending on an FGFS variable.
437 ////////////////////////////////////////////////////////////////////////
441 * An instrument constructed of multiple layers.
443 * Each individual layer can be rotated or shifted to correspond
444 * to internal FGFS instrument readings.
446 class FGLayeredInstrument : public FGPanelInstrument
449 FGLayeredInstrument (int x, int y, int w, int h);
450 virtual ~FGLayeredInstrument ();
452 virtual void draw ();
454 // Transfer pointer ownership!!
455 virtual int addLayer (FGInstrumentLayer *layer);
456 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
458 // Transfer pointer ownership!!
459 virtual void addTransformation (FGPanelTransformation * transformation);
462 typedef vector<FGInstrumentLayer *> layer_list;
468 ////////////////////////////////////////////////////////////////////////
469 // A textured layer of an instrument.
471 // This is a layer holding a single texture. Normally, the texture's
472 // backgound should be transparent so that lower layers and the panel
473 // background can show through.
474 ////////////////////////////////////////////////////////////////////////
476 class FGTexturedLayer : public FGInstrumentLayer
479 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
480 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
481 virtual ~FGTexturedLayer ();
483 virtual void draw ();
485 virtual void setTexture (const FGCroppedTexture &texture) {
488 virtual FGCroppedTexture &getTexture () { return _texture; }
489 virtual const FGCroppedTexture &getTexture () const { return _texture; }
492 mutable FGCroppedTexture _texture;
497 ////////////////////////////////////////////////////////////////////////
498 // A text layer of an instrument.
500 // This is a layer holding a string of static and/or generated text.
501 // It is useful for instruments that have text displays, such as
502 // a chronometer, GPS, or NavCom radio.
503 ////////////////////////////////////////////////////////////////////////
505 class FGTextLayer : public FGInstrumentLayer
508 typedef enum ChunkType {
516 Chunk (const string &text, const string &fmt = "%s");
517 Chunk (ChunkType type, const SGPropertyNode * node,
518 const string &fmt = "", float mult = 1.0);
520 const char * getValue () const;
524 const SGPropertyNode * _node;
527 mutable char _buf[1024];
530 FGTextLayer (int w = -1, int h = -1);
531 virtual ~FGTextLayer ();
533 virtual void draw ();
535 // Transfer pointer!!
536 virtual void addChunk (Chunk * chunk);
537 virtual void setColor (float r, float g, float b);
538 virtual void setPointSize (float size);
539 virtual void setFont (fntFont * font);
543 void recalc_value () const;
545 typedef vector<Chunk *> chunk_list;
551 mutable string _value;
552 mutable SGTimeStamp _then;
553 mutable SGTimeStamp _now;
558 ////////////////////////////////////////////////////////////////////////
559 // A layer that switches between two other layers.
560 ////////////////////////////////////////////////////////////////////////
562 class FGSwitchLayer : public FGInstrumentLayer
565 // Transfer pointers!!
566 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
567 FGInstrumentLayer * layer1,
568 FGInstrumentLayer * layer2);
569 virtual ~FGSwitchLayer ();
571 virtual void draw ();
574 const SGPropertyNode * _node;
575 FGInstrumentLayer * _layer1, * _layer2;
580 ////////////////////////////////////////////////////////////////////////
582 ////////////////////////////////////////////////////////////////////////
584 bool fgPanelVisible ();
588 ////////////////////////////////////////////////////////////////////////
589 // The current panel, if any.
590 ////////////////////////////////////////////////////////////////////////
592 extern FGPanel * current_panel;
596 #endif // __PANEL_HXX