Debian Codegraph @

src/Cockpit/panel.hxx

raw symbols SCIP
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19 // $Id$
20
21 #ifndef __PANEL_HXX
22 #define __PANEL_HXX
23
24 #include <osg/ref_ptr>
25 #include <osg/StateSet>
26 #include <osg/Texture2D>
27
28 #include <simgear/compiler.h>
29 #include <simgear/props/props.hxx>
30 #include <simgear/props/propertyObject.hxx>
31
32 #include <simgear/structure/SGBinding.hxx>
33 #include <simgear/math/interpolater.hxx>
34 #include <simgear/timing/timestamp.hxx>
35
36 #include <cmath>
37 #include <vector>
38 #include <map>
39
40 #include <Main/fg_props.hxx>
41
42 class FGPanelInstrument;
43 class fntFont;
44 class DCLGPS;
45 class IntRect;
46
47 ////////////////////////////////////////////////////////////////////////
48 // Texture management.
49 ////////////////////////////////////////////////////////////////////////
50
51
52 /**
53 * Texture manager (should migrate out into FGFS).
54 *
55 * This class ensures that no texture is loaded more than once.
56 */
57 class FGTextureManager
58 {
59 public:
60 static osg::Texture2D* createTexture(const std::string &relativePath,
61 bool staticTexture = true);
62 static void addTexture(const std::string &relativePath, osg::Texture2D* texture);
63 private:
64 static std::map<std::string,osg::ref_ptr<osg::Texture2D> > _textureMap;
65 };
66
67
68 /**
69 * Cropped texture (should migrate out into FGFS).
70 *
71 * This structure wraps an SSG texture with cropping information.
72 */
73 class FGCroppedTexture
74 {
75 public:
76
77 FGCroppedTexture ();
78 FGCroppedTexture (const std::string &path,
79 float _minX = 0.0, float _minY = 0.0,
80 float _maxX = 1.0, float _maxY = 1.0);
81 virtual ~FGCroppedTexture ();
82
83 virtual void setPath (const std::string &path) { _path = path; }
84
85 virtual const std::string &getPath () const { return _path; }
86
87 virtual osg::StateSet* getTexture ();
88
89 virtual void setCrop (float minX, float minY, float maxX, float maxY) {
90 _minX = minX; _minY = minY; _maxX = maxX; _maxY = maxY;
91 }
92
93 virtual float getMinX () const { return _minX; }
94 virtual float getMinY () const { return _minY; }
95 virtual float getMaxX () const { return _maxX; }
96 virtual float getMaxY () const { return _maxY; }
97
98
99 private:
100 std::string _path;
101 osg::ref_ptr<osg::StateSet> _texture;
102 float _minX, _minY, _maxX, _maxY;
103 };
104
105
106 ////////////////////////////////////////////////////////////////////////
107 // Top-level panel.
108 ////////////////////////////////////////////////////////////////////////
109
110
111 /**
112 * Instrument panel class.
113 *
114 * The panel is a container that has a background texture and holds
115 * zero or more instruments. The panel will order the instruments to
116 * redraw themselves when necessary, and will pass mouse clicks on to
117 * the appropriate instruments for processing.
118 */
119 class FGPanel : public SGReferenced
120 {
121 public:
122
123 FGPanel ();
124 virtual ~FGPanel ();
125
126 virtual void draw (osg::State& state);
127
128 virtual void updateMouseDelay(double dt);
129
130 // transfer pointer ownership!!!
131 virtual void addInstrument (FGPanelInstrument * instrument);
132
133 // Background texture.
134 virtual void setBackground (osg::Texture2D* texture);
135
136 // Background multiple textures.
137 virtual void setMultiBackground (osg::Texture2D* texture, int idx);
138
139 virtual void setWidth (int width) { _width = width; }
140 virtual int getWidth () const { return _width; }
141
142 // Full height of panel.
143 virtual void setHeight (int height) { _height = height; }
144 virtual int getHeight () const { return _height; }
145
146 // X-offset
147 virtual void setXOffset (int offset);
148 virtual int getXOffset () const { return _x_offset->getIntValue(); }
149
150 // Y-offset.
151 virtual void setYOffset (int offset);
152 virtual int getYOffset () const { return _y_offset->getIntValue(); }
153
154 // View height.
155 // virtual void setViewHeight (int height) { _view_height = height; }
156 // virtual int getViewHeight () const { return _view_height; }
157
158 /**
159 * find the actual logical extend of the panel, including all instruments
160 * and actions.
161 */
162 void getLogicalExtent(int &x0, int& y0, int& x1, int &y1);
163
164 // Handle a mouse click.
165 virtual bool doMouseAction (int button, int updown, int x, int y);
166 virtual bool doLocalMouseAction(int button, int updown, int x, int y);
167
168 virtual void setDepthTest (bool enable);
169
170 bool getAutohide(void) const { return _autohide; };
171 void setAutohide(bool enable) { _autohide = enable; };
172
173 double getAspectScale() const;
174
175 private:
176 void setupVirtualCockpit();
177 void cleanupVirtualCockpit();
178
179 mutable bool _mouseDown;
180 mutable int _mouseButton, _mouseX, _mouseY;
181 double _mouseActionRepeat;
182
183 mutable FGPanelInstrument * _mouseInstrument;
184 typedef std::vector<FGPanelInstrument *> instrument_list_type;
185 int _width;
186 int _height;
187 // int _view_height;
188
189 SGPropertyNode_ptr _x_offset;
190 SGPropertyNode_ptr _y_offset;
191 SGPropertyNode_ptr _jitter;
192 SGPropertyNode_ptr _flipx;
193
194 SGConstPropertyNode_ptr _xsize_node;
195 SGConstPropertyNode_ptr _ysize_node;
196
197 osg::ref_ptr<osg::StateSet> _bg;
198 osg::ref_ptr<osg::StateSet> _mbg[8];
199 // List of instruments in panel.
200 instrument_list_type _instruments;
201 bool _enable_depth_test;
202 bool _autohide;
203
204 SGPropObjBool _drawPanelHotspots;
205 };
206
207
208 ////////////////////////////////////////////////////////////////////////
209 // Actions
210 ////////////////////////////////////////////////////////////////////////
211
212
213 /**
214 * Class for user actions.
215 *
216 * The actions are command bindings, like bindings for the keyboard
217 * or joystick, but they are tied to specific mouse actions in
218 * rectangular areas of the panel.
219 */
220 class FGPanelAction : public SGConditional
221 {
222 public:
223 FGPanelAction ();
224 FGPanelAction (int button, int x, int y, int w, int h, bool repeatable);
225 virtual ~FGPanelAction ();
226
227 // Getters.
228 virtual int getButton () const { return _button; }
229 virtual int getX () const { return _x; }
230 virtual int getY () const { return _y; }
231 virtual int getWidth () const { return _w; }
232 virtual int getHeight () const { return _h; }
233
234 // Setters.
235
236 // transfer pointer ownership
237 virtual void addBinding (SGBinding * binding, int updown);
238
239 virtual void setBindings(const SGBindingList& bindings, int updown);
240
241 virtual void setButton (int button) { _button = button; }
242 virtual void setX (int x) { _x = x; }
243 virtual void setY (int y) { _y = y; }
244 virtual void setWidth (int w) { _w = w; }
245 virtual void setHeight (int h) { _h = h; }
246
247 // Check whether we're in the area.
248 virtual bool inArea (int button, int x, int y)
249 {
250 return (button == _button &&
251 x >= _x &&
252 x < _x + _w &&
253 y >= _y &&
254 y < _y + _h);
255 }
256
257 // Perform the action.
258 virtual bool doAction (int updown);
259
260 private:
261 int _button;
262 int _x;
263 int _y;
264 int _w;
265 int _h;
266 bool _repeatable;
267 int _last_state;
268 SGBindingList _bindings[2];
269 };
270
271
272 ////////////////////////////////////////////////////////////////////////
273 // Transformations.
274 ////////////////////////////////////////////////////////////////////////
275
276
277 /**
278 * A transformation for a layer.
279 */
280 class FGPanelTransformation : public SGConditional
281 {
282 public:
283
284 enum Type {
285 XSHIFT,
286 YSHIFT,
287 ROTATION
288 };
289
290 FGPanelTransformation ();
291 virtual ~FGPanelTransformation ();
292
293 Type type;
294 SGConstPropertyNode_ptr node;
295 float min;
296 float max;
297 bool has_mod;
298 float mod;
299 float factor;
300 float offset;
301 SGInterpTable * table;
302 };
303
304
305 ////////////////////////////////////////////////////////////////////////
306 // Layers
307 ////////////////////////////////////////////////////////////////////////
308
309
310 /**
311 * A single layer of a multi-layered instrument.
312 *
313 * Each layer can be subject to a series of transformations based
314 * on current FGFS instrument readings: for example, a texture
315 * representing a needle can rotate to show the airspeed.
316 */
317 class FGInstrumentLayer : public SGConditional
318 {
319 public:
320
321 FGInstrumentLayer (int w = -1, int h = -1);
322 virtual ~FGInstrumentLayer ();
323
324 virtual void draw (osg::State& state) = 0;
325 virtual void transform () const;
326
327 virtual int getWidth () const { return _w; }
328 virtual int getHeight () const { return _h; }
329 virtual void setWidth (int w) { _w = w; }
330 virtual void setHeight (int h) { _h = h; }
331
332 // Transfer pointer ownership!!
333 // DEPRECATED
334 virtual void addTransformation (FGPanelTransformation * transformation);
335
336 protected:
337 int _w, _h;
338
339 typedef std::vector<FGPanelTransformation *> transformation_list;
340 transformation_list _transformations;
341 };
342
343
344 ////////////////////////////////////////////////////////////////////////
345 // Instruments.
346 ////////////////////////////////////////////////////////////////////////
347
348
349 /**
350 * Abstract base class for a panel instrument.
351 *
352 * A panel instrument consists of zero or more actions, associated
353 * with mouse clicks in rectangular areas. Currently, the only
354 * concrete class derived from this is FGLayeredInstrument, but others
355 * may show up in the future (some complex instruments could be
356 * entirely hand-coded, for example).
357 */
358 class FGPanelInstrument : public SGConditional
359 {
360 public:
361 FGPanelInstrument ();
362 FGPanelInstrument (int x, int y, int w, int h);
363 virtual ~FGPanelInstrument ();
364
365 virtual void draw (osg::State& state) = 0;
366 virtual void drawHotspots(osg::State& state);
367
368 virtual void setPosition(int x, int y);
369 virtual void setSize(int w, int h);
370
371 virtual int getXPos () const;
372 virtual int getYPos () const;
373 virtual int getWidth () const;
374 virtual int getHeight () const;
375
376 // Coordinates relative to centre.
377 // Transfer pointer ownership!!
378 virtual void addAction (FGPanelAction * action);
379
380 // Coordinates relative to centre.
381 virtual bool doMouseAction (int button, int updown, int x, int y);
382
383 void extendRect(IntRect& r) const;
384 protected:
385 int _x, _y, _w, _h;
386 typedef std::vector<FGPanelAction *> action_list_type;
387 action_list_type _actions;
388 };
389
390
391 /**
392 * An instrument constructed of multiple layers.
393 *
394 * Each individual layer can be rotated or shifted to correspond
395 * to internal FGFS instrument readings.
396 */
397 class FGLayeredInstrument : public FGPanelInstrument
398 {
399 public:
400 FGLayeredInstrument (int x, int y, int w, int h);
401 virtual ~FGLayeredInstrument ();
402
403 virtual void draw (osg::State& state);
404
405 // Transfer pointer ownership!!
406 virtual int addLayer (FGInstrumentLayer *layer);
407 virtual int addLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
408
409 // Transfer pointer ownership!!
410 virtual void addTransformation (FGPanelTransformation * transformation);
411
412 protected:
413 typedef std::vector<FGInstrumentLayer *> layer_list;
414 layer_list _layers;
415 };
416
417
418 /**
419 * An empty-shell instrument that exists soley in
420 * order to redirect commands from the panel to a
421 * complex instrument inherited from SGSubsystem.
422 *
423 * Currently the only complex instrument is the KLN89,
424 * which we've hardwired this to for now.
425 */
426 class FGSpecialInstrument : public FGPanelInstrument
427 {
428 public:
429 FGSpecialInstrument(DCLGPS* sb);
430 //FGSpecialInstrument (int x, int y, int w, int h);
431 virtual ~FGSpecialInstrument ();
432
433 virtual void draw (osg::State& state);
434
435 protected:
436 DCLGPS* complex;
437 };
438
439
440 /**
441 * An instrument layer containing a group of sublayers.
442 *
443 * This class is useful for gathering together a group of related
444 * layers, either to hold in an external file or to work under
445 * the same condition.
446 */
447 class FGGroupLayer : public FGInstrumentLayer
448 {
449 public:
450 FGGroupLayer ();
451 virtual ~FGGroupLayer ();
452 virtual void draw (osg::State& state);
453 // transfer pointer ownership
454 virtual void addLayer (FGInstrumentLayer * layer);
455 protected:
456 std::vector<FGInstrumentLayer *> _layers;
457 };
458
459
460 /**
461 * A textured layer of an instrument.
462 *
463 * This is a layer holding a single texture. Normally, the texture's
464 * backgound should be transparent so that lower layers and the panel
465 * background can show through.
466 */
467 class FGTexturedLayer : public FGInstrumentLayer
468 {
469 public:
470 FGTexturedLayer (int w = -1, int h = -1) : FGInstrumentLayer(w, h) {}
471 FGTexturedLayer (const FGCroppedTexture &texture, int w = -1, int h = -1);
472 virtual ~FGTexturedLayer ();
473
474 virtual void draw (osg::State& state);
475
476 virtual void setTexture (const FGCroppedTexture &texture) {
477 _texture = texture;
478 }
479 virtual const FGCroppedTexture &getTexture () const { return _texture; }
480 virtual FGCroppedTexture *getTexture() { return &_texture; }
481
482 void setEmissive(bool e) { _emissive = e; }
483
484 private:
485 FGCroppedTexture _texture;
486 bool _emissive;
487 };
488
489
490 /**
491 * A text layer of an instrument.
492 *
493 * This is a layer holding a string of static and/or generated text.
494 * It is useful for instruments that have text displays, such as
495 * a chronometer, GPS, or NavCom radio.
496 */
497 class FGTextLayer : public FGInstrumentLayer
498 {
499 public:
500 enum ChunkType {
501 TEXT,
502 TEXT_VALUE,
503 DOUBLE_VALUE
504 };
505
506 class Chunk : public SGConditional
507 {
508 public:
509 Chunk (const std::string &text, const std::string &fmt = "%s");
510 Chunk (ChunkType type, const SGPropertyNode * node,
511 const std::string &fmt = "", float mult = 1.0, float offs = 0.0,
512 bool truncation = false);
513
514 const char * getValue () const;
515 private:
516 ChunkType _type;
517 std::string _text;
518 SGConstPropertyNode_ptr _node;
519 std::string _fmt;
520 float _mult;
521 float _offs;
522 bool _trunc;
523 mutable char _buf[1024];
524 };
525
526 FGTextLayer (int w = -1, int h = -1);
527 virtual ~FGTextLayer ();
528
529 virtual void draw (osg::State& state);
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 setFontName ( const std::string &name );
536 virtual void setFont (fntFont * font);
537
538 private:
539
540 void recalc_value () const;
541
542 typedef std::vector<Chunk *> chunk_list;
543 chunk_list _chunks;
544 float _color[4];
545
546 float _pointSize;
547 mutable std::string _font_name;
548 mutable std::string _value;
549 mutable SGTimeStamp _then;
550 mutable SGTimeStamp _now;
551 };
552
553
554 /**
555 * A group layer that switches among its children.
556 *
557 * The first layer that passes its condition will be drawn, and
558 * any following layers will be ignored.
559 */
560 class FGSwitchLayer : public FGGroupLayer
561 {
562 public:
563 // Transfer pointers!!
564 FGSwitchLayer ();
565 virtual void draw (osg::State& state);
566
567 };
568
569 #endif // __PANEL_HXX
570
571 // end of panel.hxx
572
573
574