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);
183 FGPanelAction * action = new FGPanelAction(button, x, y, w, h);
185 vector<SGPropertyNode_ptr>bindings = node->getChildren("binding");
186 for (unsigned int i = 0; i < bindings.size(); i++) {
187 SG_LOG(SG_INPUT, SG_INFO, "Reading binding "
188 << bindings[i]->getStringValue("command"));
189 action->addBinding(new FGBinding(bindings[i])); // TODO: allow modifiers
192 readConditions(action, node);
198 * Read a transformation from the instrument's property list.
200 * The panel module uses the transformations to slide or spin needles,
201 * knobs, and other indicators, and to place layers in the correct
202 * positions. Every layer starts centered exactly on the x,y co-ordinate,
203 * and many layers need to be moved or rotated simply to display the
204 * instrument correctly.
206 * There are three types of transformations:
208 * "x-shift" - move the layer horizontally.
210 * "y-shift" - move the layer vertically.
212 * "rotation" - rotate the layer.
214 * Each transformation may have a fixed offset, and may also have
215 * a floating-point property value to add to the offset. The
216 * floating-point property may be clamped to a minimum and/or
217 * maximum range and scaled (after clamping).
219 * Note that because of the way OpenGL works, transformations will
220 * appear to be applied backwards.
222 static FGPanelTransformation *
223 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
225 FGPanelTransformation * t = new FGPanelTransformation;
227 string name = node->getName();
228 string type = node->getStringValue("type");
229 string propName = node->getStringValue("property", "");
230 SGPropertyNode * target = 0;
233 SG_LOG( SG_COCKPIT, SG_ALERT,
234 "No type supplied for transformation " << name
235 << " assuming \"rotation\"" );
239 if (!propName.empty()) {
240 target = fgGetNode(propName.c_str(), true);
244 t->min = node->getFloatValue("min", -9999999);
245 t->max = node->getFloatValue("max", 99999999);
246 t->factor = node->getFloatValue("scale", 1.0);
247 t->offset = node->getFloatValue("offset", 0.0);
249 // Check for an interpolation table
250 const SGPropertyNode * trans_table = node->getNode("interpolation");
251 if (trans_table != 0) {
252 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
253 << trans_table->nChildren() << "children" );
254 t->table = new SGInterpTable();
255 for(int i = 0; i < trans_table->nChildren(); i++) {
256 const SGPropertyNode * node = trans_table->getChild(i);
257 if (!strcmp(node->getName(), "entry")) {
258 double ind = node->getDoubleValue("ind", 0.0);
259 double dep = node->getDoubleValue("dep", 0.0);
260 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
261 << ind << "==>" << dep );
262 t->table->addEntry(ind, dep);
264 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
265 << " in interpolation" );
272 // Move the layer horizontally.
273 if (type == "x-shift") {
274 t->type = FGPanelTransformation::XSHIFT;
275 // t->min *= w_scale; //removed by Martin Dressler
276 // t->max *= w_scale; //removed by Martin Dressler
277 t->offset *= w_scale;
278 t->factor *= w_scale; //Added by Martin Dressler
281 // Move the layer vertically.
282 else if (type == "y-shift") {
283 t->type = FGPanelTransformation::YSHIFT;
284 //t->min *= h_scale; //removed
285 //t->max *= h_scale; //removed
286 t->offset *= h_scale;
287 t->factor *= h_scale; //Added
290 // Rotate the layer. The rotation
291 // is in degrees, and does not need
292 // to scale with the instrument size.
293 else if (type == "rotation") {
294 t->type = FGPanelTransformation::ROTATION;
298 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
303 readConditions(t, node);
304 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
310 * Read a chunk of text from the instrument's property list.
312 * A text layer consists of one or more chunks of text. All chunks
313 * share the same font size and color (and eventually, font), but
314 * each can come from a different source. There are three types of
317 * "literal" - a literal text string (the default)
319 * "text-value" - the current value of a string property
321 * "number-value" - the current value of a floating-point property.
323 * All three may also include a printf-style format string.
326 readTextChunk (const SGPropertyNode * node)
328 FGTextLayer::Chunk * chunk;
329 string name = node->getStringValue("name");
330 string type = node->getStringValue("type");
331 string format = node->getStringValue("format");
333 // Default to literal text.
335 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
336 << " assuming \"literal\"");
340 // A literal text string.
341 if (type == "literal") {
342 string text = node->getStringValue("text");
343 chunk = new FGTextLayer::Chunk(text, format);
346 // The value of a string property.
347 else if (type == "text-value") {
348 SGPropertyNode * target =
349 fgGetNode(node->getStringValue("property"), true);
350 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
353 // The value of a float property.
354 else if (type == "number-value") {
355 string propName = node->getStringValue("property");
356 float scale = node->getFloatValue("scale", 1.0);
357 SGPropertyNode * target = fgGetNode(propName.c_str(), true);
358 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
364 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
365 << " for text chunk " << name );
369 readConditions(chunk, node);
375 * Read a single layer from an instrument's property list.
377 * Each instrument consists of one or more layers stacked on top
378 * of each other; the lower layers show through only where the upper
379 * layers contain an alpha component. Each layer can be moved
380 * horizontally and vertically and rotated using transformations.
382 * This module currently recognizes four kinds of layers:
384 * "texture" - a layer containing a texture (the default)
386 * "text" - a layer containing text
388 * "switch" - a layer that switches between two other layers
389 * based on the current value of a boolean property.
391 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
393 * Currently, the only built-in layer class is "compass-ribbon".
395 static FGInstrumentLayer *
396 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
398 FGInstrumentLayer * layer = NULL;
399 string name = node->getStringValue("name");
400 string type = node->getStringValue("type");
401 int w = node->getIntValue("w", -1);
402 int h = node->getIntValue("h", -1);
404 w = int(w * w_scale);
406 h = int(h * h_scale);
410 SG_LOG( SG_COCKPIT, SG_ALERT,
411 "No type supplied for layer " << name
412 << " assuming \"texture\"" );
417 // A textured instrument layer.
418 if (type == "texture") {
419 FGCroppedTexture texture = readTexture(node->getNode("texture"));
420 layer = new FGTexturedLayer(texture, w, h);
423 // A group of sublayers.
424 else if (type == "group") {
425 layer = new FGGroupLayer();
426 for (int i = 0; i < node->nChildren(); i++) {
427 const SGPropertyNode * child = node->getChild(i);
428 cerr << "Trying child " << child->getName() << endl;
429 if (!strcmp(child->getName(), "layer")) {
430 cerr << "succeeded!" << endl;
431 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
437 // A textual instrument layer.
438 else if (type == "text") {
439 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
441 // Set the text color.
442 float red = node->getFloatValue("color/red", 0.0);
443 float green = node->getFloatValue("color/green", 0.0);
444 float blue = node->getFloatValue("color/blue", 0.0);
445 tlayer->setColor(red, green, blue);
447 // Set the point size.
448 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
449 tlayer->setPointSize(pointSize);
452 string fontName = node->getStringValue("font", "default");
453 tlayer->setFontName(fontName);
455 const SGPropertyNode * chunk_group = node->getNode("chunks");
456 if (chunk_group != 0) {
457 int nChunks = chunk_group->nChildren();
458 for (int i = 0; i < nChunks; i++) {
459 const SGPropertyNode * node = chunk_group->getChild(i);
460 if (!strcmp(node->getName(), "chunk")) {
461 FGTextLayer::Chunk * chunk = readTextChunk(node);
463 tlayer->addChunk(chunk);
465 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
473 // A switch instrument layer.
474 else if (type == "switch") {
475 SGPropertyNode * target =
476 fgGetNode(node->getStringValue("property"), true);
477 FGInstrumentLayer * layer1 =
478 readLayer(node->getNode("layer[0]"), w_scale, h_scale);
479 FGInstrumentLayer * layer2 =
480 readLayer(node->getNode("layer[1]"), w_scale, h_scale);
481 layer = new FGSwitchLayer(w, h, target, layer1, layer2);
484 // A built-in instrument layer.
485 else if (type == "built-in") {
486 string layerclass = node->getStringValue("class");
488 if (layerclass == "mag-ribbon") {
489 layer = new FGMagRibbon(w, h);
492 else if (layerclass.empty()) {
493 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
499 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
507 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
513 // Get the transformations for each layer.
515 const SGPropertyNode * trans_group = node->getNode("transformations");
516 if (trans_group != 0) {
517 int nTransformations = trans_group->nChildren();
518 for (int i = 0; i < nTransformations; i++) {
519 const SGPropertyNode * node = trans_group->getChild(i);
520 if (!strcmp(node->getName(), "transformation")) {
521 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
523 layer->addTransformation(t);
525 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
526 << " in transformations" );
531 readConditions(layer, node);
532 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
538 * Read an instrument from a property list.
540 * The instrument consists of a preferred width and height
541 * (the panel may override these), together with a list of layers
542 * and a list of actions to be performed when the user clicks
543 * the mouse over the instrument. All co-ordinates are relative
544 * to the instrument's position, so instruments are fully relocatable;
545 * likewise, co-ordinates for actions and transformations will be
546 * scaled automatically if the instrument is not at its preferred size.
548 static FGPanelInstrument *
549 readInstrument (const SGPropertyNode * node)
551 const string name = node->getStringValue("name");
552 int x = node->getIntValue("x", -1);
553 int y = node->getIntValue("y", -1);
554 int real_w = node->getIntValue("w", -1);
555 int real_h = node->getIntValue("h", -1);
556 int w = node->getIntValue("w-base", -1);
557 int h = node->getIntValue("h-base", -1);
559 if (x == -1 || y == -1) {
560 SG_LOG( SG_COCKPIT, SG_ALERT,
561 "x and y positions must be specified and > 0" );
568 w_scale = float(real_w) / float(w);
572 h_scale = float(real_h) / float(h);
576 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
578 FGLayeredInstrument * instrument =
579 new FGLayeredInstrument(x, y, w, h);
582 // Get the actions for the instrument.
584 const SGPropertyNode * action_group = node->getNode("actions");
585 if (action_group != 0) {
586 int nActions = action_group->nChildren();
587 for (int i = 0; i < nActions; i++) {
588 const SGPropertyNode * node = action_group->getChild(i);
589 if (!strcmp(node->getName(), "action")) {
590 FGPanelAction * action = readAction(node, w_scale, h_scale);
592 instrument->addAction(action);
594 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
601 // Get the layers for the instrument.
603 const SGPropertyNode * layer_group = node->getNode("layers");
604 if (layer_group != 0) {
605 int nLayers = layer_group->nChildren();
606 for (int i = 0; i < nLayers; i++) {
607 const SGPropertyNode * node = layer_group->getChild(i);
608 if (!strcmp(node->getName(), "layer")) {
609 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
611 instrument->addLayer(layer);
613 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
619 readConditions(instrument, node);
620 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
626 * Construct the panel from a property tree.
629 readPanel (const SGPropertyNode * root)
631 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
632 root->getStringValue("name", "[Unnamed Panel]") );
634 FGPanel * panel = new FGPanel();
635 panel->setWidth(root->getIntValue("w", 1024));
636 panel->setHeight(root->getIntValue("h", 443));
639 // Grab the visible external viewing area, default to
641 panel->setViewHeight(root->getIntValue("view-height",
642 768 - panel->getHeight() + 2));
645 // Grab the panel's initial offsets, default to 0, 0.
647 if (!fgHasNode("/sim/panel/x-offset"))
648 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
650 // conditional removed by jim wilson to allow panel xml code
651 // with y-offset defined to work...
652 if (!fgHasNode("/sim/panel/y-offset"))
653 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
656 // Assign the background texture, if any, or a bogus chequerboard.
658 string bgTexture = root->getStringValue("background");
659 if (bgTexture.empty())
661 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
662 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
665 // Get multibackground if any...
667 string mbgTexture = root->getStringValue("multibackground[0]");
668 if (!mbgTexture.empty()) {
669 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0);
670 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
672 mbgTexture = root->getStringValue("multibackground[1]");
673 if (mbgTexture.empty())
675 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1);
676 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
678 mbgTexture = root->getStringValue("multibackground[2]");
679 if (mbgTexture.empty())
681 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2);
682 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
684 mbgTexture = root->getStringValue("multibackground[3]");
685 if (mbgTexture.empty())
687 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3);
688 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
690 mbgTexture = root->getStringValue("multibackground[4]");
691 if (mbgTexture.empty())
693 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4);
694 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
696 mbgTexture = root->getStringValue("multibackground[5]");
697 if (mbgTexture.empty())
699 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5);
700 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
702 mbgTexture = root->getStringValue("multibackground[6]");
703 if (mbgTexture.empty())
705 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6);
706 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
708 mbgTexture = root->getStringValue("multibackground[7]");
709 if (mbgTexture.empty())
711 panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7);
712 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << mbgTexture );
719 // Create each instrument.
721 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
722 const SGPropertyNode * instrument_group = root->getChild("instruments");
723 if (instrument_group != 0) {
724 int nInstruments = instrument_group->nChildren();
725 for (int i = 0; i < nInstruments; i++) {
726 const SGPropertyNode * node = instrument_group->getChild(i);
727 if (!strcmp(node->getName(), "instrument")) {
728 FGPanelInstrument * instrument = readInstrument(node);
730 panel->addInstrument(instrument);
732 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
733 << " in instruments section" );
737 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
741 // Return the new panel.
748 * Read a panel from a property list.
750 * Each panel instrument will appear in its own, separate
751 * property list. The top level simply names the panel and
752 * places the instruments in their appropriate locations (and
753 * optionally resizes them if necessary).
755 * Returns 0 if the read fails for any reason.
758 fgReadPanel (istream &input)
763 readProperties(input, &root);
764 } catch (const sg_exception &e) {
765 guiErrorMessage("Error reading panel: ", e);
768 return readPanel(&root);
773 * Read a panel from a property list.
775 * This function opens a stream to a file, then invokes the
776 * main fgReadPanel() function.
779 fgReadPanel (const string &relative_path)
781 SGPath path(globals->get_fg_root());
782 path.append(relative_path);
786 readProperties(path.str(), &root);
787 } catch (const sg_exception &e) {
788 guiErrorMessage("Error reading panel: ", e);
791 return readPanel(&root);
796 // end of panel_io.cxx