]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/panel_io.cxx
Fix standalone terrasync build
[flightgear.git] / utils / fgpanel / panel_io.cxx
1 //  panel_io.cxx - I/O for 2D panel.
2 //
3 //  Written by David Megginson, started January 2000.
4 //
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.
9 // 
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.
14 //
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.
18 //
19 //  $Id: panel_io.cxx,v 1.26 2006/08/10 11:12:39 mfranz Exp $
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H
26 #  include <windows.h>
27 #endif
28
29 #include <string.h>             // for strcmp()
30
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>
38
39 #include "panel.hxx"
40 #include "panel_io.hxx"
41 #include "ApplicationProperties.hxx"
42
43 \f
44 ////////////////////////////////////////////////////////////////////////
45 // Read and construct a panel.
46 //
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
51 // itself.
52 //
53 // A panel contains zero or more instruments.
54 //
55 // An instrument contains one or more layers and zero or more actions.
56 //
57 // A layer contains zero or more transformations.
58 //
59 // Some special types of layers also contain other objects, such as 
60 // chunks of text or other layers.
61 //
62 // There are currently four types of layers:
63 //
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)
68 //
69 // The only built-in layer so far is the ribbon for the magnetic compass
70 // (class="compass-ribbon").
71 //
72 // There are three types of actions:
73 //
74 // 1. Adjust (type="adjust"), the default
75 // 2. Swap (type="swap")
76 // 3. Toggle (type="toggle")
77 //
78 // There are three types of transformations:
79 //
80 // 1. X shift (type="x-shift"), the default
81 // 2. Y shift (type="y-shift")
82 // 3. Rotation (type="rotation")
83 //
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 ////////////////////////////////////////////////////////////////////////
88
89
90 /**
91  * Read a cropped texture from the instrument's property list.
92  *
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.
97  */
98 static FGCroppedTexture_ptr
99 readTexture (const SGPropertyNode * node)
100 {
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());
107 }
108
109 /**
110  * Test for a condition in the current node.
111  */
112 \f
113 ////////////////////////////////////////////////////////////////////////
114 // Read a condition and use it if necessary.
115 ////////////////////////////////////////////////////////////////////////
116
117 static void
118 readConditions (SGConditional *component, const SGPropertyNode *node)
119 {
120   const SGPropertyNode * conditionNode = node->getChild("condition");
121   if (conditionNode != 0)
122                                 // The top level is implicitly AND
123     component->setCondition(sgReadCondition(ApplicationProperties::Properties,
124                                             conditionNode) );
125   ;
126 }
127
128
129 /**
130  * Read a transformation from the instrument's property list.
131  *
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.
137  *
138  * There are three types of transformations:
139  *
140  * "x-shift" - move the layer horizontally.
141  *
142  * "y-shift" - move the layer vertically.
143  *
144  * "rotation" - rotate the layer.
145  *
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).
150  *
151  * Note that because of the way OpenGL works, transformations will
152  * appear to be applied backwards.
153  */
154 static FGPanelTransformation *
155 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
156 {
157   FGPanelTransformation * t = new FGPanelTransformation;
158
159   string name = node->getName();
160   string type = node->getStringValue("type");
161   string propName = node->getStringValue("property", "");
162   const SGPropertyNode * target = 0;
163
164   if (type.empty()) {
165     SG_LOG( SG_COCKPIT, SG_INFO,
166             "No type supplied for transformation " << name
167             << " assuming \"rotation\"" );
168     type = "rotation";
169   }
170
171   if (!propName.empty())
172     target = ApplicationProperties::Properties->getNode(propName.c_str(), true);
173
174   t->node = target;
175   t->min = node->getFloatValue("min", -9999999);
176   t->max = node->getFloatValue("max", 99999999);
177   t->has_mod = node->hasChild("modulator");
178   if (t->has_mod)
179       t->mod = node->getFloatValue("modulator");
180   t->factor = node->getFloatValue("scale", 1.0);
181   t->offset = node->getFloatValue("offset", 0.0);
182
183
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);
198       } else {
199         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
200                 << " in interpolation" );
201       }
202     }
203   } else {
204     t->table = 0;
205   }
206   
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
214   } 
215
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
223   } 
224
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;
230   } 
231
232   else {
233     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
234     delete t;
235     return 0;
236   }
237
238   readConditions(t, node);
239   SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
240   return t;
241 }
242
243
244 /**
245  * Read a chunk of text from the instrument's property list.
246  *
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
250  * text chunks:
251  *
252  * "literal" - a literal text string (the default)
253  *
254  * "text-value" - the current value of a string property
255  *
256  * "number-value" - the current value of a floating-point property.
257  *
258  * All three may also include a printf-style format string.
259  */
260 FGTextLayer::Chunk *
261 readTextChunk (const SGPropertyNode * node)
262 {
263   FGTextLayer::Chunk * chunk;
264   string name = node->getStringValue("name");
265   string type = node->getStringValue("type");
266   string format = node->getStringValue("format");
267
268                                 // Default to literal text.
269   if (type.empty()) {
270     SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
271             << " assuming \"literal\"");
272     type = "literal";
273   }
274
275                                 // A literal text string.
276   if (type == "literal") {
277     string text = node->getStringValue("text");
278     chunk = new FGTextLayer::Chunk(text, format);
279   }
280
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);
286   }
287
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);
297   }
298
299                                 // Unknown type.
300   else {
301     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
302             << " for text chunk " << name );
303     return 0;
304   }
305
306   readConditions(chunk, node);
307   return chunk;
308 }
309
310
311 /**
312  * Read a single layer from an instrument's property list.
313  *
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.
318  *
319  * This module currently recognizes four kinds of layers:
320  *
321  * "texture" - a layer containing a texture (the default)
322  *
323  * "text" - a layer containing text
324  *
325  * "switch" - a layer that switches between two other layers
326  *   based on the current value of a boolean property.
327  *
328  * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
329  *
330  * Currently, the only built-in layer class is "compass-ribbon".
331  */
332 static FGInstrumentLayer *
333 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
334 {
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);
341   if (w != -1)
342     w = int(w * w_scale);
343   if (h != -1)
344     h = int(h * h_scale);
345
346
347   if (type.empty()) {
348     SG_LOG( SG_COCKPIT, SG_INFO,
349             "No type supplied for layer " << name
350             << " assuming \"texture\"" );
351     type = "texture";
352   }
353
354
355                                 // A textured instrument layer.
356   if (type == "texture") {
357     FGCroppedTexture_ptr texture = readTexture(node->getNode("texture"));
358     layer = new FGTexturedLayer(texture, w, h);
359     if (emissive) {
360       FGTexturedLayer *tl=(FGTexturedLayer*)layer;
361       tl->setEmissive(true);
362     }
363
364   }
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));
372     }
373   }
374
375
376                                 // A textual instrument layer.
377   else if (type == "text") {
378     FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
379
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);
385
386                                 // Set the point size.
387     float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
388     tlayer->setPointSize(pointSize);
389
390                                 // Set the font.
391     string fontName = node->getStringValue("font", "Helvetica");
392     tlayer->setFontName(fontName);
393
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);
401           if (chunk != 0)
402             tlayer->addChunk(chunk);
403         } else {
404           SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
405                   << " in chunks" );
406         }
407       }
408       layer = tlayer;
409     }
410   }
411
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));
419     }
420   }
421
422                                 // An unknown type.
423   else {
424     SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
425     delete layer;
426     return 0;
427   }
428   
429   //
430   // Get the transformations for each layer.
431   //
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);
439         if (t != 0)
440           layer->addTransformation(t);
441       } else {
442         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
443                 << " in transformations" );
444       }
445     }
446   }
447
448   readConditions(layer, node);
449   SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
450   return layer;
451 }
452
453
454 /**
455  * Read an instrument from a property list.
456  *
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.
464  */
465 static FGPanelInstrument *
466 readInstrument (const SGPropertyNode * node)
467 {
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);
475
476   if (x == -1 || y == -1) {
477     SG_LOG( SG_COCKPIT, SG_ALERT,
478             "x and y positions must be specified and > 0" );
479     return 0;
480   }
481
482   float w_scale = 1.0;
483   float h_scale = 1.0;
484   if (real_w != -1) {
485     w_scale = float(real_w) / float(w);
486     w = real_w;
487   }
488   if (real_h != -1) {
489     h_scale = float(real_h) / float(h);
490     h = real_h;
491   }
492
493   SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
494
495   FGLayeredInstrument * instrument =
496     new FGLayeredInstrument(x, y, w, h);
497
498   //
499   // Get the layers for the instrument.
500   //
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);
508         if (layer != 0)
509           instrument->addLayer(layer);
510       } else {
511         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
512                 << " in layers" );
513       }
514     }
515   }
516
517   readConditions(instrument, node);
518   SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
519   return instrument;
520 }
521
522
523 /**
524  * Construct the panel from a property tree.
525  */
526 SGSharedPtr<FGPanel>
527 FGReadablePanel::read(SGPropertyNode_ptr root)
528 {
529   SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
530           root->getStringValue("name", "[Unnamed Panel]") );
531
532   FGPanel * panel = new FGPanel(root);
533   panel->setWidth(root->getIntValue("w", 1024));
534   panel->setHeight(root->getIntValue("h", 443));
535
536   SG_LOG( SG_COCKPIT, SG_INFO, "Size=" << panel->getWidth() << "x" << panel->getHeight() );
537
538   // Assign the background texture, if any, or a bogus chequerboard.
539   //
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 );
546
547   //
548   // Get multibackground if any...
549   //
550   for( int i = 0; i < 8; i++ ) {
551     SGPropertyNode * mbgNode = root->getChild( "multibackground", i );
552     string mbgTexture;
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
557     }
558     panel->setMultiBackground( new FGCroppedTexture(mbgTexture), i );
559     SG_LOG( SG_COCKPIT, SG_INFO, "Set multi-background texture" << i << " to " << mbgTexture );
560   }
561   //
562   // Create each instrument.
563   //
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);
572         if (instrument != 0)
573           panel->addInstrument(instrument);
574       } else {
575         SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
576         << " in instruments section" );
577       }
578     }
579   }
580   SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
581
582
583   //
584   // Return the new panel.
585   //
586   return panel;
587 }
588
589 // end of panel_io.cxx
590
591
592