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 // Default panel, instrument, and layer for when things go wrong...
56 ////////////////////////////////////////////////////////////////////////
58 static FGCroppedTexture defaultTexture("Textures/default.rgb");
62 * Default layer: the default texture.
64 class DefaultLayer : public FGTexturedLayer
67 DefaultLayer () : FGTexturedLayer(defaultTexture)
74 * Default instrument: a single default layer.
76 class DefaultInstrument : public FGLayeredInstrument
79 DefaultInstrument (int x, int y, int w, int h)
80 : FGLayeredInstrument(x, y, w, h)
82 addLayer(new DefaultLayer());
88 * Default panel: the default texture.
90 class DefaultPanel : public FGPanel
93 DefaultPanel (int x, int y, int w, int h) : FGPanel(x, y, w, h)
95 setBackground(defaultTexture.getTexture());
101 ////////////////////////////////////////////////////////////////////////
102 // Built-in layer for the magnetic compass ribbon layer.
104 // TODO: move this out into a special directory for built-in
105 // layers of various sorts.
106 ////////////////////////////////////////////////////////////////////////
108 class FGMagRibbon : public FGTexturedLayer
111 FGMagRibbon (int w, int h);
112 virtual ~FGMagRibbon () {}
114 virtual void draw ();
117 FGMagRibbon::FGMagRibbon (int w, int h)
118 : FGTexturedLayer(w, h)
120 FGCroppedTexture texture("Aircraft/c172/Instruments/Textures/compass-ribbon.rgb");
127 double heading = FGSteam::get_MH_deg();
128 double xoffset, yoffset;
130 while (heading >= 360.0) {
133 while (heading < 0.0) {
137 if (heading >= 60.0 && heading <= 180.0) {
138 xoffset = heading / 240.0;
140 } else if (heading >= 150.0 && heading <= 270.0) {
141 xoffset = (heading - 90.0) / 240.0;
143 } else if (heading >= 240.0 && heading <= 360.0) {
144 xoffset = (heading - 180.0) / 240.0;
149 xoffset = (heading - 270.0) / 240.0;
153 xoffset = 1.0 - xoffset;
154 // Adjust to put the number in the centre
157 FGCroppedTexture &t = getTexture();
158 t.setCrop(xoffset, yoffset, xoffset + 0.5, yoffset + 0.25);
159 FGTexturedLayer::draw();
164 ////////////////////////////////////////////////////////////////////////
165 // Read and construct a panel.
167 // The panel is specified as a regular property list, and each of the
168 // instruments is its own, separate property list (and thus, a separate
169 // XML document). The functions in this section read in the files
170 // as property lists, then extract properties to set up the panel
173 // A panel contains zero or more instruments.
175 // An instrument contains one or more layers and zero or more actions.
177 // A layer contains zero or more transformations.
179 // Some special types of layers also contain other objects, such as
180 // chunks of text or other layers.
182 // There are currently four types of layers:
184 // 1. Textured Layer (type="texture"), the default
185 // 2. Text Layer (type="text")
186 // 3. Switch Layer (type="switch")
187 // 4. Built-in Layer (type="built-in", must also specify class)
189 // The only built-in layer so far is the ribbon for the magnetic compass
190 // (class="compass-ribbon").
192 // There are three types of actions:
194 // 1. Adjust (type="adjust"), the default
195 // 2. Swap (type="swap")
196 // 3. Toggle (type="toggle")
198 // There are three types of transformations:
200 // 1. X shift (type="x-shift"), the default
201 // 2. Y shift (type="y-shift")
202 // 3. Rotation (type="rotation")
204 // Each of these may be associated with a property, so that a needle
205 // will rotate with the airspeed, for example, or may have a fixed
206 // floating-point value.
207 ////////////////////////////////////////////////////////////////////////
211 * Read a cropped texture from the instrument's property list.
213 * The x1 and y1 properties give the starting position of the texture
214 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
215 * ending position. For example, to use the bottom-left quarter of a
216 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
218 static FGCroppedTexture
219 readTexture (const SGPropertyNode * node)
221 FGCroppedTexture texture(node->getStringValue("path"),
222 node->getFloatValue("x1"),
223 node->getFloatValue("y1"),
224 node->getFloatValue("x2", 1.0),
225 node->getFloatValue("y2", 1.0));
226 SG_LOG(SG_COCKPIT, SG_DEBUG, "Read texture " << node->getName());
232 * Read an action from the instrument's property list.
234 * The action will be performed when the user clicks a mouse button
235 * within the specified region of the instrument. Actions always
236 * work by modifying the value of a property (see the SGValue class).
238 * The following action types are defined:
240 * "adjust" - modify the value of a floating-point property by
241 * the increment specified. This is the default.
243 * "swap" - swap the values of two-floating-point properties.
245 * "toggle" - toggle the value of a boolean property between true and
248 * For the adjust action, it is possible to specify an increment
249 * (use a negative number for a decrement), a minimum allowed value,
250 * a maximum allowed value, and a flag to indicate whether the value
251 * should freeze or wrap-around when it reachs the minimum or maximum.
253 * The action will be scaled automatically if the instrument is not
254 * being drawn at its regular size.
256 static FGPanelAction *
257 readAction (const SGPropertyNode * node, float w_scale, float h_scale)
259 FGPanelAction * action = 0;
261 string name = node->getStringValue("name");
262 string type = node->getStringValue("type");
264 int button = node->getIntValue("button");
265 int x = int(node->getIntValue("x") * w_scale);
266 int y = int(node->getIntValue("y") * h_scale);
267 int w = int(node->getIntValue("w") * w_scale);
268 int h = int(node->getIntValue("h") * h_scale);
271 SG_LOG(SG_COCKPIT, SG_ALERT,
272 "No type supplied for action " << name << " assuming \"adjust\"");
276 // Adjust a property value
277 if (type == "adjust") {
278 string propName = node->getStringValue("property");
279 SGValue * value = fgGetValue(propName, true);
280 float increment = node->getFloatValue("increment", 1.0);
281 float min = node->getFloatValue("min", 0.0);
282 float max = node->getFloatValue("max", 0.0);
283 bool wrap = node->getBoolValue("wrap", false);
285 SG_LOG(SG_COCKPIT, SG_ALERT, "Action " << node->getName()
286 << " has same min and max value");
287 action = new FGAdjustAction(button, x, y, w, h, value,
288 increment, min, max, wrap);
291 // Swap two property values
292 else if (type == "swap") {
293 string propName1 = node->getStringValue("property1");
294 string propName2 = node->getStringValue("property2");
295 SGValue * value1 = fgGetValue(propName1, true);
296 SGValue * value2 = fgGetValue(propName2, true);
297 action = new FGSwapAction(button, x, y, w, h, value1, value2);
300 // Toggle a boolean value
301 else if (type == "toggle") {
302 string propName = node->getStringValue("property");
303 SGValue * value = fgGetValue(propName, true);
304 action = new FGToggleAction(button, x, y, w, h, value);
309 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized action type " << type );
318 * Read a transformation from the instrument's property list.
320 * The panel module uses the transformations to slide or spin needles,
321 * knobs, and other indicators, and to place layers in the correct
322 * positions. Every layer starts centered exactly on the x,y co-ordinate,
323 * and many layers need to be moved or rotated simply to display the
324 * instrument correctly.
326 * There are three types of transformations:
328 * "x-shift" - move the layer horizontally.
330 * "y-shift" - move the layer vertically.
332 * "rotation" - rotate the layer.
334 * Each transformation may have a fixed offset, and may also have
335 * a floating-point property value to add to the offset. The
336 * floating-point property may be clamped to a minimum and/or
337 * maximum range and scaled (after clamping).
339 * Note that because of the way OpenGL works, transformations will
340 * appear to be applied backwards.
342 static FGPanelTransformation *
343 readTransformation (const SGPropertyNode * node, float w_scale, float h_scale)
345 FGPanelTransformation * t = new FGPanelTransformation;
347 string name = node->getName();
348 string type = node->getStringValue("type");
349 string propName = node->getStringValue("property", "");
353 SG_LOG( SG_COCKPIT, SG_ALERT,
354 "No type supplied for transformation " << name
355 << " assuming \"rotation\"" );
359 if (propName != (string)"") {
360 value = fgGetValue(propName, true);
364 t->min = node->getFloatValue("min", -9999999);
365 t->max = node->getFloatValue("max", 99999999);
366 t->factor = node->getFloatValue("scale", 1.0);
367 t->offset = node->getFloatValue("offset", 0.0);
369 // Check for an interpolation table
370 const SGPropertyNode * trans_table = node->getNode("interpolation");
371 if (trans_table != 0) {
372 SG_LOG( SG_COCKPIT, SG_INFO, "Found interpolation table with "
373 << trans_table->nChildren() << "children" );
374 t->table = new SGInterpTable();
375 for(int i = 0; i < trans_table->nChildren(); i++) {
376 const SGPropertyNode * node = trans_table->getChild(i);
377 if (node->getName() == "entry") {
378 double ind = node->getDoubleValue("ind", 0.0);
379 double dep = node->getDoubleValue("dep", 0.0);
380 SG_LOG( SG_COCKPIT, SG_INFO, "Adding interpolation entry "
381 << ind << "==>" << dep );
382 t->table->addEntry(ind, dep);
384 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
385 << " in interpolation" );
392 // Move the layer horizontally.
393 if (type == "x-shift") {
394 t->type = FGPanelTransformation::XSHIFT;
395 // t->min *= w_scale; //removed by Martin Dressler
396 // t->max *= w_scale; //removed by Martin Dressler
397 t->offset *= w_scale;
398 t->factor *= w_scale; //Added by Martin Dressler
401 // Move the layer vertically.
402 else if (type == "y-shift") {
403 t->type = FGPanelTransformation::YSHIFT;
404 //t->min *= h_scale; //removed
405 //t->max *= h_scale; //removed
406 t->offset *= h_scale;
407 t->factor *= h_scale; //Added
410 // Rotate the layer. The rotation
411 // is in degrees, and does not need
412 // to scale with the instrument size.
413 else if (type == "rotation") {
414 t->type = FGPanelTransformation::ROTATION;
418 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized transformation type " << type );
423 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read transformation " << name );
429 * Read a chunk of text from the instrument's property list.
431 * A text layer consists of one or more chunks of text. All chunks
432 * share the same font size and color (and eventually, font), but
433 * each can come from a different source. There are three types of
436 * "literal" - a literal text string (the default)
438 * "text-value" - the current value of a string property
440 * "number-value" - the current value of a floating-point property.
442 * All three may also include a printf-style format string.
445 readTextChunk (const SGPropertyNode * node)
447 FGTextLayer::Chunk * chunk;
448 string name = node->getStringValue("name");
449 string type = node->getStringValue("type");
450 string format = node->getStringValue("format");
452 // Default to literal text.
454 SG_LOG( SG_COCKPIT, SG_INFO, "No type provided for text chunk " << name
455 << " assuming \"literal\"");
459 // A literal text string.
460 if (type == "literal") {
461 string text = node->getStringValue("text");
462 chunk = new FGTextLayer::Chunk(text, format);
465 // The value of a string property.
466 else if (type == "text-value") {
468 fgGetValue(node->getStringValue("property"), true);
469 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
472 // The value of a float property.
473 else if (type == "number-value") {
474 string propName = node->getStringValue("property");
475 float scale = node->getFloatValue("scale", 1.0);
476 SGValue * value = fgGetValue(propName, true);
477 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, value,
483 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized type " << type
484 << " for text chunk " << name );
493 * Read a single layer from an instrument's property list.
495 * Each instrument consists of one or more layers stacked on top
496 * of each other; the lower layers show through only where the upper
497 * layers contain an alpha component. Each layer can be moved
498 * horizontally and vertically and rotated using transformations.
500 * This module currently recognizes four kinds of layers:
502 * "texture" - a layer containing a texture (the default)
504 * "text" - a layer containing text
506 * "switch" - a layer that switches between two other layers
507 * based on the current value of a boolean property.
509 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
511 * Currently, the only built-in layer class is "compass-ribbon".
513 static FGInstrumentLayer *
514 readLayer (const SGPropertyNode * node, float w_scale, float h_scale)
516 FGInstrumentLayer * layer = NULL;
517 string name = node->getStringValue("name");
518 string type = node->getStringValue("type");
519 int w = node->getIntValue("w", -1);
520 int h = node->getIntValue("h", -1);
522 w = int(w * w_scale);
524 h = int(h * h_scale);
528 SG_LOG( SG_COCKPIT, SG_ALERT,
529 "No type supplied for layer " << name
530 << " assuming \"texture\"" );
535 // A textured instrument layer.
536 if (type == "texture") {
537 FGCroppedTexture texture = readTexture(node->getNode("texture"));
538 layer = new FGTexturedLayer(texture, w, h);
542 // A textual instrument layer.
543 else if (type == "text") {
544 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
546 // Set the text color.
547 float red = node->getFloatValue("color/red", 0.0);
548 float green = node->getFloatValue("color/green", 0.0);
549 float blue = node->getFloatValue("color/blue", 0.0);
550 tlayer->setColor(red, green, blue);
552 // Set the point size.
553 float pointSize = node->getFloatValue("point-size", 10.0) * w_scale;
554 tlayer->setPointSize(pointSize);
559 const SGPropertyNode * chunk_group = node->getNode("chunks");
560 if (chunk_group != 0) {
561 int nChunks = chunk_group->nChildren();
562 for (int i = 0; i < nChunks; i++) {
563 const SGPropertyNode * node = chunk_group->getChild(i);
564 if (node->getName() == "chunk") {
565 FGTextLayer::Chunk * chunk = readTextChunk(node);
567 tlayer->addChunk(chunk);
569 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
577 // A switch instrument layer.
578 else if (type == "switch") {
580 fgGetValue(node->getStringValue("property"), true);
581 FGInstrumentLayer * layer1 =
582 readLayer(node->getNode("layer1"), w_scale, h_scale);
583 FGInstrumentLayer * layer2 =
584 readLayer(node->getNode("layer2"), w_scale, h_scale);
585 layer = new FGSwitchLayer(w, h, value, layer1, layer2);
588 // A built-in instrument layer.
589 else if (type == "built-in") {
590 string layerclass = node->getStringValue("class");
592 if (layerclass == "mag-ribbon") {
593 layer = new FGMagRibbon(w, h);
596 else if (layerclass == "") {
597 SG_LOG( SG_COCKPIT, SG_ALERT, "No class provided for built-in layer "
603 SG_LOG( SG_COCKPIT, SG_ALERT, "Unknown built-in layer class "
611 SG_LOG( SG_COCKPIT, SG_ALERT, "Unrecognized layer type " << type );
617 // Get the transformations for each layer.
619 const SGPropertyNode * trans_group = node->getNode("transformations");
620 if (trans_group != 0) {
621 int nTransformations = trans_group->nChildren();
622 for (int i = 0; i < nTransformations; i++) {
623 const SGPropertyNode * node = trans_group->getChild(i);
624 if (node->getName() == "transformation") {
625 FGPanelTransformation * t = readTransformation(node, w_scale, h_scale);
627 layer->addTransformation(t);
629 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
630 << " in transformations" );
635 SG_LOG( SG_COCKPIT, SG_DEBUG, "Read layer " << name );
641 * Read an instrument from a property list.
643 * The instrument consists of a preferred width and height
644 * (the panel may override these), together with a list of layers
645 * and a list of actions to be performed when the user clicks
646 * the mouse over the instrument. All co-ordinates are relative
647 * to the instrument's position, so instruments are fully relocatable;
648 * likewise, co-ordinates for actions and transformations will be
649 * scaled automatically if the instrument is not at its preferred size.
651 static FGPanelInstrument *
652 readInstrument (const SGPropertyNode * node)
654 const string &name = node->getStringValue("name");
655 int x = node->getIntValue("x", -1);
656 int y = node->getIntValue("y", -1);
657 int real_w = node->getIntValue("w", -1);
658 int real_h = node->getIntValue("h", -1);
659 int w = node->getIntValue("w-base", -1);
660 int h = node->getIntValue("h-base", -1);
662 if (x == -1 || y == -1) {
663 SG_LOG( SG_COCKPIT, SG_ALERT,
664 "x and y positions must be specified and > 0" );
671 w_scale = float(real_w) / float(w);
675 h_scale = float(real_h) / float(h);
679 SG_LOG( SG_COCKPIT, SG_DEBUG, "Reading instrument " << name );
681 FGLayeredInstrument * instrument =
682 new FGLayeredInstrument(x, y, w, h);
685 // Get the actions for the instrument.
687 const SGPropertyNode * action_group = node->getNode("actions");
688 if (action_group != 0) {
689 int nActions = action_group->nChildren();
690 for (int i = 0; i < nActions; i++) {
691 const SGPropertyNode * node = action_group->getChild(i);
692 if (node->getName() == "action") {
693 FGPanelAction * action = readAction(node, w_scale, h_scale);
695 instrument->addAction(action);
697 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
704 // Get the layers for the instrument.
706 const SGPropertyNode * layer_group = node->getNode("layers");
707 if (layer_group != 0) {
708 int nLayers = layer_group->nChildren();
709 for (int i = 0; i < nLayers; i++) {
710 const SGPropertyNode * node = layer_group->getChild(i);
711 if (node->getName() == "layer") {
712 FGInstrumentLayer * layer = readLayer(node, w_scale, h_scale);
714 instrument->addLayer(layer);
716 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
722 SG_LOG( SG_COCKPIT, SG_DEBUG, "Done reading instrument " << name );
728 * Construct the panel from a property tree.
731 readPanel (const SGPropertyNode * root)
733 SG_LOG( SG_COCKPIT, SG_INFO, "Reading properties for panel " <<
734 root->getStringValue("name", "[Unnamed Panel]") );
736 FGPanel * panel = new FGPanel(0, 0, 1024, 768);
737 panel->setWidth(root->getIntValue("w", 1024));
738 panel->setHeight(root->getIntValue("h", 443));
741 // Grab the visible external viewing area, default to
743 panel->setViewHeight(root->getIntValue("view-height",
744 768 - panel->getHeight() + 2));
747 // Grab the panel's initial offsets, default to 0, 0.
749 if (!fgHasValue("/sim/panel/x-offset"))
750 fgSetInt("/sim/panel/x-offset", root->getIntValue("x-offset", 0));
752 if (!fgHasValue("/sim/panel/y-offset"))
753 fgSetInt("/sim/panel/y-offset", root->getIntValue("y-offset", 0));
756 // Assign the background texture, if any, or a bogus chequerboard.
758 string bgTexture = root->getStringValue("background");
761 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
762 SG_LOG( SG_COCKPIT, SG_INFO, "Set background texture to " << bgTexture );
766 // Create each instrument.
768 SG_LOG( SG_COCKPIT, SG_INFO, "Reading panel instruments" );
769 const SGPropertyNode * instrument_group = root->getChild("instruments");
770 if (instrument_group != 0) {
771 int nInstruments = instrument_group->nChildren();
772 for (int i = 0; i < nInstruments; i++) {
773 const SGPropertyNode * node = instrument_group->getChild(i);
774 if (node->getName() == "instrument") {
775 FGPanelInstrument * instrument = readInstrument(node);
777 panel->addInstrument(instrument);
779 SG_LOG( SG_COCKPIT, SG_INFO, "Skipping " << node->getName()
780 << " in instruments section" );
784 SG_LOG( SG_COCKPIT, SG_INFO, "Done reading panel instruments" );
788 // Return the new panel.
795 * Read a panel from a property list.
797 * Each panel instrument will appear in its own, separate
798 * property list. The top level simply names the panel and
799 * places the instruments in their appropriate locations (and
800 * optionally resizes them if necessary).
802 * Returns 0 if the read fails for any reason.
805 fgReadPanel (istream &input)
809 if (!readProperties(input, &root)) {
810 SG_LOG( SG_COCKPIT, SG_ALERT, "Malformed property list for panel." );
813 return readPanel(&root);
818 * Read a panel from a property list.
820 * This function opens a stream to a file, then invokes the
821 * main fgReadPanel() function.
824 fgReadPanel (const string &relative_path)
826 SGPath path(globals->get_fg_root());
827 path.append(relative_path);
830 if (!readProperties(path.str(), &root)) {
831 SG_LOG( SG_COCKPIT, SG_ALERT, "Malformed property list for panel." );
834 return readPanel(&root);
839 // end of panel_io.cxx