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