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