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