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
128 FGPanel (int window_x, int window_y, int window_w, int window_h);
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;
178 int _winx, _winy, _winw, _winh;
187 // List of instruments in panel.
188 instrument_list_type _instruments;
193 ////////////////////////////////////////////////////////////////////////
194 // Base class for user action types.
196 // Individual instruments can have actions associated with a mouse
197 // click in a rectangular area. Current concrete classes include
198 // FGAdjustAction, FGSwapAction, and FGToggleAction.
199 ////////////////////////////////////////////////////////////////////////
205 FGPanelAction (int button, int x, int y, int w, int h);
206 virtual ~FGPanelAction ();
209 virtual int getButton () const { return _button; }
210 virtual int getX () const { return _x; }
211 virtual int getY () const { return _y; }
212 virtual int getWidth () const { return _w; }
213 virtual int getHeight () const { return _h; }
216 virtual void setButton (int button) { _button = button; }
217 virtual void setX (int x) { _x = x; }
218 virtual void setY (int y) { _y = y; }
219 virtual void setWidth (int w) { _w = w; }
220 virtual void setHeight (int h) { _h = h; }
222 // Check whether we're in the area.
223 virtual bool inArea (int button, int x, int y)
225 return (button == _button &&
232 // Perform the action.
233 virtual void doAction () = 0;
245 ////////////////////////////////////////////////////////////////////////
246 // Adjustment action.
248 // This is an action to increase or decrease an FGFS value by a certain
249 // increment within a certain range. If the wrap flag is true, the
250 // value will wrap around if it goes below min or above max; otherwise,
251 // it will simply stop at min or max.
252 ////////////////////////////////////////////////////////////////////////
254 class FGAdjustAction : public FGPanelAction
257 FGAdjustAction (int button, int x, int y, int w, int h,
258 SGPropertyNode * node, float increment,
259 float min, float max, bool wrap=false);
260 virtual ~FGAdjustAction ();
261 virtual void doAction ();
264 SGPropertyNode * _node;
273 ////////////////////////////////////////////////////////////////////////
276 // This is an action to swap two values. It's currently used in the
277 // navigation radios.
278 ////////////////////////////////////////////////////////////////////////
280 class FGSwapAction : public FGPanelAction
283 FGSwapAction (int button, int x, int y, int w, int h,
284 SGPropertyNode * node1, SGPropertyNode * node2);
285 virtual ~FGSwapAction ();
286 virtual void doAction ();
289 SGPropertyNode * _node1;
290 SGPropertyNode * _node2;
295 ////////////////////////////////////////////////////////////////////////
298 // This is an action to toggle a boolean value.
299 ////////////////////////////////////////////////////////////////////////
301 class FGToggleAction : public FGPanelAction
304 FGToggleAction (int button, int x, int y, int w, int h,
305 SGPropertyNode * node);
306 virtual ~FGToggleAction ();
307 virtual void doAction ();
310 SGPropertyNode * _node;
315 ////////////////////////////////////////////////////////////////////////
316 // Abstract base class for a panel instrument.
318 // A panel instrument consists of zero or more actions, associated
319 // with mouse clicks in rectangular areas. Currently, the only
320 // concrete class derived from this is FGLayeredInstrument, but others
321 // may show up in the future (some complex instruments could be
322 // entirely hand-coded, for example).
323 ////////////////////////////////////////////////////////////////////////
325 class FGPanelInstrument
328 FGPanelInstrument ();
329 FGPanelInstrument (int x, int y, int w, int h);
330 virtual ~FGPanelInstrument ();
332 virtual void draw () = 0;
334 virtual void setPosition(int x, int y);
335 virtual void setSize(int w, int h);
337 virtual int getXPos () const;
338 virtual int getYPos () const;
339 virtual int getWidth () const;
340 virtual int getHeight () const;
342 // Coordinates relative to centre.
343 // Transfer pointer ownership!!
344 virtual void addAction (FGPanelAction * action);
346 // Coordinates relative to centre.
347 virtual bool doMouseAction (int button, int x, int y);
351 typedef vector<FGPanelAction *> action_list_type;
352 action_list_type _actions;
357 ////////////////////////////////////////////////////////////////////////
358 // Abstract base class for an instrument layer.
360 // The FGLayeredInstrument class builds up instruments by using layers
361 // of textures or text. Each layer can have zero or more
362 // transformations applied to it: for example, a needle layer can
363 // rotate to show the altitude or airspeed.
364 ////////////////////////////////////////////////////////////////////////
368 * A transformation for a layer.
370 class FGPanelTransformation {
379 FGPanelTransformation ();
380 virtual ~FGPanelTransformation ();
383 const SGPropertyNode * node;
388 SGInterpTable * table;
394 * A single layer of a multi-layered instrument.
396 * Each layer can be subject to a series of transformations based
397 * on current FGFS instrument readings: for example, a texture
398 * representing a needle can rotate to show the airspeed.
400 class FGInstrumentLayer
404 FGInstrumentLayer (int w = -1, int h = -1);
405 virtual ~FGInstrumentLayer ();
407 virtual void draw () = 0;
408 virtual void transform () const;
410 virtual int getWidth () const { return _w; }
411 virtual int getHeight () const { return _h; }
412 virtual void setWidth (int w) { _w = w; }
413 virtual void setHeight (int h) { _h = h; }
415 // Transfer pointer ownership!!
417 virtual void addTransformation (FGPanelTransformation * transformation);
422 typedef vector<FGPanelTransformation *> transformation_list;
423 transformation_list _transformations;
428 ////////////////////////////////////////////////////////////////////////
429 // An instrument composed of layers.
431 // This class represents an instrument which is simply a series of
432 // layers piled one on top of the other, each one undergoing its own
433 // set of transformations. For example, one layer can represent
434 // the instrument's face (which doesn't move), while the next layer
435 // can represent a needle that rotates depending on an FGFS variable.
436 ////////////////////////////////////////////////////////////////////////
440 * An instrument constructed of multiple layers.
442 * Each individual layer can be rotated or shifted to correspond
443 * to internal FGFS instrument readings.
445 class FGLayeredInstrument : public FGPanelInstrument
448 FGLayeredInstrument (int x, int y, int w, int h);
449 virtual ~FGLayeredInstrument ();
451 virtual void draw ();
453 // Transfer pointer ownership!!
454 virtual int addLayer (FGInstrumentLayer *layer);
455 virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
457 // Transfer pointer ownership!!
458 virtual void addTransformation (FGPanelTransformation * transformation);
461 typedef vector<FGInstrumentLayer *> layer_list;
467 ////////////////////////////////////////////////////////////////////////
468 // A textured layer of an instrument.
470 // This is a layer holding a single texture. Normally, the texture's
471 // backgound should be transparent so that lower layers and the panel
472 // background can show through.
473 ////////////////////////////////////////////////////////////////////////
475 class FGTexturedLayer : public FGInstrumentLayer
478 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
479 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
480 virtual ~FGTexturedLayer ();
482 virtual void draw ();
484 virtual void setTexture (const FGCroppedTexture &texture) {
487 virtual FGCroppedTexture &getTexture () { return _texture; }
488 virtual const FGCroppedTexture &getTexture () const { return _texture; }
491 mutable FGCroppedTexture _texture;
496 ////////////////////////////////////////////////////////////////////////
497 // A text layer of an instrument.
499 // This is a layer holding a string of static and/or generated text.
500 // It is useful for instruments that have text displays, such as
501 // a chronometer, GPS, or NavCom radio.
502 ////////////////////////////////////////////////////////////////////////
504 class FGTextLayer : public FGInstrumentLayer
507 typedef enum ChunkType {
515 Chunk (const string &text, const string &fmt = "%s");
516 Chunk (ChunkType type, const SGPropertyNode * node,
517 const string &fmt = "", float mult = 1.0);
519 const char * getValue () const;
523 const SGPropertyNode * _node;
526 mutable char _buf[1024];
529 FGTextLayer (int w = -1, int h = -1);
530 virtual ~FGTextLayer ();
532 virtual void draw ();
534 // Transfer pointer!!
535 virtual void addChunk (Chunk * chunk);
536 virtual void setColor (float r, float g, float b);
537 virtual void setPointSize (float size);
538 virtual void setFont (fntFont * font);
542 void recalc_value () const;
544 typedef vector<Chunk *> chunk_list;
550 mutable string _value;
551 mutable SGTimeStamp _then;
552 mutable SGTimeStamp _now;
557 ////////////////////////////////////////////////////////////////////////
558 // A layer that switches between two other layers.
559 ////////////////////////////////////////////////////////////////////////
561 class FGSwitchLayer : public FGInstrumentLayer
564 // Transfer pointers!!
565 FGSwitchLayer (int w, int h, const SGPropertyNode * node,
566 FGInstrumentLayer * layer1,
567 FGInstrumentLayer * layer2);
568 virtual ~FGSwitchLayer ();
570 virtual void draw ();
573 const SGPropertyNode * _node;
574 FGInstrumentLayer * _layer1, * _layer2;
579 ////////////////////////////////////////////////////////////////////////
581 ////////////////////////////////////////////////////////////////////////
583 bool fgPanelVisible ();
587 ////////////////////////////////////////////////////////////////////////
588 // The current panel, if any.
589 ////////////////////////////////////////////////////////////////////////
591 extern FGPanel * current_panel;
595 #endif // __PANEL_HXX