]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.hxx
58f02569b00c1f175b0e0ccaab616bfcd930d955
[flightgear.git] / src / Cockpit / panel.hxx
1 //  panel.hxx - default, 2D single-engine prop instrument 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 #ifdef HAVE_WINDOWS_H          
34 #  include <windows.h>
35 #endif
36
37 #include <GL/glut.h>
38 #include <plib/ssg.h>
39
40 #include <vector>
41 #include <map>
42 #include <plib/fnt.h>
43
44 FG_USING_STD(vector);
45 FG_USING_STD(map);
46
47 class FGPanelInstrument;
48
49
50 \f
51 ////////////////////////////////////////////////////////////////////////
52 // Texture manager (should migrate out into FGFS).
53 ////////////////////////////////////////////////////////////////////////
54
55 class FGTextureManager
56 {
57 public:
58   static ssgTexture * createTexture(const char * relativePath);
59 private:
60   static map<const char *,ssgTexture *>_textureMap;
61 };
62
63 \f
64 ////////////////////////////////////////////////////////////////////////
65 // Instrument panel class.
66 ////////////////////////////////////////////////////////////////////////
67
68 class FGPanel
69 {
70 public:
71
72   FGPanel ();
73   virtual ~FGPanel ();
74
75                                 // transfer pointer ownership!!!
76   virtual void addInstrument (FGPanelInstrument * instrument);
77   virtual void init (int x, int y, int finx, int finy);
78   virtual void update () const;
79   
80   virtual bool getVisibility () const;
81   virtual void setVisibility (bool visibility);
82
83   virtual bool doMouseAction (int button, int updown, int x, int y);
84
85 private:
86   bool _initialized;
87   bool _visibility;
88   typedef vector<FGPanelInstrument *> instrument_list_type;
89   int _x, _y, _w, _h;
90   int _panel_h;
91   ssgTexture * _bg;
92                                 // List of instruments in panel.
93   instrument_list_type _instruments;
94 };
95
96
97 \f
98 ////////////////////////////////////////////////////////////////////////
99 // Base class for user action types.
100 ////////////////////////////////////////////////////////////////////////
101
102 class FGPanelAction
103 {
104 public:
105   virtual void doAction () = 0;
106 };
107
108
109 \f
110 ////////////////////////////////////////////////////////////////////////
111 // Adjustment action.
112 ////////////////////////////////////////////////////////////////////////
113
114 class FGAdjustAction : public FGPanelAction
115 {
116 public:
117   typedef double (*getter_type)();
118   typedef void (*setter_type)(double);
119
120   FGAdjustAction (getter_type getter, setter_type setter, double increment,
121                   double min, double max, bool wrap=false);
122   virtual ~FGAdjustAction ();
123   virtual void doAction ();
124 private:
125   getter_type _getter;
126   setter_type _setter;
127   double _increment;
128   double _min;
129   double _max;
130   bool _wrap;
131 };
132
133
134 \f
135 ////////////////////////////////////////////////////////////////////////
136 // Instrument base class.
137 ////////////////////////////////////////////////////////////////////////
138
139 class FGPanelInstrument
140 {
141 public:
142   FGPanelInstrument ();
143   FGPanelInstrument (int x, int y, int w, int h);
144   virtual ~FGPanelInstrument ();
145
146   virtual void draw () const = 0;
147
148   virtual void setPosition(int x, int y);
149   virtual void setSize(int w, int h);
150
151   virtual int getXPos () const;
152   virtual int getYPos () const;
153   virtual int getWidth () const;
154   virtual int getHeight () const;
155
156                                 // Coordinates relative to centre.
157                                 // Transfer pointer ownership!!
158   virtual void addAction (int x, int y, int w, int h,
159                           FGPanelAction * action);
160
161                                 // Coordinates relative to centre.
162   virtual bool doMouseAction (int button, int updown, int x, int y);
163
164 protected:
165   int _x, _y, _w, _h;
166   typedef struct {
167     int x;
168     int y;
169     int w;
170     int h;
171     FGPanelAction * action;
172   } inst_action;
173   typedef vector<inst_action> action_list_type;
174   action_list_type _actions;
175 };
176
177
178 \f
179 ////////////////////////////////////////////////////////////////////////
180 // A single layer of an instrument.
181 ////////////////////////////////////////////////////////////////////////
182
183 /**
184  * A single layer of a multi-layered instrument.
185  *
186  * Each layer can be subject to a series of transformations based
187  * on current FGFS instrument readings: for example, a texture
188  * representing a needle can rotate to show the airspeed.
189  */
190 class FGInstrumentLayer
191 {
192 public:
193   typedef enum {
194     XSHIFT,
195     YSHIFT,
196     ROTATION
197   } transform_type;
198
199   typedef double (*transform_func)();
200
201
202   FGInstrumentLayer ();
203   FGInstrumentLayer (int w, int h, int z);
204   virtual ~FGInstrumentLayer ();
205
206   virtual void draw () const = 0;
207   virtual void transform () const;
208
209   virtual void addTransformation (transform_type type, transform_func func,
210                                   double min, double max,
211                                   double factor = 1.0, double offset = 0.0);
212
213 protected:
214   int _w, _h, _z;
215
216   typedef struct {
217     transform_type type;
218     transform_func func;
219     double min;
220     double max;
221     double factor;
222     double offset;
223   } transformation;
224   typedef vector<transformation *> transformation_list;
225   transformation_list _transformations;
226 };
227
228
229 \f
230 ////////////////////////////////////////////////////////////////////////
231 // An instrument composed of layered textures.
232 ////////////////////////////////////////////////////////////////////////
233
234
235 /**
236  * An instrument constructed of multiple layers.
237  *
238  * Each individual layer can be rotated or shifted to correspond
239  * to internal FGFS instrument readings.
240  */
241 class FGLayeredInstrument : public FGPanelInstrument
242 {
243 public:
244   typedef vector<FGInstrumentLayer *> layer_list;
245   FGLayeredInstrument (int x, int y, int w, int h);
246   virtual ~FGLayeredInstrument ();
247
248   virtual void draw () const;
249
250                                 // Transfer pointer ownership!!
251   virtual void addLayer (FGInstrumentLayer *layer);
252   virtual void addLayer (int i, ssgTexture * texture);
253   virtual void addTransformation (int layer,
254                                   FGInstrumentLayer::transform_type type,
255                                   FGInstrumentLayer::transform_func func,
256                                   double min, double max,
257                                   double factor = 1.0, double offset = 0.0);
258   virtual void addTransformation (int layer,
259                                   FGInstrumentLayer::transform_type type,
260                                   double offset) {
261     addTransformation(layer, type, 0, 0.0, 0.0, 1.0, offset);
262   }
263
264 protected:
265   layer_list _layers;
266 };
267
268
269 \f
270 ////////////////////////////////////////////////////////////////////////
271 // A textured layer of an instrument.
272 ////////////////////////////////////////////////////////////////////////
273
274 /**
275  * A textured layer of an instrument.
276  *
277  * This is a type of layer designed to hold a texture; normally,
278  * the texture's background should be transparent so that
279  * other layers or the panel background show through.
280  */
281 class FGTexturedInstrumentLayer : public FGInstrumentLayer
282 {
283 public:
284   FGTexturedInstrumentLayer (ssgTexture * texture,
285                              int w, int h, int z);
286   virtual ~FGTexturedInstrumentLayer ();
287
288   virtual void draw () const;
289
290   virtual void setTexture (ssgTexture * texture) { _texture = texture; }
291
292 private:
293   ssgTexture * _texture;
294 };
295
296
297 \f
298 ////////////////////////////////////////////////////////////////////////
299 // A text layer of an instrument.
300 ////////////////////////////////////////////////////////////////////////
301
302 class FGCharInstrumentLayer : public FGInstrumentLayer
303 {
304 public:
305   typedef char * (*text_func)(char *);
306   FGCharInstrumentLayer (text_func func,
307                          int w, int h, int z);
308   virtual ~FGCharInstrumentLayer ();
309
310   virtual void draw () const;
311   virtual void setColor (float r, float g, float b);
312   virtual void setPointSize (float size);
313   virtual void setFont (fntFont * font);
314
315 private:
316   text_func _func;
317   float _color[4];
318                                 // FIXME: need only one globally
319   mutable fntRenderer _renderer;
320   mutable char _buf[1024];
321 };
322
323
324 \f
325 ////////////////////////////////////////////////////////////////////////
326 // The current panel, if any.
327 ////////////////////////////////////////////////////////////////////////
328
329 extern FGPanel current_panel;
330
331
332 \f
333 #endif // __PANEL_HXX
334
335 // end of panel.hxx
336
337