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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <string.h> // for strcmp()
31 #include <simgear/compiler.h>
32 #include <simgear/misc/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/misc/props.hxx>
42 #include <Main/globals.hxx>
43 #include <Main/fg_props.hxx>
48 #include "panel_io.hxx"
51 #include "built_in/FGMagRibbon.hxx"
53 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
54 SG_USING_STD(istream);
55 SG_USING_STD(ifstream);
61 ////////////////////////////////////////////////////////////////////////
62 // Read and construct a panel.
64 // The panel is specified as a regular property list, and each of the
65 // instruments is its own, separate property list (and thus, a separate
66 // XML document). The functions in this section read in the files
67 // as property lists, then extract properties to set up the panel
70 // A panel contains zero or more instruments.
72 // An instrument contains one or more layers and zero or more actions.
74 // A layer contains zero or more transformations.
76 // Some special types of layers also contain other objects, such as
77 // chunks of text or other layers.
79 // There are currently four types of layers:
81 // 1. Textured Layer (type="texture"), the default
82 // 2. Text Layer (type="text")
83 // 3. Switch Layer (type="switch")
84 // 4. Built-in Layer (type="built-in", must also specify class)
86 // The only built-in layer so far is the ribbon for the magnetic compass
87 // (class="compass-ribbon").
89 // There are three types of actions:
91 // 1. Adjust (type="adjust"), the default
92 // 2. Swap (type="swap")
93 // 3. Toggle (type="toggle")
95 // There are three types of transformations:
97 // 1. X shift (type="x-shift"), the default
98 // 2. Y shift (type="y-shift")
99 // 3. Rotation (type="rotation")
101 // Each of these may be associated with a property, so that a needle
102 // will rotate with the airspeed, for example, or may have a fixed
103 // floating-point value.
104 ////////////////////////////////////////////////////////////////////////
108 * Read a cropped texture from the instrument's property list.
110 * The x1 and y1 properties give the starting position of the texture
111 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
112 * ending position. For example, to use the bottom-left quarter of a
113 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
115 static FGCroppedTexture
116 readTexture (const SGPropertyNode * node)
118 FGCroppedTexture texture(node->getStringValue("path"),
119 node->getFloatValue("x1"),
120 node->getFloatValue("y1"),
121 node->getFloatValue("x2", 1.0),
122 node->getFloatValue("y2", 1.0));
123 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
129 * Test for a condition in the current node.
132 ////////////////////////////////////////////////////////////////////////
133 // Read a condition and use it if necessary.
134 ////////////////////////////////////////////////////////////////////////
137 readConditions (FGConditional * component, const SGPropertyNode * node)
139 const SGPropertyNode * conditionNode = node->getChild("condition");
140 if (conditionNode != 0)
141 // The top level is implicitly AND
142 component->setCondition(fgReadCondition(conditionNode));
147 * Read an action from the instrument's property list.
149 * The action will be performed when the user clicks a mouse button
150 * within the specified region of the instrument. Actions always work
151 * by modifying the value of a property (see the SGPropertyNode
154 * The following action types are defined:
156 * "adjust" - modify the value of a floating-point property by
157 * the increment specified. This is the default.
159 * "swap" - swap the values of two-floating-point properties.
161 * "toggle" - toggle the value of a boolean property between true and
164 * For the adjust action, it is possible to specify an increment
165 * (use a negative number for a decrement), a minimum allowed value,
166 * a maximum allowed value, and a flag to indicate whether the value
167 * should freeze or wrap-around when it reachs the minimum or maximum.
169 * The action will be scaled automatically if the instrument is not
170 * being drawn at its regular size.
172 static FGPanelAction *
173 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
175 string name = node->getStringValue("name");
177 int button = node->getIntValue("button");
178 int x = int(node->getIntValue("x") * w_scale);
179 int y = int(node->getIntValue("y") * h_scale);
180 int w = int(node->getIntValue("w") * w_scale);
181 int h = int(node->getIntValue("h") * h_scale);
182 bool repeatable = node->getBoolValue("repeatable", true);
184 FGPanelAction * action = new FGPanelAction(button, x, y, w, h, repeatable);
186 vector<SGPropertyNode_ptr>bindings = node->getChildren("binding");
189 for (i = 0; i < bindings.size(); i++) {
190 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
191 << bindings[i]->getStringValue("command"));
192 action->addBinding(new FGBinding(bindings[i]), 0);
195 if (node->hasChild("mod-up")) {
196 bindings = node->getChild("mod-up")->getChildren("binding");
197 for (i = 0; i < bindings.size(); i++) {
198 action->addBinding(new FGBinding(bindings[i]), 1);
202 readConditions(action, node);
208 * Read a transformation from the instrument's property list.
210 * The panel module uses the transformations to slide or spin needles,
211 * knobs, and other indicators, and to place layers in the correct
212 * positions. Every layer starts centered exactly on the x,y co-ordinate,
213 * and many layers need to be moved or rotated simply to display the
214 * instrument correctly.
216 * There are three types of transformations:
218 * "x-shift" - move the layer horizontally.
220 * "y-shift" - move the layer vertically.
222 * "rotation" - rotate the layer.
224 * Each transformation may have a fixed offset, and may also have
225 * a floating-point property value to add to the offset. The
226 * floating-point property may be clamped to a minimum and/or
227 * maximum range and scaled (after clamping).
229 * Note that because of the way OpenGL works, transformations will
230 * appear to be applied backwards.
232 static FGPanelTransformation *
233 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
235 FGPanelTransformation * t = new FGPanelTransformation;
237 string name = node->getName();
238 string type = node->getStringValue("type");
239 string propName = node->getStringValue("property", "");
240 SGPropertyNode * target = 0;
243 SG_LOG( SG_COCKPIT, SG_ALERT,
244 "No type supplied for transformation " << name
245 << " assuming \"rotation\"" );
249 if (!propName.empty()) {
250 target = fgGetNode(propName.c_str(), true);
254 t->min = node->getFloatValue("min", -9999999);
255 t->max = node->getFloatValue("max", 99999999);
256 t->factor = node->getFloatValue("scale", 1.0);
257 t->offset = node->getFloatValue("offset", 0.0);
259 // Check for an interpolation table
260 const SGPropertyNode * trans_table = node->getNode("interpolation");
261 if (trans_table != 0) {
262 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
263 << trans_table->nChildren() << "children" );
264 t->table = new SGInterpTable();
265 for(int i = 0; i < trans_table->nChildren(); i++) {
266 const SGPropertyNode * node = trans_table->getChild(i);
267 if (!strcmp(node->getName(), "entry")) {
268 double ind = node->getDoubleValue("ind", 0.0);
269 double dep = node->getDoubleValue("dep", 0.0);
270 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
271 << ind << "==>" << dep );
272 t->table->addEntry(ind, dep);
274 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
275 << " in interpolation" );
282 // Move the layer horizontally.
283 if (type == "x-shift") {
284 t->type = FGPanelTransformation::XSHIFT;
285 // t->min *= w_scale; //removed by Martin Dressler
286 // t->max *= w_scale; //removed by Martin Dressler
287 t->offset *= w_scale;
288 t->factor *= w_scale; //Added by Martin Dressler
291 // Move the layer vertically.
292 else if (type == "y-shift") {
293 t->type = FGPanelTransformation::YSHIFT;
294 //t->min *= h_scale; //removed
295 //t->max *= h_scale; //removed
296 t->offset *= h_scale;
297 t->factor *= h_scale; //Added
300 // Rotate the layer. The rotation
301 // is in degrees, and does not need
302 // to scale with the instrument size.
303 else if (type == "rotation") {
304 t->type = FGPanelTransformation::ROTATION;
308 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
313 readConditions(t, node);
314 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
320 * Read a chunk of text from the instrument's property list.
322 * A text layer consists of one or more chunks of text. All chunks
323 * share the same font size and color (and eventually, font), but
324 * each can come from a different source. There are three types of
327 * "literal" - a literal text string (the default)
329 * "text-value" - the current value of a string property
331 * "number-value" - the current value of a floating-point property.
333 * All three may also include a printf-style format string.
336 readTextChunk (const SGPropertyNode * node)
338 FGTextLayer::Chunk * chunk;
339 string name = node->getStringValue("name");
340 string type = node->getStringValue("type");
341 string format = node->getStringValue("format");
343 // Default to literal text.
345 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
346 << " assuming \"literal\"");
350 // A literal text string.
351 if (type == "literal") {
352 string text = node->getStringValue("text");
353 chunk = new FGTextLayer::Chunk(text, format);
356 // The value of a string property.
357 else if (type == "text-value") {
358 SGPropertyNode * target =
359 fgGetNode(node->getStringValue("property"), true);
360 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
363 // The value of a float property.
364 else if (type == "number-value") {
365 string propName = node->getStringValue("property");
366 float scale = node->getFloatValue("scale", 1.0);
367 SGPropertyNode * target = fgGetNode(propName.c_str(), true);
368 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
374 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
375 << " for text chunk " << name );
379 readConditions(chunk, node);
385 * Read a single layer from an instrument's property list.
387 * Each instrument consists of one or more layers stacked on top
388 * of each other; the lower layers show through only where the upper
389 * layers contain an alpha component. Each layer can be moved
390 * horizontally and vertically and rotated using transformations.
392 * This module currently recognizes four kinds of layers:
394 * "texture" - a layer containing a texture (the default)
396 * "text" - a layer containing text
398 * "switch" - a layer that switches between two other layers
399 * based on the current value of a boolean property.
401 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
403 * Currently, the only built-in layer class is "compass-ribbon".
405 static FGInstrumentLayer *
406 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
408 FGInstrumentLayer * layer = NULL;
409 string name = node->getStringValue("name");
410 string type = node->getStringValue("type");
411 int w = node->getIntValue("w", -1);
412 int h = node->getIntValue("h", -1);
414 w = int(w * w_scale);
416 h = int(h * h_scale);
420 SG_LOG( SG_COCKPIT, SG_ALERT,
421 "No type supplied for layer " << name
422 << " assuming \"texture\"" );
427 // A textured instrument layer.
428 if (type == "texture") {
429 FGCroppedTexture texture = readTexture(node->getNode("texture"));
430 layer = new FGTexturedLayer(texture, w, h);
433 // A group of sublayers.
434 else if (type == "group") {
435 layer = new FGGroupLayer();
436 for (int i = 0; i < node->nChildren(); i++) {
437 const SGPropertyNode * child = node->getChild(i);
438 cerr << "Trying child " << child->getName() << endl;
439 if (!strcmp(child->getName(), "layer")) {
440 cerr << "succeeded!" << endl;
441 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
447 // A textual instrument layer.
448 else if (type == "text") {
449 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
451 // Set the text color.
452 float red = node->getFloatValue("color/red", 0.0);
453 float green = node->getFloatValue("color/green", 0.0);
454 float blue = node->getFloatValue("color/blue", 0.0);
455 tlayer->setColor(red, green, blue);
457 // Set the point size.
458 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
459 tlayer->setPointSize(pointSize);
462 string fontName = node->getStringValue("font", "default");
463 tlayer->setFontName(fontName);
465 const SGPropertyNode * chunk_group = node->getNode("chunks");
466 if (chunk_group != 0) {
467 int nChunks = chunk_group->nChildren();
468 for (int i = 0; i < nChunks; i++) {
469 const SGPropertyNode * node = chunk_group->getChild(i);
470 if (!strcmp(node->getName(), "chunk")) {
471 FGTextLayer::Chunk * chunk = readTextChunk(node);
473 tlayer->addChunk(chunk);
475 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
483 // A switch instrument layer.
484 else if (type == "switch") {
485 SGPropertyNode * target =
486 fgGetNode(node->getStringValue("property"), true);
487 FGInstrumentLayer * layer1 =
488 readLayer(node->getNode("layer[0]"), w_scale, h_scale);
489 FGInstrumentLayer * layer2 =
490 readLayer(node->getNode("layer[1]"), w_scale, h_scale);
491 layer = new FGSwitchLayer(w, h, target, layer1, layer2);
494 // A built-in instrument layer.
495 else if (type == "built-in") {
496 string layerclass = node->getStringValue("class");
498 if (layerclass == "mag-ribbon") {
499 layer = new FGMagRibbon(w, h);
502 else if (layerclass.empty()) {
503 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
509 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
517 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
523 // Get the transformations for each layer.
525 const SGPropertyNode * trans_group = node->getNode("transformations");
526 if (trans_group != 0) {
527 int nTransformations = trans_group->nChildren();
528 for (int i = 0; i < nTransformations; i++) {
529 const SGPropertyNode * node = trans_group->getChild(i);
530 if (!strcmp(node->getName(), "transformation")) {
531 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
533 layer->addTransformation(t);
535 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
536 << " in transformations" );
541 readConditions(layer, node);
542 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
548 * Read an instrument from a property list.
550 * The instrument consists of a preferred width and height
551 * (the panel may override these), together with a list of layers
552 * and a list of actions to be performed when the user clicks
553 * the mouse over the instrument. All co-ordinates are relative
554 * to the instrument's position, so instruments are fully relocatable;
555 * likewise, co-ordinates for actions and transformations will be
556 * scaled automatically if the instrument is not at its preferred size.
558 static FGPanelInstrument *
559 readInstrument (const SGPropertyNode * node)
561 const string name = node->getStringValue("name");
562 int x = node->getIntValue("x", -1);
563 int y = node->getIntValue("y", -1);
564 int real_w = node->getIntValue("w", -1);
565 int real_h = node->getIntValue("h", -1);
566 int w = node->getIntValue("w-base", -1);
567 int h = node->getIntValue("h-base", -1);
569 if (x == -1 || y == -1) {
570 SG_LOG( SG_COCKPIT, SG_ALERT,
571 "x and y positions must be specified and > 0" );
578 w_scale = float(real_w) / float(w);
582 h_scale = float(real_h) / float(h);
586 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
588 FGLayeredInstrument * instrument =
589 new FGLayeredInstrument(x, y, w, h);
592 // Get the actions for the instrument.
594 const SGPropertyNode * action_group = node->getNode("actions");
595 if (action_group != 0) {
596 int nActions = action_group->nChildren();
597 for (int i = 0; i < nActions; i++) {
598 const SGPropertyNode * node = action_group->getChild(i);
599 if (!strcmp(node->getName(), "action")) {
600 FGPanelAction * action = readAction(node, w_scale, h_scale);
602 instrument->addAction(action);
604 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
611 // Get the layers for the instrument.
613 const SGPropertyNode * layer_group = node->getNode("layers");
614 if (layer_group != 0) {
615 int nLayers = layer_group->nChildren();
616 for (int i = 0; i < nLayers; i++) {
617 const SGPropertyNode * node = layer_group->getChild(i);
618 if (!strcmp(node->getName(), "layer")) {
619 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
621 instrument->addLayer(layer);
623 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
629 readConditions(instrument, node);
630 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
636 * Construct the panel from a property tree.
639 readPanel (const SGPropertyNode * root)
641 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
642 root->getStringValue("name", "[Unnamed Panel]") );
644 FGPanel * panel = new FGPanel();
645 panel->setWidth(root->getIntValue("w", 1024));
646 panel->setHeight(root->getIntValue("h", 443));
649 // Grab the visible external viewing area, default to
651 panel->setViewHeight(root->getIntValue("view-height",
652 768 - panel->getHeight() + 2));
655 // Grab the panel's initial offsets, default to 0, 0.
657 if (!fgHasNode("/sim/panel/x-offset"))
658 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
660 // conditional removed by jim wilson to allow panel xml code
661 // with y-offset defined to work...
662 if (!fgHasNode("/sim/panel/y-offset"))
663 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
666 // Assign the background texture, if any, or a bogus chequerboard.
668 string bgTexture = root->getStringValue("background");
669 if (bgTexture.empty())
671 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
672 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
675 // Get multibackground if any...
677 string mbgTexture = root->getStringValue("multibackground[0]");
678 if (!mbgTexture.empty()) {
679 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0);
680 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
682 mbgTexture = root->getStringValue("multibackground[1]");
683 if (mbgTexture.empty())
685 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
686 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
688 mbgTexture = root->getStringValue("multibackground[2]");
689 if (mbgTexture.empty())
691 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
692 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
694 mbgTexture = root->getStringValue("multibackground[3]");
695 if (mbgTexture.empty())
697 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
698 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
700 mbgTexture = root->getStringValue("multibackground[4]");
701 if (mbgTexture.empty())
703 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
704 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
706 mbgTexture = root->getStringValue("multibackground[5]");
707 if (mbgTexture.empty())
709 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
710 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
712 mbgTexture = root->getStringValue("multibackground[6]");
713 if (mbgTexture.empty())
715 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
716 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
718 mbgTexture = root->getStringValue("multibackground[7]");
719 if (mbgTexture.empty())
721 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
722 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
729 // Create each instrument.
731 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
732 const SGPropertyNode * instrument_group = root->getChild("instruments");
733 if (instrument_group != 0) {
734 int nInstruments = instrument_group->nChildren();
735 for (int i = 0; i < nInstruments; i++) {
736 const SGPropertyNode * node = instrument_group->getChild(i);
737 if (!strcmp(node->getName(), "instrument")) {
738 FGPanelInstrument * instrument = readInstrument(node);
740 panel->addInstrument(instrument);
742 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
743 << " in instruments section" );
747 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
751 // Return the new panel.
758 * Read a panel from a property list.
760 * Each panel instrument will appear in its own, separate
761 * property list. The top level simply names the panel and
762 * places the instruments in their appropriate locations (and
763 * optionally resizes them if necessary).
765 * Returns 0 if the read fails for any reason.
768 fgReadPanel (istream &input)
773 readProperties(input, &root);
774 } catch (const sg_exception &e) {
775 guiErrorMessage("Error reading panel: ", e);
778 return readPanel(&root);
783 * Read a panel from a property list.
785 * This function opens a stream to a file, then invokes the
786 * main fgReadPanel() function.
789 fgReadPanel (const string &relative_path)
791 SGPath path(globals->get_fg_root());
792 path.append(relative_path);
796 readProperties(path.str(), &root);
797 } catch (const sg_exception &e) {
798 guiErrorMessage("Error reading panel: ", e);
801 return readPanel(&root);
806 // end of panel_io.cxx