1 // panel.hxx - default, 2D single-engine prop instrument 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++
40 #include <simgear/misc/props.hxx>
49 class FGPanelInstrument;
53 ////////////////////////////////////////////////////////////////////////
54 // Texture manager (should migrate out into FGFS).
56 // This class ensures that no texture is loaded more than once.
57 ////////////////////////////////////////////////////////////////////////
59 class FGTextureManager
62 static ssgTexture * createTexture(const string &relativePath);
64 static map<string,ssgTexture *>_textureMap;
69 ////////////////////////////////////////////////////////////////////////
70 // Cropped texture (should migrate out into FGFS).
72 // This class defines a rectangular cropped area of a texture.
73 ////////////////////////////////////////////////////////////////////////
78 CroppedTexture (const string &path,
79 float _minX = 0.0, float _minY = 0.0,
80 float _maxX = 1.0, float _maxY = 1.0)
81 : texture(FGTextureManager::createTexture(path)),
82 minX(_minX), minY(_minY), maxX(_maxX), maxY(_maxY) {}
85 float minX, minY, maxX, maxY;
90 ////////////////////////////////////////////////////////////////////////
91 // Instrument panel class.
93 // The panel is a container that has a background texture and holds
94 // zero or more instruments. The panel will order the instruments to
95 // redraw themselves when necessary, and will pass mouse clicks on to
96 // the appropriate instruments for processing.
97 ////////////////////////////////////////////////////////////////////////
103 FGPanel (int x, int y, int w, int h);
106 // transfer pointer ownership!!!
107 virtual void addInstrument (FGPanelInstrument * instrument);
109 // Update the panel (every frame).
110 virtual void update () const;
112 // Background texture.
113 virtual void setBackground (ssgTexture * texture);
115 // Make the panel visible or invisible.
116 virtual bool getVisibility () const;
117 virtual void setVisibility (bool visibility);
119 // Handle a mouse click.
120 virtual bool doMouseAction (int button, int updown, int x, int y);
123 mutable bool _visibility;
124 mutable bool _mouseDown;
125 mutable int _mouseButton, _mouseX, _mouseY;
126 mutable int _mouseDelay;
127 mutable FGPanelInstrument * _mouseInstrument;
128 typedef vector<FGPanelInstrument *> instrument_list_type;
132 // List of instruments in panel.
133 instrument_list_type _instruments;
138 ////////////////////////////////////////////////////////////////////////
139 // Base class for user action types.
141 // Individual instruments can have actions associated with a mouse
142 // click in a rectangular area. Current concrete classes include
143 // FGAdjustAction, FGSwapAction, and FGToggleAction.
144 ////////////////////////////////////////////////////////////////////////
150 FGPanelAction (int button, int x, int y, int w, int h);
151 virtual ~FGPanelAction ();
153 virtual int getButton () const { return _button; }
154 virtual int getX () const { return _x; }
155 virtual int getY () const { return _y; }
156 virtual int getWidth () const { return _w; }
157 virtual int getHeight () const { return _h; }
159 virtual void setButton (int button) { _button = button; }
160 virtual void setX (int x) { _x = x; }
161 virtual void setY (int y) { _y = y; }
162 virtual void setWidth (int w) { _w = w; }
163 virtual void setHeight (int h) { _h = h; }
165 virtual bool inArea (int button, int x, int y)
167 return (button == _button &&
174 virtual void doAction () = 0;
186 ////////////////////////////////////////////////////////////////////////
187 // Adjustment action.
189 // This is an action to increase or decrease an FGFS value by a certain
190 // increment within a certain range. If the wrap flag is true, the
191 // value will wrap around if it goes below min or above max; otherwise,
192 // it will simply stop at min or max.
193 ////////////////////////////////////////////////////////////////////////
195 class FGAdjustAction : public FGPanelAction
198 FGAdjustAction (int button, int x, int y, int w, int h,
199 SGValue * value, float increment,
200 float min, float max, bool wrap=false);
201 virtual ~FGAdjustAction ();
202 virtual void doAction ();
214 ////////////////////////////////////////////////////////////////////////
217 // This is an action to swap two values. It's currently used in the
218 // navigation radios.
219 ////////////////////////////////////////////////////////////////////////
221 class FGSwapAction : public FGPanelAction
224 FGSwapAction (int button, int x, int y, int w, int h,
225 SGValue * value1, SGValue * value2);
226 virtual ~FGSwapAction ();
227 virtual void doAction ();
236 ////////////////////////////////////////////////////////////////////////
239 // This is an action to toggle a boolean value.
240 ////////////////////////////////////////////////////////////////////////
242 class FGToggleAction : public FGPanelAction
245 FGToggleAction (int button, int x, int y, int w, int h,
247 virtual ~FGToggleAction ();
248 virtual void doAction ();
256 ////////////////////////////////////////////////////////////////////////
257 // Abstract base class for a panel instrument.
259 // A panel instrument consists of zero or more actions, associated
260 // with mouse clicks in rectangular areas. Currently, the only
261 // concrete class derived from this is FGLayeredInstrument, but others
262 // may show up in the future (some complex instruments could be
263 // entirely hand-coded, for example).
264 ////////////////////////////////////////////////////////////////////////
266 class FGPanelInstrument
269 FGPanelInstrument ();
270 FGPanelInstrument (int x, int y, int w, int h);
271 virtual ~FGPanelInstrument ();
273 virtual void draw () = 0;
275 virtual void setPosition(int x, int y);
276 virtual void setSize(int w, int h);
278 virtual int getXPos () const;
279 virtual int getYPos () const;
280 virtual int getWidth () const;
281 virtual int getHeight () const;
283 // Coordinates relative to centre.
284 // Transfer pointer ownership!!
285 virtual void addAction (FGPanelAction * action);
287 // Coordinates relative to centre.
288 virtual bool doMouseAction (int button, int x, int y);
292 typedef vector<FGPanelAction *> action_list_type;
293 action_list_type _actions;
298 ////////////////////////////////////////////////////////////////////////
299 // Abstract base class for an instrument layer.
301 // The FGLayeredInstrument class builds up instruments by using layers
302 // of textures or text. Each layer can have zero or more
303 // transformations applied to it: for example, a needle layer can
304 // rotate to show the altitude or airspeed.
305 ////////////////////////////////////////////////////////////////////////
309 * A transformation for a layer.
311 class FGPanelTransformation {
320 FGPanelTransformation ();
321 FGPanelTransformation (Type type, const SGValue * value,
322 float min, float max,
323 float factor, float offset);
324 virtual ~FGPanelTransformation ();
327 const SGValue * value;
337 * A single layer of a multi-layered instrument.
339 * Each layer can be subject to a series of transformations based
340 * on current FGFS instrument readings: for example, a texture
341 * representing a needle can rotate to show the airspeed.
343 class FGInstrumentLayer
347 FGInstrumentLayer (int w = -1, int h = -1);
348 virtual ~FGInstrumentLayer ();
350 virtual void draw () = 0;
351 virtual void transform () const;
353 virtual int getWidth () const { return _w; }
354 virtual int getHeight () const { return _h; }
355 virtual void setWidth (int w) { _w = w; }
356 virtual void setHeight (int h) { _h = h; }
358 // Transfer pointer ownership!!
360 virtual void addTransformation (FGPanelTransformation * transformation);
365 typedef vector<FGPanelTransformation *> transformation_list;
366 transformation_list _transformations;
371 ////////////////////////////////////////////////////////////////////////
372 // An instrument composed of layers.
374 // This class represents an instrument which is simply a series of
375 // layers piled one on top of the other, each one undergoing its own
376 // set of transformations. For example, one layer can represent
377 // the instrument's face (which doesn't move), while the next layer
378 // can represent a needle that rotates depending on an FGFS variable.
379 ////////////////////////////////////////////////////////////////////////
383 * An instrument constructed of multiple layers.
385 * Each individual layer can be rotated or shifted to correspond
386 * to internal FGFS instrument readings.
388 class FGLayeredInstrument : public FGPanelInstrument
391 typedef vector<FGInstrumentLayer *> layer_list;
392 FGLayeredInstrument (int x, int y, int w, int h);
393 virtual ~FGLayeredInstrument ();
395 virtual void draw ();
397 // Transfer pointer ownership!!
398 virtual int addLayer (FGInstrumentLayer *layer);
399 virtual int addLayer (CroppedTexture &texture, int w = -1, int h = -1);
401 // Transfer pointer ownership!!
402 virtual void addTransformation (FGPanelTransformation * transformation);
410 ////////////////////////////////////////////////////////////////////////
411 // A textured layer of an instrument.
413 // This is a layer holding a single texture. Normally, the texture's
414 // backgound should be transparent so that lower layers and the panel
415 // background can show through.
416 ////////////////////////////////////////////////////////////////////////
418 class FGTexturedLayer : public FGInstrumentLayer
421 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
422 FGTexturedLayer (CroppedTexture &texture, int w = -1, int h = -1);
423 virtual ~FGTexturedLayer ();
425 virtual void draw ();
427 virtual void setTexture (CroppedTexture &texture) { _texture = texture; }
428 virtual CroppedTexture &getTexture () { return _texture; }
431 mutable CroppedTexture _texture;
436 ////////////////////////////////////////////////////////////////////////
437 // A text layer of an instrument.
439 // This is a layer holding a string of static and/or generated text.
440 // It is useful for instruments that have text displays, such as
441 // a chronometer, GPS, or NavCom radio.
442 ////////////////////////////////////////////////////////////////////////
444 class FGTextLayer : public FGInstrumentLayer
447 typedef enum ChunkType {
455 Chunk (char * text, char * fmt = "%s");
456 Chunk (ChunkType type, const SGValue * value,
457 char * fmt = 0, float mult = 1.0);
459 char * getValue () const;
464 const SGValue * _value;
468 mutable char _buf[1024];
471 FGTextLayer (int w = -1, int h = -1, Chunk * chunk1 = 0, Chunk * chunk2 = 0,
473 virtual ~FGTextLayer ();
475 virtual void draw ();
477 // Transfer pointer!!
478 virtual void addChunk (Chunk * chunk);
479 virtual void setColor (float r, float g, float b);
480 virtual void setPointSize (float size);
481 virtual void setFont (fntFont * font);
484 typedef vector<Chunk *> chunk_list;
487 // FIXME: need only one globally
488 mutable fntRenderer _renderer;
493 ////////////////////////////////////////////////////////////////////////
494 // A layer that switches between two other layers.
495 ////////////////////////////////////////////////////////////////////////
497 class FGSwitchLayer : public FGInstrumentLayer
500 // Transfer pointers!!
501 FGSwitchLayer (int w, int h, const SGValue * value,
502 FGInstrumentLayer * layer1,
503 FGInstrumentLayer * layer2);
504 virtual ~FGSwitchLayer ();
506 virtual void draw ();
509 const SGValue * _value;
510 FGInstrumentLayer * _layer1, * _layer2;
515 ////////////////////////////////////////////////////////////////////////
516 // The current panel, if any.
517 ////////////////////////////////////////////////////////////////////////
519 extern FGPanel * current_panel;
523 #endif // __PANEL_HXX