Debian Codegraph @

utils/fgpanel/panel_io.cxx

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2000 David Megginson
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #ifdef HAVE_WINDOWS_H
9 #include <windows.h>
10 #endif
11
12 #include <string>
13 using std::string;
14 #include <string.h> // for strcmp()
15
16 #include <simgear/compiler.h>
17 #include <simgear/structure/exception.hxx>
18 #include <simgear/debug/logstream.hxx>
19 #include <simgear/misc/sg_path.hxx>
20 #include <simgear/props/props.hxx>
21 #include <simgear/props/condition.hxx>
22 #include <simgear/props/props_io.hxx>
23
24 #include "panel_io.hxx"
25 #include "ApplicationProperties.hxx"
26 #include "FGGroupLayer.hxx"
27 #include "FGLayeredInstrument.hxx"
28 #include "FGSwitchLayer.hxx"
29 #include "FGTextLayer.hxx"
30 #include "FGTexturedLayer.hxx"
31
32
33 ////////////////////////////////////////////////////////////////////////
34 // Read and construct a panel.
35 //
36 // The panel is specified as a regular property list, and each of the
37 // instruments is its own, separate property list (and thus, a separate
38 // XML document). The functions in this section read in the files
39 // as property lists, then extract properties to set up the panel
40 // itself.
41 //
42 // A panel contains zero or more instruments.
43 //
44 // An instrument contains one or more layers and zero or more actions.
45 //
46 // A layer contains zero or more transformations.
47 //
48 // Some special types of layers also contain other objects, such as
49 // chunks of text or other layers.
50 //
51 // There are currently four types of layers:
52 //
53 // 1. Textured Layer (type="texture"), the default
54 // 2. Text Layer (type="text")
55 // 3. Switch Layer (type="switch")
56 // 4. Built-in Layer (type="built-in", must also specify class)
57 //
58 // The only built-in layer so far is the ribbon for the magnetic compass
59 // (class="compass-ribbon").
60 //
61 // There are three types of actions:
62 //
63 // 1. Adjust (type="adjust"), the default
64 // 2. Swap (type="swap")
65 // 3. Toggle (type="toggle")
66 //
67 // There are three types of transformations:
68 //
69 // 1. X shift (type="x-shift"), the default
70 // 2. Y shift (type="y-shift")
71 // 3. Rotation (type="rotation")
72 //
73 // Each of these may be associated with a property, so that a needle
74 // will rotate with the airspeed, for example, or may have a fixed
75 // floating-point value.
76 ////////////////////////////////////////////////////////////////////////
77
78
79 /**
80 * Read a cropped texture from the instrument's property list.
81 *
82 * The x1 and y1 properties give the starting position of the texture
83 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
84 * ending position. For example, to use the bottom-left quarter of a
85 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
86 */
87 static FGCroppedTexture_ptr
88 readTexture (const SGPropertyNode *node) {
89 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getNameString());
90
91 return new FGCroppedTexture(node->getStringValue("path"),
92 node->getFloatValue("x1"),
93 node->getFloatValue("y1"),
94 node->getFloatValue("x2", 1.0),
95 node->getFloatValue("y2", 1.0));
96 }
97
98 /**
99 * Test for a condition in the current node.
100 */
101
102
103 ////////////////////////////////////////////////////////////////////////
104 // Read a condition and use it if necessary.
105 ////////////////////////////////////////////////////////////////////////
106
107 static void
108 readConditions (SGConditional *component, const SGPropertyNode *node) {
109 const SGPropertyNode * conditionNode (node->getChild ("condition"));
110 if (conditionNode != 0) {
111 // The top level is implicitly AND
112 component->setCondition (sgReadCondition (ApplicationProperties::Properties,
113 conditionNode));
114 }
115 }
116
117 /**
118 * Read a transformation from the instrument's property list.
119 *
120 * The panel module uses the transformations to slide or spin needles,
121 * knobs, and other indicators, and to place layers in the correct
122 * positions. Every layer starts centered exactly on the x,y coordinate,
123 * and many layers need to be moved or rotated simply to display the
124 * instrument correctly.
125 *
126 * There are three types of transformations:
127 *
128 * "x-shift" - move the layer horizontally.
129 *
130 * "y-shift" - move the layer vertically.
131 *
132 * "rotation" - rotate the layer.
133 *
134 * Each transformation may have a fixed offset, and may also have
135 * a floating-point property value to add to the offset. The
136 * floating-point property may be clamped to a minimum and/or
137 * maximum range and scaled (after clamping).
138 *
139 * Note that because of the way OpenGL works, transformations will
140 * appear to be applied backwards.
141 */
142 static FGPanelTransformation *
143 readTransformation (const SGPropertyNode *node, const float w_scale, const float h_scale) {
144 FGPanelTransformation *t (new FGPanelTransformation);
145
146 const string name(node->getNameString());
147 string type (node->getStringValue ("type"));
148 const string propName (node->getStringValue ("property", ""));
149 const SGPropertyNode *target (0);
150
151 if (type.empty ()) {
152 SG_LOG(SG_COCKPIT, SG_INFO,
153 "No type supplied for transformation " << name
154 << " assuming \"rotation\"");
155 type = "rotation";
156 }
157
158 if (!propName.empty ()) {
159 target = ApplicationProperties::Properties->getNode (propName.c_str (), true);
160 }
161 t->node = target;
162 t->min = node->getFloatValue ("min", -9999999);
163 t->max = node->getFloatValue ("max", 99999999);
164 t->has_mod = node->hasChild ("modulator");
165 if (t->has_mod) {
166 t->mod = node->getFloatValue ("modulator");
167 }
168 t->factor = node->getFloatValue ("scale", 1.0);
169 t->offset = node->getFloatValue ("offset", 0.0);
170
171 // Check for an interpolation table
172 const SGPropertyNode *trans_table (node->getNode ("interpolation"));
173 if (trans_table != 0) {
174 SG_LOG(SG_COCKPIT, SG_INFO, "Found interpolation table with "
175 << trans_table->nChildren() << " children");
176 t->table = new SGInterpTable();
177 for (int i = 0; i < trans_table->nChildren(); i++) {
178 const SGPropertyNode * node = trans_table->getChild(i);
179 if (node->getNameString() == "entry") {
180 double ind = node->getDoubleValue("ind", 0.0);
181 double dep = node->getDoubleValue("dep", 0.0);
182 SG_LOG(SG_COCKPIT, SG_INFO, "Adding interpolation entry " << ind << "==>" << dep);
183 t->table->addEntry(ind, dep);
184 } else {
185 SG_LOG(SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() << " in interpolation");
186 }
187 }
188 } else {
189 t->table = 0;
190 }
191
192 // Move the layer horizontally.
193 if (type == "x-shift") {
194 t->type = FGPanelTransformation::XSHIFT;
195 // t->min *= w_scale; //removed by Martin Dressler
196 // t->max *= w_scale; //removed by Martin Dressler
197 t->offset *= w_scale;
198 t->factor *= w_scale; //Added by Martin Dressler
199 }
200
201 // Move the layer vertically.
202 else if (type == "y-shift") {
203 t->type = FGPanelTransformation::YSHIFT;
204 //t->min *= h_scale; //removed
205 //t->max *= h_scale; //removed
206 t->offset *= h_scale;
207 t->factor *= h_scale; //Added
208 }
209
210 // Rotate the layer. The rotation
211 // is in degrees, and does not need
212 // to scale with the instrument size.
213 else if (type == "rotation") {
214 t->type = FGPanelTransformation::ROTATION;
215 }
216
217 else {
218 SG_LOG(SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type);
219 delete t;
220 return 0;
221 }
222
223 readConditions(t, node);
224 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read transformation " << name);
225 return t;
226 }
227
228
229 /**
230 * Read a chunk of text from the instrument's property list.
231 *
232 * A text layer consists of one or more chunks of text. All chunks
233 * share the same font size and color (and eventually, font), but
234 * each can come from a different source. There are three types of
235 * text chunks:
236 *
237 * "literal" - a literal text string (the default)
238 *
239 * "text-value" - the current value of a string property
240 *
241 * "number-value" - the current value of a floating-point property.
242 *
243 * All three may also include a printf-style format string.
244 */
245 FGTextLayer::Chunk *
246 readTextChunk (const SGPropertyNode *node) {
247 FGTextLayer::Chunk *chunk;
248 const string name (node->getStringValue ("name"));
249 string type (node->getStringValue ("type"));
250 const string format (node->getStringValue ("format"));
251
252 // Default to literal text.
253 if (type.empty ()) {
254 SG_LOG(SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
255 << " assuming \"literal\"");
256 type = "literal";
257 }
258
259 // A literal text string.
260 if (type == "literal") {
261 const string text (node->getStringValue ("text"));
262 chunk = new FGTextLayer::Chunk (text, format);
263 } else if (type == "text-value") {
264 // The value of a string property.
265 const SGPropertyNode *target
266 (ApplicationProperties::Properties->getNode (node->getStringValue ("property"), true));
267 chunk = new FGTextLayer::Chunk (FGTextLayer::TEXT_VALUE, target, format);
268 } else if (type == "number-value") {
269 // The value of a float property.
270 const string propName (node->getStringValue ("property"));
271 const float scale (node->getFloatValue ("scale", 1.0));
272 const float offset (node->getFloatValue ("offset", 0.0));
273 const bool truncation (node->getBoolValue ("truncate", false));
274 const SGPropertyNode *target (ApplicationProperties::Properties->getNode (propName.c_str (), true));
275 chunk = new FGTextLayer::Chunk (FGTextLayer::DOUBLE_VALUE, target,
276 format, scale, offset, truncation);
277 } else {
278 // Unknown type.
279 SG_LOG(SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
280 << " for text chunk " << name);
281 return 0;
282 }
283
284 readConditions (chunk, node);
285 return chunk;
286 }
287
288 /**
289 * Read a single layer from an instrument's property list.
290 *
291 * Each instrument consists of one or more layers stacked on top
292 * of each other; the lower layers show through only where the upper
293 * layers contain an alpha component. Each layer can be moved
294 * horizontally and vertically and rotated using transformations.
295 *
296 * This module currently recognizes four kinds of layers:
297 *
298 * "texture" - a layer containing a texture (the default)
299 *
300 * "text" - a layer containing text
301 *
302 * "switch" - a layer that switches between two other layers
303 * based on the current value of a boolean property.
304 *
305 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
306 *
307 * Currently, the only built-in layer class is "compass-ribbon".
308 */
309 static FGInstrumentLayer *
310 readLayer (const SGPropertyNode *node, const float w_scale, const float h_scale) {
311 FGInstrumentLayer *layer (NULL);
312 const string name (node->getStringValue ("name"));
313 string type (node->getStringValue ("type"));
314 int w (node->getIntValue ("w", -1));
315 int h (node->getIntValue ("h", -1));
316 const bool emissive (node->getBoolValue ("emissive", false));
317 if (w != -1) {
318 w = int (w * w_scale);
319 }
320 if (h != -1) {
321 h = int (h * h_scale);
322 }
323 if (type.empty ()) {
324 SG_LOG(SG_COCKPIT, SG_INFO,
325 "No type supplied for layer " << name
326 << " assuming \"texture\"");
327 type = "texture";
328 }
329
330 // A textured instrument layer.
331 if (type == "texture") {
332 const FGCroppedTexture_ptr texture (readTexture (node->getNode ("texture")));
333 layer = new FGTexturedLayer (texture, w, h);
334 if (emissive) {
335 FGTexturedLayer *tl = (FGTexturedLayer*) layer;
336 tl->setEmissive (true);
337 }
338 } else if (type == "group") {
339 // A group of sublayers.
340 layer = new FGGroupLayer ();
341 for (int i = 0; i < node->nChildren(); i++) {
342 const SGPropertyNode *child = node->getChild (i);
343 if (child->getNameString() == "layer") {
344 ((FGGroupLayer*)layer)->addLayer(readLayer(child, w_scale, h_scale));
345 }
346 }
347 } else if (type == "text") {
348 // A textual instrument layer.
349 FGTextLayer *tlayer (new FGTextLayer (w, h)); // FIXME
350
351 // Set the text color.
352 const float red (node->getFloatValue ("color/red", 0.0));
353 const float green (node->getFloatValue ("color/green", 0.0));
354 const float blue (node->getFloatValue ("color/blue", 0.0));
355 tlayer->setColor (red, green, blue);
356
357 // Set the point size.
358 const float pointSize (node->getFloatValue ("point-size", 10.0) * w_scale);
359 tlayer->setPointSize (pointSize);
360
361 // Set the font.
362 const string fontName (node->getStringValue ("font", "7-Segment"));
363 tlayer->setFontName (fontName);
364
365 const SGPropertyNode *chunk_group (node->getNode ("chunks"));
366 if (chunk_group != 0) {
367 const int nChunks (chunk_group->nChildren ());
368 for (int i = 0; i < nChunks; i++) {
369 const SGPropertyNode *node (chunk_group->getChild (i));
370 if (node->getNameString() == "chunk") {
371 FGTextLayer::Chunk* const chunk(readTextChunk(node));
372 if (chunk != 0) {
373 tlayer->addChunk(chunk);
374 }
375 } else {
376 SG_LOG(SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() << " in chunks");
377 }
378 }
379 layer = tlayer;
380 }
381 } else if (type == "switch") {
382 // A switch instrument layer.
383 layer = new FGSwitchLayer ();
384 for (int i = 0; i < node->nChildren (); i++) {
385 const SGPropertyNode *child (node->getChild (i));
386 if (child->getNameString() == "layer") {
387 ((FGGroupLayer*)layer)->addLayer(readLayer(child, w_scale, h_scale));
388 }
389 }
390 } else {
391 // An unknown type.
392 SG_LOG(SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type);
393 delete layer;
394 return 0;
395 }
396
397 //
398 // Get the transformations for each layer.
399 //
400 const SGPropertyNode *trans_group (node->getNode ("transformations"));
401 if (trans_group != 0) {
402 const int nTransformations (trans_group->nChildren ());
403 for (int i = 0; i < nTransformations; i++) {
404 const SGPropertyNode *node (trans_group->getChild (i));
405 if (node->getNameString() == "transformation") {
406 FGPanelTransformation* const t(readTransformation(node, w_scale, h_scale));
407 if (t != 0) {
408 layer->addTransformation(t);
409 }
410 } else {
411 SG_LOG(SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() << " in transformations");
412 }
413 }
414 }
415
416 readConditions (layer, node);
417 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read layer " << name);
418 return layer;
419 }
420
421 /**
422 * Read an instrument from a property list.
423 *
424 * The instrument consists of a preferred width and height
425 * (the panel may override these), together with a list of layers
426 * and a list of actions to be performed when the user clicks
427 * the mouse over the instrument. All coordinates are relative
428 * to the instrument's position, so instruments are fully relocatable;
429 * likewise, coordinates for actions and transformations will be
430 * scaled automatically if the instrument is not at its preferred size.
431 */
432 static FGPanelInstrument *
433 readInstrument (const SGPropertyNode *node) {
434 const string name (node->getStringValue ("name"));
435 const int x (node->getIntValue ("x", -1));
436 const int y (node->getIntValue ("y", -1));
437 const int real_w (node->getIntValue ("w", -1));
438 const int real_h (node->getIntValue ("h", -1));
439 int w (node->getIntValue ("w-base", -1));
440 int h (node->getIntValue ("h-base", -1));
441
442 if (x == -1 || y == -1) {
443 SG_LOG(SG_COCKPIT, SG_ALERT,
444 "x and y positions must be specified and > 0");
445 return 0;
446 }
447
448 float w_scale (1.0);
449 float h_scale (1.0);
450 if (real_w != -1) {
451 w_scale = float (real_w) / float (w);
452 w = real_w;
453 }
454 if (real_h != -1) {
455 h_scale = float (real_h) / float (h);
456 h = real_h;
457 }
458
459 SG_LOG(SG_COCKPIT, SG_DEBUG, "Reading instrument " << name);
460
461 FGLayeredInstrument * const instrument
462 (new FGLayeredInstrument (x, y, w, h));
463
464 //
465 // Get the layers for the instrument.
466 //
467 const SGPropertyNode *layer_group (node->getNode ("layers"));
468 if (layer_group != 0) {
469 const int nLayers (layer_group->nChildren ());
470 for (int i = 0; i < nLayers; i++) {
471 const SGPropertyNode *node (layer_group->getChild (i));
472 if (node->getNameString() == "layer") {
473 FGInstrumentLayer* const layer(readLayer(node, w_scale, h_scale));
474 if (layer != 0) {
475 instrument->addLayer(layer);
476 }
477 } else {
478 SG_LOG(SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() << " in layers");
479 }
480 }
481 }
482
483 readConditions (instrument, node);
484 SG_LOG(SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name);
485 return instrument;
486 }
487
488 /**
489 * Construct the panel from a property tree.
490 */
491 SGSharedPtr<FGPanel>
492 FGReadablePanel::read (SGPropertyNode_ptr root) {
493 SG_LOG(SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
494 root->getStringValue ("name", "[Unnamed Panel]"));
495
496 FGPanel * const panel (new FGPanel (root));
497 panel->setWidth (root->getIntValue ("w", 1024));
498 panel->setHeight (root->getIntValue ("h", 443));
499
500 SG_LOG(SG_COCKPIT, SG_INFO, "Size=" << panel->getWidth () << "x" << panel->getHeight ());
501
502 // Assign the background texture, if any, or a bogus chequerboard.
503 //
504 const string bgTexture (root->getStringValue ("background"));
505 if (!bgTexture.empty ()) {
506 panel->setBackground (new FGCroppedTexture (bgTexture));
507 }
508 panel->setBackgroundWidth (root->getDoubleValue( "background-width", 1.0));
509 panel->setBackgroundHeight (root->getDoubleValue( "background-height", 1.0));
510 SG_LOG(SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture);
511
512 //
513 // Get multibackground if any...
514 //
515 for (int i = 0; i < 8; i++) {
516 SGPropertyNode * const mbgNode (root->getChild ("multibackground", i));
517 string mbgTexture;
518 if (mbgNode != NULL) {
519 mbgTexture = mbgNode->getStringValue ();
520 }
521 if (mbgTexture.empty ()) {
522 if (i == 0) {
523 break; // if first texture is missing, ignore the rest
524 } else {
525 mbgTexture = "FOO"; // if others are missing - set default texture
526 }
527 }
528 panel->setMultiBackground (new FGCroppedTexture (mbgTexture), i);
529 SG_LOG(SG_COCKPIT, SG_INFO, "Set multi-background texture" << i << " to " << mbgTexture);
530 }
531 //
532 // Create each instrument.
533 //
534 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
535 const SGPropertyNode *instrument_group (root->getChild ("instruments"));
536 if (instrument_group != 0) {
537 const int nInstruments (instrument_group->nChildren ());
538 for (int i = 0; i < nInstruments; i++) {
539 const SGPropertyNode *node = instrument_group->getChild (i);
540 if (node->getNameString() == "instrument") {
541 FGPanelInstrument* const instrument(readInstrument(node));
542 if (instrument != 0) {
543 panel->addInstrument(instrument);
544 }
545 } else {
546 SG_LOG(SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() << " in instruments section");
547 }
548 }
549 }
550 SG_LOG(SG_COCKPIT, SG_INFO, "Done reading panel instruments");
551
552
553 //
554 // Return the new panel.
555 //
556 return panel;
557 }
558
559 // end of panel_io.cxx