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 <simgear/compiler.h>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/props.hxx>
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
44 #include "panel_io.hxx"
46 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
47 SG_USING_STD(istream);
48 SG_USING_STD(ifstream);
54 ////////////////////////////////////////////////////////////////////////
55 // Built-in layer for the magnetic compass ribbon layer.
57 // TODO: move this out into a special directory for built-in
58 // layers of various sorts.
59 ////////////////////////////////////////////////////////////////////////
61 class FGMagRibbon : public FGTexturedLayer
64 FGMagRibbon (int w, int h);
65 virtual ~FGMagRibbon () {}
70 FGMagRibbon::FGMagRibbon (int w, int h)
71 : FGTexturedLayer(w, h)
73 FGCroppedTexture texture("Aircraft/c172/Instruments/Textures/compass-ribbon.rgb");
80 double heading = FGSteam::get_MH_deg();
81 double xoffset, yoffset;
83 while (heading >= 360.0) {
86 while (heading < 0.0) {
90 if (heading >= 60.0 && heading <= 180.0) {
91 xoffset = heading / 240.0;
93 } else if (heading >= 150.0 && heading <= 270.0) {
94 xoffset = (heading - 90.0) / 240.0;
96 } else if (heading >= 240.0 && heading <= 360.0) {
97 xoffset = (heading - 180.0) / 240.0;
102 xoffset = (heading - 270.0) / 240.0;
106 xoffset = 1.0 - xoffset;
107 // Adjust to put the number in the centre
110 FGCroppedTexture &t = getTexture();
111 t.setCrop(xoffset, yoffset, xoffset + 0.5, yoffset + 0.25);
112 FGTexturedLayer::draw();
117 ////////////////////////////////////////////////////////////////////////
118 // Read and construct a panel.
120 // The panel is specified as a regular property list, and each of the
121 // instruments is its own, separate property list (and thus, a separate
122 // XML document). The functions in this section read in the files
123 // as property lists, then extract properties to set up the panel
126 // A panel contains zero or more instruments.
128 // An instrument contains one or more layers and zero or more actions.
130 // A layer contains zero or more transformations.
132 // Some special types of layers also contain other objects, such as
133 // chunks of text or other layers.
135 // There are currently four types of layers:
137 // 1. Textured Layer (type="texture"), the default
138 // 2. Text Layer (type="text")
139 // 3. Switch Layer (type="switch")
140 // 4. Built-in Layer (type="built-in", must also specify class)
142 // The only built-in layer so far is the ribbon for the magnetic compass
143 // (class="compass-ribbon").
145 // There are three types of actions:
147 // 1. Adjust (type="adjust"), the default
148 // 2. Swap (type="swap")
149 // 3. Toggle (type="toggle")
151 // There are three types of transformations:
153 // 1. X shift (type="x-shift"), the default
154 // 2. Y shift (type="y-shift")
155 // 3. Rotation (type="rotation")
157 // Each of these may be associated with a property, so that a needle
158 // will rotate with the airspeed, for example, or may have a fixed
159 // floating-point value.
160 ////////////////////////////////////////////////////////////////////////
164 * Read a cropped texture from the instrument's property list.
166 * The x1 and y1 properties give the starting position of the texture
167 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
168 * ending position. For example, to use the bottom-left quarter of a
169 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
171 static FGCroppedTexture
172 readTexture (const SGPropertyNode * node)
174 FGCroppedTexture texture(node->getStringValue("path"),
175 node->getFloatValue("x1"),
176 node->getFloatValue("y1"),
177 node->getFloatValue("x2", 1.0),
178 node->getFloatValue("y2", 1.0));
179 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
185 * Read an action from the instrument's property list.
187 * The action will be performed when the user clicks a mouse button
188 * within the specified region of the instrument. Actions always work
189 * by modifying the value of a property (see the SGPropertyNode
192 * The following action types are defined:
194 * "adjust" - modify the value of a floating-point property by
195 * the increment specified. This is the default.
197 * "swap" - swap the values of two-floating-point properties.
199 * "toggle" - toggle the value of a boolean property between true and
202 * For the adjust action, it is possible to specify an increment
203 * (use a negative number for a decrement), a minimum allowed value,
204 * a maximum allowed value, and a flag to indicate whether the value
205 * should freeze or wrap-around when it reachs the minimum or maximum.
207 * The action will be scaled automatically if the instrument is not
208 * being drawn at its regular size.
210 static FGPanelAction *
211 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
213 FGPanelAction * action = 0;
215 string name = node->getStringValue("name");
216 string type = node->getStringValue("type");
218 int button = node->getIntValue("button");
219 int x = int(node->getIntValue("x") * w_scale);
220 int y = int(node->getIntValue("y") * h_scale);
221 int w = int(node->getIntValue("w") * w_scale);
222 int h = int(node->getIntValue("h") * h_scale);
225 SG_LOG(SG_COCKPIT, SG_ALERT,
226 "No type supplied for action " << name << " assuming \"adjust\"");
230 // Adjust a property value
231 if (type == "adjust") {
232 string propName = node->getStringValue("property");
233 SGPropertyNode * target = fgGetNode(propName, true);
234 float increment = node->getFloatValue("increment", 1.0);
235 float min = node->getFloatValue("min", 0.0);
236 float max = node->getFloatValue("max", 0.0);
237 bool wrap = node->getBoolValue("wrap", false);
239 SG_LOG(SG_COCKPIT, SG_ALERT, "Action " << node->getName()
240 << " has same min and max value");
241 action = new FGAdjustAction(button, x, y, w, h, target,
242 increment, min, max, wrap);
245 // Swap two property values
246 else if (type == "swap") {
247 string propName1 = node->getStringValue("property1");
248 string propName2 = node->getStringValue("property2");
249 SGPropertyNode * target1 = fgGetNode(propName1, true);
250 SGPropertyNode * target2 = fgGetNode(propName2, true);
251 action = new FGSwapAction(button, x, y, w, h, target1, target2);
254 // Toggle a boolean value
255 else if (type == "toggle") {
256 string propName = node->getStringValue("property");
257 SGPropertyNode * target = fgGetNode(propName, true);
258 action = new FGToggleAction(button, x, y, w, h, target);
263 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized action type " << type );
272 * Read a transformation from the instrument's property list.
274 * The panel module uses the transformations to slide or spin needles,
275 * knobs, and other indicators, and to place layers in the correct
276 * positions. Every layer starts centered exactly on the x,y co-ordinate,
277 * and many layers need to be moved or rotated simply to display the
278 * instrument correctly.
280 * There are three types of transformations:
282 * "x-shift" - move the layer horizontally.
284 * "y-shift" - move the layer vertically.
286 * "rotation" - rotate the layer.
288 * Each transformation may have a fixed offset, and may also have
289 * a floating-point property value to add to the offset. The
290 * floating-point property may be clamped to a minimum and/or
291 * maximum range and scaled (after clamping).
293 * Note that because of the way OpenGL works, transformations will
294 * appear to be applied backwards.
296 static FGPanelTransformation *
297 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
299 FGPanelTransformation * t = new FGPanelTransformation;
301 string name = node->getName();
302 string type = node->getStringValue("type");
303 string propName = node->getStringValue("property", "");
304 SGPropertyNode * target = 0;
307 SG_LOG( SG_COCKPIT, SG_ALERT,
308 "No type supplied for transformation " << name
309 << " assuming \"rotation\"" );
313 if (propName != (string)"") {
314 target = fgGetNode(propName, true);
318 t->min = node->getFloatValue("min", -9999999);
319 t->max = node->getFloatValue("max", 99999999);
320 t->factor = node->getFloatValue("scale", 1.0);
321 t->offset = node->getFloatValue("offset", 0.0);
323 // Check for an interpolation table
324 const SGPropertyNode * trans_table = node->getNode("interpolation");
325 if (trans_table != 0) {
326 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
327 << trans_table->nChildren() << "children" );
328 t->table = new SGInterpTable();
329 for(int i = 0; i < trans_table->nChildren(); i++) {
330 const SGPropertyNode * node = trans_table->getChild(i);
331 if (node->getName() == "entry") {
332 double ind = node->getDoubleValue("ind", 0.0);
333 double dep = node->getDoubleValue("dep", 0.0);
334 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
335 << ind << "==>" << dep );
336 t->table->addEntry(ind, dep);
338 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
339 << " in interpolation" );
346 // Move the layer horizontally.
347 if (type == "x-shift") {
348 t->type = FGPanelTransformation::XSHIFT;
349 // t->min *= w_scale; //removed by Martin Dressler
350 // t->max *= w_scale; //removed by Martin Dressler
351 t->offset *= w_scale;
352 t->factor *= w_scale; //Added by Martin Dressler
355 // Move the layer vertically.
356 else if (type == "y-shift") {
357 t->type = FGPanelTransformation::YSHIFT;
358 //t->min *= h_scale; //removed
359 //t->max *= h_scale; //removed
360 t->offset *= h_scale;
361 t->factor *= h_scale; //Added
364 // Rotate the layer. The rotation
365 // is in degrees, and does not need
366 // to scale with the instrument size.
367 else if (type == "rotation") {
368 t->type = FGPanelTransformation::ROTATION;
372 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
377 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
383 * Read a chunk of text from the instrument's property list.
385 * A text layer consists of one or more chunks of text. All chunks
386 * share the same font size and color (and eventually, font), but
387 * each can come from a different source. There are three types of
390 * "literal" - a literal text string (the default)
392 * "text-value" - the current value of a string property
394 * "number-value" - the current value of a floating-point property.
396 * All three may also include a printf-style format string.
399 readTextChunk (const SGPropertyNode * node)
401 FGTextLayer::Chunk * chunk;
402 string name = node->getStringValue("name");
403 string type = node->getStringValue("type");
404 string format = node->getStringValue("format");
406 // Default to literal text.
408 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
409 << " assuming \"literal\"");
413 // A literal text string.
414 if (type == "literal") {
415 string text = node->getStringValue("text");
416 chunk = new FGTextLayer::Chunk(text, format);
419 // The value of a string property.
420 else if (type == "text-value") {
421 SGPropertyNode * target =
422 fgGetNode(node->getStringValue("property"), true);
423 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, target, format);
426 // The value of a float property.
427 else if (type == "number-value") {
428 string propName = node->getStringValue("property");
429 float scale = node->getFloatValue("scale", 1.0);
430 SGPropertyNode * target = fgGetNode(propName, true);
431 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, target,
437 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
438 << " for text chunk " << name );
447 * Read a single layer from an instrument's property list.
449 * Each instrument consists of one or more layers stacked on top
450 * of each other; the lower layers show through only where the upper
451 * layers contain an alpha component. Each layer can be moved
452 * horizontally and vertically and rotated using transformations.
454 * This module currently recognizes four kinds of layers:
456 * "texture" - a layer containing a texture (the default)
458 * "text" - a layer containing text
460 * "switch" - a layer that switches between two other layers
461 * based on the current value of a boolean property.
463 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
465 * Currently, the only built-in layer class is "compass-ribbon".
467 static FGInstrumentLayer *
468 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
470 FGInstrumentLayer * layer = NULL;
471 string name = node->getStringValue("name");
472 string type = node->getStringValue("type");
473 int w = node->getIntValue("w", -1);
474 int h = node->getIntValue("h", -1);
476 w = int(w * w_scale);
478 h = int(h * h_scale);
482 SG_LOG( SG_COCKPIT, SG_ALERT,
483 "No type supplied for layer " << name
484 << " assuming \"texture\"" );
489 // A textured instrument layer.
490 if (type == "texture") {
491 FGCroppedTexture texture = readTexture(node->getNode("texture"));
492 layer = new FGTexturedLayer(texture, w, h);
496 // A textual instrument layer.
497 else if (type == "text") {
498 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
500 // Set the text color.
501 float red = node->getFloatValue("color/red", 0.0);
502 float green = node->getFloatValue("color/green", 0.0);
503 float blue = node->getFloatValue("color/blue", 0.0);
504 tlayer->setColor(red, green, blue);
506 // Set the point size.
507 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
508 tlayer->setPointSize(pointSize);
513 const SGPropertyNode * chunk_group = node->getNode("chunks");
514 if (chunk_group != 0) {
515 int nChunks = chunk_group->nChildren();
516 for (int i = 0; i < nChunks; i++) {
517 const SGPropertyNode * node = chunk_group->getChild(i);
518 if (node->getName() == "chunk") {
519 FGTextLayer::Chunk * chunk = readTextChunk(node);
521 tlayer->addChunk(chunk);
523 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
531 // A switch instrument layer.
532 else if (type == "switch") {
533 SGPropertyNode * target =
534 fgGetNode(node->getStringValue("property"), true);
535 FGInstrumentLayer * layer1 =
536 readLayer(node->getNode("layer1"), w_scale, h_scale);
537 FGInstrumentLayer * layer2 =
538 readLayer(node->getNode("layer2"), w_scale, h_scale);
539 layer = new FGSwitchLayer(w, h, target, layer1, layer2);
542 // A built-in instrument layer.
543 else if (type == "built-in") {
544 string layerclass = node->getStringValue("class");
546 if (layerclass == "mag-ribbon") {
547 layer = new FGMagRibbon(w, h);
550 else if (layerclass == "") {
551 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
557 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
565 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
571 // Get the transformations for each layer.
573 const SGPropertyNode * trans_group = node->getNode("transformations");
574 if (trans_group != 0) {
575 int nTransformations = trans_group->nChildren();
576 for (int i = 0; i < nTransformations; i++) {
577 const SGPropertyNode * node = trans_group->getChild(i);
578 if (node->getName() == "transformation") {
579 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
581 layer->addTransformation(t);
583 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
584 << " in transformations" );
589 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
595 * Read an instrument from a property list.
597 * The instrument consists of a preferred width and height
598 * (the panel may override these), together with a list of layers
599 * and a list of actions to be performed when the user clicks
600 * the mouse over the instrument. All co-ordinates are relative
601 * to the instrument's position, so instruments are fully relocatable;
602 * likewise, co-ordinates for actions and transformations will be
603 * scaled automatically if the instrument is not at its preferred size.
605 static FGPanelInstrument *
606 readInstrument (const SGPropertyNode * node)
608 const string &name = node->getStringValue("name");
609 int x = node->getIntValue("x", -1);
610 int y = node->getIntValue("y", -1);
611 int real_w = node->getIntValue("w", -1);
612 int real_h = node->getIntValue("h", -1);
613 int w = node->getIntValue("w-base", -1);
614 int h = node->getIntValue("h-base", -1);
616 if (x == -1 || y == -1) {
617 SG_LOG( SG_COCKPIT, SG_ALERT,
618 "x and y positions must be specified and > 0" );
625 w_scale = float(real_w) / float(w);
629 h_scale = float(real_h) / float(h);
633 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
635 FGLayeredInstrument * instrument =
636 new FGLayeredInstrument(x, y, w, h);
639 // Get the actions for the instrument.
641 const SGPropertyNode * action_group = node->getNode("actions");
642 if (action_group != 0) {
643 int nActions = action_group->nChildren();
644 for (int i = 0; i < nActions; i++) {
645 const SGPropertyNode * node = action_group->getChild(i);
646 if (node->getName() == "action") {
647 FGPanelAction * action = readAction(node, w_scale, h_scale);
649 instrument->addAction(action);
651 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
658 // Get the layers for the instrument.
660 const SGPropertyNode * layer_group = node->getNode("layers");
661 if (layer_group != 0) {
662 int nLayers = layer_group->nChildren();
663 for (int i = 0; i < nLayers; i++) {
664 const SGPropertyNode * node = layer_group->getChild(i);
665 if (node->getName() == "layer") {
666 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
668 instrument->addLayer(layer);
670 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
676 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
682 * Construct the panel from a property tree.
685 readPanel (const SGPropertyNode * root)
687 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
688 root->getStringValue("name", "[Unnamed Panel]") );
690 FGPanel * panel = new FGPanel();
691 panel->setWidth(root->getIntValue("w", 1024));
692 panel->setHeight(root->getIntValue("h", 443));
695 // Grab the visible external viewing area, default to
697 panel->setViewHeight(root->getIntValue("view-height",
698 768 - panel->getHeight() + 2));
701 // Grab the panel's initial offsets, default to 0, 0.
703 if (!fgHasNode("/sim/panel/x-offset"))
704 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
706 if (!fgHasNode("/sim/panel/y-offset"))
707 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
710 // Assign the background texture, if any, or a bogus chequerboard.
712 string bgTexture = root->getStringValue("background");
715 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
716 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
720 // Create each instrument.
722 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
723 const SGPropertyNode * instrument_group = root->getChild("instruments");
724 if (instrument_group != 0) {
725 int nInstruments = instrument_group->nChildren();
726 for (int i = 0; i < nInstruments; i++) {
727 const SGPropertyNode * node = instrument_group->getChild(i);
728 if (node->getName() == "instrument") {
729 FGPanelInstrument * instrument = readInstrument(node);
731 panel->addInstrument(instrument);
733 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
734 << " in instruments section" );
738 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
742 // Return the new panel.
749 * Read a panel from a property list.
751 * Each panel instrument will appear in its own, separate
752 * property list. The top level simply names the panel and
753 * places the instruments in their appropriate locations (and
754 * optionally resizes them if necessary).
756 * Returns 0 if the read fails for any reason.
759 fgReadPanel (istream &input)
763 if (!readProperties(input, &root)) {
764 SG_LOG( SG_COCKPIT, SG_ALERT, "Malformed property list for panel." );
767 return readPanel(&root);
772 * Read a panel from a property list.
774 * This function opens a stream to a file, then invokes the
775 * main fgReadPanel() function.
778 fgReadPanel (const string &relative_path)
780 SGPath path(globals->get_fg_root());
781 path.append(relative_path);
784 if (!readProperties(path.str(), &root)) {
785 SG_LOG( SG_COCKPIT, SG_ALERT, "Malformed property list for panel." );
788 return readPanel(&root);
793 // end of panel_io.cxx