1 // panel_io.cxx - I/O for 2D panel.
3 // Written by David Megginson, started January 2000.
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.
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.
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.
25 #include <string.h> // for strcmp()
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 #include <simgear/props/props_io.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
43 // #include "panel.hxx"
44 #include "panel_io.hxx"
45 #include <Instrumentation/KLN89/kln89.hxx>
48 #include "built_in/FGMagRibbon.hxx"
56 ////////////////////////////////////////////////////////////////////////
57 // Read and construct a panel.
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
65 // A panel contains zero or more instruments.
67 // An instrument contains one or more layers and zero or more actions.
69 // A layer contains zero or more transformations.
71 // Some special types of layers also contain other objects, such as
72 // chunks of text or other layers.
74 // There are currently four types of layers:
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)
81 // The only built-in layer so far is the ribbon for the magnetic compass
82 // (class="compass-ribbon").
84 // There are three types of actions:
86 // 1. Adjust (type="adjust"), the default
87 // 2. Swap (type="swap")
88 // 3. Toggle (type="toggle")
90 // There are three types of transformations:
92 // 1. X shift (type="x-shift"), the default
93 // 2. Y shift (type="y-shift")
94 // 3. Rotation (type="rotation")
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 ////////////////////////////////////////////////////////////////////////
103 * Read a cropped texture from the instrument's property list.
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.
110 static FGCroppedTexture
111 readTexture (const SGPropertyNode * node)
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());
124 * Test for a condition in the current node.
127 ////////////////////////////////////////////////////////////////////////
128 // Read a condition and use it if necessary.
129 ////////////////////////////////////////////////////////////////////////
132 readConditions (SGConditional *component, const SGPropertyNode *node)
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(),
143 * Read an action from the instrument's property list.
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
150 * The following action types are defined:
152 * "adjust" - modify the value of a floating-point property by
153 * the increment specified. This is the default.
155 * "swap" - swap the values of two-floating-point properties.
157 * "toggle" - toggle the value of a boolean property between true and
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.
165 * The action will be scaled automatically if the instrument is not
166 * being drawn at its regular size.
168 static FGPanelAction *
169 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
172 SGPropertyNode *binding;
173 vector<SGPropertyNode_ptr>bindings = node->getChildren("binding");
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());
184 string name = node->getStringValue("name");
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);
193 FGPanelAction * action = new FGPanelAction(button, x, y, w, h, repeatable);
195 SGPropertyNode * dest = fgGetNode("/sim/bindings/panel", true);
197 for (i = 0; i < bindings.size(); i++) {
198 SG_LOG(SG_INPUT, SG_BULK, "Reading binding "
199 << bindings[i]->getStringValue("command"));
202 while (dest->getChild("binding", j))
205 binding = dest->getChild("binding", j, true);
206 copyProperties(bindings[i], binding);
207 action->addBinding(new SGBinding(binding, globals->get_props()), 0);
210 if (node->hasChild("mod-up")) {
211 bindings = node->getChild("mod-up")->getChildren("binding");
212 for (i = 0; i < bindings.size(); i++) {
214 while (dest->getChild("binding", j))
217 binding = dest->getChild("binding", j, true);
218 copyProperties(bindings[i], binding);
219 action->addBinding(new SGBinding(binding, globals->get_props()), 1);
223 readConditions(action, node);
229 * Read a transformation from the instrument's property list.
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.
237 * There are three types of transformations:
239 * "x-shift" - move the layer horizontally.
241 * "y-shift" - move the layer vertically.
243 * "rotation" - rotate the layer.
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).
250 * Note that because of the way OpenGL works, transformations will
251 * appear to be applied backwards.
253 static FGPanelTransformation *
254 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
256 FGPanelTransformation * t = new FGPanelTransformation;
258 string name = node->getName();
259 string type = node->getStringValue("type");
260 string propName = node->getStringValue("property", "");
261 SGPropertyNode * target = 0;
264 SG_LOG( SG_COCKPIT, SG_BULK,
265 "No type supplied for transformation " << name
266 << " assuming \"rotation\"" );
270 if (!propName.empty()) {
271 target = fgGetNode(propName.c_str(), true);
275 t->min = node->getFloatValue("min", -9999999);
276 t->max = node->getFloatValue("max", 99999999);
277 t->has_mod = node->hasChild("modulator");
279 t->mod = node->getFloatValue("modulator");
280 t->factor = node->getFloatValue("scale", 1.0);
281 t->offset = node->getFloatValue("offset", 0.0);
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);
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
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
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;
319 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
324 readConditions(t, node);
325 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
331 * Read a chunk of text from the instrument's property list.
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
338 * "literal" - a literal text string (the default)
340 * "text-value" - the current value of a string property
342 * "number-value" - the current value of a floating-point property.
344 * All three may also include a printf-style format string.
347 readTextChunk (const SGPropertyNode * node)
349 FGTextLayer::Chunk * chunk;
350 string name = node->getStringValue("name");
351 string type = node->getStringValue("type");
352 string format = node->getStringValue("format");
354 // Default to literal text.
356 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
357 << " assuming \"literal\"");
361 // A literal text string.
362 if (type == "literal") {
363 string text = node->getStringValue("text");
364 chunk = new FGTextLayer::Chunk(text, format);
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);
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);
387 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
388 << " for text chunk " << name );
392 readConditions(chunk, node);
398 * Read a single layer from an instrument's property list.
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.
405 * This module currently recognizes four kinds of layers:
407 * "texture" - a layer containing a texture (the default)
409 * "text" - a layer containing text
411 * "switch" - a layer that switches between two other layers
412 * based on the current value of a boolean property.
414 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
416 * Currently, the only built-in layer class is "compass-ribbon".
418 static FGInstrumentLayer *
419 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
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);
428 w = int(w * w_scale);
430 h = int(h * h_scale);
434 SG_LOG( SG_COCKPIT, SG_BULK,
435 "No type supplied for layer " << name
436 << " assuming \"texture\"" );
441 // A textured instrument layer.
442 if (type == "texture") {
443 FGCroppedTexture texture = readTexture(node->getNode("texture"));
444 layer = new FGTexturedLayer(texture, w, h);
446 FGTexturedLayer *tl=(FGTexturedLayer*)layer;
447 tl->setEmissive(true);
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));
463 // A textual instrument layer.
464 else if (type == "text") {
465 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
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);
473 // Set the point size.
474 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
475 tlayer->setPointSize(pointSize);
478 string fontName = node->getStringValue("font", "Helvetica");
479 tlayer->setFontName(fontName);
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);
489 tlayer->addChunk(chunk);
491 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
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));
509 // A built-in instrument layer.
510 else if (type == "built-in") {
511 string layerclass = node->getStringValue("class");
513 if (layerclass == "mag-ribbon") {
514 layer = new FGMagRibbon(w, h);
517 else if (layerclass.empty()) {
518 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
524 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
532 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
538 // Get the transformations for each layer.
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);
548 layer->addTransformation(t);
550 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
551 << " in transformations" );
556 readConditions(layer, node);
557 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
563 * Read an instrument from a property list.
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.
573 static FGPanelInstrument *
574 readInstrument (const SGPropertyNode * node)
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);
584 if (x == -1 || y == -1) {
585 SG_LOG( SG_COCKPIT, SG_ALERT,
586 "x and y positions must be specified and > 0" );
593 w_scale = float(real_w) / float(w);
597 h_scale = float(real_h) / float(h);
601 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
603 FGLayeredInstrument * instrument =
604 new FGLayeredInstrument(x, y, w, h);
607 // Get the actions for the instrument.
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);
617 instrument->addAction(action);
619 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
626 // Get the layers for the instrument.
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);
636 instrument->addLayer(layer);
638 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
644 readConditions(instrument, node);
650 * Construct the panel from a property tree.
653 readPanel (const SGPropertyNode * root)
655 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
656 root->getStringValue("name", "[Unnamed Panel]") );
658 FGPanel * panel = new FGPanel();
659 panel->setWidth(root->getIntValue("w", 1024));
660 panel->setHeight(root->getIntValue("h", 443));
663 // Grab the visible external viewing area, default to
665 panel->setViewHeight(root->getIntValue("view-height",
666 768 - panel->getHeight() + 2));
669 // Grab the panel's initial offsets, default to 0, 0.
671 if (!fgHasNode("/sim/panel/x-offset"))
672 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
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));
679 panel->setAutohide(root->getBoolValue("autohide", true));
682 // Assign the background texture, if any, or a bogus chequerboard.
684 string bgTexture = root->getStringValue("background");
685 if (bgTexture.empty())
687 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
690 // Get multibackground if any...
692 string mbgTexture = root->getStringValue("multibackground[0]");
693 if (!mbgTexture.empty()) {
694 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0);
696 mbgTexture = root->getStringValue("multibackground[1]");
697 if (mbgTexture.empty())
699 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
701 mbgTexture = root->getStringValue("multibackground[2]");
702 if (mbgTexture.empty())
704 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
706 mbgTexture = root->getStringValue("multibackground[3]");
707 if (mbgTexture.empty())
709 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
711 mbgTexture = root->getStringValue("multibackground[4]");
712 if (mbgTexture.empty())
714 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
716 mbgTexture = root->getStringValue("multibackground[5]");
717 if (mbgTexture.empty())
719 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
721 mbgTexture = root->getStringValue("multibackground[6]");
722 if (mbgTexture.empty())
724 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
726 mbgTexture = root->getStringValue("multibackground[7]");
727 if (mbgTexture.empty())
729 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
736 // Create each instrument.
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);
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";
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);
761 if (x == -1 || y == -1) {
762 SG_LOG( SG_COCKPIT, SG_ALERT,
763 "x and y positions must be specified and > 0" );
767 // float w_scale = 1.0;
768 // float h_scale = 1.0;
770 // w_scale = float(real_w) / float(w);
774 // h_scale = float(real_h) / float(h);
778 SG_LOG( SG_COCKPIT, SG_BULK, "Reading instrument " << name );
780 // Warning - hardwired size!!!
781 RenderArea2D* instrument = new RenderArea2D(158, 40, 158, 40, x, y);
782 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
784 gps = new KLN89(instrument);
785 globals->add_subsystem("kln89", gps);
787 //gps->init(); // init seems to get called automagically.
788 FGSpecialInstrument* gpsinst = new FGSpecialInstrument(gps);
789 panel->addInstrument(gpsinst);
791 SG_LOG( SG_COCKPIT, SG_WARN, "Unknown special instrument found" );
794 SG_LOG( SG_COCKPIT, SG_WARN, "Skipping " << node->getName()
795 << " in instruments section" );
799 SG_LOG( SG_COCKPIT, SG_BULK, "Done reading panel instruments" );
803 // Return the new panel.
810 * Read a panel from a property list.
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).
817 * Returns 0 if the read fails for any reason.
820 fgReadPanel (istream &input)
825 readProperties(input, &root);
826 } catch (const sg_exception &e) {
827 guiErrorMessage("Error reading panel: ", e);
830 return readPanel(&root);
835 * Read a panel from a property list.
837 * This function opens a stream to a file, then invokes the
838 * main fgReadPanel() function.
841 fgReadPanel (const string &relative_path)
843 SGPath path = globals->resolve_aircraft_path(relative_path);
847 readProperties(path.str(), &root);
848 } catch (const sg_exception &e) {
849 guiErrorMessage("Error reading panel: ", e);
852 return readPanel(&root);
857 // end of panel_io.cxx