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/structure/exception.hxx>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/props/props.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/fg_props.hxx>
46 // #include "panel.hxx"
47 #include "panel_io.hxx"
50 #include "built_in/FGMagRibbon.hxx"
52 SG_USING_STD(istream);
53 SG_USING_STD(ifstream);
58 ////////////////////////////////////////////////////////////////////////
59 // Read and construct a panel.
61 // The panel is specified as a regular property list, and each of the
62 // instruments is its own, separate property list (and thus, a separate
63 // XML document). The functions in this section read in the files
64 // as property lists, then extract properties to set up the panel
67 // A panel contains zero or more instruments.
69 // An instrument contains one or more layers and zero or more actions.
71 // A layer contains zero or more transformations.
73 // Some special types of layers also contain other objects, such as
74 // chunks of text or other layers.
76 // There are currently four types of layers:
78 // 1. Textured Layer (type="texture"), the default
79 // 2. Text Layer (type="text")
80 // 3. Switch Layer (type="switch")
81 // 4. Built-in Layer (type="built-in", must also specify class)
83 // The only built-in layer so far is the ribbon for the magnetic compass
84 // (class="compass-ribbon").
86 // There are three types of actions:
88 // 1. Adjust (type="adjust"), the default
89 // 2. Swap (type="swap")
90 // 3. Toggle (type="toggle")
92 // There are three types of transformations:
94 // 1. X shift (type="x-shift"), the default
95 // 2. Y shift (type="y-shift")
96 // 3. Rotation (type="rotation")
98 // Each of these may be associated with a property, so that a needle
99 // will rotate with the airspeed, for example, or may have a fixed
100 // floating-point value.
101 ////////////////////////////////////////////////////////////////////////
105 * Read a cropped texture from the instrument's property list.
107 * The x1 and y1 properties give the starting position of the texture
108 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
109 * ending position. For example, to use the bottom-left quarter of a
110 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
112 static FGCroppedTexture
113 readTexture (const SGPropertyNode * node)
115 FGCroppedTexture texture(node->getStringValue("path"),
116 node->getFloatValue("x1"),
117 node->getFloatValue("y1"),
118 node->getFloatValue("x2", 1.0),
119 node->getFloatValue("y2", 1.0));
120 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
126 * Test for a condition in the current node.
129 ////////////////////////////////////////////////////////////////////////
130 // Read a condition and use it if necessary.
131 ////////////////////////////////////////////////////////////////////////
134 readConditions (SGConditional *component, const SGPropertyNode *node)
136 const SGPropertyNode * conditionNode = node->getChild("condition");
137 if (conditionNode != 0)
138 // The top level is implicitly AND
139 component->setCondition(sgReadCondition(globals->get_props(),
145 * Read an action from the instrument's property list.
147 * The action will be performed when the user clicks a mouse button
148 * within the specified region of the instrument. Actions always work
149 * by modifying the value of a property (see the SGPropertyNode
152 * The following action types are defined:
154 * "adjust" - modify the value of a floating-point property by
155 * the increment specified. This is the default.
157 * "swap" - swap the values of two-floating-point properties.
159 * "toggle" - toggle the value of a boolean property between true and
162 * For the adjust action, it is possible to specify an increment
163 * (use a negative number for a decrement), a minimum allowed value,
164 * a maximum allowed value, and a flag to indicate whether the value
165 * should freeze or wrap-around when it reachs the minimum or maximum.
167 * The action will be scaled automatically if the instrument is not
168 * being drawn at its regular size.
170 static FGPanelAction *
171 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
173 string name = node->getStringValue("name");
175 int button = node->getIntValue("button");
176 int x = int(node->getIntValue("x") * w_scale);
177 int y = int(node->getIntValue("y") * h_scale);
178 int w = int(node->getIntValue("w") * w_scale);
179 int h = int(node->getIntValue("h") * h_scale);
180 bool repeatable = node->getBoolValue("repeatable", true);
182 FGPanelAction * action = new FGPanelAction(button, x, y, w, h, repeatable);
184 vector<SGPropertyNode_ptr>bindings = node->getChildren("binding");
185 SGPropertyNode * dest = fgGetNode("/sim/bindings", true);
187 SGPropertyNode *binding;
189 for (i = 0; i < bindings.size(); i++) {
190 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
191 << bindings[i]->getStringValue("command"));
194 while (dest->getChild("binding", j))
197 binding = dest->getChild("binding", j, true);
198 copyProperties(bindings[i], binding);
199 action->addBinding(new FGBinding(binding), 0);
202 if (node->hasChild("mod-up")) {
203 bindings = node->getChild("mod-up")->getChildren("binding");
204 for (i = 0; i < bindings.size(); i++) {
206 while (dest->getChild("binding", j))
209 binding = dest->getChild("binding", j, true);
210 copyProperties(bindings[i], binding);
211 action->addBinding(new FGBinding(binding), 1);
215 readConditions(action, node);
221 * Read a transformation from the instrument's property list.
223 * The panel module uses the transformations to slide or spin needles,
224 * knobs, and other indicators, and to place layers in the correct
225 * positions. Every layer starts centered exactly on the x,y co-ordinate,
226 * and many layers need to be moved or rotated simply to display the
227 * instrument correctly.
229 * There are three types of transformations:
231 * "x-shift" - move the layer horizontally.
233 * "y-shift" - move the layer vertically.
235 * "rotation" - rotate the layer.
237 * Each transformation may have a fixed offset, and may also have
238 * a floating-point property value to add to the offset. The
239 * floating-point property may be clamped to a minimum and/or
240 * maximum range and scaled (after clamping).
242 * Note that because of the way OpenGL works, transformations will
243 * appear to be applied backwards.
245 static FGPanelTransformation *
246 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
248 FGPanelTransformation * t = new FGPanelTransformation;
250 string name = node->getName();
251 string type = node->getStringValue("type");
252 string propName = node->getStringValue("property", "");
253 SGPropertyNode * target = 0;
256 SG_LOG( SG_COCKPIT, SG_INFO,
257 "No type supplied for transformation " << name
258 << " assuming \"rotation\"" );
262 if (!propName.empty()) {
263 target = fgGetNode(propName.c_str(), true);
267 t->min = node->getFloatValue("min", -9999999);
268 t->max = node->getFloatValue("max", 99999999);
269 t->has_mod = node->hasChild("modulator");
271 t->mod = node->getFloatValue("modulator");
272 t->factor = node->getFloatValue("scale", 1.0);
273 t->offset = node->getFloatValue("offset", 0.0);
275 // Check for an interpolation table
276 const SGPropertyNode * trans_table = node->getNode("interpolation");
277 if (trans_table != 0) {
278 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
279 << trans_table->nChildren() << "children" );
280 t->table = new SGInterpTable();
281 for(int i = 0; i < trans_table->nChildren(); i++) {
282 const SGPropertyNode * node = trans_table->getChild(i);
283 if (!strcmp(node->getName(), "entry")) {
284 double ind = node->getDoubleValue("ind", 0.0);
285 double dep = node->getDoubleValue("dep", 0.0);
286 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
287 << ind << "==>" << dep );
288 t->table->addEntry(ind, dep);
290 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
291 << " in interpolation" );
298 // Move the layer horizontally.
299 if (type == "x-shift") {
300 t->type = FGPanelTransformation::XSHIFT;
301 // t->min *= w_scale; //removed by Martin Dressler
302 // t->max *= w_scale; //removed by Martin Dressler
303 t->offset *= w_scale;
304 t->factor *= w_scale; //Added by Martin Dressler
307 // Move the layer vertically.
308 else if (type == "y-shift") {
309 t->type = FGPanelTransformation::YSHIFT;
310 //t->min *= h_scale; //removed
311 //t->max *= h_scale; //removed
312 t->offset *= h_scale;
313 t->factor *= h_scale; //Added
316 // Rotate the layer. The rotation
317 // is in degrees, and does not need
318 // to scale with the instrument size.
319 else if (type == "rotation") {
320 t->type = FGPanelTransformation::ROTATION;
324 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
329 readConditions(t, node);
330 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
336 * Read a chunk of text from the instrument's property list.
338 * A text layer consists of one or more chunks of text. All chunks
339 * share the same font size and color (and eventually, font), but
340 * each can come from a different source. There are three types of
343 * "literal" - a literal text string (the default)
345 * "text-value" - the current value of a string property
347 * "number-value" - the current value of a floating-point property.
349 * All three may also include a printf-style format string.
352 readTextChunk (const SGPropertyNode * node)
354 FGTextLayer::Chunk * chunk;
355 string name = node->getStringValue("name");
356 string type = node->getStringValue("type");
357 string format = node->getStringValue("format");
359 // Default to literal text.
361 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
362 << " assuming \"literal\"");
366 // A literal text string.
367 if (type == "literal") {
368 string text = node->getStringValue("text");
369 chunk = new FGTextLayer::Chunk(text, format);
372 // The value of a string property.
373 else if (type == "text-value") {
374 SGPropertyNode * target =
375 fgGetNode(node->getStringValue("property"), true);
376 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
379 // The value of a float property.
380 else if (type == "number-value") {
381 string propName = node->getStringValue("property");
382 float scale = node->getFloatValue("scale", 1.0);
383 float offset = node->getFloatValue("offset", 0.0);
384 bool truncation = node->getBoolValue("truncate", false);
385 SGPropertyNode * target = fgGetNode(propName.c_str(), true);
386 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
387 format, scale, offset, truncation);
392 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
393 << " for text chunk " << name );
397 readConditions(chunk, node);
403 * Read a single layer from an instrument's property list.
405 * Each instrument consists of one or more layers stacked on top
406 * of each other; the lower layers show through only where the upper
407 * layers contain an alpha component. Each layer can be moved
408 * horizontally and vertically and rotated using transformations.
410 * This module currently recognizes four kinds of layers:
412 * "texture" - a layer containing a texture (the default)
414 * "text" - a layer containing text
416 * "switch" - a layer that switches between two other layers
417 * based on the current value of a boolean property.
419 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
421 * Currently, the only built-in layer class is "compass-ribbon".
423 static FGInstrumentLayer *
424 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
426 FGInstrumentLayer * layer = NULL;
427 string name = node->getStringValue("name");
428 string type = node->getStringValue("type");
429 int w = node->getIntValue("w", -1);
430 int h = node->getIntValue("h", -1);
432 w = int(w * w_scale);
434 h = int(h * h_scale);
438 SG_LOG( SG_COCKPIT, SG_INFO,
439 "No type supplied for layer " << name
440 << " assuming \"texture\"" );
445 // A textured instrument layer.
446 if (type == "texture") {
447 FGCroppedTexture texture = readTexture(node->getNode("texture"));
448 layer = new FGTexturedLayer(texture, w, h);
451 // A group of sublayers.
452 else if (type == "group") {
453 layer = new FGGroupLayer();
454 for (int i = 0; i < node->nChildren(); i++) {
455 const SGPropertyNode * child = node->getChild(i);
456 if (!strcmp(child->getName(), "layer"))
457 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
462 // A textual instrument layer.
463 else if (type == "text") {
464 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
466 // Set the text color.
467 float red = node->getFloatValue("color/red", 0.0);
468 float green = node->getFloatValue("color/green", 0.0);
469 float blue = node->getFloatValue("color/blue", 0.0);
470 tlayer->setColor(red, green, blue);
472 // Set the point size.
473 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
474 tlayer->setPointSize(pointSize);
477 string fontName = node->getStringValue("font", "default");
478 tlayer->setFontName(fontName);
480 const SGPropertyNode * chunk_group = node->getNode("chunks");
481 if (chunk_group != 0) {
482 int nChunks = chunk_group->nChildren();
483 for (int i = 0; i < nChunks; i++) {
484 const SGPropertyNode * node = chunk_group->getChild(i);
485 if (!strcmp(node->getName(), "chunk")) {
486 FGTextLayer::Chunk * chunk = readTextChunk(node);
488 tlayer->addChunk(chunk);
490 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
498 // A switch instrument layer.
499 else if (type == "switch") {
500 layer = new FGSwitchLayer();
501 for (int i = 0; i < node->nChildren(); i++) {
502 const SGPropertyNode * child = node->getChild(i);
503 if (!strcmp(child->getName(), "layer"))
504 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
508 // A built-in instrument layer.
509 else if (type == "built-in") {
510 string layerclass = node->getStringValue("class");
512 if (layerclass == "mag-ribbon") {
513 layer = new FGMagRibbon(w, h);
516 else if (layerclass.empty()) {
517 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
523 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
531 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
537 // Get the transformations for each layer.
539 const SGPropertyNode * trans_group = node->getNode("transformations");
540 if (trans_group != 0) {
541 int nTransformations = trans_group->nChildren();
542 for (int i = 0; i < nTransformations; i++) {
543 const SGPropertyNode * node = trans_group->getChild(i);
544 if (!strcmp(node->getName(), "transformation")) {
545 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
547 layer->addTransformation(t);
549 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
550 << " in transformations" );
555 readConditions(layer, node);
556 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
562 * Read an instrument from a property list.
564 * The instrument consists of a preferred width and height
565 * (the panel may override these), together with a list of layers
566 * and a list of actions to be performed when the user clicks
567 * the mouse over the instrument. All co-ordinates are relative
568 * to the instrument's position, so instruments are fully relocatable;
569 * likewise, co-ordinates for actions and transformations will be
570 * scaled automatically if the instrument is not at its preferred size.
572 static FGPanelInstrument *
573 readInstrument (const SGPropertyNode * node)
575 const string name = node->getStringValue("name");
576 int x = node->getIntValue("x", -1);
577 int y = node->getIntValue("y", -1);
578 int real_w = node->getIntValue("w", -1);
579 int real_h = node->getIntValue("h", -1);
580 int w = node->getIntValue("w-base", -1);
581 int h = node->getIntValue("h-base", -1);
583 if (x == -1 || y == -1) {
584 SG_LOG( SG_COCKPIT, SG_ALERT,
585 "x and y positions must be specified and > 0" );
592 w_scale = float(real_w) / float(w);
596 h_scale = float(real_h) / float(h);
600 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
602 FGLayeredInstrument * instrument =
603 new FGLayeredInstrument(x, y, w, h);
606 // Get the actions for the instrument.
608 const SGPropertyNode * action_group = node->getNode("actions");
609 if (action_group != 0) {
610 int nActions = action_group->nChildren();
611 for (int i = 0; i < nActions; i++) {
612 const SGPropertyNode * node = action_group->getChild(i);
613 if (!strcmp(node->getName(), "action")) {
614 FGPanelAction * action = readAction(node, w_scale, h_scale);
616 instrument->addAction(action);
618 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
625 // Get the layers for the instrument.
627 const SGPropertyNode * layer_group = node->getNode("layers");
628 if (layer_group != 0) {
629 int nLayers = layer_group->nChildren();
630 for (int i = 0; i < nLayers; i++) {
631 const SGPropertyNode * node = layer_group->getChild(i);
632 if (!strcmp(node->getName(), "layer")) {
633 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
635 instrument->addLayer(layer);
637 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
643 readConditions(instrument, node);
644 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
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));
680 // Assign the background texture, if any, or a bogus chequerboard.
682 string bgTexture = root->getStringValue("background");
683 if (bgTexture.empty())
685 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
686 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
689 // Get multibackground if any...
691 string mbgTexture = root->getStringValue("multibackground[0]");
692 if (!mbgTexture.empty()) {
693 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0);
694 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
696 mbgTexture = root->getStringValue("multibackground[1]");
697 if (mbgTexture.empty())
699 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
700 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
702 mbgTexture = root->getStringValue("multibackground[2]");
703 if (mbgTexture.empty())
705 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
706 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
708 mbgTexture = root->getStringValue("multibackground[3]");
709 if (mbgTexture.empty())
711 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
712 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
714 mbgTexture = root->getStringValue("multibackground[4]");
715 if (mbgTexture.empty())
717 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
718 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
720 mbgTexture = root->getStringValue("multibackground[5]");
721 if (mbgTexture.empty())
723 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
724 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
726 mbgTexture = root->getStringValue("multibackground[6]");
727 if (mbgTexture.empty())
729 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
730 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
732 mbgTexture = root->getStringValue("multibackground[7]");
733 if (mbgTexture.empty())
735 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
736 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
743 // Create each instrument.
745 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
746 const SGPropertyNode * instrument_group = root->getChild("instruments");
747 if (instrument_group != 0) {
748 int nInstruments = instrument_group->nChildren();
749 for (int i = 0; i < nInstruments; i++) {
750 const SGPropertyNode * node = instrument_group->getChild(i);
751 if (!strcmp(node->getName(), "instrument")) {
752 FGPanelInstrument * instrument = readInstrument(node);
754 panel->addInstrument(instrument);
756 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
757 << " in instruments section" );
761 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
765 // Return the new panel.
772 * Read a panel from a property list.
774 * Each panel instrument will appear in its own, separate
775 * property list. The top level simply names the panel and
776 * places the instruments in their appropriate locations (and
777 * optionally resizes them if necessary).
779 * Returns 0 if the read fails for any reason.
782 fgReadPanel (istream &input)
787 readProperties(input, &root);
788 } catch (const sg_exception &e) {
789 guiErrorMessage("Error reading panel: ", e);
792 return readPanel(&root);
797 * Read a panel from a property list.
799 * This function opens a stream to a file, then invokes the
800 * main fgReadPanel() function.
803 fgReadPanel (const string &relative_path)
805 SGPath path(globals->get_fg_root());
806 path.append(relative_path);
810 readProperties(path.str(), &root);
811 } catch (const sg_exception &e) {
812 guiErrorMessage("Error reading panel: ", e);
815 return readPanel(&root);
820 // end of panel_io.cxx