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 ();
136 virtual void update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh);
138 // transfer pointer ownership!!!
139 virtual void addInstrument (FGPanelInstrument * instrument);
141 // Background texture.
142 virtual void setBackground (ssgTexture * texture);
144 // Make the panel visible or invisible.
145 virtual bool getVisibility () const;
146 virtual void setVisibility (bool visibility);
148 // Full width of panel.
149 virtual void setWidth (int width) { _width = width; }
150 virtual int getWidth () const { return _width; }
152 // Full height of panel.
153 virtual void setHeight (int height) { _height = height; }
154 virtual int getHeight () const { return _height; }
157 virtual void setXOffset (int offset);
158 virtual int getXOffset () const { return _x_offset; }
161 virtual void setYOffset (int offset);
162 virtual int getYOffset () const { return _y_offset; }
165 virtual void setViewHeight (int height) { _view_height = height; }
166 virtual int getViewHeight () const { return _view_height; }
168 // Handle a mouse click.
169 virtual bool doMouseAction (int button, int updown, int x, int y);
172 mutable bool _visibility;
173 mutable bool _mouseDown;
174 mutable int _mouseButton, _mouseX, _mouseY;
175 mutable int _mouseDelay;
176 mutable FGPanelInstrument * _mouseInstrument;
177 typedef vector<FGPanelInstrument *> instrument_list_type;
185 const SGPropertyNode * _xsize_node;
186 const SGPropertyNode * _ysize_node;
189 // List of instruments in panel.
190 instrument_list_type _instruments;
195 ////////////////////////////////////////////////////////////////////////
196 // Base class for user action types.
198 // Individual instruments can have actions associated with a mouse
199 // click in a rectangular area. Current concrete classes include
200 // FGAdjustAction, FGSwapAction, and FGToggleAction.
201 ////////////////////////////////////////////////////////////////////////
207 FGPanelAction (int button, int x, int y, int w, int h);
208 virtual ~FGPanelAction ();
211 virtual int getButton () const { return _button; }
212 virtual int getX () const { return _x; }
213 virtual int getY () const { return _y; }
214 virtual int getWidth () const { return _w; }
215 virtual int getHeight () const { return _h; }
218 virtual void setButton (int button) { _button = button; }
219 virtual void setX (int x) { _x = x; }
220 virtual void setY (int y) { _y = y; }
221 virtual void setWidth (int w) { _w = w; }
222 virtual void setHeight (int h) { _h = h; }
224 // Check whether we're in the area.
225 virtual bool inArea (int button, int x, int y)
227 return (button == _button &&
234 // Perform the action.
235 virtual void doAction () = 0;
247 ////////////////////////////////////////////////////////////////////////
248 // Adjustment action.
250 // This is an action to increase or decrease an FGFS value by a certain
251 // increment within a certain range. If the wrap flag is true, the
252 // value will wrap around if it goes below min or above max; otherwise,
253 // it will simply stop at min or max.
254 ////////////////////////////////////////////////////////////////////////
256 class FGAdjustAction : public FGPanelAction
259 FGAdjustAction (int button, int x, int y, int w, int h,
260 SGPropertyNode * node, float increment,
261 float min, float max, bool wrap=false);
262 virtual ~FGAdjustAction ();
263 virtual void doAction ();
266 SGPropertyNode * _node;
275 ////////////////////////////////////////////////////////////////////////
278 // This is an action to swap two values. It's currently used in the
279 // navigation radios.
280 ////////////////////////////////////////////////////////////////////////
282 class FGSwapAction : public FGPanelAction
285 FGSwapAction (int button, int x, int y, int w, int h,
286 SGPropertyNode * node1, SGPropertyNode * node2);
287 virtual ~FGSwapAction ();
288 virtual void doAction ();
291 SGPropertyNode * _node1;
292 SGPropertyNode * _node2;
297 ////////////////////////////////////////////////////////////////////////
300 // This is an action to toggle a boolean value.
301 ////////////////////////////////////////////////////////////////////////
303 class FGToggleAction : public FGPanelAction
306 FGToggleAction (int button, int x, int y, int w, int h,
307 SGPropertyNode * node);
308 virtual ~FGToggleAction ();
309 virtual void doAction ();
312 SGPropertyNode * _node;
317 ////////////////////////////////////////////////////////////////////////
318 // Abstract base class for a panel instrument.
320 // A panel instrument consists of zero or more actions, associated
321 // with mouse clicks in rectangular areas. Currently, the only
322 // concrete class derived from this is FGLayeredInstrument, but others
323 // may show up in the future (some complex instruments could be
324 // entirely hand-coded, for example).
325 ////////////////////////////////////////////////////////////////////////
327 class FGPanelInstrument
330 FGPanelInstrument ();
331 FGPanelInstrument (int x, int y, int w, int h);
332 virtual ~FGPanelInstrument ();
334 virtual void draw () = 0;
336 virtual void setPosition(int x, int y);
337 virtual void setSize(int w, int h);
339 virtual int getXPos () const;
340 virtual int getYPos () const;
341 virtual int getWidth () const;
342 virtual int getHeight () const;
344 // Coordinates relative to centre.
345 // Transfer pointer ownership!!
346 virtual void addAction (FGPanelAction * action);
348 // Coordinates relative to centre.
349 virtual bool doMouseAction (int button, int x, int y);
353 typedef vector<FGPanelAction *> action_list_type;
354 action_list_type _actions;
359 ////////////////////////////////////////////////////////////////////////
360 // Abstract base class for an instrument layer.
362 // The FGLayeredInstrument class builds up instruments by using layers
363 // of textures or text. Each layer can have zero or more
364 // transformations applied to it: for example, a needle layer can
365 // rotate to show the altitude or airspeed.
366 ////////////////////////////////////////////////////////////////////////
370 * A transformation for a layer.
372 class FGPanelTransformation {
381 FGPanelTransformation ();
382 virtual ~FGPanelTransformation ();
385 const SGPropertyNode * node;
390 SGInterpTable * table;
396 * A single layer of a multi-layered instrument.
398 * Each layer can be subject to a series of transformations based
399 * on current FGFS instrument readings: for example, a texture
400 * representing a needle can rotate to show the airspeed.
402 class FGInstrumentLayer
406 FGInstrumentLayer (int w = -1, int h = -1);
407 virtual ~FGInstrumentLayer ();
409 virtual void draw () = 0;
410 virtual void transform () const;
412 virtual int getWidth () const { return _w; }
413 virtual int getHeight () const { return _h; }
414 virtual void setWidth (int w) { _w = w; }
415 virtual void setHeight (int h) { _h = h; }
417 // Transfer pointer ownership!!
419 virtual void addTransformation (FGPanelTransformation * transformation);
424 typedef vector<FGPanelTransformation *> transformation_list;
425 transformation_list _transformations;
430 ////////////////////////////////////////////////////////////////////////
431 // An instrument composed of layers.
433 // This class represents an instrument which is simply a series of
434 // layers piled one on top of the other, each one undergoing its own
435 // set of transformations. For example, one layer can represent
436 // the instrument's face (which doesn't move), while the next layer
437 // can represent a needle that rotates depending on an FGFS variable.
438 ////////////////////////////////////////////////////////////////////////
442 * An instrument constructed of multiple layers.
444 * Each individual layer can be rotated or shifted to correspond
445 * to internal FGFS instrument readings.
447 class FGLayeredInstrument : public FGPanelInstrument
450 FGLayeredInstrument (int x, int y, int w, int h);
451 virtual ~FGLayeredInstrument ();
453 virtual void draw ();
455 // Transfer pointer ownership!!
456 virtual int addLayer (FGInstrumentLayer *layer);
457 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
459 // Transfer pointer ownership!!
460 virtual void addTransformation (FGPanelTransformation * transformation);
463 typedef vector<FGInstrumentLayer *> layer_list;
469 ////////////////////////////////////////////////////////////////////////
470 // A textured layer of an instrument.
472 // This is a layer holding a single texture. Normally, the texture's
473 // backgound should be transparent so that lower layers and the panel
474 // background can show through.
475 ////////////////////////////////////////////////////////////////////////
477 class FGTexturedLayer : public FGInstrumentLayer
480 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
481 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
482 virtual ~FGTexturedLayer ();
484 virtual void draw ();
486 virtual void setTexture (const FGCroppedTexture &texture) {
489 virtual FGCroppedTexture &getTexture () { return _texture; }
490 virtual const FGCroppedTexture &getTexture () const { return _texture; }
493 mutable FGCroppedTexture _texture;
498 ////////////////////////////////////////////////////////////////////////
499 // A text layer of an instrument.
501 // This is a layer holding a string of static and/or generated text.
502 // It is useful for instruments that have text displays, such as
503 // a chronometer, GPS, or NavCom radio.
504 ////////////////////////////////////////////////////////////////////////
506 class FGTextLayer : public FGInstrumentLayer
509 typedef enum ChunkType {
517 Chunk (const string &text, const string &fmt = "%s");
518 Chunk (ChunkType type, const SGPropertyNode * node,
519 const string &fmt = "", float mult = 1.0);
521 const char * getValue () const;
525 const SGPropertyNode * _node;
528 mutable char _buf[1024];
531 FGTextLayer (int w = -1, int h = -1);
532 virtual ~FGTextLayer ();
534 virtual void draw ();
536 // Transfer pointer!!
537 virtual void addChunk (Chunk * chunk);
538 virtual void setColor (float r, float g, float b);
539 virtual void setPointSize (float size);
540 virtual void setFont (fntFont * font);
544 void recalc_value () const;
546 typedef vector<Chunk *> chunk_list;
552 mutable string _value;
553 mutable SGTimeStamp _then;
554 mutable SGTimeStamp _now;
559 ////////////////////////////////////////////////////////////////////////
560 // A layer that switches between two other layers.
561 ////////////////////////////////////////////////////////////////////////
563 class FGSwitchLayer : public FGInstrumentLayer
566 // Transfer pointers!!
567 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
568 FGInstrumentLayer * layer1,
569 FGInstrumentLayer * layer2);
570 virtual ~FGSwitchLayer ();
572 virtual void draw ();
575 const SGPropertyNode * _node;
576 FGInstrumentLayer * _layer1, * _layer2;
581 ////////////////////////////////////////////////////////////////////////
583 ////////////////////////////////////////////////////////////////////////
585 bool fgPanelVisible ();
589 ////////////////////////////////////////////////////////////////////////
590 // The current panel, if any.
591 ////////////////////////////////////////////////////////////////////////
593 extern FGPanel * current_panel;
597 #endif // __PANEL_HXX