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/fgpath.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_INPUT, SG_INFO, "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 hscale, float vscale)
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") * hscale);
266 int y = int(node->getIntValue("y") * vscale);
267 int w = int(node->getIntValue("w") * hscale);
268 int h = int(node->getIntValue("h") * vscale);
271 SG_LOG(SG_INPUT, 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_INPUT, 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_INPUT, 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 hscale, float vscale)
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_INPUT, 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 // Move the layer horizontally.
370 if (type == "x-shift") {
371 t->type = FGPanelTransformation::XSHIFT;
377 // Move the layer vertically.
378 else if (type == "y-shift") {
379 t->type = FGPanelTransformation::YSHIFT;
385 // Rotate the layer. The rotation
386 // is in degrees, and does not need
387 // to scale with the instrument size.
388 else if (type == "rotation") {
389 t->type = FGPanelTransformation::ROTATION;
393 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized transformation type " << type);
398 SG_LOG(SG_INPUT, SG_INFO, "Read transformation " << name);
404 * Read a chunk of text from the instrument's property list.
406 * A text layer consists of one or more chunks of text. All chunks
407 * share the same font size and color (and eventually, font), but
408 * each can come from a different source. There are three types of
411 * "literal" - a literal text string (the default)
413 * "text-value" - the current value of a string property
415 * "number-value" - the current value of a floating-point property.
417 * All three may also include a printf-style format string.
420 readTextChunk (const SGPropertyNode * node)
422 FGTextLayer::Chunk * chunk;
423 string name = node->getStringValue("name");
424 string type = node->getStringValue("type");
425 string format = node->getStringValue("format");
427 // Default to literal text.
429 SG_LOG(SG_INPUT, SG_INFO, "No type provided for text chunk " << name
430 << " assuming \"literal\"");
434 // A literal text string.
435 if (type == "literal") {
436 string text = node->getStringValue("text");
437 chunk = new FGTextLayer::Chunk(text, format);
440 // The value of a string property.
441 else if (type == "text-value") {
443 fgGetValue(node->getStringValue("property"), true);
444 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
447 // The value of a float property.
448 else if (type == "number-value") {
449 string propName = node->getStringValue("property");
450 float scale = node->getFloatValue("scale", 1.0);
451 SGValue * value = fgGetValue(propName, true);
452 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, value,
458 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized type " << type
459 << " for text chunk " << name);
468 * Read a single layer from an instrument's property list.
470 * Each instrument consists of one or more layers stacked on top
471 * of each other; the lower layers show through only where the upper
472 * layers contain an alpha component. Each layer can be moved
473 * horizontally and vertically and rotated using transformations.
475 * This module currently recognizes four kinds of layers:
477 * "texture" - a layer containing a texture (the default)
479 * "text" - a layer containing text
481 * "switch" - a layer that switches between two other layers
482 * based on the current value of a boolean property.
484 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
486 * Currently, the only built-in layer class is "compass-ribbon".
488 static FGInstrumentLayer *
489 readLayer (const SGPropertyNode * node, float hscale, float vscale)
491 FGInstrumentLayer * layer = NULL;
492 string name = node->getStringValue("name");
493 string type = node->getStringValue("type");
494 int w = node->getIntValue("w", -1);
495 int h = node->getIntValue("h", -1);
503 SG_LOG(SG_INPUT, SG_ALERT,
504 "No type supplied for layer " << name
505 << " assuming \"texture\"");
510 // A textured instrument layer.
511 if (type == "texture") {
512 FGCroppedTexture texture = readTexture(node->getNode("texture"));
513 layer = new FGTexturedLayer(texture, w, h);
517 // A textual instrument layer.
518 else if (type == "text") {
519 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
521 // Set the text color.
522 float red = node->getFloatValue("color/red", 0.0);
523 float green = node->getFloatValue("color/green", 0.0);
524 float blue = node->getFloatValue("color/blue", 0.0);
525 tlayer->setColor(red, green, blue);
527 // Set the point size.
528 float pointSize = node->getFloatValue("point-size", 10.0) * hscale;
529 tlayer->setPointSize(pointSize);
534 const SGPropertyNode * chunk_group = node->getNode("chunks");
535 if (chunk_group != 0) {
536 int nChunks = chunk_group->nChildren();
537 for (int i = 0; i < nChunks; i++) {
538 FGTextLayer::Chunk * chunk = readTextChunk(chunk_group->getChild(i));
543 tlayer->addChunk(chunk);
549 // A switch instrument layer.
550 else if (type == "switch") {
552 fgGetValue(node->getStringValue("property"), true);
553 FGInstrumentLayer * layer1 =
554 readLayer(node->getNode("layer1"), hscale, vscale);
555 FGInstrumentLayer * layer2 =
556 readLayer(node->getNode("layer2"), hscale, vscale);
557 layer = new FGSwitchLayer(w, h, value, layer1, layer2);
560 // A built-in instrument layer.
561 else if (type == "built-in") {
562 string layerclass = node->getStringValue("class");
564 if (layerclass == "mag-ribbon") {
565 layer = new FGMagRibbon(w, h);
568 else if (layerclass == "") {
569 SG_LOG(SG_INPUT, SG_ALERT, "No class provided for built-in layer "
575 SG_LOG(SG_INPUT, SG_ALERT, "Unknown built-in layer class "
583 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized layer type " << type);
589 // Get the transformations for each layer.
591 const SGPropertyNode * trans_group = node->getNode("transformations");
592 if (trans_group != 0) {
593 int nTransformations = trans_group->nChildren();
594 for (int i = 0; i < nTransformations; i++) {
595 FGPanelTransformation * t = readTransformation(trans_group->getChild(i),
601 layer->addTransformation(t);
605 SG_LOG(SG_INPUT, SG_INFO, "Read layer " << name);
611 * Read an instrument from a property list.
613 * The instrument consists of a preferred width and height
614 * (the panel may override these), together with a list of layers
615 * and a list of actions to be performed when the user clicks
616 * the mouse over the instrument. All co-ordinates are relative
617 * to the instrument's position, so instruments are fully relocatable;
618 * likewise, co-ordinates for actions and transformations will be
619 * scaled automatically if the instrument is not at its preferred size.
621 static FGPanelInstrument *
622 readInstrument (const SGPropertyNode * node, int x, int y,
623 int real_w, int real_h)
625 int w = node->getIntValue("w");
626 int h = node->getIntValue("h");
627 const string &name = node->getStringValue("name");
632 hscale = float(real_w) / float(w);
636 vscale = float(real_h) / float(h);
640 SG_LOG(SG_INPUT, SG_INFO, "Reading instrument " << name);
642 FGLayeredInstrument * instrument =
643 new FGLayeredInstrument(x, y, w, h);
646 // Get the actions for the instrument.
648 const SGPropertyNode * action_group = node->getNode("actions");
649 if (action_group != 0) {
650 int nActions = action_group->nChildren();
651 for (int i = 0; i < nActions; i++) {
652 FGPanelAction * action = readAction(action_group->getChild(i),
656 return new DefaultInstrument(x, y, w, h);
658 instrument->addAction(action);
663 // Get the layers for the instrument.
665 const SGPropertyNode * layer_group = node->getNode("layers");
666 if (layer_group != 0) {
667 int nLayers = layer_group->nChildren();
668 for (int i = 0; i < nLayers; i++) {
669 FGInstrumentLayer * layer = readLayer(layer_group->getChild(i),
673 return new DefaultInstrument(x, y, w, h);
675 instrument->addLayer(layer);
679 SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
685 * Read a panel from a property list.
687 * Each panel instrument will appear in its own, separate
688 * property list. The top level simply names the panel and
689 * places the instruments in their appropriate locations (and
690 * optionally resizes them if necessary).
692 * Returns 0 if the read fails for any reason.
695 fgReadPanel (istream &input)
701 // Read the property list from disk.
703 if (!readProperties(input, &root)) {
704 SG_LOG(SG_INPUT, SG_ALERT, "Malformed property list for panel.");
707 SG_LOG(SG_INPUT, SG_INFO, "Read properties for panel " <<
708 root.getStringValue("name"));
711 // Construct a new, empty panel.
713 FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
717 // Grab the panel's dimensions, default to 1024x443.
719 int panel_w = (root.hasValue("w") ? root.getIntValue("w") : 1024);
720 int panel_h = (root.hasValue("h") ? root.getIntValue("h") : 443);
721 panel->setWidth(panel_w);
722 panel->setHeight(panel_h);
725 // Grab the visible external viewing area, default to
727 panel->setViewHeight(root.hasValue("view-height") ?
728 root.getIntValue("view-height") :
732 // Grab the panel's initial offsets, default to 0, 0.
734 int xoffset = (root.hasValue("x-offset") ?
735 root.getIntValue("x-offset") :
737 int yoffset = (root.hasValue("y-offset") ?
738 root.getIntValue("y-offset") :
740 panel->setXOffset(xoffset);
741 panel->setYOffset(yoffset);
744 // Assign the background texture, if any, or a bogus chequerboard.
746 string bgTexture = root.getStringValue("background");
749 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
750 SG_LOG(SG_INPUT, SG_INFO, "Set background texture to " << bgTexture);
754 // Create each instrument.
756 SG_LOG(SG_INPUT, SG_INFO, "Reading panel instruments");
757 const SGPropertyNode * instrument_group = root.getChild("instruments");
758 if (instrument_group != 0) {
759 int nInstruments = instrument_group->nChildren();
760 for (int i = 0; i < nInstruments; i++) {
761 const SGPropertyNode * node = instrument_group->getChild(i);
763 FGPath path( globals->get_fg_root() );
764 path.append(node->getStringValue("path"));
766 SG_LOG(SG_INPUT, SG_INFO, "Reading instrument "
771 int x = node->getIntValue("x", -1);
772 int y = node->getIntValue("y", -1);
773 int w = node->getIntValue("w", -1);
774 int h = node->getIntValue("h", -1);
776 if (x == -1 || y == -1) {
777 SG_LOG(SG_INPUT, SG_ALERT, "x and y positions must be specified and >0");
782 // Read the instrument from
784 FGPanelInstrument * instrument = 0;
786 SGPropertyNode root2;
788 if (readProperties(path.str(), &root2)) {
789 cerr << "Read " << root2.nChildren() << " top-level nodes from "
790 << path.c_str() << endl;
791 instrument = readInstrument(&root2, x, y, w, h);
794 instrument = new DefaultInstrument(x, y, w, h);
795 panel->addInstrument(instrument);
798 SG_LOG(SG_INPUT, SG_INFO, "Done reading panel instruments");
802 // Return the new panel.
809 * Read a panel from a property list.
811 * This function opens a stream to a file, then invokes the
812 * main fgReadPanel() function.
815 fgReadPanel (const string &relative_path)
818 FGPath path(globals->get_fg_root());
819 path.append(relative_path);
820 ifstream input(path.c_str());
822 SG_LOG(SG_INPUT, SG_ALERT,
823 "Cannot read panel configuration from " << path.str());
825 panel = fgReadPanel(input);
829 panel = new DefaultPanel(0, 0, 1024, 768);
835 // end of panel_io.cxx