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