]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel_io.cxx
- modified FGBinding to keep a local copy of the argument property
[flightgear.git] / src / Cockpit / panel_io.cxx
1 //  panel_io.cxx - I/O for 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 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H          
26 #  include <windows.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/props.hxx>
34
35 #include STL_IOSTREAM
36 #include STL_FSTREAM
37 #include STL_STRING
38
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
41
42 #include "panel.hxx"
43 #include "steam.hxx"
44 #include "panel_io.hxx"
45
46 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
47 SG_USING_STD(istream);
48 SG_USING_STD(ifstream);
49 #endif
50 SG_USING_STD(string);
51
52
53 \f
54 ////////////////////////////////////////////////////////////////////////
55 // Built-in layer for the magnetic compass ribbon layer.
56 //
57 // TODO: move this out into a special directory for built-in
58 // layers of various sorts.
59 ////////////////////////////////////////////////////////////////////////
60
61 class FGMagRibbon : public FGTexturedLayer
62 {
63 public:
64   FGMagRibbon (int w, int h);
65   virtual ~FGMagRibbon () {}
66
67   virtual void draw ();
68 };
69
70 FGMagRibbon::FGMagRibbon (int w, int h)
71   : FGTexturedLayer(w, h)
72 {
73   FGCroppedTexture texture("Aircraft/c172/Instruments/Textures/compass-ribbon.rgb");
74   setTexture(texture);
75 }
76
77 void
78 FGMagRibbon::draw ()
79 {
80   double heading = FGSteam::get_MH_deg();
81   double xoffset, yoffset;
82
83   while (heading >= 360.0) {
84     heading -= 360.0;
85   }
86   while (heading < 0.0) {
87     heading += 360.0;
88   }
89
90   if (heading >= 60.0 && heading <= 180.0) {
91     xoffset = heading / 240.0;
92     yoffset = 0.75;
93   } else if (heading >= 150.0 && heading <= 270.0) {
94     xoffset = (heading - 90.0) / 240.0;
95     yoffset = 0.50;
96   } else if (heading >= 240.0 && heading <= 360.0) {
97     xoffset = (heading - 180.0) / 240.0;
98     yoffset = 0.25;
99   } else {
100     if (heading < 270.0)
101       heading += 360.0;
102     xoffset = (heading - 270.0) / 240.0;
103     yoffset = 0.0;
104   }
105
106   xoffset = 1.0 - xoffset;
107                                 // Adjust to put the number in the centre
108   xoffset -= 0.25;
109
110   FGCroppedTexture &t = getTexture();
111   t.setCrop(xoffset, yoffset, xoffset + 0.5, yoffset + 0.25);
112   FGTexturedLayer::draw();
113 }
114
115
116 \f
117 ////////////////////////////////////////////////////////////////////////
118 // Read and construct a panel.
119 //
120 // The panel is specified as a regular property list, and each of the
121 // instruments is its own, separate property list (and thus, a separate
122 // XML document).  The functions in this section read in the files
123 // as property lists, then extract properties to set up the panel
124 // itself.
125 //
126 // A panel contains zero or more instruments.
127 //
128 // An instrument contains one or more layers and zero or more actions.
129 //
130 // A layer contains zero or more transformations.
131 //
132 // Some special types of layers also contain other objects, such as 
133 // chunks of text or other layers.
134 //
135 // There are currently four types of layers:
136 //
137 // 1. Textured Layer (type="texture"), the default
138 // 2. Text Layer (type="text")
139 // 3. Switch Layer (type="switch")
140 // 4. Built-in Layer (type="built-in", must also specify class)
141 //
142 // The only built-in layer so far is the ribbon for the magnetic compass
143 // (class="compass-ribbon").
144 //
145 // There are three types of actions:
146 //
147 // 1. Adjust (type="adjust"), the default
148 // 2. Swap (type="swap")
149 // 3. Toggle (type="toggle")
150 //
151 // There are three types of transformations:
152 //
153 // 1. X shift (type="x-shift"), the default
154 // 2. Y shift (type="y-shift")
155 // 3. Rotation (type="rotation")
156 //
157 // Each of these may be associated with a property, so that a needle
158 // will rotate with the airspeed, for example, or may have a fixed
159 // floating-point value.
160 ////////////////////////////////////////////////////////////////////////
161
162
163 /**
164  * Read a cropped texture from the instrument's property list.
165  *
166  * The x1 and y1 properties give the starting position of the texture
167  * (between 0.0 and 1.0), and the the x2 and y2 properties give the
168  * ending position.  For example, to use the bottom-left quarter of a
169  * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
170  */
171 static FGCroppedTexture
172 readTexture (const SGPropertyNode * node)
173 {
174   FGCroppedTexture texture(node->getStringValue("path"),
175                            node->getFloatValue("x1"),
176                            node->getFloatValue("y1"),
177                            node->getFloatValue("x2", 1.0),
178                            node->getFloatValue("y2", 1.0));
179   SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
180   return texture;
181 }
182
183
184 /**
185  * Read an action from the instrument's property list.
186  *
187  * The action will be performed when the user clicks a mouse button
188  * within the specified region of the instrument.  Actions always work
189  * by modifying the value of a property (see the SGPropertyNode
190  * class).
191  *
192  * The following action types are defined:
193  *
194  * "adjust" - modify the value of a floating-point property by
195  *    the increment specified.  This is the default.
196  *
197  * "swap" - swap the values of two-floating-point properties.
198  *
199  * "toggle" - toggle the value of a boolean property between true and
200  *    false.
201  *
202  * For the adjust action, it is possible to specify an increment
203  * (use a negative number for a decrement), a minimum allowed value,
204  * a maximum allowed value, and a flag to indicate whether the value
205  * should freeze or wrap-around when it reachs the minimum or maximum.
206  *
207  * The action will be scaled automatically if the instrument is not
208  * being drawn at its regular size.
209  */
210 static FGPanelAction *
211 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
212 {
213   FGPanelAction * action = 0;
214
215   string name = node->getStringValue("name");
216   string type = node->getStringValue("type");
217
218   int button = node->getIntValue("button");
219   int x = int(node->getIntValue("x") * w_scale);
220   int y = int(node->getIntValue("y") * h_scale);
221   int w = int(node->getIntValue("w") * w_scale);
222   int h = int(node->getIntValue("h") * h_scale);
223
224   if (type == "") {
225     SG_LOG(SG_COCKPIT, SG_ALERT,
226            "No type supplied for action " << name << " assuming \"adjust\"");
227     type = "adjust";
228   }
229
230                                 // Adjust a property value
231   if (type == "adjust") {
232     string propName = node->getStringValue("property");
233     SGPropertyNode * target = fgGetNode(propName, true);
234     float increment = node->getFloatValue("increment", 1.0);
235     float min = node->getFloatValue("min", 0.0);
236     float max = node->getFloatValue("max", 0.0);
237     bool wrap = node->getBoolValue("wrap", false);
238     if (min == max)
239       SG_LOG(SG_COCKPIT, SG_ALERT, "Action " << node->getName()
240              << " has same min and max value");
241     action = new FGAdjustAction(button, x, y, w, h, target,
242                                 increment, min, max, wrap);
243   } 
244
245                                 // Swap two property values
246   else if (type == "swap") {
247     string propName1 = node->getStringValue("property1");
248     string propName2 = node->getStringValue("property2");
249     SGPropertyNode * target1 = fgGetNode(propName1, true);
250     SGPropertyNode * target2 = fgGetNode(propName2, true);
251     action = new FGSwapAction(button, x, y, w, h, target1, target2);
252   } 
253
254                                 // Toggle a boolean value
255   else if (type == "toggle") {
256     string propName = node->getStringValue("property");
257     SGPropertyNode * target = fgGetNode(propName, true);
258     action = new FGToggleAction(button, x, y, w, h, target);
259   } 
260
261                                 // Unrecognized type
262   else {
263     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized action type " << type );
264     return 0;
265   }
266
267   return action;
268 }
269
270
271 /**
272  * Read a transformation from the instrument's property list.
273  *
274  * The panel module uses the transformations to slide or spin needles,
275  * knobs, and other indicators, and to place layers in the correct
276  * positions.  Every layer starts centered exactly on the x,y co-ordinate,
277  * and many layers need to be moved or rotated simply to display the
278  * instrument correctly.
279  *
280  * There are three types of transformations:
281  *
282  * "x-shift" - move the layer horizontally.
283  *
284  * "y-shift" - move the layer vertically.
285  *
286  * "rotation" - rotate the layer.
287  *
288  * Each transformation may have a fixed offset, and may also have
289  * a floating-point property value to add to the offset.  The
290  * floating-point property may be clamped to a minimum and/or
291  * maximum range and scaled (after clamping).
292  *
293  * Note that because of the way OpenGL works, transformations will
294  * appear to be applied backwards.
295  */
296 static FGPanelTransformation *
297 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
298 {
299   FGPanelTransformation * t = new FGPanelTransformation;
300
301   string name = node->getName();
302   string type = node->getStringValue("type");
303   string propName = node->getStringValue("property", "");
304   SGPropertyNode * target = 0;
305
306   if (type == "") {
307     SG_LOG( SG_COCKPIT, SG_ALERT,
308             "No type supplied for transformation " << name
309             << " assuming \"rotation\"" );
310     type = "rotation";
311   }
312
313   if (propName != (string)"") {
314     target = fgGetNode(propName, true);
315   }
316
317   t->node = target;
318   t->min = node->getFloatValue("min", -9999999);
319   t->max = node->getFloatValue("max", 99999999);
320   t->factor = node->getFloatValue("scale", 1.0);
321   t->offset = node->getFloatValue("offset", 0.0);
322
323                                 // Check for an interpolation table
324   const SGPropertyNode * trans_table = node->getNode("interpolation");
325   if (trans_table != 0) {
326     SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
327             << trans_table->nChildren() << "children" );
328     t->table = new SGInterpTable();
329     for(int i = 0; i < trans_table->nChildren(); i++) {
330       const SGPropertyNode * node = trans_table->getChild(i);
331       if (node->getName() == "entry") {
332         double ind = node->getDoubleValue("ind", 0.0);
333         double dep = node->getDoubleValue("dep", 0.0);
334         SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
335                 << ind << "==>" << dep );
336         t->table->addEntry(ind, dep);
337       } else {
338         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
339                 << " in interpolation" );
340       }
341     }
342   } else {
343     t->table = 0;
344   }
345   
346                                 // Move the layer horizontally.
347   if (type == "x-shift") {
348     t->type = FGPanelTransformation::XSHIFT;
349 //     t->min *= w_scale; //removed by Martin Dressler
350 //     t->max *= w_scale; //removed by Martin Dressler
351     t->offset *= w_scale;
352     t->factor *= w_scale; //Added by Martin Dressler
353   } 
354
355                                 // Move the layer vertically.
356   else if (type == "y-shift") {
357     t->type = FGPanelTransformation::YSHIFT;
358     //t->min *= h_scale; //removed
359     //t->max *= h_scale; //removed
360     t->offset *= h_scale;
361     t->factor *= h_scale; //Added
362   } 
363
364                                 // Rotate the layer.  The rotation
365                                 // is in degrees, and does not need
366                                 // to scale with the instrument size.
367   else if (type == "rotation") {
368     t->type = FGPanelTransformation::ROTATION;
369   } 
370
371   else {
372     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
373     delete t;
374     return 0;
375   }
376
377   SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
378   return t;
379 }
380
381
382 /**
383  * Read a chunk of text from the instrument's property list.
384  *
385  * A text layer consists of one or more chunks of text.  All chunks
386  * share the same font size and color (and eventually, font), but
387  * each can come from a different source.  There are three types of
388  * text chunks:
389  *
390  * "literal" - a literal text string (the default)
391  *
392  * "text-value" - the current value of a string property
393  *
394  * "number-value" - the current value of a floating-point property.
395  *
396  * All three may also include a printf-style format string.
397  */
398 FGTextLayer::Chunk *
399 readTextChunk (const SGPropertyNode * node)
400 {
401   FGTextLayer::Chunk * chunk;
402   string name = node->getStringValue("name");
403   string type = node->getStringValue("type");
404   string format = node->getStringValue("format");
405
406                                 // Default to literal text.
407   if (type == "") {
408     SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
409             << " assuming \"literal\"");
410     type = "literal";
411   }
412
413                                 // A literal text string.
414   if (type == "literal") {
415     string text = node->getStringValue("text");
416     chunk = new FGTextLayer::Chunk(text, format);
417   }
418
419                                 // The value of a string property.
420   else if (type == "text-value") {
421     SGPropertyNode * target =
422       fgGetNode(node->getStringValue("property"), true);
423     chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
424   }
425
426                                 // The value of a float property.
427   else if (type == "number-value") {
428     string propName = node->getStringValue("property");
429     float scale = node->getFloatValue("scale", 1.0);
430     SGPropertyNode * target = fgGetNode(propName, true);
431     chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
432                                    format, scale);
433   }
434
435                                 // Unknown type.
436   else {
437     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
438             << " for text chunk " << name );
439     return 0;
440   }
441
442   return chunk;
443 }
444
445
446 /**
447  * Read a single layer from an instrument's property list.
448  *
449  * Each instrument consists of one or more layers stacked on top
450  * of each other; the lower layers show through only where the upper
451  * layers contain an alpha component.  Each layer can be moved
452  * horizontally and vertically and rotated using transformations.
453  *
454  * This module currently recognizes four kinds of layers:
455  *
456  * "texture" - a layer containing a texture (the default)
457  *
458  * "text" - a layer containing text
459  *
460  * "switch" - a layer that switches between two other layers
461  *   based on the current value of a boolean property.
462  *
463  * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
464  *
465  * Currently, the only built-in layer class is "compass-ribbon".
466  */
467 static FGInstrumentLayer *
468 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
469 {
470   FGInstrumentLayer * layer = NULL;
471   string name = node->getStringValue("name");
472   string type = node->getStringValue("type");
473   int w = node->getIntValue("w", -1);
474   int h = node->getIntValue("h", -1);
475   if (w != -1)
476     w = int(w * w_scale);
477   if (h != -1)
478     h = int(h * h_scale);
479
480
481   if (type == "") {
482     SG_LOG( SG_COCKPIT, SG_ALERT,
483             "No type supplied for layer " << name
484             << " assuming \"texture\"" );
485     type = "texture";
486   }
487
488
489                                 // A textured instrument layer.
490   if (type == "texture") {
491     FGCroppedTexture texture = readTexture(node->getNode("texture"));
492     layer = new FGTexturedLayer(texture, w, h);
493   }
494
495
496                                 // A textual instrument layer.
497   else if (type == "text") {
498     FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
499
500                                 // Set the text color.
501     float red = node->getFloatValue("color/red", 0.0);
502     float green = node->getFloatValue("color/green", 0.0);
503     float blue = node->getFloatValue("color/blue", 0.0);
504     tlayer->setColor(red, green, blue);
505
506                                 // Set the point size.
507     float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
508     tlayer->setPointSize(pointSize);
509
510                                 // Set the font.
511     // TODO
512
513     const SGPropertyNode * chunk_group = node->getNode("chunks");
514     if (chunk_group != 0) {
515       int nChunks = chunk_group->nChildren();
516       for (int i = 0; i < nChunks; i++) {
517         const SGPropertyNode * node = chunk_group->getChild(i);
518         if (node->getName() == "chunk") {
519           FGTextLayer::Chunk * chunk = readTextChunk(node);
520           if (chunk != 0)
521             tlayer->addChunk(chunk);
522         } else {
523           SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
524                   << " in chunks" );
525         }
526       }
527       layer = tlayer;
528     }
529   }
530
531                                 // A switch instrument layer.
532   else if (type == "switch") {
533     SGPropertyNode * target =
534       fgGetNode(node->getStringValue("property"), true);
535     FGInstrumentLayer * layer1 =
536       readLayer(node->getNode("layer1"), w_scale, h_scale);
537     FGInstrumentLayer * layer2 =
538       readLayer(node->getNode("layer2"), w_scale, h_scale);
539     layer = new FGSwitchLayer(w, h, target, layer1, layer2);
540   }
541
542                                 // A built-in instrument layer.
543   else if (type == "built-in") {
544     string layerclass = node->getStringValue("class");
545
546     if (layerclass == "mag-ribbon") {
547       layer = new FGMagRibbon(w, h);
548     }
549
550     else if (layerclass == "") {
551       SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
552               << name );
553       return 0;
554     }
555
556     else {
557       SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
558               << layerclass);
559       return 0;
560     }
561   }
562
563                                 // An unknown type.
564   else {
565     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
566     delete layer;
567     return 0;
568   }
569   
570   //
571   // Get the transformations for each layer.
572   //
573   const SGPropertyNode * trans_group = node->getNode("transformations");
574   if (trans_group != 0) {
575     int nTransformations = trans_group->nChildren();
576     for (int i = 0; i < nTransformations; i++) {
577       const SGPropertyNode * node = trans_group->getChild(i);
578       if (node->getName() == "transformation") {
579         FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
580         if (t != 0)
581           layer->addTransformation(t);
582       } else {
583         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
584                 << " in transformations" );
585       }
586     }
587   }
588   
589   SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
590   return layer;
591 }
592
593
594 /**
595  * Read an instrument from a property list.
596  *
597  * The instrument consists of a preferred width and height
598  * (the panel may override these), together with a list of layers
599  * and a list of actions to be performed when the user clicks 
600  * the mouse over the instrument.  All co-ordinates are relative
601  * to the instrument's position, so instruments are fully relocatable;
602  * likewise, co-ordinates for actions and transformations will be
603  * scaled automatically if the instrument is not at its preferred size.
604  */
605 static FGPanelInstrument *
606 readInstrument (const SGPropertyNode * node)
607 {
608   const string &name = node->getStringValue("name");
609   int x = node->getIntValue("x", -1);
610   int y = node->getIntValue("y", -1);
611   int real_w = node->getIntValue("w", -1);
612   int real_h = node->getIntValue("h", -1);
613   int w = node->getIntValue("w-base", -1);
614   int h = node->getIntValue("h-base", -1);
615
616   if (x == -1 || y == -1) {
617     SG_LOG( SG_COCKPIT, SG_ALERT,
618             "x and y positions must be specified and > 0" );
619     return 0;
620   }
621
622   float w_scale = 1.0;
623   float h_scale = 1.0;
624   if (real_w != -1) {
625     w_scale = float(real_w) / float(w);
626     w = real_w;
627   }
628   if (real_h != -1) {
629     h_scale = float(real_h) / float(h);
630     h = real_h;
631   }
632
633   SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
634
635   FGLayeredInstrument * instrument =
636     new FGLayeredInstrument(x, y, w, h);
637
638   //
639   // Get the actions for the instrument.
640   //
641   const SGPropertyNode * action_group = node->getNode("actions");
642   if (action_group != 0) {
643     int nActions = action_group->nChildren();
644     for (int i = 0; i < nActions; i++) {
645       const SGPropertyNode * node = action_group->getChild(i);
646       if (node->getName() == "action") {
647         FGPanelAction * action = readAction(node, w_scale, h_scale);
648         if (action != 0)
649           instrument->addAction(action);
650       } else {
651         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
652                 << " in actions" );
653       }
654     }
655   }
656
657   //
658   // Get the layers for the instrument.
659   //
660   const SGPropertyNode * layer_group = node->getNode("layers");
661   if (layer_group != 0) {
662     int nLayers = layer_group->nChildren();
663     for (int i = 0; i < nLayers; i++) {
664       const SGPropertyNode * node = layer_group->getChild(i);
665       if (node->getName() == "layer") {
666         FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
667         if (layer != 0)
668           instrument->addLayer(layer);
669       } else {
670         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
671                 << " in layers" );
672       }
673     }
674   }
675     
676   SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
677   return instrument;
678 }
679
680
681 /**
682  * Construct the panel from a property tree.
683  */
684 FGPanel *
685 readPanel (const SGPropertyNode * root)
686 {
687   SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
688           root->getStringValue("name", "[Unnamed Panel]") );
689
690   FGPanel * panel = new FGPanel();
691   panel->setWidth(root->getIntValue("w", 1024));
692   panel->setHeight(root->getIntValue("h", 443));
693
694   //
695   // Grab the visible external viewing area, default to 
696   //
697   panel->setViewHeight(root->getIntValue("view-height",
698                                          768 - panel->getHeight() + 2));
699
700   //
701   // Grab the panel's initial offsets, default to 0, 0.
702   //
703   if (!fgHasNode("/sim/panel/x-offset"))
704     fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
705
706   if (!fgHasNode("/sim/panel/y-offset"))
707     fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
708
709   //
710   // Assign the background texture, if any, or a bogus chequerboard.
711   //
712   string bgTexture = root->getStringValue("background");
713   if (bgTexture == "")
714     bgTexture = "FOO";
715   panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
716   SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
717
718
719   //
720   // Create each instrument.
721   //
722   SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
723   const SGPropertyNode * instrument_group = root->getChild("instruments");
724   if (instrument_group != 0) {
725     int nInstruments = instrument_group->nChildren();
726     for (int i = 0; i < nInstruments; i++) {
727       const SGPropertyNode * node = instrument_group->getChild(i);
728       if (node->getName() == "instrument") {
729         FGPanelInstrument * instrument = readInstrument(node);
730         if (instrument != 0)
731           panel->addInstrument(instrument);
732       } else {
733         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
734                 << " in instruments section" );
735       }
736     }
737   }
738   SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
739
740
741   //
742   // Return the new panel.
743   //
744   return panel;
745 }
746
747
748 /**
749  * Read a panel from a property list.
750  *
751  * Each panel instrument will appear in its own, separate
752  * property list.  The top level simply names the panel and
753  * places the instruments in their appropriate locations (and
754  * optionally resizes them if necessary).
755  *
756  * Returns 0 if the read fails for any reason.
757  */
758 FGPanel *
759 fgReadPanel (istream &input)
760 {
761   SGPropertyNode root;
762
763   if (!readProperties(input, &root)) {
764     SG_LOG( SG_COCKPIT, SG_ALERT, "Malformed property list for panel." );
765     return 0;
766   }
767   return readPanel(&root);
768 }
769
770
771 /**
772  * Read a panel from a property list.
773  *
774  * This function opens a stream to a file, then invokes the
775  * main fgReadPanel() function.
776  */
777 FGPanel *
778 fgReadPanel (const string &relative_path)
779 {
780   SGPath path(globals->get_fg_root());
781   path.append(relative_path);
782   SGPropertyNode root;
783
784   if (!readProperties(path.str(), &root)) {
785     SG_LOG( SG_COCKPIT, SG_ALERT, "Malformed property list for panel." );
786     return 0;
787   }
788   return readPanel(&root);
789 }
790
791
792
793 // end of panel_io.cxx