]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.hxx
Tweaks for Mips Irix compilers.
[flightgear.git] / src / Cockpit / panel.hxx
1 //  panel.hxx - generic support classes for a 2D panel.
2 //
3 //  Written by David Megginson, started January 2000.
4 //
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.
9 // 
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.
14 // 
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.
18 //
19 //  $Id$
20
21 #ifndef __PANEL_HXX
22 #define __PANEL_HXX
23
24 #ifndef __cplusplus                                                          
25 # error This library requires C++
26 #endif                                   
27
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #ifdef HAVE_WINDOWS_H          
36 #  include <windows.h>
37 #endif
38
39 #include <GL/glut.h>
40 #include <plib/ssg.h>
41
42 #include <simgear/misc/props.hxx>
43 #include <simgear/timing/timestamp.hxx>
44
45 #include <vector>
46 #include <map>
47 #include <plib/fnt.h>
48
49 #include <Main/fgfs.hxx>
50
51
52 FG_USING_STD(vector);
53 FG_USING_STD(map);
54
55 class FGPanelInstrument;
56
57
58 \f
59 ////////////////////////////////////////////////////////////////////////
60 // Texture manager (should migrate out into FGFS).
61 //
62 // This class ensures that no texture is loaded more than once.
63 ////////////////////////////////////////////////////////////////////////
64
65 class FGTextureManager
66 {
67 public:
68   static ssgTexture * createTexture(const string &relativePath);
69 private:
70   static map<string,ssgTexture *> _textureMap;
71 };
72
73
74 \f
75 ////////////////////////////////////////////////////////////////////////
76 // Cropped texture (should migrate out into FGFS).
77 //
78 // This structure wraps an SSG texture with cropping information.
79 ////////////////////////////////////////////////////////////////////////
80
81 class FGCroppedTexture
82 {
83 public:
84
85   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 ();
90
91   virtual void setPath (const string &path) { _path = path; }
92
93   virtual const string &getPath () const { return _path; }
94
95   virtual ssgTexture * getTexture ();
96
97   virtual void setCrop (float minX, float minY, float maxX, float maxY) {
98     _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
99   }
100
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; }
105
106
107 private:
108   string _path;
109   ssgTexture * _texture;
110   float _minX, _minY, _maxX, _maxY;
111 };
112
113
114 \f
115 ////////////////////////////////////////////////////////////////////////
116 // Instrument panel class.
117 //
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 ////////////////////////////////////////////////////////////////////////
123
124 class FGPanel : public FGSubsystem
125 {
126 public:
127
128   FGPanel (int window_x, int window_y, int window_w, int window_h);
129   virtual ~FGPanel ();
130
131                                 // Update the panel (every frame).
132   virtual void init ();
133   virtual void bind ();
134   virtual void unbind ();
135   virtual void update ();
136
137                                 // transfer pointer ownership!!!
138   virtual void addInstrument (FGPanelInstrument * instrument);
139
140                                 // Background texture.
141   virtual void setBackground (ssgTexture * texture);
142
143                                 // Make the panel visible or invisible.
144   virtual bool getVisibility () const;
145   virtual void setVisibility (bool visibility);
146
147                                 // Full width of panel.
148   virtual void setWidth (int width) { _width = width; }
149   virtual int getWidth () const { return _width; }
150
151                                 // Full height of panel.
152   virtual void setHeight (int height) { _height = height; }
153   virtual int getHeight () const { return _height; }
154
155                                 // X-offset
156   virtual void setXOffset (int offset);
157   virtual int getXOffset () const { return _x_offset; }
158
159                                 // Y-offset.
160   virtual void setYOffset (int offset);
161   virtual int getYOffset () const { return _y_offset; }
162
163                                 // View height.
164   virtual void setViewHeight (int height) { _view_height = height; }
165   virtual int getViewHeight () const { return _view_height; }
166
167                                 // Handle a mouse click.
168   virtual bool doMouseAction (int button, int updown, int x, int y);
169
170 private:
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;
177   int _winx, _winy, _winw, _winh;
178   int _width;
179   int _height;
180   int _x_offset;
181   int _y_offset;
182   int _view_height;
183   
184   ssgTexture * _bg;
185                                 // List of instruments in panel.
186   instrument_list_type _instruments;
187 };
188
189
190 \f
191 ////////////////////////////////////////////////////////////////////////
192 // Base class for user action types.
193 //
194 // Individual instruments can have actions associated with a mouse
195 // click in a rectangular area.  Current concrete classes include
196 // FGAdjustAction, FGSwapAction, and FGToggleAction.
197 ////////////////////////////////////////////////////////////////////////
198
199 class FGPanelAction
200 {
201 public:
202   FGPanelAction ();
203   FGPanelAction (int button, int x, int y, int w, int h);
204   virtual ~FGPanelAction ();
205
206                                 // Getters.
207   virtual int getButton () const { return _button; }
208   virtual int getX () const { return _x; }
209   virtual int getY () const { return _y; }
210   virtual int getWidth () const { return _w; }
211   virtual int getHeight () const { return _h; }
212
213                                 // Setters.
214   virtual void setButton (int button) { _button = button; }
215   virtual void setX (int x) { _x = x; }
216   virtual void setY (int y) { _y = y; }
217   virtual void setWidth (int w) { _w = w; }
218   virtual void setHeight (int h) { _h = h; }
219
220                                 // Check whether we're in the area.
221   virtual bool inArea (int button, int x, int y)
222   {
223     return (button == _button &&
224             x >= _x &&
225             x < _x + _w &&
226             y >= _y &&
227             y < _y + _h);
228   }
229
230                                 // Perform the action.
231   virtual void doAction () = 0;
232
233 private:
234   int _button;
235   int _x;
236   int _y;
237   int _w;
238   int _h;
239 };
240
241
242 \f
243 ////////////////////////////////////////////////////////////////////////
244 // Adjustment action.
245 //
246 // This is an action to increase or decrease an FGFS value by a certain
247 // increment within a certain range.  If the wrap flag is true, the
248 // value will wrap around if it goes below min or above max; otherwise,
249 // it will simply stop at min or max.
250 ////////////////////////////////////////////////////////////////////////
251
252 class FGAdjustAction : public FGPanelAction
253 {
254 public:
255   FGAdjustAction (int button, int x, int y, int w, int h,
256                   SGValue * value, float increment,
257                   float min, float max, bool wrap=false);
258   virtual ~FGAdjustAction ();
259   virtual void doAction ();
260
261 private:
262   SGValue * _value;
263   float _increment;
264   float _min;
265   float _max;
266   bool _wrap;
267 };
268
269
270 \f
271 ////////////////////////////////////////////////////////////////////////
272 // Swap action.
273 //
274 // This is an action to swap two values.  It's currently used in the
275 // navigation radios.
276 ////////////////////////////////////////////////////////////////////////
277
278 class FGSwapAction : public FGPanelAction
279 {
280 public:
281   FGSwapAction (int button, int x, int y, int w, int h,
282                 SGValue * value1, SGValue * value2);
283   virtual ~FGSwapAction ();
284   virtual void doAction ();
285
286 private:
287   SGValue * _value1;
288   SGValue * _value2;
289 };
290
291
292 \f
293 ////////////////////////////////////////////////////////////////////////
294 // Toggle action.
295 //
296 // This is an action to toggle a boolean value.
297 ////////////////////////////////////////////////////////////////////////
298
299 class FGToggleAction : public FGPanelAction
300 {
301 public:
302   FGToggleAction (int button, int x, int y, int w, int h,
303                   SGValue * value);
304   virtual ~FGToggleAction ();
305   virtual void doAction ();
306
307 private:
308   SGValue * _value;
309 };
310
311
312 \f
313 ////////////////////////////////////////////////////////////////////////
314 // Abstract base class for a panel instrument.
315 //
316 // A panel instrument consists of zero or more actions, associated
317 // with mouse clicks in rectangular areas.  Currently, the only
318 // concrete class derived from this is FGLayeredInstrument, but others
319 // may show up in the future (some complex instruments could be 
320 // entirely hand-coded, for example).
321 ////////////////////////////////////////////////////////////////////////
322
323 class FGPanelInstrument
324 {
325 public:
326   FGPanelInstrument ();
327   FGPanelInstrument (int x, int y, int w, int h);
328   virtual ~FGPanelInstrument ();
329
330   virtual void draw () = 0;
331
332   virtual void setPosition(int x, int y);
333   virtual void setSize(int w, int h);
334
335   virtual int getXPos () const;
336   virtual int getYPos () const;
337   virtual int getWidth () const;
338   virtual int getHeight () const;
339
340                                 // Coordinates relative to centre.
341                                 // Transfer pointer ownership!!
342   virtual void addAction (FGPanelAction * action);
343
344                                 // Coordinates relative to centre.
345   virtual bool doMouseAction (int button, int x, int y);
346
347 protected:
348   int _x, _y, _w, _h;
349   typedef vector<FGPanelAction *> action_list_type;
350   action_list_type _actions;
351 };
352
353
354 \f
355 ////////////////////////////////////////////////////////////////////////
356 // Abstract base class for an instrument layer.
357 //
358 // The FGLayeredInstrument class builds up instruments by using layers
359 // of textures or text.  Each layer can have zero or more
360 // transformations applied to it: for example, a needle layer can
361 // rotate to show the altitude or airspeed.
362 ////////////////////////////////////////////////////////////////////////
363
364
365 /**
366  * A transformation for a layer.
367  */
368 class FGPanelTransformation {
369 public:
370
371   enum Type {
372     XSHIFT,
373     YSHIFT,
374     ROTATION
375   };
376
377   FGPanelTransformation ();
378   virtual ~FGPanelTransformation ();
379
380   Type type;
381   const SGValue * value;
382   float min;
383   float max;
384   float factor;
385   float offset;
386 };
387
388
389
390 /**
391  * A single layer of a multi-layered instrument.
392  *
393  * Each layer can be subject to a series of transformations based
394  * on current FGFS instrument readings: for example, a texture
395  * representing a needle can rotate to show the airspeed.
396  */
397 class FGInstrumentLayer
398 {
399 public:
400
401   FGInstrumentLayer (int w = -1, int h = -1);
402   virtual ~FGInstrumentLayer ();
403
404   virtual void draw () = 0;
405   virtual void transform () const;
406
407   virtual int getWidth () const { return _w; }
408   virtual int getHeight () const { return _h; }
409   virtual void setWidth (int w) { _w = w; }
410   virtual void setHeight (int h) { _h = h; }
411
412                                 // Transfer pointer ownership!!
413                                 // DEPRECATED
414   virtual void addTransformation (FGPanelTransformation * transformation);
415
416 protected:
417   int _w, _h;
418
419   typedef vector<FGPanelTransformation *> transformation_list;
420   transformation_list _transformations;
421 };
422
423
424 \f
425 ////////////////////////////////////////////////////////////////////////
426 // An instrument composed of layers.
427 //
428 // This class represents an instrument which is simply a series of
429 // layers piled one on top of the other, each one undergoing its own
430 // set of transformations.  For example, one layer can represent
431 // the instrument's face (which doesn't move), while the next layer
432 // can represent a needle that rotates depending on an FGFS variable.
433 ////////////////////////////////////////////////////////////////////////
434
435
436 /**
437  * An instrument constructed of multiple layers.
438  *
439  * Each individual layer can be rotated or shifted to correspond
440  * to internal FGFS instrument readings.
441  */
442 class FGLayeredInstrument : public FGPanelInstrument
443 {
444 public:
445   FGLayeredInstrument (int x, int y, int w, int h);
446   virtual ~FGLayeredInstrument ();
447
448   virtual void draw ();
449
450                                 // Transfer pointer ownership!!
451   virtual int addLayer (FGInstrumentLayer *layer);
452   virtual int addLayer (FGCroppedTexture &texture, int w = -1, int h = -1);
453
454                                 // Transfer pointer ownership!!
455   virtual void addTransformation (FGPanelTransformation * transformation);
456
457 protected:
458   typedef vector<FGInstrumentLayer *> layer_list;
459   layer_list _layers;
460 };
461
462
463 \f
464 ////////////////////////////////////////////////////////////////////////
465 // A textured layer of an instrument.
466 //
467 // This is a layer holding a single texture.  Normally, the texture's
468 // backgound should be transparent so that lower layers and the panel
469 // background can show through.
470 ////////////////////////////////////////////////////////////////////////
471
472 class FGTexturedLayer : public FGInstrumentLayer
473 {
474 public:
475   FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
476   FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
477   virtual ~FGTexturedLayer ();
478
479   virtual void draw ();
480
481   virtual void setTexture (const FGCroppedTexture &texture) {
482     _texture = texture;
483   }
484   virtual FGCroppedTexture &getTexture () { return _texture; }
485   virtual const FGCroppedTexture &getTexture () const { return _texture; }
486
487 private:
488   mutable FGCroppedTexture _texture;
489 };
490
491
492 \f
493 ////////////////////////////////////////////////////////////////////////
494 // A text layer of an instrument.
495 //
496 // This is a layer holding a string of static and/or generated text.
497 // It is useful for instruments that have text displays, such as
498 // a chronometer, GPS, or NavCom radio.
499 ////////////////////////////////////////////////////////////////////////
500
501 class FGTextLayer : public FGInstrumentLayer
502 {
503 public:
504   typedef enum ChunkType {
505     TEXT,
506     TEXT_VALUE,
507     DOUBLE_VALUE
508   };
509
510   class Chunk {
511   public:
512     Chunk (const string &text, const string &fmt = "%s");
513     Chunk (ChunkType type, const SGValue * value,
514            const string &fmt = "", float mult = 1.0);
515
516     const char * getValue () const;
517   private:
518     ChunkType _type;
519     string _text;
520     const SGValue * _value;
521     string _fmt;
522     float _mult;
523     mutable char _buf[1024];
524   };
525
526   FGTextLayer (int w = -1, int h = -1);
527   virtual ~FGTextLayer ();
528
529   virtual void draw ();
530
531                                 // Transfer pointer!!
532   virtual void addChunk (Chunk * chunk);
533   virtual void setColor (float r, float g, float b);
534   virtual void setPointSize (float size);
535   virtual void setFont (fntFont * font);
536
537 private:
538
539   void recalc_value () const;
540
541   typedef vector<Chunk *> chunk_list;
542   chunk_list _chunks;
543   float _color[4];
544
545   float _pointSize;
546
547   mutable string _value;
548   mutable SGTimeStamp _then;
549   mutable SGTimeStamp _now;
550 };
551
552
553 \f
554 ////////////////////////////////////////////////////////////////////////
555 // A layer that switches between two other layers.
556 ////////////////////////////////////////////////////////////////////////
557
558 class FGSwitchLayer : public FGInstrumentLayer
559 {
560 public:
561                                 // Transfer pointers!!
562   FGSwitchLayer (int w, int h, const SGValue * value,
563                  FGInstrumentLayer * layer1,
564                  FGInstrumentLayer * layer2);
565   virtual ~FGSwitchLayer ();
566
567   virtual void draw ();
568
569 private:
570   const SGValue * _value;
571   FGInstrumentLayer * _layer1, * _layer2;
572 };
573
574
575 \f
576 ////////////////////////////////////////////////////////////////////////
577 // Functions.
578 ////////////////////////////////////////////////////////////////////////
579
580 bool fgPanelVisible ();
581
582
583 \f
584 ////////////////////////////////////////////////////////////////////////
585 // The current panel, if any.
586 ////////////////////////////////////////////////////////////////////////
587
588 extern FGPanel * current_panel;
589
590
591 \f
592 #endif // __PANEL_HXX
593
594 // end of panel.hxx
595
596