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>
49 #include "panel_io.hxx"
52 #include "built_in/FGMagRibbon.hxx"
54 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
55 SG_USING_STD(istream);
56 SG_USING_STD(ifstream);
62 ////////////////////////////////////////////////////////////////////////
63 // Read and construct a panel.
65 // The panel is specified as a regular property list, and each of the
66 // instruments is its own, separate property list (and thus, a separate
67 // XML document). The functions in this section read in the files
68 // as property lists, then extract properties to set up the panel
71 // A panel contains zero or more instruments.
73 // An instrument contains one or more layers and zero or more actions.
75 // A layer contains zero or more transformations.
77 // Some special types of layers also contain other objects, such as
78 // chunks of text or other layers.
80 // There are currently four types of layers:
82 // 1. Textured Layer (type="texture"), the default
83 // 2. Text Layer (type="text")
84 // 3. Switch Layer (type="switch")
85 // 4. Built-in Layer (type="built-in", must also specify class)
87 // The only built-in layer so far is the ribbon for the magnetic compass
88 // (class="compass-ribbon").
90 // There are three types of actions:
92 // 1. Adjust (type="adjust"), the default
93 // 2. Swap (type="swap")
94 // 3. Toggle (type="toggle")
96 // There are three types of transformations:
98 // 1. X shift (type="x-shift"), the default
99 // 2. Y shift (type="y-shift")
100 // 3. Rotation (type="rotation")
102 // Each of these may be associated with a property, so that a needle
103 // will rotate with the airspeed, for example, or may have a fixed
104 // floating-point value.
105 ////////////////////////////////////////////////////////////////////////
109 * Read a cropped texture from the instrument's property list.
111 * The x1 and y1 properties give the starting position of the texture
112 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
113 * ending position. For example, to use the bottom-left quarter of a
114 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
116 static FGCroppedTexture
117 readTexture (const SGPropertyNode * node)
119 FGCroppedTexture texture(node->getStringValue("path"),
120 node->getFloatValue("x1"),
121 node->getFloatValue("y1"),
122 node->getFloatValue("x2", 1.0),
123 node->getFloatValue("y2", 1.0));
124 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
130 * Test for a condition in the current node.
133 ////////////////////////////////////////////////////////////////////////
134 // Read a condition and use it if necessary.
135 ////////////////////////////////////////////////////////////////////////
138 readConditions (FGConditional * component, const SGPropertyNode * node)
140 const SGPropertyNode * conditionNode = node->getChild("condition");
141 if (conditionNode != 0)
142 // The top level is implicitly AND
143 component->setCondition(fgReadCondition(conditionNode));
148 * Read an action from the instrument's property list.
150 * The action will be performed when the user clicks a mouse button
151 * within the specified region of the instrument. Actions always work
152 * by modifying the value of a property (see the SGPropertyNode
155 * The following action types are defined:
157 * "adjust" - modify the value of a floating-point property by
158 * the increment specified. This is the default.
160 * "swap" - swap the values of two-floating-point properties.
162 * "toggle" - toggle the value of a boolean property between true and
165 * For the adjust action, it is possible to specify an increment
166 * (use a negative number for a decrement), a minimum allowed value,
167 * a maximum allowed value, and a flag to indicate whether the value
168 * should freeze or wrap-around when it reachs the minimum or maximum.
170 * The action will be scaled automatically if the instrument is not
171 * being drawn at its regular size.
173 static FGPanelAction *
174 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
176 string name = node->getStringValue("name");
178 int button = node->getIntValue("button");
179 int x = int(node->getIntValue("x") * w_scale);
180 int y = int(node->getIntValue("y") * h_scale);
181 int w = int(node->getIntValue("w") * w_scale);
182 int h = int(node->getIntValue("h") * h_scale);
184 FGPanelAction * action = new FGPanelAction(button, x, y, w, h);
186 vector<SGPropertyNode_ptr>bindings = node->getChildren("binding");
187 for (unsigned int i = 0; i < bindings.size(); i++) {
188 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
189 << bindings[i]->getStringValue("command"));
190 action->addBinding(new FGBinding(bindings[i])); // TODO: allow modifiers
193 readConditions(action, node);
199 * Read a transformation from the instrument's property list.
201 * The panel module uses the transformations to slide or spin needles,
202 * knobs, and other indicators, and to place layers in the correct
203 * positions. Every layer starts centered exactly on the x,y co-ordinate,
204 * and many layers need to be moved or rotated simply to display the
205 * instrument correctly.
207 * There are three types of transformations:
209 * "x-shift" - move the layer horizontally.
211 * "y-shift" - move the layer vertically.
213 * "rotation" - rotate the layer.
215 * Each transformation may have a fixed offset, and may also have
216 * a floating-point property value to add to the offset. The
217 * floating-point property may be clamped to a minimum and/or
218 * maximum range and scaled (after clamping).
220 * Note that because of the way OpenGL works, transformations will
221 * appear to be applied backwards.
223 static FGPanelTransformation *
224 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
226 FGPanelTransformation * t = new FGPanelTransformation;
228 string name = node->getName();
229 string type = node->getStringValue("type");
230 string propName = node->getStringValue("property", "");
231 SGPropertyNode * target = 0;
234 SG_LOG( SG_COCKPIT, SG_ALERT,
235 "No type supplied for transformation " << name
236 << " assuming \"rotation\"" );
240 if (!propName.empty()) {
241 target = fgGetNode(propName.c_str(), true);
245 t->min = node->getFloatValue("min", -9999999);
246 t->max = node->getFloatValue("max", 99999999);
247 t->factor = node->getFloatValue("scale", 1.0);
248 t->offset = node->getFloatValue("offset", 0.0);
250 // Check for an interpolation table
251 const SGPropertyNode * trans_table = node->getNode("interpolation");
252 if (trans_table != 0) {
253 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
254 << trans_table->nChildren() << "children" );
255 t->table = new SGInterpTable();
256 for(int i = 0; i < trans_table->nChildren(); i++) {
257 const SGPropertyNode * node = trans_table->getChild(i);
258 if (!strcmp(node->getName(), "entry")) {
259 double ind = node->getDoubleValue("ind", 0.0);
260 double dep = node->getDoubleValue("dep", 0.0);
261 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
262 << ind << "==>" << dep );
263 t->table->addEntry(ind, dep);
265 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
266 << " in interpolation" );
273 // Move the layer horizontally.
274 if (type == "x-shift") {
275 t->type = FGPanelTransformation::XSHIFT;
276 // t->min *= w_scale; //removed by Martin Dressler
277 // t->max *= w_scale; //removed by Martin Dressler
278 t->offset *= w_scale;
279 t->factor *= w_scale; //Added by Martin Dressler
282 // Move the layer vertically.
283 else if (type == "y-shift") {
284 t->type = FGPanelTransformation::YSHIFT;
285 //t->min *= h_scale; //removed
286 //t->max *= h_scale; //removed
287 t->offset *= h_scale;
288 t->factor *= h_scale; //Added
291 // Rotate the layer. The rotation
292 // is in degrees, and does not need
293 // to scale with the instrument size.
294 else if (type == "rotation") {
295 t->type = FGPanelTransformation::ROTATION;
299 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
304 readConditions(t, node);
305 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
311 * Read a chunk of text from the instrument's property list.
313 * A text layer consists of one or more chunks of text. All chunks
314 * share the same font size and color (and eventually, font), but
315 * each can come from a different source. There are three types of
318 * "literal" - a literal text string (the default)
320 * "text-value" - the current value of a string property
322 * "number-value" - the current value of a floating-point property.
324 * All three may also include a printf-style format string.
327 readTextChunk (const SGPropertyNode * node)
329 FGTextLayer::Chunk * chunk;
330 string name = node->getStringValue("name");
331 string type = node->getStringValue("type");
332 string format = node->getStringValue("format");
334 // Default to literal text.
336 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
337 << " assuming \"literal\"");
341 // A literal text string.
342 if (type == "literal") {
343 string text = node->getStringValue("text");
344 chunk = new FGTextLayer::Chunk(text, format);
347 // The value of a string property.
348 else if (type == "text-value") {
349 SGPropertyNode * target =
350 fgGetNode(node->getStringValue("property"), true);
351 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
354 // The value of a float property.
355 else if (type == "number-value") {
356 string propName = node->getStringValue("property");
357 float scale = node->getFloatValue("scale", 1.0);
358 SGPropertyNode * target = fgGetNode(propName.c_str(), true);
359 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
365 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
366 << " for text chunk " << name );
370 readConditions(chunk, node);
376 * Read a single layer from an instrument's property list.
378 * Each instrument consists of one or more layers stacked on top
379 * of each other; the lower layers show through only where the upper
380 * layers contain an alpha component. Each layer can be moved
381 * horizontally and vertically and rotated using transformations.
383 * This module currently recognizes four kinds of layers:
385 * "texture" - a layer containing a texture (the default)
387 * "text" - a layer containing text
389 * "switch" - a layer that switches between two other layers
390 * based on the current value of a boolean property.
392 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
394 * Currently, the only built-in layer class is "compass-ribbon".
396 static FGInstrumentLayer *
397 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
399 FGInstrumentLayer * layer = NULL;
400 string name = node->getStringValue("name");
401 string type = node->getStringValue("type");
402 int w = node->getIntValue("w", -1);
403 int h = node->getIntValue("h", -1);
405 w = int(w * w_scale);
407 h = int(h * h_scale);
411 SG_LOG( SG_COCKPIT, SG_ALERT,
412 "No type supplied for layer " << name
413 << " assuming \"texture\"" );
418 // A textured instrument layer.
419 if (type == "texture") {
420 FGCroppedTexture texture = readTexture(node->getNode("texture"));
421 layer = new FGTexturedLayer(texture, w, h);
424 // A group of sublayers.
425 else if (type == "group") {
426 layer = new FGGroupLayer();
427 for (int i = 0; i < node->nChildren(); i++) {
428 const SGPropertyNode * child = node->getChild(i);
429 cerr << "Trying child " << child->getName() << endl;
430 if (!strcmp(child->getName(), "layer")) {
431 cerr << "succeeded!" << endl;
432 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
438 // A textual instrument layer.
439 else if (type == "text") {
440 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
442 // Set the text color.
443 float red = node->getFloatValue("color/red", 0.0);
444 float green = node->getFloatValue("color/green", 0.0);
445 float blue = node->getFloatValue("color/blue", 0.0);
446 tlayer->setColor(red, green, blue);
448 // Set the point size.
449 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
450 tlayer->setPointSize(pointSize);
453 string fontName = node->getStringValue("font", "default");
454 tlayer->setFontName(fontName);
456 const SGPropertyNode * chunk_group = node->getNode("chunks");
457 if (chunk_group != 0) {
458 int nChunks = chunk_group->nChildren();
459 for (int i = 0; i < nChunks; i++) {
460 const SGPropertyNode * node = chunk_group->getChild(i);
461 if (!strcmp(node->getName(), "chunk")) {
462 FGTextLayer::Chunk * chunk = readTextChunk(node);
464 tlayer->addChunk(chunk);
466 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
474 // A switch instrument layer.
475 else if (type == "switch") {
476 SGPropertyNode * target =
477 fgGetNode(node->getStringValue("property"), true);
478 FGInstrumentLayer * layer1 =
479 readLayer(node->getNode("layer[0]"), w_scale, h_scale);
480 FGInstrumentLayer * layer2 =
481 readLayer(node->getNode("layer[1]"), w_scale, h_scale);
482 layer = new FGSwitchLayer(w, h, target, layer1, layer2);
485 // A built-in instrument layer.
486 else if (type == "built-in") {
487 string layerclass = node->getStringValue("class");
489 if (layerclass == "mag-ribbon") {
490 layer = new FGMagRibbon(w, h);
493 else if (layerclass.empty()) {
494 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
500 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
508 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
514 // Get the transformations for each layer.
516 const SGPropertyNode * trans_group = node->getNode("transformations");
517 if (trans_group != 0) {
518 int nTransformations = trans_group->nChildren();
519 for (int i = 0; i < nTransformations; i++) {
520 const SGPropertyNode * node = trans_group->getChild(i);
521 if (!strcmp(node->getName(), "transformation")) {
522 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
524 layer->addTransformation(t);
526 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
527 << " in transformations" );
532 readConditions(layer, node);
533 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
539 * Read an instrument from a property list.
541 * The instrument consists of a preferred width and height
542 * (the panel may override these), together with a list of layers
543 * and a list of actions to be performed when the user clicks
544 * the mouse over the instrument. All co-ordinates are relative
545 * to the instrument's position, so instruments are fully relocatable;
546 * likewise, co-ordinates for actions and transformations will be
547 * scaled automatically if the instrument is not at its preferred size.
549 static FGPanelInstrument *
550 readInstrument (const SGPropertyNode * node)
552 const string &name = node->getStringValue("name");
553 int x = node->getIntValue("x", -1);
554 int y = node->getIntValue("y", -1);
555 int real_w = node->getIntValue("w", -1);
556 int real_h = node->getIntValue("h", -1);
557 int w = node->getIntValue("w-base", -1);
558 int h = node->getIntValue("h-base", -1);
560 if (x == -1 || y == -1) {
561 SG_LOG( SG_COCKPIT, SG_ALERT,
562 "x and y positions must be specified and > 0" );
569 w_scale = float(real_w) / float(w);
573 h_scale = float(real_h) / float(h);
577 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
579 FGLayeredInstrument * instrument =
580 new FGLayeredInstrument(x, y, w, h);
583 // Get the actions for the instrument.
585 const SGPropertyNode * action_group = node->getNode("actions");
586 if (action_group != 0) {
587 int nActions = action_group->nChildren();
588 for (int i = 0; i < nActions; i++) {
589 const SGPropertyNode * node = action_group->getChild(i);
590 if (!strcmp(node->getName(), "action")) {
591 FGPanelAction * action = readAction(node, w_scale, h_scale);
593 instrument->addAction(action);
595 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
602 // Get the layers for the instrument.
604 const SGPropertyNode * layer_group = node->getNode("layers");
605 if (layer_group != 0) {
606 int nLayers = layer_group->nChildren();
607 for (int i = 0; i < nLayers; i++) {
608 const SGPropertyNode * node = layer_group->getChild(i);
609 if (!strcmp(node->getName(), "layer")) {
610 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
612 instrument->addLayer(layer);
614 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
620 readConditions(instrument, node);
621 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
627 * Construct the panel from a property tree.
630 readPanel (const SGPropertyNode * root)
632 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
633 root->getStringValue("name", "[Unnamed Panel]") );
635 FGPanel * panel = new FGPanel();
636 panel->setWidth(root->getIntValue("w", 1024));
637 panel->setHeight(root->getIntValue("h", 443));
640 // Grab the visible external viewing area, default to
642 panel->setViewHeight(root->getIntValue("view-height",
643 768 - panel->getHeight() + 2));
646 // Grab the panel's initial offsets, default to 0, 0.
648 if (!fgHasNode("/sim/panel/x-offset"))
649 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
651 // conditional removed by jim wilson to allow panel xml code
652 // with y-offset defined to work...
653 if (!fgHasNode("/sim/panel/y-offset"))
654 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
657 // Assign the background texture, if any, or a bogus chequerboard.
659 string bgTexture = root->getStringValue("background");
660 if (bgTexture.empty())
662 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
663 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
666 // Get multibackground if any...
668 string mbgTexture = root->getStringValue("multibackground[0]");
669 if (!mbgTexture.empty()) {
670 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0);
671 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
673 mbgTexture = root->getStringValue("multibackground[1]");
674 if (mbgTexture.empty())
676 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
677 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
679 mbgTexture = root->getStringValue("multibackground[2]");
680 if (mbgTexture.empty())
682 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
683 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
685 mbgTexture = root->getStringValue("multibackground[3]");
686 if (mbgTexture.empty())
688 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
689 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
691 mbgTexture = root->getStringValue("multibackground[4]");
692 if (mbgTexture.empty())
694 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
695 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
697 mbgTexture = root->getStringValue("multibackground[5]");
698 if (mbgTexture.empty())
700 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
701 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
703 mbgTexture = root->getStringValue("multibackground[6]");
704 if (mbgTexture.empty())
706 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
707 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
709 mbgTexture = root->getStringValue("multibackground[7]");
710 if (mbgTexture.empty())
712 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
713 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
720 // Create each instrument.
722 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
723 const SGPropertyNode * instrument_group = root->getChild("instruments");
724 if (instrument_group != 0) {
725 int nInstruments = instrument_group->nChildren();
726 for (int i = 0; i < nInstruments; i++) {
727 const SGPropertyNode * node = instrument_group->getChild(i);
728 if (!strcmp(node->getName(), "instrument")) {
729 FGPanelInstrument * instrument = readInstrument(node);
731 panel->addInstrument(instrument);
733 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
734 << " in instruments section" );
738 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
742 // Return the new panel.
749 * Read a panel from a property list.
751 * Each panel instrument will appear in its own, separate
752 * property list. The top level simply names the panel and
753 * places the instruments in their appropriate locations (and
754 * optionally resizes them if necessary).
756 * Returns 0 if the read fails for any reason.
759 fgReadPanel (istream &input)
764 readProperties(input, &root);
765 } catch (const sg_exception &e) {
766 guiErrorMessage("Error reading panel: ", e);
769 return readPanel(&root);
774 * Read a panel from a property list.
776 * This function opens a stream to a file, then invokes the
777 * main fgReadPanel() function.
780 fgReadPanel (const string &relative_path)
782 SGPath path(globals->get_fg_root());
783 path.append(relative_path);
787 readProperties(path.str(), &root);
788 } catch (const sg_exception &e) {
789 guiErrorMessage("Error reading panel: ", e);
792 return readPanel(&root);
797 // end of panel_io.cxx