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");
187 for (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]), 0);
193 if (node->hasChild("mod-up")) {
194 bindings = node->getChild("mod-up")->getChildren("binding");
195 for (i = 0; i < bindings.size(); i++) {
196 action->addBinding(new FGBinding(bindings[i]), 1);
200 readConditions(action, node);
206 * Read a transformation from the instrument's property list.
208 * The panel module uses the transformations to slide or spin needles,
209 * knobs, and other indicators, and to place layers in the correct
210 * positions. Every layer starts centered exactly on the x,y co-ordinate,
211 * and many layers need to be moved or rotated simply to display the
212 * instrument correctly.
214 * There are three types of transformations:
216 * "x-shift" - move the layer horizontally.
218 * "y-shift" - move the layer vertically.
220 * "rotation" - rotate the layer.
222 * Each transformation may have a fixed offset, and may also have
223 * a floating-point property value to add to the offset. The
224 * floating-point property may be clamped to a minimum and/or
225 * maximum range and scaled (after clamping).
227 * Note that because of the way OpenGL works, transformations will
228 * appear to be applied backwards.
230 static FGPanelTransformation *
231 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
233 FGPanelTransformation * t = new FGPanelTransformation;
235 string name = node->getName();
236 string type = node->getStringValue("type");
237 string propName = node->getStringValue("property", "");
238 SGPropertyNode * target = 0;
241 SG_LOG( SG_COCKPIT, SG_INFO,
242 "No type supplied for transformation " << name
243 << " assuming \"rotation\"" );
247 if (!propName.empty()) {
248 target = fgGetNode(propName.c_str(), true);
252 t->min = node->getFloatValue("min", -9999999);
253 t->max = node->getFloatValue("max", 99999999);
254 t->has_mod = node->hasChild("modulator");
256 t->mod = node->getFloatValue("modulator");
257 t->factor = node->getFloatValue("scale", 1.0);
258 t->offset = node->getFloatValue("offset", 0.0);
260 // Check for an interpolation table
261 const SGPropertyNode * trans_table = node->getNode("interpolation");
262 if (trans_table != 0) {
263 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
264 << trans_table->nChildren() << "children" );
265 t->table = new SGInterpTable();
266 for(int i = 0; i < trans_table->nChildren(); i++) {
267 const SGPropertyNode * node = trans_table->getChild(i);
268 if (!strcmp(node->getName(), "entry")) {
269 double ind = node->getDoubleValue("ind", 0.0);
270 double dep = node->getDoubleValue("dep", 0.0);
271 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
272 << ind << "==>" << dep );
273 t->table->addEntry(ind, dep);
275 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
276 << " in interpolation" );
283 // Move the layer horizontally.
284 if (type == "x-shift") {
285 t->type = FGPanelTransformation::XSHIFT;
286 // t->min *= w_scale; //removed by Martin Dressler
287 // t->max *= w_scale; //removed by Martin Dressler
288 t->offset *= w_scale;
289 t->factor *= w_scale; //Added by Martin Dressler
292 // Move the layer vertically.
293 else if (type == "y-shift") {
294 t->type = FGPanelTransformation::YSHIFT;
295 //t->min *= h_scale; //removed
296 //t->max *= h_scale; //removed
297 t->offset *= h_scale;
298 t->factor *= h_scale; //Added
301 // Rotate the layer. The rotation
302 // is in degrees, and does not need
303 // to scale with the instrument size.
304 else if (type == "rotation") {
305 t->type = FGPanelTransformation::ROTATION;
309 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
314 readConditions(t, node);
315 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
321 * Read a chunk of text from the instrument's property list.
323 * A text layer consists of one or more chunks of text. All chunks
324 * share the same font size and color (and eventually, font), but
325 * each can come from a different source. There are three types of
328 * "literal" - a literal text string (the default)
330 * "text-value" - the current value of a string property
332 * "number-value" - the current value of a floating-point property.
334 * All three may also include a printf-style format string.
337 readTextChunk (const SGPropertyNode * node)
339 FGTextLayer::Chunk * chunk;
340 string name = node->getStringValue("name");
341 string type = node->getStringValue("type");
342 string format = node->getStringValue("format");
344 // Default to literal text.
346 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
347 << " assuming \"literal\"");
351 // A literal text string.
352 if (type == "literal") {
353 string text = node->getStringValue("text");
354 chunk = new FGTextLayer::Chunk(text, format);
357 // The value of a string property.
358 else if (type == "text-value") {
359 SGPropertyNode * target =
360 fgGetNode(node->getStringValue("property"), true);
361 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
364 // The value of a float property.
365 else if (type == "number-value") {
366 string propName = node->getStringValue("property");
367 float scale = node->getFloatValue("scale", 1.0);
368 float offset = node->getFloatValue("offset", 0.0);
369 bool truncation = node->getBoolValue("truncate", false);
370 SGPropertyNode * target = fgGetNode(propName.c_str(), true);
371 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
372 format, scale, offset, truncation);
377 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
378 << " for text chunk " << name );
382 readConditions(chunk, node);
388 * Read a single layer from an instrument's property list.
390 * Each instrument consists of one or more layers stacked on top
391 * of each other; the lower layers show through only where the upper
392 * layers contain an alpha component. Each layer can be moved
393 * horizontally and vertically and rotated using transformations.
395 * This module currently recognizes four kinds of layers:
397 * "texture" - a layer containing a texture (the default)
399 * "text" - a layer containing text
401 * "switch" - a layer that switches between two other layers
402 * based on the current value of a boolean property.
404 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
406 * Currently, the only built-in layer class is "compass-ribbon".
408 static FGInstrumentLayer *
409 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
411 FGInstrumentLayer * layer = NULL;
412 string name = node->getStringValue("name");
413 string type = node->getStringValue("type");
414 int w = node->getIntValue("w", -1);
415 int h = node->getIntValue("h", -1);
417 w = int(w * w_scale);
419 h = int(h * h_scale);
423 SG_LOG( SG_COCKPIT, SG_INFO,
424 "No type supplied for layer " << name
425 << " assuming \"texture\"" );
430 // A textured instrument layer.
431 if (type == "texture") {
432 FGCroppedTexture texture = readTexture(node->getNode("texture"));
433 layer = new FGTexturedLayer(texture, w, h);
436 // A group of sublayers.
437 else if (type == "group") {
438 layer = new FGGroupLayer();
439 for (int i = 0; i < node->nChildren(); i++) {
440 const SGPropertyNode * child = node->getChild(i);
441 if (!strcmp(child->getName(), "layer"))
442 ((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 layer = new FGSwitchLayer();
486 for (int i = 0; i < node->nChildren(); i++) {
487 const SGPropertyNode * child = node->getChild(i);
488 if (!strcmp(child->getName(), "layer"))
489 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
493 // A built-in instrument layer.
494 else if (type == "built-in") {
495 string layerclass = node->getStringValue("class");
497 if (layerclass == "mag-ribbon") {
498 layer = new FGMagRibbon(w, h);
501 else if (layerclass.empty()) {
502 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
508 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
516 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
522 // Get the transformations for each layer.
524 const SGPropertyNode * trans_group = node->getNode("transformations");
525 if (trans_group != 0) {
526 int nTransformations = trans_group->nChildren();
527 for (int i = 0; i < nTransformations; i++) {
528 const SGPropertyNode * node = trans_group->getChild(i);
529 if (!strcmp(node->getName(), "transformation")) {
530 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
532 layer->addTransformation(t);
534 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
535 << " in transformations" );
540 readConditions(layer, node);
541 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
547 * Read an instrument from a property list.
549 * The instrument consists of a preferred width and height
550 * (the panel may override these), together with a list of layers
551 * and a list of actions to be performed when the user clicks
552 * the mouse over the instrument. All co-ordinates are relative
553 * to the instrument's position, so instruments are fully relocatable;
554 * likewise, co-ordinates for actions and transformations will be
555 * scaled automatically if the instrument is not at its preferred size.
557 static FGPanelInstrument *
558 readInstrument (const SGPropertyNode * node)
560 const string name = node->getStringValue("name");
561 int x = node->getIntValue("x", -1);
562 int y = node->getIntValue("y", -1);
563 int real_w = node->getIntValue("w", -1);
564 int real_h = node->getIntValue("h", -1);
565 int w = node->getIntValue("w-base", -1);
566 int h = node->getIntValue("h-base", -1);
568 if (x == -1 || y == -1) {
569 SG_LOG( SG_COCKPIT, SG_ALERT,
570 "x and y positions must be specified and > 0" );
577 w_scale = float(real_w) / float(w);
581 h_scale = float(real_h) / float(h);
585 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
587 FGLayeredInstrument * instrument =
588 new FGLayeredInstrument(x, y, w, h);
591 // Get the actions for the instrument.
593 const SGPropertyNode * action_group = node->getNode("actions");
594 if (action_group != 0) {
595 int nActions = action_group->nChildren();
596 for (int i = 0; i < nActions; i++) {
597 const SGPropertyNode * node = action_group->getChild(i);
598 if (!strcmp(node->getName(), "action")) {
599 FGPanelAction * action = readAction(node, w_scale, h_scale);
601 instrument->addAction(action);
603 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
610 // Get the layers for the instrument.
612 const SGPropertyNode * layer_group = node->getNode("layers");
613 if (layer_group != 0) {
614 int nLayers = layer_group->nChildren();
615 for (int i = 0; i < nLayers; i++) {
616 const SGPropertyNode * node = layer_group->getChild(i);
617 if (!strcmp(node->getName(), "layer")) {
618 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
620 instrument->addLayer(layer);
622 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
628 readConditions(instrument, node);
629 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
635 * Construct the panel from a property tree.
638 readPanel (const SGPropertyNode * root)
640 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
641 root->getStringValue("name", "[Unnamed Panel]") );
643 FGPanel * panel = new FGPanel();
644 panel->setWidth(root->getIntValue("w", 1024));
645 panel->setHeight(root->getIntValue("h", 443));
648 // Grab the visible external viewing area, default to
650 panel->setViewHeight(root->getIntValue("view-height",
651 768 - panel->getHeight() + 2));
654 // Grab the panel's initial offsets, default to 0, 0.
656 if (!fgHasNode("/sim/panel/x-offset"))
657 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
659 // conditional removed by jim wilson to allow panel xml code
660 // with y-offset defined to work...
661 if (!fgHasNode("/sim/panel/y-offset"))
662 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
665 // Assign the background texture, if any, or a bogus chequerboard.
667 string bgTexture = root->getStringValue("background");
668 if (bgTexture.empty())
670 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
671 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
674 // Get multibackground if any...
676 string mbgTexture = root->getStringValue("multibackground[0]");
677 if (!mbgTexture.empty()) {
678 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0);
679 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
681 mbgTexture = root->getStringValue("multibackground[1]");
682 if (mbgTexture.empty())
684 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
685 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
687 mbgTexture = root->getStringValue("multibackground[2]");
688 if (mbgTexture.empty())
690 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
691 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
693 mbgTexture = root->getStringValue("multibackground[3]");
694 if (mbgTexture.empty())
696 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
697 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
699 mbgTexture = root->getStringValue("multibackground[4]");
700 if (mbgTexture.empty())
702 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
703 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
705 mbgTexture = root->getStringValue("multibackground[5]");
706 if (mbgTexture.empty())
708 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
709 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
711 mbgTexture = root->getStringValue("multibackground[6]");
712 if (mbgTexture.empty())
714 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
715 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
717 mbgTexture = root->getStringValue("multibackground[7]");
718 if (mbgTexture.empty())
720 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
721 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
728 // Create each instrument.
730 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
731 const SGPropertyNode * instrument_group = root->getChild("instruments");
732 if (instrument_group != 0) {
733 int nInstruments = instrument_group->nChildren();
734 for (int i = 0; i < nInstruments; i++) {
735 const SGPropertyNode * node = instrument_group->getChild(i);
736 if (!strcmp(node->getName(), "instrument")) {
737 FGPanelInstrument * instrument = readInstrument(node);
739 panel->addInstrument(instrument);
741 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
742 << " in instruments section" );
746 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
750 // Return the new panel.
757 * Read a panel from a property list.
759 * Each panel instrument will appear in its own, separate
760 * property list. The top level simply names the panel and
761 * places the instruments in their appropriate locations (and
762 * optionally resizes them if necessary).
764 * Returns 0 if the read fails for any reason.
767 fgReadPanel (istream &input)
772 readProperties(input, &root);
773 } catch (const sg_exception &e) {
774 guiErrorMessage("Error reading panel: ", e);
777 return readPanel(&root);
782 * Read a panel from a property list.
784 * This function opens a stream to a file, then invokes the
785 * main fgReadPanel() function.
788 fgReadPanel (const string &relative_path)
790 SGPath path(globals->get_fg_root());
791 path.append(relative_path);
795 readProperties(path.str(), &root);
796 } catch (const sg_exception &e) {
797 guiErrorMessage("Error reading panel: ", e);
800 return readPanel(&root);
805 // end of panel_io.cxx