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 #include <config.h>
22
23 #include <string.h> // for strcmp()
24
25 #include <simgear/compiler.h>
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/props/props.hxx>
30 #include <simgear/props/props_io.hxx>
31 #include <simgear/debug/ErrorReportingCallback.hxx>
32
33 #include <istream>
34 #include <fstream>
35 #include <string>
36
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Main/sentryIntegration.hxx>
40
41 #include <GUI/gui.h>
42
43 // #include "panel.hxx"
44 #include "panel_io.hxx"
45 #include <Instrumentation/KLN89/kln89.hxx>
46
47 //built-in layers
48 #include "built_in/FGMagRibbon.hxx"
49
50 using std::istream;
51 using std::ifstream;
52 using std::string;
53
54
55
56 ////////////////////////////////////////////////////////////////////////
57 // Read and construct a panel.
58 //
59 // The panel is specified as a regular property list, and each of the
60 // instruments is its own, separate property list (and thus, a separate
61 // XML document). The functions in this section read in the files
62 // as property lists, then extract properties to set up the panel
63 // itself.
64 //
65 // A panel contains zero or more instruments.
66 //
67 // An instrument contains one or more layers and zero or more actions.
68 //
69 // A layer contains zero or more transformations.
70 //
71 // Some special types of layers also contain other objects, such as
72 // chunks of text or other layers.
73 //
74 // There are currently four types of layers:
75 //
76 // 1. Textured Layer (type="texture"), the default
77 // 2. Text Layer (type="text")
78 // 3. Switch Layer (type="switch")
79 // 4. Built-in Layer (type="built-in", must also specify class)
80 //
81 // The only built-in layer so far is the ribbon for the magnetic compass
82 // (class="compass-ribbon").
83 //
84 // There are three types of actions:
85 //
86 // 1. Adjust (type="adjust"), the default
87 // 2. Swap (type="swap")
88 // 3. Toggle (type="toggle")
89 //
90 // There are three types of transformations:
91 //
92 // 1. X shift (type="x-shift"), the default
93 // 2. Y shift (type="y-shift")
94 // 3. Rotation (type="rotation")
95 //
96 // Each of these may be associated with a property, so that a needle
97 // will rotate with the airspeed, for example, or may have a fixed
98 // floating-point value.
99 ////////////////////////////////////////////////////////////////////////
100
101
102 /**
103 * Read a cropped texture from the instrument's property list.
104 *
105 * The x1 and y1 properties give the starting position of the texture
106 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
107 * ending position. For example, to use the bottom-left quarter of a
108 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
109 */
110 static FGCroppedTexture
readTexture(const SGPropertyNode * node)111 readTexture (const SGPropertyNode * node)
112 {
113 FGCroppedTexture texture(node->getStringValue("path"),
114 node->getFloatValue("x1"),
115 node->getFloatValue("y1"),
116 node->getFloatValue("x2", 1.0),
117 node->getFloatValue("y2", 1.0));
118 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
119 return texture;
120 }
121
122
123 /**
124 * Test for a condition in the current node.
125 */
126
127 ////////////////////////////////////////////////////////////////////////
128 // Read a condition and use it if necessary.
129 ////////////////////////////////////////////////////////////////////////
130
131 static void
readConditions(SGConditional * component,const SGPropertyNode * node)132 readConditions (SGConditional *component, const SGPropertyNode *node)
133 {
134 const SGPropertyNode * conditionNode = node->getChild("condition");
135 if (conditionNode != 0)
136 // The top level is implicitly AND
137 component->setCondition(sgReadCondition(globals->get_props(),
138 conditionNode) );
139 }
140
141
142 /**
143 * Read an action from the instrument's property list.
144 *
145 * The action will be performed when the user clicks a mouse button
146 * within the specified region of the instrument. Actions always work
147 * by modifying the value of a property (see the SGPropertyNode
148 * class).
149 *
150 * The following action types are defined:
151 *
152 * "adjust" - modify the value of a floating-point property by
153 * the increment specified. This is the default.
154 *
155 * "swap" - swap the values of two-floating-point properties.
156 *
157 * "toggle" - toggle the value of a boolean property between true and
158 * false.
159 *
160 * For the adjust action, it is possible to specify an increment
161 * (use a negative number for a decrement), a minimum allowed value,
162 * a maximum allowed value, and a flag to indicate whether the value
163 * should freeze or wrap-around when it reachs the minimum or maximum.
164 *
165 * The action will be scaled automatically if the instrument is not
166 * being drawn at its regular size.
167 */
168 static FGPanelAction *
readAction(const SGPropertyNode * node,float w_scale,float h_scale)169 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
170 {
171 unsigned int i, j;
172 SGPropertyNode *binding;
173 vector<SGPropertyNode_ptr>bindings = node->getChildren("binding");
174
175 // button-less actions are fired initially
176 if (!node->hasValue("w") || !node->hasValue("h")) {
177 for (i = 0; i < bindings.size(); i++) {
178 SGBinding b(bindings[i], globals->get_props());
179 b.fire();
180 }
181 return 0;
182 }
183
184 string name = node->getStringValue("name");
185
186 int button = node->getIntValue("button");
187 int x = int(node->getIntValue("x") * w_scale);
188 int y = int(node->getIntValue("y") * h_scale);
189 int w = int(node->getIntValue("w") * w_scale);
190 int h = int(node->getIntValue("h") * h_scale);
191 bool repeatable = node->getBoolValue("repeatable", true);
192
193 FGPanelAction * action = new FGPanelAction(button, x, y, w, h, repeatable);
194
195 SGPropertyNode * dest = fgGetNode("/sim/bindings/panel", true);
196
197 for (i = 0; i < bindings.size(); i++) {
198 SG_LOG(SG_INPUT, SG_BULK, "Reading binding "
199 << bindings[i]->getStringValue("command"));
200
201 j = 0;
202 while (dest->getChild("binding", j))
203 j++;
204
205 binding = dest->getChild("binding", j, true);
206 copyProperties(bindings[i], binding);
207 action->addBinding(new SGBinding(binding, globals->get_props()), 0);
208 }
209
210 if (node->hasChild("mod-up")) {
211 bindings = node->getChild("mod-up")->getChildren("binding");
212 for (i = 0; i < bindings.size(); i++) {
213 j = 0;
214 while (dest->getChild("binding", j))
215 j++;
216
217 binding = dest->getChild("binding", j, true);
218 copyProperties(bindings[i], binding);
219 action->addBinding(new SGBinding(binding, globals->get_props()), 1);
220 }
221 }
222
223 readConditions(action, node);
224 return action;
225 }
226
227
228 /**
229 * Read a transformation from the instrument's property list.
230 *
231 * The panel module uses the transformations to slide or spin needles,
232 * knobs, and other indicators, and to place layers in the correct
233 * positions. Every layer starts centered exactly on the x,y co-ordinate,
234 * and many layers need to be moved or rotated simply to display the
235 * instrument correctly.
236 *
237 * There are three types of transformations:
238 *
239 * "x-shift" - move the layer horizontally.
240 *
241 * "y-shift" - move the layer vertically.
242 *
243 * "rotation" - rotate the layer.
244 *
245 * Each transformation may have a fixed offset, and may also have
246 * a floating-point property value to add to the offset. The
247 * floating-point property may be clamped to a minimum and/or
248 * maximum range and scaled (after clamping).
249 *
250 * Note that because of the way OpenGL works, transformations will
251 * appear to be applied backwards.
252 */
253 static FGPanelTransformation *
readTransformation(const SGPropertyNode * node,float w_scale,float h_scale)254 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
255 {
256 FGPanelTransformation * t = new FGPanelTransformation;
257
258 string name = node->getName();
259 string type = node->getStringValue("type");
260 string propName = node->getStringValue("property", "");
261 SGPropertyNode * target = 0;
262
263 if (type.empty()) {
264 SG_LOG( SG_COCKPIT, SG_BULK,
265 "No type supplied for transformation " << name
266 << " assuming \"rotation\"" );
267 type = "rotation";
268 }
269
270 if (!propName.empty()) {
271 target = fgGetNode(propName.c_str(), true);
272 }
273
274 t->node = target;
275 t->min = node->getFloatValue("min", -9999999);
276 t->max = node->getFloatValue("max", 99999999);
277 t->has_mod = node->hasChild("modulator");
278 if (t->has_mod)
279 t->mod = node->getFloatValue("modulator");
280 t->factor = node->getFloatValue("scale", 1.0);
281 t->offset = node->getFloatValue("offset", 0.0);
282
283 // Check for an interpolation table
284 const SGPropertyNode * trans_table = node->getNode("interpolation");
285 if (trans_table != 0) {
286 SG_LOG( SG_COCKPIT, SG_DEBUG, "Found interpolation table with "
287 << trans_table->nChildren() << " children" );
288 t->table = new SGInterpTable(trans_table);
289 } else {
290 t->table = 0;
291 }
292
293 // Move the layer horizontally.
294 if (type == "x-shift") {
295 t->type = FGPanelTransformation::XSHIFT;
296 // t->min *= w_scale; //removed by Martin Dressler
297 // t->max *= w_scale; //removed by Martin Dressler
298 t->offset *= w_scale;
299 t->factor *= w_scale; //Added by Martin Dressler
300 }
301
302 // Move the layer vertically.
303 else if (type == "y-shift") {
304 t->type = FGPanelTransformation::YSHIFT;
305 //t->min *= h_scale; //removed
306 //t->max *= h_scale; //removed
307 t->offset *= h_scale;
308 t->factor *= h_scale; //Added
309 }
310
311 // Rotate the layer. The rotation
312 // is in degrees, and does not need
313 // to scale with the instrument size.
314 else if (type == "rotation") {
315 t->type = FGPanelTransformation::ROTATION;
316 }
317
318 else {
319 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
320 delete t;
321 return 0;
322 }
323
324 readConditions(t, node);
325 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
326 return t;
327 }
328
329
330 /**
331 * Read a chunk of text from the instrument's property list.
332 *
333 * A text layer consists of one or more chunks of text. All chunks
334 * share the same font size and color (and eventually, font), but
335 * each can come from a different source. There are three types of
336 * text chunks:
337 *
338 * "literal" - a literal text string (the default)
339 *
340 * "text-value" - the current value of a string property
341 *
342 * "number-value" - the current value of a floating-point property.
343 *
344 * All three may also include a printf-style format string.
345 */
346 FGTextLayer::Chunk *
readTextChunk(const SGPropertyNode * node)347 readTextChunk (const SGPropertyNode * node)
348 {
349 FGTextLayer::Chunk * chunk;
350 string name = node->getStringValue("name");
351 string type = node->getStringValue("type");
352 string format = node->getStringValue("format");
353
354 // Default to literal text.
355 if (type.empty()) {
356 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
357 << " assuming \"literal\"");
358 type = "literal";
359 }
360
361 // A literal text string.
362 if (type == "literal") {
363 string text = node->getStringValue("text");
364 chunk = new FGTextLayer::Chunk(text, format);
365 }
366
367 // The value of a string property.
368 else if (type == "text-value") {
369 SGPropertyNode * target =
370 fgGetNode(node->getStringValue("property"), true);
371 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
372 }
373
374 // The value of a float property.
375 else if (type == "number-value") {
376 string propName = node->getStringValue("property");
377 float scale = node->getFloatValue("scale", 1.0);
378 float offset = node->getFloatValue("offset", 0.0);
379 bool truncation = node->getBoolValue("truncate", false);
380 SGPropertyNode * target = fgGetNode(propName.c_str(), true);
381 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
382 format, scale, offset, truncation);
383 }
384
385 // Unknown type.
386 else {
387 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
388 << " for text chunk " << name );
389 return 0;
390 }
391
392 readConditions(chunk, node);
393 return chunk;
394 }
395
396
397 /**
398 * Read a single layer from an instrument's property list.
399 *
400 * Each instrument consists of one or more layers stacked on top
401 * of each other; the lower layers show through only where the upper
402 * layers contain an alpha component. Each layer can be moved
403 * horizontally and vertically and rotated using transformations.
404 *
405 * This module currently recognizes four kinds of layers:
406 *
407 * "texture" - a layer containing a texture (the default)
408 *
409 * "text" - a layer containing text
410 *
411 * "switch" - a layer that switches between two other layers
412 * based on the current value of a boolean property.
413 *
414 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
415 *
416 * Currently, the only built-in layer class is "compass-ribbon".
417 */
418 static FGInstrumentLayer *
readLayer(const SGPropertyNode * node,float w_scale,float h_scale)419 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
420 {
421 FGInstrumentLayer * layer = NULL;
422 string name = node->getStringValue("name");
423 string type = node->getStringValue("type");
424 int w = node->getIntValue("w", -1);
425 int h = node->getIntValue("h", -1);
426 bool emissive = node->getBoolValue("emissive", false);
427 if (w != -1)
428 w = int(w * w_scale);
429 if (h != -1)
430 h = int(h * h_scale);
431
432
433 if (type.empty()) {
434 SG_LOG( SG_COCKPIT, SG_BULK,
435 "No type supplied for layer " << name
436 << " assuming \"texture\"" );
437 type = "texture";
438 }
439
440
441 // A textured instrument layer.
442 if (type == "texture") {
443 FGCroppedTexture texture = readTexture(node->getNode("texture"));
444 layer = new FGTexturedLayer(texture, w, h);
445 if (emissive) {
446 FGTexturedLayer *tl=(FGTexturedLayer*)layer;
447 tl->setEmissive(true);
448 }
449
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", "Helvetica");
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 *
readInstrument(const SGPropertyNode * node,const SGPath & path)574 readInstrument (const SGPropertyNode * node, const SGPath& path)
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 simgear::reportFailure(simgear::LoadFailure::Misconfigured, simgear::ErrorCode::AircraftSystems,
586 "Panel x and y positions must be specified and > 0", path);
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 return instrument;
646 }
647
648
649 /**
650 * Construct the panel from a property tree.
651 */
652 FGPanel *
readPanel(const SGPropertyNode * root,const SGPath & path)653 readPanel (const SGPropertyNode * root, const SGPath& path)
654 {
655 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
656 root->getStringValue("name", "[Unnamed Panel]") );
657
658 FGPanel * panel = new FGPanel();
659 panel->setWidth(root->getIntValue("w", 1024));
660 panel->setHeight(root->getIntValue("h", 443));
661
662 //
663 // Grab the visible external viewing area, default to
664 //
665 // panel->setViewHeight(root->getIntValue("view-height",
666 // 768 - panel->getHeight() + 2));
667
668 //
669 // Grab the panel's initial offsets, default to 0, 0.
670 //
671 if (!fgHasNode("/sim/panel/x-offset"))
672 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
673
674 // conditional removed by jim wilson to allow panel xml code
675 // with y-offset defined to work...
676 if (!fgHasNode("/sim/panel/y-offset"))
677 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
678
679 panel->setAutohide(root->getBoolValue("autohide", true));
680
681 //
682 // Assign the background texture, if any, or a bogus chequerboard.
683 //
684 string bgTexture = root->getStringValue("background");
685 if (bgTexture.empty())
686 bgTexture = "FOO";
687 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
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
696 mbgTexture = root->getStringValue("multibackground[1]");
697 if (mbgTexture.empty())
698 mbgTexture = "FOO";
699 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
700
701 mbgTexture = root->getStringValue("multibackground[2]");
702 if (mbgTexture.empty())
703 mbgTexture = "FOO";
704 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
705
706 mbgTexture = root->getStringValue("multibackground[3]");
707 if (mbgTexture.empty())
708 mbgTexture = "FOO";
709 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
710
711 mbgTexture = root->getStringValue("multibackground[4]");
712 if (mbgTexture.empty())
713 mbgTexture = "FOO";
714 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
715
716 mbgTexture = root->getStringValue("multibackground[5]");
717 if (mbgTexture.empty())
718 mbgTexture = "FOO";
719 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
720
721 mbgTexture = root->getStringValue("multibackground[6]");
722 if (mbgTexture.empty())
723 mbgTexture = "FOO";
724 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
725
726 mbgTexture = root->getStringValue("multibackground[7]");
727 if (mbgTexture.empty())
728 mbgTexture = "FOO";
729 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
730
731 }
732
733
734
735 //
736 // Create each instrument.
737 //
738 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading panel instruments" );
739 const SGPropertyNode * instrument_group = root->getChild("instruments");
740 if (instrument_group != 0) {
741 int nInstruments = instrument_group->nChildren();
742 for (int i = 0; i < nInstruments; i++) {
743 const SGPropertyNode * node = instrument_group->getChild(i);
744 if (!strcmp(node->getName(), "instrument")) {
745 FGPanelInstrument * instrument = readInstrument(node, path);
746 if (instrument != 0)
747 panel->addInstrument(instrument);
748 } else if (!strcmp(node->getName(), "special-instrument")) {
749 //cout << "Special instrument found in instruments section!\n";
750 const string name = node->getStringValue("name");
751 if (name == "KLN89 GPS") {
752 //cout << "Special instrument is KLN89\n";
753
754 int x = node->getIntValue("x", -1);
755 int y = node->getIntValue("y", -1);
756 int real_w = node->getIntValue("w", -1);
757 int real_h = node->getIntValue("h", -1);
758 // int w = node->getIntValue("w-base", -1);
759 // int h = node->getIntValue("h-base", -1);
760
761 if (x == -1 || y == -1) {
762 SG_LOG( SG_COCKPIT, SG_ALERT,
763 "x and y positions must be specified and > 0" );
764 return 0;
765 }
766
767 // float w_scale = 1.0;
768 // float h_scale = 1.0;
769 if (real_w != -1) {
770 // w_scale = float(real_w) / float(w);
771 // w = real_w;
772 }
773 if (real_h != -1) {
774 // h_scale = float(real_h) / float(h);
775 // h = real_h;
776 }
777
778 SG_LOG( SG_COCKPIT, SG_BULK, "Reading instrument " << name );
779
780 // Warning - hardwired size!!!
781 RenderArea2D* instrument = new RenderArea2D(158, 40, 158, 40, x, y);
782 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
783 if (gps == NULL) {
784 gps = new KLN89(instrument);
785 globals->add_subsystem("kln89", gps);
786 }
787 //gps->init(); // init seems to get called automagically.
788 FGSpecialInstrument* gpsinst = new FGSpecialInstrument(gps);
789 panel->addInstrument(gpsinst);
790 } else {
791 SG_LOG( SG_COCKPIT, SG_WARN, "Unknown special instrument found" );
792 }
793 } else {
794 SG_LOG( SG_COCKPIT, SG_WARN, "Skipping " << node->getName()
795 << " in instruments section" );
796 }
797 }
798 }
799 SG_LOG( SG_COCKPIT, SG_BULK, "Done reading panel instruments" );
800
801
802 //
803 // Return the new panel.
804 //
805 return panel;
806 }
807
808
809 /**
810 * Read a panel from a property list.
811 *
812 * Each panel instrument will appear in its own, separate
813 * property list. The top level simply names the panel and
814 * places the instruments in their appropriate locations (and
815 * optionally resizes them if necessary).
816 *
817 * Returns 0 if the read fails for any reason.
818 */
819 FGPanel *
fgReadPanel(istream & input)820 fgReadPanel (istream &input)
821 {
822 SGPropertyNode root;
823
824 try {
825 readProperties(input, &root);
826 } catch (const sg_exception &e) {
827 simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AircraftSystems,
828 "Failed to load aircraft panel:" + e.getFormattedMessage());
829 return 0;
830 }
831 return readPanel(&root, SGPath{});
832 }
833
834
835 /**
836 * Read a panel from a property list.
837 *
838 * This function opens a stream to a file, then invokes the
839 * main fgReadPanel() function.
840 */
841 FGPanel *
fgReadPanel(const string & relative_path)842 fgReadPanel (const string &relative_path)
843 {
844 SGPath path = globals->resolve_aircraft_path(relative_path);
845 SGPropertyNode root;
846
847 if (!path.exists()) {
848 simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::AircraftSystems,
849 "Missing panel file:" + relative_path, path);
850 return nullptr;
851 }
852
853 try {
854 readProperties(path, &root);
855 } catch (const sg_exception &e) {
856 simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AircraftSystems,
857 "Error parsing panel file:" + e.getFormattedMessage(), path);
858 return 0;
859 }
860 return readPanel(&root, path);
861 }
862
863
864
865 // end of panel_io.cxx
866
867
868
869