| 1 | /* |
| 2 | * SPDX-FileName: panel_io.cxx |
| 3 | * SPDX-FileComment: I/O for 2D panel |
| 4 | * SPDX-FileCopyrightText: Written by David Megginson, started January 2000 |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <config.h> |
| 9 | |
| 10 | #include <string.h> // for strcmp() |
| 11 | |
| 12 | #include <simgear/compiler.h> |
| 13 | #include <simgear/structure/exception.hxx> |
| 14 | #include <simgear/debug/logstream.hxx> |
| 15 | #include <simgear/misc/sg_path.hxx> |
| 16 | #include <simgear/props/props.hxx> |
| 17 | #include <simgear/props/props_io.hxx> |
| 18 | #include <simgear/debug/ErrorReportingCallback.hxx> |
| 19 | |
| 20 | #include <istream> |
| 21 | #include <fstream> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <Main/globals.hxx> |
| 25 | #include <Main/fg_props.hxx> |
| 26 | #include <Main/sentryIntegration.hxx> |
| 27 | |
| 28 | #include <GUI/gui.h> |
| 29 | |
| 30 | // #include "panel.hxx" |
| 31 | #include "panel_io.hxx" |
| 32 | #include <Instrumentation/KLN89/kln89.hxx> |
| 33 | |
| 34 | //built-in layers |
| 35 | #include "built_in/FGMagRibbon.hxx" |
| 36 | |
| 37 | using std::istream; |
| 38 | using std::ifstream; |
| 39 | using std::string; |
| 40 | |
| 41 | |
| 42 | |
| 43 | //////////////////////////////////////////////////////////////////////// |
| 44 | // Read and construct a panel. |
| 45 | // |
| 46 | // The panel is specified as a regular property list, and each of the |
| 47 | // instruments is its own, separate property list (and thus, a separate |
| 48 | // XML document). The functions in this section read in the files |
| 49 | // as property lists, then extract properties to set up the panel |
| 50 | // itself. |
| 51 | // |
| 52 | // A panel contains zero or more instruments. |
| 53 | // |
| 54 | // An instrument contains one or more layers and zero or more actions. |
| 55 | // |
| 56 | // A layer contains zero or more transformations. |
| 57 | // |
| 58 | // Some special types of layers also contain other objects, such as |
| 59 | // chunks of text or other layers. |
| 60 | // |
| 61 | // There are currently four types of layers: |
| 62 | // |
| 63 | // 1. Textured Layer (type="texture"), the default |
| 64 | // 2. Text Layer (type="text") |
| 65 | // 3. Switch Layer (type="switch") |
| 66 | // 4. Built-in Layer (type="built-in", must also specify class) |
| 67 | // |
| 68 | // The only built-in layer so far is the ribbon for the magnetic compass |
| 69 | // (class="compass-ribbon"). |
| 70 | // |
| 71 | // There are three types of actions: |
| 72 | // |
| 73 | // 1. Adjust (type="adjust"), the default |
| 74 | // 2. Swap (type="swap") |
| 75 | // 3. Toggle (type="toggle") |
| 76 | // |
| 77 | // There are three types of transformations: |
| 78 | // |
| 79 | // 1. X shift (type="x-shift"), the default |
| 80 | // 2. Y shift (type="y-shift") |
| 81 | // 3. Rotation (type="rotation") |
| 82 | // |
| 83 | // Each of these may be associated with a property, so that a needle |
| 84 | // will rotate with the airspeed, for example, or may have a fixed |
| 85 | // floating-point value. |
| 86 | //////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Read a cropped texture from the instrument's property list. |
| 91 | * |
| 92 | * The x1 and y1 properties give the starting position of the texture |
| 93 | * (between 0.0 and 1.0), and the the x2 and y2 properties give the |
| 94 | * ending position. For example, to use the bottom-left quarter of a |
| 95 | * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5. |
| 96 | */ |
| 97 | static FGCroppedTexture |
| 98 | readTexture (const SGPropertyNode * node) |
| 99 | { |
| 100 | FGCroppedTexture texture(node->getStringValue("path"), |
| 101 | node->getFloatValue("x1"), |
| 102 | node->getFloatValue("y1"), |
| 103 | node->getFloatValue("x2", 1.0), |
| 104 | node->getFloatValue("y2", 1.0)); |
| 105 | SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getNameString()); |
| 106 | return texture; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Test for a condition in the current node. |
| 112 | */ |
| 113 | |
| 114 | //////////////////////////////////////////////////////////////////////// |
| 115 | // Read a condition and use it if necessary. |
| 116 | //////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | static void |
| 119 | readConditions (SGConditional *component, const SGPropertyNode *node) |
| 120 | { |
| 121 | const SGPropertyNode * conditionNode = node->getChild("condition"); |
| 122 | if (conditionNode != 0) |
| 123 | // The top level is implicitly AND |
| 124 | component->setCondition(sgReadCondition(globals->get_props(), |
| 125 | conditionNode) ); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * Read an action from the instrument's property list. |
| 131 | * |
| 132 | * The action will be performed when the user clicks a mouse button |
| 133 | * within the specified region of the instrument. Actions always work |
| 134 | * by modifying the value of a property (see the SGPropertyNode |
| 135 | * class). |
| 136 | * |
| 137 | * The following action types are defined: |
| 138 | * |
| 139 | * "adjust" - modify the value of a floating-point property by |
| 140 | * the increment specified. This is the default. |
| 141 | * |
| 142 | * "swap" - swap the values of two-floating-point properties. |
| 143 | * |
| 144 | * "toggle" - toggle the value of a boolean property between true and |
| 145 | * false. |
| 146 | * |
| 147 | * For the adjust action, it is possible to specify an increment |
| 148 | * (use a negative number for a decrement), a minimum allowed value, |
| 149 | * a maximum allowed value, and a flag to indicate whether the value |
| 150 | * should freeze or wrap-around when it reaches the minimum or maximum. |
| 151 | * |
| 152 | * The action will be scaled automatically if the instrument is not |
| 153 | * being drawn at its regular size. |
| 154 | */ |
| 155 | static FGPanelAction * |
| 156 | readAction (const SGPropertyNode * node, float w_scale, float h_scale) |
| 157 | { |
| 158 | auto propRoot = globals->get_props(); |
| 159 | |
| 160 | // button-less actions are fired initially |
| 161 | if (!node->hasValue("w") || !node->hasValue("h")) { |
| 162 | for (auto bn : node->getChildren("binding")) { |
| 163 | SGBinding b(bn, propRoot); |
| 164 | b.fire(); |
| 165 | } |
| 166 | |
| 167 | return nullptr; |
| 168 | } |
| 169 | |
| 170 | string name = node->getStringValue("name"); |
| 171 | |
| 172 | int button = node->getIntValue("button"); |
| 173 | int x = int(node->getIntValue("x") * w_scale); |
| 174 | int y = int(node->getIntValue("y") * h_scale); |
| 175 | int w = int(node->getIntValue("w") * w_scale); |
| 176 | int h = int(node->getIntValue("h") * h_scale); |
| 177 | bool repeatable = node->getBoolValue("repeatable", true); |
| 178 | |
| 179 | FGPanelAction * action = new FGPanelAction(button, x, y, w, h, repeatable); |
| 180 | |
| 181 | action->setBindings(readBindingList(node->getChildren("binding"), propRoot), 0); |
| 182 | if (node->hasChild("mod-up")) { |
| 183 | action->setBindings(readBindingList(node->getChild("mod-up")->getChildren("binding"), propRoot), 1); |
| 184 | } |
| 185 | |
| 186 | readConditions(action, node); |
| 187 | return action; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Read a transformation from the instrument's property list. |
| 193 | * |
| 194 | * The panel module uses the transformations to slide or spin needles, |
| 195 | * knobs, and other indicators, and to place layers in the correct |
| 196 | * positions. Every layer starts centered exactly on the x,y co-ordinate, |
| 197 | * and many layers need to be moved or rotated simply to display the |
| 198 | * instrument correctly. |
| 199 | * |
| 200 | * There are three types of transformations: |
| 201 | * |
| 202 | * "x-shift" - move the layer horizontally. |
| 203 | * |
| 204 | * "y-shift" - move the layer vertically. |
| 205 | * |
| 206 | * "rotation" - rotate the layer. |
| 207 | * |
| 208 | * Each transformation may have a fixed offset, and may also have |
| 209 | * a floating-point property value to add to the offset. The |
| 210 | * floating-point property may be clamped to a minimum and/or |
| 211 | * maximum range and scaled (after clamping). |
| 212 | * |
| 213 | * Note that because of the way OpenGL works, transformations will |
| 214 | * appear to be applied backwards. |
| 215 | */ |
| 216 | static FGPanelTransformation * |
| 217 | readTransformation (const SGPropertyNode * node, float w_scale, float h_scale) |
| 218 | { |
| 219 | FGPanelTransformation * t = new FGPanelTransformation; |
| 220 | |
| 221 | string name = node->getNameString(); |
| 222 | string type = node->getStringValue("type"); |
| 223 | string propName = node->getStringValue("property", ""); |
| 224 | SGPropertyNode * target = 0; |
| 225 | |
| 226 | if (type.empty()) { |
| 227 | SG_LOG( SG_COCKPIT, SG_BULK, |
| 228 | "No type supplied for transformation " << name |
| 229 | << " assuming \"rotation\"" ); |
| 230 | type = "rotation"; |
| 231 | } |
| 232 | |
| 233 | if (!propName.empty()) { |
| 234 | target = fgGetNode(propName.c_str(), true); |
| 235 | } |
| 236 | |
| 237 | t->node = target; |
| 238 | t->min = node->getFloatValue("min", -9999999); |
| 239 | t->max = node->getFloatValue("max", 99999999); |
| 240 | t->has_mod = node->hasChild("modulator"); |
| 241 | if (t->has_mod) |
| 242 | t->mod = node->getFloatValue("modulator"); |
| 243 | t->factor = node->getFloatValue("scale", 1.0); |
| 244 | t->offset = node->getFloatValue("offset", 0.0); |
| 245 | |
| 246 | // Check for an interpolation table |
| 247 | const SGPropertyNode * trans_table = node->getNode("interpolation"); |
| 248 | if (trans_table != 0) { |
| 249 | SG_LOG( SG_COCKPIT, SG_DEBUG, "Found interpolation table with " |
| 250 | << trans_table->nChildren() << " children" ); |
| 251 | t->table = new SGInterpTable(trans_table); |
| 252 | } else { |
| 253 | t->table = 0; |
| 254 | } |
| 255 | |
| 256 | // Move the layer horizontally. |
| 257 | if (type == "x-shift") { |
| 258 | t->type = FGPanelTransformation::XSHIFT; |
| 259 | // t->min *= w_scale; //removed by Martin Dressler |
| 260 | // t->max *= w_scale; //removed by Martin Dressler |
| 261 | t->offset *= w_scale; |
| 262 | t->factor *= w_scale; //Added by Martin Dressler |
| 263 | } |
| 264 | |
| 265 | // Move the layer vertically. |
| 266 | else if (type == "y-shift") { |
| 267 | t->type = FGPanelTransformation::YSHIFT; |
| 268 | //t->min *= h_scale; //removed |
| 269 | //t->max *= h_scale; //removed |
| 270 | t->offset *= h_scale; |
| 271 | t->factor *= h_scale; //Added |
| 272 | } |
| 273 | |
| 274 | // Rotate the layer. The rotation |
| 275 | // is in degrees, and does not need |
| 276 | // to scale with the instrument size. |
| 277 | else if (type == "rotation") { |
| 278 | t->type = FGPanelTransformation::ROTATION; |
| 279 | } |
| 280 | |
| 281 | else { |
| 282 | SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type ); |
| 283 | delete t; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | readConditions(t, node); |
| 288 | SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name ); |
| 289 | return t; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * Read a chunk of text from the instrument's property list. |
| 295 | * |
| 296 | * A text layer consists of one or more chunks of text. All chunks |
| 297 | * share the same font size and color (and eventually, font), but |
| 298 | * each can come from a different source. There are three types of |
| 299 | * text chunks: |
| 300 | * |
| 301 | * "literal" - a literal text string (the default) |
| 302 | * |
| 303 | * "text-value" - the current value of a string property |
| 304 | * |
| 305 | * "number-value" - the current value of a floating-point property. |
| 306 | * |
| 307 | * All three may also include a printf-style format string. |
| 308 | */ |
| 309 | FGTextLayer::Chunk * |
| 310 | readTextChunk (const SGPropertyNode * node) |
| 311 | { |
| 312 | FGTextLayer::Chunk * chunk; |
| 313 | string name = node->getStringValue("name"); |
| 314 | string type = node->getStringValue("type"); |
| 315 | string format = node->getStringValue("format"); |
| 316 | |
| 317 | // Default to literal text. |
| 318 | if (type.empty()) { |
| 319 | SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name |
| 320 | << " assuming \"literal\""); |
| 321 | type = "literal"; |
| 322 | } |
| 323 | |
| 324 | // A literal text string. |
| 325 | if (type == "literal") { |
| 326 | string text = node->getStringValue("text"); |
| 327 | chunk = new FGTextLayer::Chunk(text, format); |
| 328 | } |
| 329 | |
| 330 | // The value of a string property. |
| 331 | else if (type == "text-value") { |
| 332 | SGPropertyNode * target = |
| 333 | fgGetNode(node->getStringValue("property"), true); |
| 334 | chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format); |
| 335 | } |
| 336 | |
| 337 | // The value of a float property. |
| 338 | else if (type == "number-value") { |
| 339 | string propName = node->getStringValue("property"); |
| 340 | float scale = node->getFloatValue("scale", 1.0); |
| 341 | float offset = node->getFloatValue("offset", 0.0); |
| 342 | bool truncation = node->getBoolValue("truncate", false); |
| 343 | SGPropertyNode * target = fgGetNode(propName.c_str(), true); |
| 344 | chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target, |
| 345 | format, scale, offset, truncation); |
| 346 | } |
| 347 | |
| 348 | // Unknown type. |
| 349 | else { |
| 350 | SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type |
| 351 | << " for text chunk " << name ); |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | readConditions(chunk, node); |
| 356 | return chunk; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /** |
| 361 | * Read a single layer from an instrument's property list. |
| 362 | * |
| 363 | * Each instrument consists of one or more layers stacked on top |
| 364 | * of each other; the lower layers show through only where the upper |
| 365 | * layers contain an alpha component. Each layer can be moved |
| 366 | * horizontally and vertically and rotated using transformations. |
| 367 | * |
| 368 | * This module currently recognizes four kinds of layers: |
| 369 | * |
| 370 | * "texture" - a layer containing a texture (the default) |
| 371 | * |
| 372 | * "text" - a layer containing text |
| 373 | * |
| 374 | * "switch" - a layer that switches between two other layers |
| 375 | * based on the current value of a boolean property. |
| 376 | * |
| 377 | * "built-in" - a hard-coded layer supported by C++ code in FlightGear. |
| 378 | * |
| 379 | * Currently, the only built-in layer class is "compass-ribbon". |
| 380 | */ |
| 381 | static FGInstrumentLayer * |
| 382 | readLayer (const SGPropertyNode * node, float w_scale, float h_scale) |
| 383 | { |
| 384 | FGInstrumentLayer * layer = NULL; |
| 385 | string name = node->getStringValue("name"); |
| 386 | string type = node->getStringValue("type"); |
| 387 | int w = node->getIntValue("w", -1); |
| 388 | int h = node->getIntValue("h", -1); |
| 389 | bool emissive = node->getBoolValue("emissive", false); |
| 390 | if (w != -1) |
| 391 | w = int(w * w_scale); |
| 392 | if (h != -1) |
| 393 | h = int(h * h_scale); |
| 394 | |
| 395 | |
| 396 | if (type.empty()) { |
| 397 | SG_LOG( SG_COCKPIT, SG_BULK, |
| 398 | "No type supplied for layer " << name |
| 399 | << " assuming \"texture\"" ); |
| 400 | type = "texture"; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | // A textured instrument layer. |
| 405 | if (type == "texture") { |
| 406 | FGCroppedTexture texture = readTexture(node->getNode("texture")); |
| 407 | layer = new FGTexturedLayer(texture, w, h); |
| 408 | if (emissive) { |
| 409 | FGTexturedLayer *tl=(FGTexturedLayer*)layer; |
| 410 | tl->setEmissive(true); |
| 411 | } |
| 412 | |
| 413 | } |
| 414 | |
| 415 | // A group of sublayers. |
| 416 | else if (type == "group") { |
| 417 | layer = new FGGroupLayer(); |
| 418 | for (int i = 0; i < node->nChildren(); i++) { |
| 419 | const SGPropertyNode * child = node->getChild(i); |
| 420 | if (child->getNameString() == "layer") |
| 421 | ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale)); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | |
| 426 | // A textual instrument layer. |
| 427 | else if (type == "text") { |
| 428 | FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME |
| 429 | |
| 430 | // Set the text color. |
| 431 | float red = node->getFloatValue("color/red", 0.0); |
| 432 | float green = node->getFloatValue("color/green", 0.0); |
| 433 | float blue = node->getFloatValue("color/blue", 0.0); |
| 434 | tlayer->setColor(red, green, blue); |
| 435 | |
| 436 | // Set the point size. |
| 437 | float pointSize = node->getFloatValue("point-size", 10.0) * w_scale; |
| 438 | tlayer->setPointSize(pointSize); |
| 439 | |
| 440 | // Set the font. |
| 441 | string fontName = node->getStringValue("font", "Helvetica"); |
| 442 | tlayer->setFontName(fontName); |
| 443 | |
| 444 | const SGPropertyNode * chunk_group = node->getNode("chunks"); |
| 445 | if (chunk_group != 0) { |
| 446 | int nChunks = chunk_group->nChildren(); |
| 447 | for (int i = 0; i < nChunks; i++) { |
| 448 | const SGPropertyNode * node = chunk_group->getChild(i); |
| 449 | if (node->getNameString() == "chunk") { |
| 450 | FGTextLayer::Chunk * chunk = readTextChunk(node); |
| 451 | if (chunk != 0) |
| 452 | tlayer->addChunk(chunk); |
| 453 | } else { |
| 454 | SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() |
| 455 | << " in chunks" ); |
| 456 | } |
| 457 | } |
| 458 | layer = tlayer; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // A switch instrument layer. |
| 463 | else if (type == "switch") { |
| 464 | layer = new FGSwitchLayer(); |
| 465 | for (int i = 0; i < node->nChildren(); i++) { |
| 466 | const SGPropertyNode * child = node->getChild(i); |
| 467 | if (child->getNameString() == "layer") |
| 468 | ((FGGroupLayer *)layer)->addLayer(readLayer(child, w_scale, h_scale)); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // A built-in instrument layer. |
| 473 | else if (type == "built-in") { |
| 474 | string layerclass = node->getStringValue("class"); |
| 475 | |
| 476 | if (layerclass == "mag-ribbon") { |
| 477 | layer = new FGMagRibbon(w, h); |
| 478 | } |
| 479 | |
| 480 | else if (layerclass.empty()) { |
| 481 | SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer " |
| 482 | << name ); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | else { |
| 487 | SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class " |
| 488 | << layerclass); |
| 489 | return 0; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // An unknown type. |
| 494 | else { |
| 495 | SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type ); |
| 496 | delete layer; |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | // |
| 501 | // Get the transformations for each layer. |
| 502 | // |
| 503 | const SGPropertyNode * trans_group = node->getNode("transformations"); |
| 504 | if (trans_group != 0) { |
| 505 | int nTransformations = trans_group->nChildren(); |
| 506 | for (int i = 0; i < nTransformations; i++) { |
| 507 | const SGPropertyNode * node = trans_group->getChild(i); |
| 508 | if (node->getNameString() == "transformation") { |
| 509 | FGPanelTransformation * t = readTransformation(node, w_scale, h_scale); |
| 510 | if (t != 0) |
| 511 | layer->addTransformation(t); |
| 512 | } else { |
| 513 | SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() |
| 514 | << " in transformations" ); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | readConditions(layer, node); |
| 520 | SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name ); |
| 521 | return layer; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /** |
| 526 | * Read an instrument from a property list. |
| 527 | * |
| 528 | * The instrument consists of a preferred width and height |
| 529 | * (the panel may override these), together with a list of layers |
| 530 | * and a list of actions to be performed when the user clicks |
| 531 | * the mouse over the instrument. All co-ordinates are relative |
| 532 | * to the instrument's position, so instruments are fully relocatable; |
| 533 | * likewise, co-ordinates for actions and transformations will be |
| 534 | * scaled automatically if the instrument is not at its preferred size. |
| 535 | */ |
| 536 | static FGPanelInstrument * |
| 537 | readInstrument (const SGPropertyNode * node, const SGPath& path) |
| 538 | { |
| 539 | const string name = node->getStringValue("name"); |
| 540 | int x = node->getIntValue("x", -1); |
| 541 | int y = node->getIntValue("y", -1); |
| 542 | int real_w = node->getIntValue("w", -1); |
| 543 | int real_h = node->getIntValue("h", -1); |
| 544 | int w = node->getIntValue("w-base", -1); |
| 545 | int h = node->getIntValue("h-base", -1); |
| 546 | |
| 547 | if (x == -1 || y == -1) { |
| 548 | simgear::reportFailure(simgear::LoadFailure::Misconfigured, simgear::ErrorCode::AircraftSystems, |
| 549 | "Panel x and y positions must be specified and > 0", path); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | float w_scale = 1.0; |
| 554 | float h_scale = 1.0; |
| 555 | if (real_w != -1) { |
| 556 | w_scale = float(real_w) / float(w); |
| 557 | w = real_w; |
| 558 | } |
| 559 | if (real_h != -1) { |
| 560 | h_scale = float(real_h) / float(h); |
| 561 | h = real_h; |
| 562 | } |
| 563 | |
| 564 | SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name ); |
| 565 | |
| 566 | FGLayeredInstrument * instrument = |
| 567 | new FGLayeredInstrument(x, y, w, h); |
| 568 | |
| 569 | // |
| 570 | // Get the actions for the instrument. |
| 571 | // |
| 572 | const SGPropertyNode * action_group = node->getNode("actions"); |
| 573 | if (action_group != 0) { |
| 574 | int nActions = action_group->nChildren(); |
| 575 | for (int i = 0; i < nActions; i++) { |
| 576 | const SGPropertyNode * node = action_group->getChild(i); |
| 577 | if (node->getNameString() == "action") { |
| 578 | FGPanelAction * action = readAction(node, w_scale, h_scale); |
| 579 | if (action != 0) |
| 580 | instrument->addAction(action); |
| 581 | } else { |
| 582 | SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() |
| 583 | << " in actions" ); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // |
| 589 | // Get the layers for the instrument. |
| 590 | // |
| 591 | const SGPropertyNode * layer_group = node->getNode("layers"); |
| 592 | if (layer_group != 0) { |
| 593 | int nLayers = layer_group->nChildren(); |
| 594 | for (int i = 0; i < nLayers; i++) { |
| 595 | const SGPropertyNode * node = layer_group->getChild(i); |
| 596 | if (node->getNameString() == "layer") { |
| 597 | FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale); |
| 598 | if (layer != 0) |
| 599 | instrument->addLayer(layer); |
| 600 | } else { |
| 601 | SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getNameString() |
| 602 | << " in layers" ); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | readConditions(instrument, node); |
| 608 | return instrument; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /** |
| 613 | * Construct the panel from a property tree. |
| 614 | */ |
| 615 | FGPanel * |
| 616 | readPanel (const SGPropertyNode * root, const SGPath& path) |
| 617 | { |
| 618 | SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " << |
| 619 | root->getStringValue("name", "[Unnamed Panel]") ); |
| 620 | simgear::ErrorReportContext ec("2d-panel", path.utf8Str()); |
| 621 | |
| 622 | FGPanel * panel = new FGPanel(); |
| 623 | panel->setWidth(root->getIntValue("w", 1024)); |
| 624 | panel->setHeight(root->getIntValue("h", 443)); |
| 625 | |
| 626 | // |
| 627 | // Grab the visible external viewing area, default to |
| 628 | // |
| 629 | // panel->setViewHeight(root->getIntValue("view-height", |
| 630 | // 768 - panel->getHeight() + 2)); |
| 631 | |
| 632 | // |
| 633 | // Grab the panel's initial offsets, default to 0, 0. |
| 634 | // |
| 635 | if (!fgHasNode("/sim/panel/x-offset")) |
| 636 | fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0)); |
| 637 | |
| 638 | // conditional removed by jim wilson to allow panel xml code |
| 639 | // with y-offset defined to work... |
| 640 | if (!fgHasNode("/sim/panel/y-offset")) |
| 641 | fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0)); |
| 642 | |
| 643 | panel->setAutohide(root->getBoolValue("autohide", true)); |
| 644 | |
| 645 | // |
| 646 | // Assign the background texture, if any, or a bogus chequerboard. |
| 647 | // |
| 648 | string bgTexture = root->getStringValue("background"); |
| 649 | if (bgTexture.empty()) |
| 650 | bgTexture = "FOO"; |
| 651 | panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str())); |
| 652 | |
| 653 | // |
| 654 | // Get multibackground if any... |
| 655 | // |
| 656 | string mbgTexture = root->getStringValue("multibackground[0]"); |
| 657 | if (!mbgTexture.empty()) { |
| 658 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 0); |
| 659 | |
| 660 | mbgTexture = root->getStringValue("multibackground[1]"); |
| 661 | if (mbgTexture.empty()) |
| 662 | mbgTexture = "FOO"; |
| 663 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 1); |
| 664 | |
| 665 | mbgTexture = root->getStringValue("multibackground[2]"); |
| 666 | if (mbgTexture.empty()) |
| 667 | mbgTexture = "FOO"; |
| 668 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 2); |
| 669 | |
| 670 | mbgTexture = root->getStringValue("multibackground[3]"); |
| 671 | if (mbgTexture.empty()) |
| 672 | mbgTexture = "FOO"; |
| 673 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 3); |
| 674 | |
| 675 | mbgTexture = root->getStringValue("multibackground[4]"); |
| 676 | if (mbgTexture.empty()) |
| 677 | mbgTexture = "FOO"; |
| 678 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 4); |
| 679 | |
| 680 | mbgTexture = root->getStringValue("multibackground[5]"); |
| 681 | if (mbgTexture.empty()) |
| 682 | mbgTexture = "FOO"; |
| 683 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 5); |
| 684 | |
| 685 | mbgTexture = root->getStringValue("multibackground[6]"); |
| 686 | if (mbgTexture.empty()) |
| 687 | mbgTexture = "FOO"; |
| 688 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 6); |
| 689 | |
| 690 | mbgTexture = root->getStringValue("multibackground[7]"); |
| 691 | if (mbgTexture.empty()) |
| 692 | mbgTexture = "FOO"; |
| 693 | panel->setMultiBackground(FGTextureManager::createTexture(mbgTexture.c_str()), 7); |
| 694 | |
| 695 | } |
| 696 | |
| 697 | |
| 698 | |
| 699 | // |
| 700 | // Create each instrument. |
| 701 | // |
| 702 | SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading panel instruments" ); |
| 703 | const SGPropertyNode * instrument_group = root->getChild("instruments"); |
| 704 | if (instrument_group != 0) { |
| 705 | int nInstruments = instrument_group->nChildren(); |
| 706 | for (int i = 0; i < nInstruments; i++) { |
| 707 | const SGPropertyNode * node = instrument_group->getChild(i); |
| 708 | if (node->getNameString() == "instrument") { |
| 709 | FGPanelInstrument * instrument = readInstrument(node, path); |
| 710 | if (instrument != 0) |
| 711 | panel->addInstrument(instrument); |
| 712 | } else if (node->getNameString() == "special-instrument") { |
| 713 | //cout << "Special instrument found in instruments section!\n"; |
| 714 | const string name = node->getStringValue("name"); |
| 715 | if (name == "KLN89 GPS") { |
| 716 | //cout << "Special instrument is KLN89\n"; |
| 717 | |
| 718 | int x = node->getIntValue("x", -1); |
| 719 | int y = node->getIntValue("y", -1); |
| 720 | int real_w = node->getIntValue("w", -1); |
| 721 | int real_h = node->getIntValue("h", -1); |
| 722 | // int w = node->getIntValue("w-base", -1); |
| 723 | // int h = node->getIntValue("h-base", -1); |
| 724 | |
| 725 | if (x == -1 || y == -1) { |
| 726 | SG_LOG( SG_COCKPIT, SG_ALERT, |
| 727 | "x and y positions must be specified and > 0" ); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | // float w_scale = 1.0; |
| 732 | // float h_scale = 1.0; |
| 733 | if (real_w != -1) { |
| 734 | // w_scale = float(real_w) / float(w); |
| 735 | // w = real_w; |
| 736 | } |
| 737 | if (real_h != -1) { |
| 738 | // h_scale = float(real_h) / float(h); |
| 739 | // h = real_h; |
| 740 | } |
| 741 | |
| 742 | SG_LOG( SG_COCKPIT, SG_BULK, "Reading instrument " << name ); |
| 743 | |
| 744 | // Warning - hardwired size!!! |
| 745 | RenderArea2D* instrument = new RenderArea2D(158, 40, 158, 40, x, y); |
| 746 | auto gps = globals->get_subsystem<KLN89>(); |
| 747 | if (gps == NULL) { |
| 748 | gps = new KLN89(instrument); |
| 749 | globals->get_subsystem_mgr()->add("kln89", gps); |
| 750 | } |
| 751 | //gps->init(); // init seems to get called automagically. |
| 752 | FGSpecialInstrument* gpsinst = new FGSpecialInstrument(gps); |
| 753 | panel->addInstrument(gpsinst); |
| 754 | } else { |
| 755 | simgear::reportFailure(simgear::LoadFailure::Misconfigured, simgear::ErrorCode::AircraftSystems, |
| 756 | "Unknown special instrument found:" + name, path); |
| 757 | } |
| 758 | } else { |
| 759 | SG_LOG( SG_COCKPIT, SG_WARN, "Skipping " << node->getNameString() |
| 760 | << " in instruments section" ); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | SG_LOG( SG_COCKPIT, SG_BULK, "Done reading panel instruments" ); |
| 765 | |
| 766 | |
| 767 | // |
| 768 | // Return the new panel. |
| 769 | // |
| 770 | return panel; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | /** |
| 775 | * Read a panel from a property list. |
| 776 | * |
| 777 | * Each panel instrument will appear in its own, separate |
| 778 | * property list. The top level simply names the panel and |
| 779 | * places the instruments in their appropriate locations (and |
| 780 | * optionally resizes them if necessary). |
| 781 | * |
| 782 | * Returns 0 if the read fails for any reason. |
| 783 | */ |
| 784 | FGPanel * |
| 785 | fgReadPanel (istream &input) |
| 786 | { |
| 787 | SGPropertyNode root; |
| 788 | |
| 789 | try { |
| 790 | readProperties(input, &root); |
| 791 | } catch (const sg_exception &e) { |
| 792 | simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AircraftSystems, |
| 793 | "Failed to load aircraft panel:" + e.getFormattedMessage()); |
| 794 | return 0; |
| 795 | } |
| 796 | return readPanel(&root, SGPath{}); |
| 797 | } |
| 798 | |
| 799 | |
| 800 | /** |
| 801 | * Read a panel from a property list. |
| 802 | * |
| 803 | * This function opens a stream to a file, then invokes the |
| 804 | * main fgReadPanel() function. |
| 805 | */ |
| 806 | FGPanel * |
| 807 | fgReadPanel (const string &relative_path) |
| 808 | { |
| 809 | simgear::ErrorReportContext ec("2d-panel", relative_path); |
| 810 | |
| 811 | SGPath path = globals->resolve_aircraft_path(relative_path); |
| 812 | SGPropertyNode root; |
| 813 | |
| 814 | if (!path.exists()) { |
| 815 | simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::AircraftSystems, |
| 816 | "Missing panel file:" + relative_path, path); |
| 817 | return nullptr; |
| 818 | } |
| 819 | |
| 820 | try { |
| 821 | readProperties(path, &root); |
| 822 | } catch (const sg_exception &e) { |
| 823 | simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AircraftSystems, |
| 824 | "Error parsing panel file:" + e.getFormattedMessage(), path); |
| 825 | return 0; |
| 826 | } |
| 827 | return readPanel(&root, path); |
| 828 | } |
| 829 | |
| 830 | |
| 831 | |
| 832 | // end of panel_io.cxx |
| 833 | |
| 834 | |
| 835 |