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++
47 class FGPanelInstrument;
51 ////////////////////////////////////////////////////////////////////////
52 // Texture manager (should migrate out into FGFS).
54 // This class ensures that no texture is loaded more than once.
55 ////////////////////////////////////////////////////////////////////////
57 class FGTextureManager
60 static ssgTexture * createTexture(const char * relativePath);
62 static map<const char *,ssgTexture *>_textureMap;
66 ////////////////////////////////////////////////////////////////////////
67 // Instrument panel class.
69 // The panel is a container that has a background texture and holds
70 // zero or more instruments. The panel will order the instruments to
71 // redraw themselves when necessary, and will pass mouse clicks on to
72 // the appropriate instruments for processing.
73 ////////////////////////////////////////////////////////////////////////
79 FGPanel (int x, int y, int w, int h);
82 // transfer pointer ownership!!!
83 virtual void addInstrument (FGPanelInstrument * instrument);
85 // Update the panel (every frame).
86 virtual void update () const;
88 // Background texture.
89 virtual void setBackground (ssgTexture * texture);
91 // Make the panel visible or invisible.
92 virtual bool getVisibility () const;
93 virtual void setVisibility (bool visibility);
95 // Handle a mouse click.
96 virtual bool doMouseAction (int button, int updown, int x, int y);
101 int _mouseButton, _mouseX, _mouseY;
102 mutable int _mouseDelay;
103 FGPanelInstrument * _mouseInstrument;
104 typedef vector<FGPanelInstrument *> instrument_list_type;
108 // List of instruments in panel.
109 instrument_list_type _instruments;
114 ////////////////////////////////////////////////////////////////////////
115 // Base class for user action types.
117 // Individual instruments can have actions associated with a mouse
118 // click in a rectangular area. Current concrete classes are
119 // FGAdjustAction and FGSwapAction.
120 ////////////////////////////////////////////////////////////////////////
125 virtual void doAction () = 0;
130 ////////////////////////////////////////////////////////////////////////
131 // Adjustment action.
133 // This is an action to increase or decrease an FGFS value by a certain
134 // increment within a certain range. If the wrap flag is true, the
135 // value will wrap around if it goes below min or above max; otherwise,
136 // it will simply stop at min or max.
137 ////////////////////////////////////////////////////////////////////////
139 class FGAdjustAction : public FGPanelAction
142 typedef double (*getter_type)();
143 typedef void (*setter_type)(double);
145 FGAdjustAction (getter_type getter, setter_type setter, float increment,
146 float min, float max, bool wrap=false);
147 virtual ~FGAdjustAction ();
148 virtual void doAction ();
161 ////////////////////////////////////////////////////////////////////////
164 // This is an action to swap two values. It's currently used in the
165 // navigation radios.
166 ////////////////////////////////////////////////////////////////////////
168 class FGSwapAction : public FGPanelAction
171 typedef double (*getter_type)();
172 typedef void (*setter_type)(double);
174 FGSwapAction (getter_type getter1, setter_type setter1,
175 getter_type getter2, setter_type setter2);
176 virtual ~FGSwapAction ();
177 virtual void doAction ();
180 getter_type _getter1, _getter2;
181 setter_type _setter1, _setter2;
186 ////////////////////////////////////////////////////////////////////////
189 // This is an action to toggle a boolean value.
190 ////////////////////////////////////////////////////////////////////////
192 class FGToggleAction : public FGPanelAction
195 typedef bool (*getter_type)();
196 typedef void (*setter_type)(bool);
198 FGToggleAction (getter_type getter, setter_type setter);
199 virtual ~FGToggleAction ();
200 virtual void doAction ();
209 ////////////////////////////////////////////////////////////////////////
210 // Abstract base class for a panel instrument.
212 // A panel instrument consists of zero or more actions, associated
213 // with mouse clicks in rectangular areas. Currently, the only
214 // concrete class derived from this is FGLayeredInstrument, but others
215 // may show up in the future (some complex instruments could be
216 // entirely hand-coded, for example).
217 ////////////////////////////////////////////////////////////////////////
219 class FGPanelInstrument
222 FGPanelInstrument ();
223 FGPanelInstrument (int x, int y, int w, int h);
224 virtual ~FGPanelInstrument ();
226 virtual void draw () const = 0;
228 virtual void setPosition(int x, int y);
229 virtual void setSize(int w, int h);
231 virtual int getXPos () const;
232 virtual int getYPos () const;
233 virtual int getWidth () const;
234 virtual int getHeight () const;
236 // Coordinates relative to centre.
237 // Transfer pointer ownership!!
238 virtual void addAction (int button, int x, int y, int w, int h,
239 FGPanelAction * action);
241 // Coordinates relative to centre.
242 virtual bool doMouseAction (int button, int x, int y);
252 FGPanelAction * action;
254 typedef vector<inst_action> action_list_type;
255 action_list_type _actions;
260 ////////////////////////////////////////////////////////////////////////
261 // Abstract base class for an instrument layer.
263 // The FGLayeredInstrument class builds up instruments by using layers
264 // of textures or text. Each layer can have zero or more
265 // transformations applied to it: for example, a needle layer can
266 // rotate to show the altitude or airspeed.
267 ////////////////////////////////////////////////////////////////////////
270 * A single layer of a multi-layered instrument.
272 * Each layer can be subject to a series of transformations based
273 * on current FGFS instrument readings: for example, a texture
274 * representing a needle can rotate to show the airspeed.
276 class FGInstrumentLayer
285 typedef double (*transform_func)();
288 FGInstrumentLayer ();
289 FGInstrumentLayer (int w, int h);
290 virtual ~FGInstrumentLayer ();
292 virtual void draw () const = 0;
293 virtual void transform () const;
295 virtual void addTransformation (transform_type type, transform_func func,
296 float min, float max,
297 float factor = 1.0, float offset = 0.0);
310 typedef vector<transformation *> transformation_list;
311 transformation_list _transformations;
316 ////////////////////////////////////////////////////////////////////////
317 // An instrument composed of layers.
319 // This class represents an instrument which is simply a series of
320 // layers piled one on top of the other, each one undergoing its own
321 // set of transformations. For example, one layer can represent
322 // the instrument's face (which doesn't move), while the next layer
323 // can represent a needle that rotates depending on an FGFS variable.
324 ////////////////////////////////////////////////////////////////////////
328 * An instrument constructed of multiple layers.
330 * Each individual layer can be rotated or shifted to correspond
331 * to internal FGFS instrument readings.
333 class FGLayeredInstrument : public FGPanelInstrument
336 typedef vector<FGInstrumentLayer *> layer_list;
337 FGLayeredInstrument (int x, int y, int w, int h);
338 virtual ~FGLayeredInstrument ();
340 virtual void draw () const;
342 // Transfer pointer ownership!!
343 virtual int addLayer (FGInstrumentLayer *layer);
344 virtual int addLayer (ssgTexture * texture,
345 int w = -1, int h = -1,
346 float texX1 = 0.0, float texY1 = 0.0,
347 float texX2 = 1.0, float texY2 = 1.0);
348 virtual void addTransformation (FGInstrumentLayer::transform_type type,
349 FGInstrumentLayer::transform_func func,
350 float min, float max,
351 float factor = 1.0, float offset = 0.0);
352 virtual void addTransformation (FGInstrumentLayer::transform_type type,
361 ////////////////////////////////////////////////////////////////////////
362 // A textured layer of an instrument.
364 // This is a layer holding a single texture. Normally, the texture's
365 // backgound should be transparent so that lower layers and the panel
366 // background can show through.
367 ////////////////////////////////////////////////////////////////////////
369 class FGTexturedLayer : public FGInstrumentLayer
372 FGTexturedLayer (ssgTexture * texture, int w, int h,
373 float texX1 = 0.0, float texY1 = 0.0,
374 float texX2 = 1.0, float texY2 = 1.0);
375 virtual ~FGTexturedLayer ();
377 virtual void draw () const;
379 virtual void setTexture (ssgTexture * texture) { _texture = texture; }
380 virtual void setTextureCoords (float x1, float y1, float x2, float y2) {
381 _texX1 = x1; _texY1 = y1; _texX2 = x2; _texY2 = y2;
386 virtual void setTextureCoords (float x1, float y1,
387 float x2, float y2) const {
388 _texX1 = x1; _texY1 = y1; _texX2 = x2; _texY2 = y2;
393 ssgTexture * _texture;
394 mutable float _texX1, _texY1, _texX2, _texY2;
399 ////////////////////////////////////////////////////////////////////////
400 // A text layer of an instrument.
402 // This is a layer holding a string of static and/or generated text.
403 // It is useful for instruments that have text displays, such as
404 // a chronometer, GPS, or NavCom radio.
405 ////////////////////////////////////////////////////////////////////////
407 class FGTextLayer : public FGInstrumentLayer
410 typedef char * (*text_func)();
411 typedef double (*double_func)();
412 typedef enum ChunkType {
420 Chunk (char * text, char * fmt = "%s");
421 Chunk (text_func func, char * fmt = "%s");
422 Chunk (double_func func, char * fmt = "%.2f", float mult = 1.0);
424 char * getValue () const;
434 mutable char _buf[1024];
437 FGTextLayer (int w, int h);
438 virtual ~FGTextLayer ();
440 virtual void draw () const;
442 // Transfer pointer!!
443 virtual void addChunk (Chunk * chunk);
444 virtual void setColor (float r, float g, float b);
445 virtual void setPointSize (float size);
446 virtual void setFont (fntFont * font);
449 typedef vector<Chunk *> chunk_list;
452 // FIXME: need only one globally
453 mutable fntRenderer _renderer;
458 ////////////////////////////////////////////////////////////////////////
459 // A layer that switches between two other layers.
460 ////////////////////////////////////////////////////////////////////////
462 class FGSwitchLayer : public FGInstrumentLayer
465 typedef bool (*switch_func)();
467 // Transfer pointers!!
468 FGSwitchLayer (int w, int h, switch_func func,
469 FGInstrumentLayer * layer1,
470 FGInstrumentLayer * layer2);
471 virtual ~FGSwitchLayer ();
473 virtual void draw () const;
477 FGInstrumentLayer * _layer1, * _layer2;
482 ////////////////////////////////////////////////////////////////////////
483 // The current panel, if any.
484 ////////////////////////////////////////////////////////////////////////
486 extern FGPanel * current_panel;
490 #endif // __PANEL_HXX