]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel_io.cxx
David Luff: Here is an update to the engine model. It now takes
[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/fgpath.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 (FG_HAVE_NATIVE_SGI_COMPILERS)
47 FG_USING_STD(istream);
48 FG_USING_STD(ifstream);
49 #endif
50 FG_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   FG_LOG(FG_INPUT, FG_INFO, "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 hscale, float vscale)
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") * hscale);
266   int y = int(node->getIntValue("y") * vscale);
267   int w = int(node->getIntValue("w") * hscale);
268   int h = int(node->getIntValue("h") * vscale);
269
270   if (type == "") {
271     FG_LOG(FG_INPUT, FG_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       FG_LOG(FG_INPUT, FG_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     FG_LOG(FG_INPUT, FG_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 hscale, float vscale)
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     FG_LOG(FG_INPUT, FG_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                                 // Move the layer horizontally.
370   if (type == "x-shift") {
371     t->type = FGPanelTransformation::XSHIFT;
372     t->min *= hscale;
373     t->max *= hscale;
374     t->offset *= hscale;
375   } 
376
377                                 // Move the layer vertically.
378   else if (type == "y-shift") {
379     t->type = FGPanelTransformation::YSHIFT;
380     t->min *= vscale;
381     t->max *= vscale;
382     t->offset *= vscale;
383   } 
384
385                                 // Rotate the layer.  The rotation
386                                 // is in degrees, and does not need
387                                 // to scale with the instrument size.
388   else if (type == "rotation") {
389     t->type = FGPanelTransformation::ROTATION;
390   } 
391
392   else {
393     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized transformation type " << type);
394     delete t;
395     return 0;
396   }
397
398   FG_LOG(FG_INPUT, FG_INFO, "Read transformation " << name);
399   return t;
400 }
401
402
403 /**
404  * Read a chunk of text from the instrument's property list.
405  *
406  * A text layer consists of one or more chunks of text.  All chunks
407  * share the same font size and color (and eventually, font), but
408  * each can come from a different source.  There are three types of
409  * text chunks:
410  *
411  * "literal" - a literal text string (the default)
412  *
413  * "text-value" - the current value of a string property
414  *
415  * "number-value" - the current value of a floating-point property.
416  *
417  * All three may also include a printf-style format string.
418  */
419 FGTextLayer::Chunk *
420 readTextChunk (const SGPropertyNode * node)
421 {
422   FGTextLayer::Chunk * chunk;
423   string name = node->getStringValue("name");
424   string type = node->getStringValue("type");
425   string format = node->getStringValue("format");
426
427                                 // Default to literal text.
428   if (type == "") {
429     FG_LOG(FG_INPUT, FG_INFO, "No type provided for text chunk " << name
430            << " assuming \"literal\"");
431     type = "literal";
432   }
433
434                                 // A literal text string.
435   if (type == "literal") {
436     string text = node->getStringValue("text");
437     chunk = new FGTextLayer::Chunk(text, format);
438   }
439
440                                 // The value of a string property.
441   else if (type == "text-value") {
442     SGValue * value =
443       fgGetValue(node->getStringValue("property"), true);
444     chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
445   }
446
447                                 // The value of a float property.
448   else if (type == "number-value") {
449     string propName = node->getStringValue("property");
450     float scale = node->getFloatValue("scale", 1.0);
451     SGValue * value = fgGetValue(propName, true);
452     chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, value,
453                                    format, scale);
454   }
455
456                                 // Unknown type.
457   else {
458     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
459            << " for text chunk " << name);
460     return 0;
461   }
462
463   return chunk;
464 }
465
466
467 /**
468  * Read a single layer from an instrument's property list.
469  *
470  * Each instrument consists of one or more layers stacked on top
471  * of each other; the lower layers show through only where the upper
472  * layers contain an alpha component.  Each layer can be moved
473  * horizontally and vertically and rotated using transformations.
474  *
475  * This module currently recognizes four kinds of layers:
476  *
477  * "texture" - a layer containing a texture (the default)
478  *
479  * "text" - a layer containing text
480  *
481  * "switch" - a layer that switches between two other layers
482  *   based on the current value of a boolean property.
483  *
484  * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
485  *
486  * Currently, the only built-in layer class is "compass-ribbon".
487  */
488 static FGInstrumentLayer *
489 readLayer (const SGPropertyNode * node, float hscale, float vscale)
490 {
491   FGInstrumentLayer * layer = NULL;
492   string name = node->getStringValue("name");
493   string type = node->getStringValue("type");
494   int w = node->getIntValue("w", -1);
495   int h = node->getIntValue("h", -1);
496   if (w != -1)
497     w = int(w * hscale);
498   if (h != -1)
499     h = int(h * vscale);
500
501
502   if (type == "") {
503     FG_LOG(FG_INPUT, FG_ALERT,
504            "No type supplied for layer " << name
505            << " assuming \"texture\"");
506     type = "texture";
507   }
508
509
510                                 // A textured instrument layer.
511   if (type == "texture") {
512     FGCroppedTexture texture = readTexture(node->getNode("texture"));
513     layer = new FGTexturedLayer(texture, w, h);
514   }
515
516
517                                 // A textual instrument layer.
518   else if (type == "text") {
519     FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
520
521                                 // Set the text color.
522     float red = node->getFloatValue("color/red", 0.0);
523     float green = node->getFloatValue("color/green", 0.0);
524     float blue = node->getFloatValue("color/blue", 0.0);
525     tlayer->setColor(red, green, blue);
526
527                                 // Set the point size.
528     float pointSize = node->getFloatValue("point-size", 10.0) * hscale;
529     tlayer->setPointSize(pointSize);
530
531                                 // Set the font.
532     // TODO
533
534     const SGPropertyNode * chunk_group = node->getNode("chunks");
535     if (chunk_group != 0) {
536       int nChunks = chunk_group->nChildren();
537       for (int i = 0; i < nChunks; i++) {
538         FGTextLayer::Chunk * chunk = readTextChunk(chunk_group->getChild(i));
539         if (chunk == 0) {
540           delete layer;
541           return 0;
542         }
543         tlayer->addChunk(chunk);
544       }
545       layer = tlayer;
546     }
547   }
548
549                                 // A switch instrument layer.
550   else if (type == "switch") {
551     SGValue * value =
552       fgGetValue(node->getStringValue("property"), true);
553     FGInstrumentLayer * layer1 =
554       readLayer(node->getNode("layer1"), hscale, vscale);
555     FGInstrumentLayer * layer2 =
556       readLayer(node->getNode("layer2"), hscale, vscale);
557     layer = new FGSwitchLayer(w, h, value, layer1, layer2);
558   }
559
560                                 // A built-in instrument layer.
561   else if (type == "built-in") {
562     string layerclass = node->getStringValue("class");
563
564     if (layerclass == "mag-ribbon") {
565       layer = new FGMagRibbon(w, h);
566     }
567
568     else if (layerclass == "") {
569       FG_LOG(FG_INPUT, FG_ALERT, "No class provided for built-in layer "
570              << name);
571       return 0;
572     }
573
574     else {
575       FG_LOG(FG_INPUT, FG_ALERT, "Unknown built-in layer class "
576              << layerclass);
577       return 0;
578     }
579   }
580
581                                 // An unknown type.
582   else {
583     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized layer type " << type);
584     delete layer;
585     return 0;
586   }
587   
588   //
589   // Get the transformations for each layer.
590   //
591   const SGPropertyNode * trans_group = node->getNode("transformations");
592   if (trans_group != 0) {
593     int nTransformations = trans_group->nChildren();
594     for (int i = 0; i < nTransformations; i++) {
595       FGPanelTransformation * t = readTransformation(trans_group->getChild(i),
596                                                      hscale, vscale);
597       if (t == 0) {
598         delete layer;
599         return 0;
600       }
601       layer->addTransformation(t);
602     }
603   }
604   
605   FG_LOG(FG_INPUT, FG_INFO, "Read layer " << name);
606   return layer;
607 }
608
609
610 /**
611  * Read an instrument from a property list.
612  *
613  * The instrument consists of a preferred width and height
614  * (the panel may override these), together with a list of layers
615  * and a list of actions to be performed when the user clicks 
616  * the mouse over the instrument.  All co-ordinates are relative
617  * to the instrument's position, so instruments are fully relocatable;
618  * likewise, co-ordinates for actions and transformations will be
619  * scaled automatically if the instrument is not at its preferred size.
620  */
621 static FGPanelInstrument *
622 readInstrument (const SGPropertyNode * node, int x, int y,
623                 int real_w, int real_h)
624 {
625   int w = node->getIntValue("w");
626   int h = node->getIntValue("h");
627   const string &name = node->getStringValue("name");
628
629   float hscale = 1.0;
630   float vscale = 1.0;
631   if (real_w != -1) {
632     hscale = float(real_w) / float(w);
633     w = real_w;
634   }
635   if (real_h != -1) {
636     vscale = float(real_h) / float(h);
637     h = real_h;
638   }
639
640   FG_LOG(FG_INPUT, FG_INFO, "Reading instrument " << name);
641
642   FGLayeredInstrument * instrument =
643     new FGLayeredInstrument(x, y, w, h);
644
645   //
646   // Get the actions for the instrument.
647   //
648   const SGPropertyNode * action_group = node->getNode("actions");
649   if (action_group != 0) {
650     int nActions = action_group->nChildren();
651     for (int i = 0; i < nActions; i++) {
652       FGPanelAction * action = readAction(action_group->getChild(i),
653                                           hscale, vscale);
654       if (action == 0) {
655         delete instrument;
656         return new DefaultInstrument(x, y, w, h);
657       }
658       instrument->addAction(action);
659     }
660   }
661
662   //
663   // Get the layers for the instrument.
664   //
665   const SGPropertyNode * layer_group = node->getNode("layers");
666   if (layer_group != 0) {
667     int nLayers = layer_group->nChildren();
668     for (int i = 0; i < nLayers; i++) {
669       FGInstrumentLayer * layer = readLayer(layer_group->getChild(i),
670                                             hscale, vscale);
671       if (layer == 0) {
672         delete instrument;
673         return new DefaultInstrument(x, y, w, h);
674       }
675       instrument->addLayer(layer);
676     }
677   }
678     
679   FG_LOG(FG_INPUT, FG_INFO, "Done reading instrument " << name);
680   return instrument;
681 }
682
683
684 /**
685  * Read a panel from a property list.
686  *
687  * Each panel instrument will appear in its own, separate
688  * property list.  The top level simply names the panel and
689  * places the instruments in their appropriate locations (and
690  * optionally resizes them if necessary).
691  *
692  * Returns 0 if the read fails for any reason.
693  */
694 FGPanel *
695 fgReadPanel (istream &input)
696 {
697   SGPropertyNode root;
698
699
700   //
701   // Read the property list from disk.
702   //
703   if (!readProperties(input, &root)) {
704     FG_LOG(FG_INPUT, FG_ALERT, "Malformed property list for panel.");
705     return 0;
706   }
707   FG_LOG(FG_INPUT, FG_INFO, "Read properties for panel " <<
708          root.getStringValue("name"));
709
710   //
711   // Construct a new, empty panel.
712   //
713   FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
714
715
716   //
717   // Grab the panel's dimensions, default to 1024x443.
718   //
719   int panel_w = (root.hasValue("w") ? root.getIntValue("w") : 1024);
720   int panel_h = (root.hasValue("h") ? root.getIntValue("h") : 443);
721   panel->setWidth(panel_w);
722   panel->setHeight(panel_h);
723
724   //
725   // Grab the visible external viewing area, default to 
726   //
727   panel->setViewHeight(root.hasValue("view-height") ?
728                        root.getIntValue("view-height") :
729                        768 - panel_h + 2);
730
731   //
732   // Grab the panel's initial offsets, default to 0, 0.
733   //
734   int xoffset = (root.hasValue("x-offset") ?
735                  root.getIntValue("x-offset") :
736                  0);
737   int yoffset = (root.hasValue("y-offset") ?
738                  root.getIntValue("y-offset") :
739                  0);
740   panel->setXOffset(xoffset);
741   panel->setYOffset(yoffset);
742
743   //
744   // Assign the background texture, if any, or a bogus chequerboard.
745   //
746   string bgTexture = root.getStringValue("background");
747   if (bgTexture == "")
748     bgTexture = "FOO";
749   panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
750   FG_LOG(FG_INPUT, FG_INFO, "Set background texture to " << bgTexture);
751
752
753   //
754   // Create each instrument.
755   //
756   FG_LOG(FG_INPUT, FG_INFO, "Reading panel instruments");
757   const SGPropertyNode * instrument_group = root.getChild("instruments");
758   if (instrument_group != 0) {
759     int nInstruments = instrument_group->nChildren();
760     for (int i = 0; i < nInstruments; i++) {
761       const SGPropertyNode * node = instrument_group->getChild(i);
762       
763       FGPath path( globals->get_fg_root() );
764       path.append(node->getStringValue("path"));
765       
766       FG_LOG(FG_INPUT, FG_INFO, "Reading instrument "
767              << node->getName()
768              << " from "
769              << path.str());
770       
771       int x = node->getIntValue("x", -1);
772       int y = node->getIntValue("y", -1);
773       int w = node->getIntValue("w", -1);
774       int h = node->getIntValue("h", -1);
775       
776       if (x == -1 || y == -1) {
777         FG_LOG(FG_INPUT, FG_ALERT, "x and y positions must be specified and >0");
778         delete panel;
779         return 0;
780       }
781
782                                 // Read the instrument from
783                                 // a separate file.
784       FGPanelInstrument * instrument = 0;
785       
786       SGPropertyNode root2;
787       
788       if (readProperties(path.str(), &root2)) {
789         cerr << "Read " << root2.nChildren() << " top-level nodes from "
790              << path.c_str() << endl;
791         instrument = readInstrument(&root2, x, y, w, h);
792       }
793       if (instrument == 0)
794         instrument = new DefaultInstrument(x, y, w, h);
795       panel->addInstrument(instrument);
796     }
797   }
798   FG_LOG(FG_INPUT, FG_INFO, "Done reading panel instruments");
799
800
801   //
802   // Return the new panel.
803   //
804   return panel;
805 }
806
807
808 /**
809  * Read a panel from a property list.
810  *
811  * This function opens a stream to a file, then invokes the
812  * main fgReadPanel() function.
813  */
814 FGPanel *
815 fgReadPanel (const string &relative_path)
816 {
817   FGPanel * panel = 0;
818   FGPath path(globals->get_fg_root());
819   path.append(relative_path);
820   ifstream input(path.c_str());
821   if (!input.good()) {
822     FG_LOG(FG_INPUT, FG_ALERT,
823            "Cannot read panel configuration from " << path.str());
824   } else {
825     panel = fgReadPanel(input);
826     input.close();
827   }
828   if (panel == 0)
829     panel = new DefaultPanel(0, 0, 1024, 768);
830   return panel;
831 }
832
833
834
835 // end of panel_io.cxx