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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 // $Id: panel_io.cxx,v 1.26 2006/08/10 11:12:39 mfranz Exp $
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>
36 #include <simgear/props/condition.hxx>
37 #include <simgear/props/props_io.hxx>
40 #include "panel_io.hxx"
41 #include "ApplicationProperties.hxx"
44 ////////////////////////////////////////////////////////////////////////
45 // Read and construct a panel.
47 // The panel is specified as a regular property list, and each of the
48 // instruments is its own, separate property list (and thus, a separate
49 // XML document). The functions in this section read in the files
50 // as property lists, then extract properties to set up the panel
53 // A panel contains zero or more instruments.
55 // An instrument contains one or more layers and zero or more actions.
57 // A layer contains zero or more transformations.
59 // Some special types of layers also contain other objects, such as
60 // chunks of text or other layers.
62 // There are currently four types of layers:
64 // 1. Textured Layer (type="texture"), the default
65 // 2. Text Layer (type="text")
66 // 3. Switch Layer (type="switch")
67 // 4. Built-in Layer (type="built-in", must also specify class)
69 // The only built-in layer so far is the ribbon for the magnetic compass
70 // (class="compass-ribbon").
72 // There are three types of actions:
74 // 1. Adjust (type="adjust"), the default
75 // 2. Swap (type="swap")
76 // 3. Toggle (type="toggle")
78 // There are three types of transformations:
80 // 1. X shift (type="x-shift"), the default
81 // 2. Y shift (type="y-shift")
82 // 3. Rotation (type="rotation")
84 // Each of these may be associated with a property, so that a needle
85 // will rotate with the airspeed, for example, or may have a fixed
86 // floating-point value.
87 ////////////////////////////////////////////////////////////////////////
91 * Read a cropped texture from the instrument's property list.
93 * The x1 and y1 properties give the starting position of the texture
94 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
95 * ending position. For example, to use the bottom-left quarter of a
96 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
98 static FGCroppedTexture_ptr
99 readTexture (const SGPropertyNode * node)
101 return new FGCroppedTexture(node->getStringValue("path"),
102 node->getFloatValue("x1"),
103 node->getFloatValue("y1"),
104 node->getFloatValue("x2", 1.0),
105 node->getFloatValue("y2", 1.0));
106 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
110 * Test for a condition in the current node.
113 ////////////////////////////////////////////////////////////////////////
114 // Read a condition and use it if necessary.
115 ////////////////////////////////////////////////////////////////////////
118 readConditions (SGConditional *component, const SGPropertyNode *node)
120 const SGPropertyNode * conditionNode = node->getChild("condition");
121 if (conditionNode != 0)
122 // The top level is implicitly AND
123 component->setCondition(sgReadCondition(ApplicationProperties::Properties,
130 * Read a transformation from the instrument's property list.
132 * The panel module uses the transformations to slide or spin needles,
133 * knobs, and other indicators, and to place layers in the correct
134 * positions. Every layer starts centered exactly on the x,y co-ordinate,
135 * and many layers need to be moved or rotated simply to display the
136 * instrument correctly.
138 * There are three types of transformations:
140 * "x-shift" - move the layer horizontally.
142 * "y-shift" - move the layer vertically.
144 * "rotation" - rotate the layer.
146 * Each transformation may have a fixed offset, and may also have
147 * a floating-point property value to add to the offset. The
148 * floating-point property may be clamped to a minimum and/or
149 * maximum range and scaled (after clamping).
151 * Note that because of the way OpenGL works, transformations will
152 * appear to be applied backwards.
154 static FGPanelTransformation *
155 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
157 FGPanelTransformation * t = new FGPanelTransformation;
159 string name = node->getName();
160 string type = node->getStringValue("type");
161 string propName = node->getStringValue("property", "");
162 const SGPropertyNode * target = 0;
165 SG_LOG( SG_COCKPIT, SG_INFO,
166 "No type supplied for transformation " << name
167 << " assuming \"rotation\"" );
171 if (!propName.empty())
172 target = ApplicationProperties::Properties->getNode(propName.c_str(), true);
175 t->min = node->getFloatValue("min", -9999999);
176 t->max = node->getFloatValue("max", 99999999);
177 t->has_mod = node->hasChild("modulator");
179 t->mod = node->getFloatValue("modulator");
180 t->factor = node->getFloatValue("scale", 1.0);
181 t->offset = node->getFloatValue("offset", 0.0);
184 // Check for an interpolation table
185 const SGPropertyNode * trans_table = node->getNode("interpolation");
186 if (trans_table != 0) {
187 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
188 << trans_table->nChildren() << " children" );
189 t->table = new SGInterpTable();
190 for (int i = 0; i < trans_table->nChildren(); i++) {
191 const SGPropertyNode * node = trans_table->getChild(i);
192 if (!strcmp(node->getName(), "entry")) {
193 double ind = node->getDoubleValue("ind", 0.0);
194 double dep = node->getDoubleValue("dep", 0.0);
195 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
196 << ind << "==>" << dep );
197 t->table->addEntry(ind, dep);
199 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
200 << " in interpolation" );
207 // Move the layer horizontally.
208 if (type == "x-shift") {
209 t->type = FGPanelTransformation::XSHIFT;
210 // t->min *= w_scale; //removed by Martin Dressler
211 // t->max *= w_scale; //removed by Martin Dressler
212 t->offset *= w_scale;
213 t->factor *= w_scale; //Added by Martin Dressler
216 // Move the layer vertically.
217 else if (type == "y-shift") {
218 t->type = FGPanelTransformation::YSHIFT;
219 //t->min *= h_scale; //removed
220 //t->max *= h_scale; //removed
221 t->offset *= h_scale;
222 t->factor *= h_scale; //Added
225 // Rotate the layer. The rotation
226 // is in degrees, and does not need
227 // to scale with the instrument size.
228 else if (type == "rotation") {
229 t->type = FGPanelTransformation::ROTATION;
233 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
238 readConditions(t, node);
239 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
245 * Read a chunk of text from the instrument's property list.
247 * A text layer consists of one or more chunks of text. All chunks
248 * share the same font size and color (and eventually, font), but
249 * each can come from a different source. There are three types of
252 * "literal" - a literal text string (the default)
254 * "text-value" - the current value of a string property
256 * "number-value" - the current value of a floating-point property.
258 * All three may also include a printf-style format string.
261 readTextChunk (const SGPropertyNode * node)
263 FGTextLayer::Chunk * chunk;
264 string name = node->getStringValue("name");
265 string type = node->getStringValue("type");
266 string format = node->getStringValue("format");
268 // Default to literal text.
270 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
271 << " assuming \"literal\"");
275 // A literal text string.
276 if (type == "literal") {
277 string text = node->getStringValue("text");
278 chunk = new FGTextLayer::Chunk(text, format);
281 // The value of a string property.
282 else if (type == "text-value") {
283 SGPropertyNode * target =
284 ApplicationProperties::Properties->getNode( node->getStringValue("property"), true);
285 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
288 // The value of a float property.
289 else if (type == "number-value") {
290 string propName = node->getStringValue("property");
291 float scale = node->getFloatValue("scale", 1.0);
292 float offset = node->getFloatValue("offset", 0.0);
293 bool truncation = node->getBoolValue("truncate", false);
294 SGPropertyNode * target = ApplicationProperties::Properties->getNode(propName.c_str(), true);
295 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
296 format, scale, offset, truncation);
301 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
302 << " for text chunk " << name );
306 readConditions(chunk, node);
312 * Read a single layer from an instrument's property list.
314 * Each instrument consists of one or more layers stacked on top
315 * of each other; the lower layers show through only where the upper
316 * layers contain an alpha component. Each layer can be moved
317 * horizontally and vertically and rotated using transformations.
319 * This module currently recognizes four kinds of layers:
321 * "texture" - a layer containing a texture (the default)
323 * "text" - a layer containing text
325 * "switch" - a layer that switches between two other layers
326 * based on the current value of a boolean property.
328 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
330 * Currently, the only built-in layer class is "compass-ribbon".
332 static FGInstrumentLayer *
333 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
335 FGInstrumentLayer * layer = NULL;
336 string name = node->getStringValue("name");
337 string type = node->getStringValue("type");
338 int w = node->getIntValue("w", -1);
339 int h = node->getIntValue("h", -1);
340 bool emissive = node->getBoolValue("emissive", false);
342 w = int(w * w_scale);
344 h = int(h * h_scale);
348 SG_LOG( SG_COCKPIT, SG_INFO,
349 "No type supplied for layer " << name
350 << " assuming \"texture\"" );
355 // A textured instrument layer.
356 if (type == "texture") {
357 FGCroppedTexture_ptr texture = readTexture(node->getNode("texture"));
358 layer = new FGTexturedLayer(texture, w, h);
360 FGTexturedLayer *tl=(FGTexturedLayer*)layer;
361 tl->setEmissive(true);
365 // A group of sublayers.
366 else if (type == "group") {
367 layer = new FGGroupLayer();
368 for (int i = 0; i < node->nChildren(); i++) {
369 const SGPropertyNode * child = node->getChild(i);
370 if (!strcmp(child->getName(), "layer"))
371 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
376 // A textual instrument layer.
377 else if (type == "text") {
378 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
380 // Set the text color.
381 float red = node->getFloatValue("color/red", 0.0);
382 float green = node->getFloatValue("color/green", 0.0);
383 float blue = node->getFloatValue("color/blue", 0.0);
384 tlayer->setColor(red, green, blue);
386 // Set the point size.
387 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
388 tlayer->setPointSize(pointSize);
391 string fontName = node->getStringValue("font", "Helvetica");
392 tlayer->setFontName(fontName);
394 const SGPropertyNode * chunk_group = node->getNode("chunks");
395 if (chunk_group != 0) {
396 int nChunks = chunk_group->nChildren();
397 for (int i = 0; i < nChunks; i++) {
398 const SGPropertyNode * node = chunk_group->getChild(i);
399 if (!strcmp(node->getName(), "chunk")) {
400 FGTextLayer::Chunk * chunk = readTextChunk(node);
402 tlayer->addChunk(chunk);
404 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
412 // A switch instrument layer.
413 else if (type == "switch") {
414 layer = new FGSwitchLayer();
415 for (int i = 0; i < node->nChildren(); i++) {
416 const SGPropertyNode * child = node->getChild(i);
417 if (!strcmp(child->getName(), "layer"))
418 ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale));
424 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
430 // Get the transformations for each layer.
432 const SGPropertyNode * trans_group = node->getNode("transformations");
433 if (trans_group != 0) {
434 int nTransformations = trans_group->nChildren();
435 for (int i = 0; i < nTransformations; i++) {
436 const SGPropertyNode * node = trans_group->getChild(i);
437 if (!strcmp(node->getName(), "transformation")) {
438 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
440 layer->addTransformation(t);
442 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
443 << " in transformations" );
448 readConditions(layer, node);
449 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
455 * Read an instrument from a property list.
457 * The instrument consists of a preferred width and height
458 * (the panel may override these), together with a list of layers
459 * and a list of actions to be performed when the user clicks
460 * the mouse over the instrument. All co-ordinates are relative
461 * to the instrument's position, so instruments are fully relocatable;
462 * likewise, co-ordinates for actions and transformations will be
463 * scaled automatically if the instrument is not at its preferred size.
465 static FGPanelInstrument *
466 readInstrument (const SGPropertyNode * node)
468 const string name = node->getStringValue("name");
469 int x = node->getIntValue("x", -1);
470 int y = node->getIntValue("y", -1);
471 int real_w = node->getIntValue("w", -1);
472 int real_h = node->getIntValue("h", -1);
473 int w = node->getIntValue("w-base", -1);
474 int h = node->getIntValue("h-base", -1);
476 if (x == -1 || y == -1) {
477 SG_LOG( SG_COCKPIT, SG_ALERT,
478 "x and y positions must be specified and > 0" );
485 w_scale = float(real_w) / float(w);
489 h_scale = float(real_h) / float(h);
493 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
495 FGLayeredInstrument * instrument =
496 new FGLayeredInstrument(x, y, w, h);
499 // Get the layers for the instrument.
501 const SGPropertyNode * layer_group = node->getNode("layers");
502 if (layer_group != 0) {
503 int nLayers = layer_group->nChildren();
504 for (int i = 0; i < nLayers; i++) {
505 const SGPropertyNode * node = layer_group->getChild(i);
506 if (!strcmp(node->getName(), "layer")) {
507 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
509 instrument->addLayer(layer);
511 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
517 readConditions(instrument, node);
518 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
524 * Construct the panel from a property tree.
527 FGReadablePanel::read(SGPropertyNode_ptr root)
529 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
530 root->getStringValue("name", "[Unnamed Panel]") );
532 FGPanel * panel = new FGPanel(root);
533 panel->setWidth(root->getIntValue("w", 1024));
534 panel->setHeight(root->getIntValue("h", 443));
536 SG_LOG( SG_COCKPIT, SG_INFO, "Size=" << panel->getWidth() << "x" << panel->getHeight() );
538 // Assign the background texture, if any, or a bogus chequerboard.
540 string bgTexture = root->getStringValue("background");
541 if( !bgTexture.empty() )
542 panel->setBackground( new FGCroppedTexture( bgTexture ) );
543 panel->setBackgroundWidth( root->getDoubleValue( "background-width", 1.0 ) );
544 panel->setBackgroundHeight( root->getDoubleValue( "background-height", 1.0 ) );
545 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
548 // Get multibackground if any...
550 for( int i = 0; i < 8; i++ ) {
551 SGPropertyNode * mbgNode = root->getChild( "multibackground", i );
553 if( mbgNode != NULL ) mbgTexture = mbgNode->getStringValue();
554 if( mbgTexture.empty() ) {
555 if( i == 0 ) break; // if first texture is missing, ignore the rest
556 else mbgTexture = "FOO"; // if others are missing - set default texture
558 panel->setMultiBackground( new FGCroppedTexture(mbgTexture), i );
559 SG_LOG( SG_COCKPIT, SG_INFO, "Set multi-background texture" << i << " to " << mbgTexture );
562 // Create each instrument.
564 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
565 const SGPropertyNode * instrument_group = root->getChild("instruments");
566 if (instrument_group != 0) {
567 int nInstruments = instrument_group->nChildren();
568 for (int i = 0; i < nInstruments; i++) {
569 const SGPropertyNode * node = instrument_group->getChild(i);
570 if (!strcmp(node->getName(), "instrument")) {
571 FGPanelInstrument * instrument = readInstrument(node);
573 panel->addInstrument(instrument);
575 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
576 << " in instruments section" );
580 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
584 // Return the new panel.
589 // end of panel_io.cxx