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>
30 #include <simgear/misc/fgpath.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/props.hxx>
38 #include <Main/options.hxx>
42 #include "panel_io.hxx"
44 FG_USING_STD(istream);
45 FG_USING_STD(ifstream);
50 ////////////////////////////////////////////////////////////////////////
51 // Default panel, instrument, and layer for when things go wrong...
52 ////////////////////////////////////////////////////////////////////////
54 static FGCroppedTexture defaultTexture("Textures/default.rgb");
58 * Default layer: the default texture.
60 class DefaultLayer : public FGTexturedLayer
63 DefaultLayer () : FGTexturedLayer(defaultTexture)
70 * Default instrument: a single default layer.
72 class DefaultInstrument : public FGLayeredInstrument
75 DefaultInstrument (int x, int y, int w, int h)
76 : FGLayeredInstrument(x, y, w, h)
78 addLayer(new DefaultLayer());
84 * Default panel: the default texture.
86 class DefaultPanel : public FGPanel
89 DefaultPanel (int x, int y, int w, int h) : FGPanel(x, y, w, h)
91 setBackground(defaultTexture.getTexture());
97 ////////////////////////////////////////////////////////////////////////
98 // Built-in layer for the magnetic compass ribbon layer.
100 // TODO: move this out into a special directory for built-in
101 // layers of various sorts.
102 ////////////////////////////////////////////////////////////////////////
104 class FGMagRibbon : public FGTexturedLayer
107 FGMagRibbon (int w, int h);
108 virtual ~FGMagRibbon () {}
110 virtual void draw ();
113 FGMagRibbon::FGMagRibbon (int w, int h)
114 : FGTexturedLayer(w, h)
116 FGCroppedTexture texture("Instruments/Default/Textures/compass-ribbon.rgb");
123 double heading = FGSteam::get_MH_deg();
124 double xoffset, yoffset;
126 while (heading >= 360.0) {
129 while (heading < 0.0) {
133 if (heading >= 60.0 && heading <= 180.0) {
134 xoffset = heading / 240.0;
136 } else if (heading >= 150.0 && heading <= 270.0) {
137 xoffset = (heading - 90.0) / 240.0;
139 } else if (heading >= 240.0 && heading <= 360.0) {
140 xoffset = (heading - 180.0) / 240.0;
145 xoffset = (heading - 270.0) / 240.0;
149 xoffset = 1.0 - xoffset;
150 // Adjust to put the number in the centre
153 FGCroppedTexture &t = getTexture();
154 t.setCrop(xoffset, yoffset, xoffset + 0.5, yoffset + 0.25);
155 FGTexturedLayer::draw();
160 ////////////////////////////////////////////////////////////////////////
161 // Read and construct a panel.
163 // The panel is specified as a regular property list, and each of the
164 // instruments is its own, separate property list (and thus, a separate
165 // XML document). The functions in this section read in the files
166 // as property lists, then extract properties to set up the panel
169 // A panel contains zero or more instruments.
171 // An instrument contains one or more layers and zero or more actions.
173 // A layer contains zero or more transformations.
175 // Some special types of layers also contain other objects, such as
176 // chunks of text or other layers.
178 // There are currently four types of layers:
180 // 1. Textured Layer (type="texture"), the default
181 // 2. Text Layer (type="text")
182 // 3. Switch Layer (type="switch")
183 // 4. Built-in Layer (type="built-in", must also specify class)
185 // The only built-in layer so far is the ribbon for the magnetic compass
186 // (class="compass-ribbon").
188 // There are three types of actions:
190 // 1. Adjust (type="adjust"), the default
191 // 2. Swap (type="swap")
192 // 3. Toggle (type="toggle")
194 // There are three types of transformations:
196 // 1. X shift (type="x-shift"), the default
197 // 2. Y shift (type="y-shift")
198 // 3. Rotation (type="rotation")
200 // Each of these may be associated with a property, so that a needle
201 // will rotate with the airspeed, for example, or may have a fixed
202 // floating-point value.
203 ////////////////////////////////////////////////////////////////////////
207 * Read a cropped texture from the instrument's property list.
209 * The x1 and y1 properties give the starting position of the texture
210 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
211 * ending position. For example, to use the bottom-left quarter of a
212 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
214 static FGCroppedTexture
215 readTexture (SGPropertyNode node)
217 FGCroppedTexture texture(node.getStringValue("path"),
218 node.getFloatValue("x1"),
219 node.getFloatValue("y1"),
220 node.getFloatValue("x2", 1.0),
221 node.getFloatValue("y2", 1.0));
222 FG_LOG(FG_INPUT, FG_INFO, "Read texture " << node.getName());
228 * Read an action from the instrument's property list.
230 * The action will be performed when the user clicks a mouse button
231 * within the specified region of the instrument. Actions always
232 * work by modifying the value of a property (see the SGValue class).
234 * The following action types are defined:
236 * "adjust" - modify the value of a floating-point property by
237 * the increment specified. This is the default.
239 * "swap" - swap the values of two-floating-point properties.
241 * "toggle" - toggle the value of a boolean property between true and
244 * For the adjust action, it is possible to specify an increment
245 * (use a negative number for a decrement), a minimum allowed value,
246 * a maximum allowed value, and a flag to indicate whether the value
247 * should freeze or wrap-around when it reachs the minimum or maximum.
249 * The action will be scaled automatically if the instrument is not
250 * being drawn at its regular size.
252 static FGPanelAction *
253 readAction (SGPropertyNode node, float hscale, float vscale)
255 FGPanelAction * action = 0;
257 string name = node.getStringValue("name");
258 string type = node.getStringValue("type");
260 int button = node.getIntValue("button");
261 int x = int(node.getIntValue("x") * hscale);
262 int y = int(node.getIntValue("y") * vscale);
263 int w = int(node.getIntValue("w") * hscale);
264 int h = int(node.getIntValue("h") * vscale);
267 FG_LOG(FG_INPUT, FG_ALERT,
268 "No type supplied for action " << name << " assuming \"adjust\"");
272 // Adjust a property value
273 if (type == "adjust") {
274 string propName = node.getStringValue("property");
275 SGValue * value = current_properties.getValue(propName, true);
276 float increment = node.getFloatValue("increment", 1.0);
277 float min = node.getFloatValue("min", 0.0);
278 float max = node.getFloatValue("max", 0.0);
279 bool wrap = node.getBoolValue("wrap", false);
281 FG_LOG(FG_INPUT, FG_ALERT, "Action " << node.getName()
282 << " has same min and max value");
283 action = new FGAdjustAction(button, x, y, w, h, value,
284 increment, min, max, wrap);
287 // Swap two property values
288 else if (type == "swap") {
289 string propName1 = node.getStringValue("property1");
290 string propName2 = node.getStringValue("property2");
291 SGValue * value1 = current_properties.getValue(propName1, true);
292 SGValue * value2 = current_properties.getValue(propName2, true);
293 action = new FGSwapAction(button, x, y, w, h, value1, value2);
296 // Toggle a boolean value
297 else if (type == "toggle") {
298 string propName = node.getStringValue("property");
299 SGValue * value = current_properties.getValue(propName, true);
300 action = new FGToggleAction(button, x, y, w, h, value);
305 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized action type " << type);
314 * Read a transformation from the instrument's property list.
316 * The panel module uses the transformations to slide or spin needles,
317 * knobs, and other indicators, and to place layers in the correct
318 * positions. Every layer starts centered exactly on the x,y co-ordinate,
319 * and many layers need to be moved or rotated simply to display the
320 * instrument correctly.
322 * There are three types of transformations:
324 * "x-shift" - move the layer horizontally.
326 * "y-shift" - move the layer vertically.
328 * "rotation" - rotate the layer.
330 * Each transformation may have a fixed offset, and may also have
331 * a floating-point property value to add to the offset. The
332 * floating-point property may be clamped to a minimum and/or
333 * maximum range and scaled (after clamping).
335 * Note that because of the way OpenGL works, transformations will
336 * appear to be applied backwards.
338 static FGPanelTransformation *
339 readTransformation (SGPropertyNode node, float hscale, float vscale)
341 FGPanelTransformation * t = new FGPanelTransformation;
343 string name = node.getName();
344 string type = node.getStringValue("type");
345 string propName = node.getStringValue("property", "");
349 FG_LOG(FG_INPUT, FG_ALERT,
350 "No type supplied for transformation " << name
351 << " assuming \"rotation\"");
355 if (propName != "") {
356 value = current_properties.getValue(propName, true);
360 t->min = node.getFloatValue("min", -9999999);
361 t->max = node.getFloatValue("max", 99999999);
362 t->factor = node.getFloatValue("scale", 1.0);
363 t->offset = node.getFloatValue("offset", 0.0);
365 // Move the layer horizontally.
366 if (type == "x-shift") {
367 t->type = FGPanelTransformation::XSHIFT;
373 // Move the layer vertically.
374 else if (type == "y-shift") {
375 t->type = FGPanelTransformation::YSHIFT;
381 // Rotate the layer. The rotation
382 // is in degrees, and does not need
383 // to scale with the instrument size.
384 else if (type == "rotation") {
385 t->type = FGPanelTransformation::ROTATION;
389 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized transformation type " << type);
394 FG_LOG(FG_INPUT, FG_INFO, "Read transformation " << name);
400 * Read a chunk of text from the instrument's property list.
402 * A text layer consists of one or more chunks of text. All chunks
403 * share the same font size and color (and eventually, font), but
404 * each can come from a different source. There are three types of
407 * "literal" - a literal text string (the default)
409 * "text-value" - the current value of a string property
411 * "number-value" - the current value of a floating-point property.
413 * All three may also include a printf-style format string.
416 readTextChunk (SGPropertyNode node)
418 FGTextLayer::Chunk * chunk;
419 string name = node.getStringValue("name");
420 string type = node.getStringValue("type");
421 string format = node.getStringValue("format");
423 // Default to literal text.
425 FG_LOG(FG_INPUT, FG_INFO, "No type provided for text chunk " << name
426 << " assuming \"literal\"");
430 // A literal text string.
431 if (type == "literal") {
432 string text = node.getStringValue("text");
433 chunk = new FGTextLayer::Chunk(text, format);
436 // The value of a string property.
437 else if (type == "text-value") {
439 current_properties.getValue(node.getStringValue("property"), true);
440 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
443 // The value of a float property.
444 else if (type == "number-value") {
445 string propName = node.getStringValue("property");
446 float scale = node.getFloatValue("scale", 1.0);
447 SGValue * value = current_properties.getValue(propName, true);
448 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, value,
454 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
455 << " for text chunk " << name);
464 * Read a single layer from an instrument's property list.
466 * Each instrument consists of one or more layers stacked on top
467 * of each other; the lower layers show through only where the upper
468 * layers contain an alpha component. Each layer can be moved
469 * horizontally and vertically and rotated using transformations.
471 * This module currently recognizes four kinds of layers:
473 * "texture" - a layer containing a texture (the default)
475 * "text" - a layer containing text
477 * "switch" - a layer that switches between two other layers
478 * based on the current value of a boolean property.
480 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
482 * Currently, the only built-in layer class is "compass-ribbon".
484 static FGInstrumentLayer *
485 readLayer (SGPropertyNode node, float hscale, float vscale)
487 FGInstrumentLayer * layer = NULL;
488 string name = node.getStringValue("name");
489 string type = node.getStringValue("type");
490 int w = node.getIntValue("w", -1);
491 int h = node.getIntValue("h", -1);
499 FG_LOG(FG_INPUT, FG_ALERT,
500 "No type supplied for layer " << name
501 << " assuming \"texture\"");
506 // A textured instrument layer.
507 if (type == "texture") {
508 FGCroppedTexture texture = readTexture(node.getSubNode("texture"));
509 layer = new FGTexturedLayer(texture, w, h);
513 // A textual instrument layer.
514 else if (type == "text") {
515 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
517 // Set the text color.
518 float red = node.getFloatValue("color/red", 0.0);
519 float green = node.getFloatValue("color/green", 0.0);
520 float blue = node.getFloatValue("color/blue", 0.0);
521 tlayer->setColor(red, green, blue);
523 // Set the point size.
524 float pointSize = node.getFloatValue("point-size", 10.0) * hscale;
525 tlayer->setPointSize(pointSize);
530 SGPropertyNode chunk_group = node.getSubNode("chunks");
531 int nChunks = chunk_group.size();
532 for (int i = 0; i < nChunks; i++) {
533 FGTextLayer::Chunk * chunk = readTextChunk(chunk_group.getChild(i));
538 tlayer->addChunk(chunk);
543 // A switch instrument layer.
544 else if (type == "switch") {
546 current_properties.getValue(node.getStringValue("property"), true);
547 FGInstrumentLayer * layer1 =
548 readLayer(node.getSubNode("layer1"), hscale, vscale);
549 FGInstrumentLayer * layer2 =
550 readLayer(node.getSubNode("layer2"), hscale, vscale);
551 layer = new FGSwitchLayer(w, h, value, layer1, layer2);
554 // A built-in instrument layer.
555 else if (type == "built-in") {
556 string layerclass = node.getStringValue("class");
558 if (layerclass == "mag-ribbon") {
559 layer = new FGMagRibbon(w, h);
562 else if (layerclass == "") {
563 FG_LOG(FG_INPUT, FG_ALERT, "No class provided for built-in layer "
569 FG_LOG(FG_INPUT, FG_ALERT, "Unknown built-in layer class "
577 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized layer type " << type);
583 // Get the transformations for each layer.
585 SGPropertyNode trans_group = node.getSubNode("transformations");
586 int nTransformations = trans_group.size();
587 for (int k = 0; k < nTransformations; k++) {
588 FGPanelTransformation * t = readTransformation(trans_group.getChild(k),
594 layer->addTransformation(t);
597 FG_LOG(FG_INPUT, FG_INFO, "Read layer " << name);
603 * Read an instrument from a property list.
605 * The instrument consists of a preferred width and height
606 * (the panel may override these), together with a list of layers
607 * and a list of actions to be performed when the user clicks
608 * the mouse over the instrument. All co-ordinates are relative
609 * to the instrument's position, so instruments are fully relocatable;
610 * likewise, co-ordinates for actions and transformations will be
611 * scaled automatically if the instrument is not at its preferred size.
613 static FGPanelInstrument *
614 readInstrument (SGPropertyNode node, int x, int y, int real_w, int real_h)
616 int w = node.getIntValue("w");
617 int h = node.getIntValue("h");
618 const string &name = node.getStringValue("name");
623 hscale = float(real_w) / float(w);
627 vscale = float(real_h) / float(h);
631 FG_LOG(FG_INPUT, FG_INFO, "Reading instrument " << name);
633 FGLayeredInstrument * instrument =
634 new FGLayeredInstrument(x, y, w, h);
637 // Get the actions for the instrument.
639 SGPropertyNode action_group = node.getSubNode("actions");
640 int nActions = action_group.size();
642 for (j = 0; j < nActions; j++) {
643 FGPanelAction * action = readAction(action_group.getChild(j),
647 return new DefaultInstrument(x, y, w, h);
649 instrument->addAction(action);
653 // Get the layers for the instrument.
655 SGPropertyNode layer_group = node.getSubNode("layers");
656 int nLayers = layer_group.size();
657 for (j = 0; j < nLayers; j++) {
658 FGInstrumentLayer * layer = readLayer(layer_group.getChild(j),
662 return new DefaultInstrument(x, y, w, h);
664 instrument->addLayer(layer);
667 FG_LOG(FG_INPUT, FG_INFO, "Done reading instrument " << name);
673 * Read a panel from a property list.
675 * Each panel instrument will appear in its own, separate
676 * property list. The top level simply names the panel and
677 * places the instruments in their appropriate locations (and
678 * optionally resizes them if necessary).
680 * Returns 0 if the read fails for any reason.
683 fgReadPanel (istream &input)
685 SGPropertyList props;
689 // Read the property list from disk.
691 if (!readPropertyList(input, &props)) {
692 FG_LOG(FG_INPUT, FG_ALERT, "Malformed property list for panel.");
695 FG_LOG(FG_INPUT, FG_INFO, "Read properties for panel " <<
696 props.getStringValue("/name"));
699 // Construct a new, empty panel.
701 FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
705 // Grab the panel's dimensions, default to 1024x443.
707 int panel_w = (props.hasValue("/w") ? props.getIntValue("/w") : 1024);
708 int panel_h = (props.hasValue("/h") ? props.getIntValue("/h") : 443);
709 panel->setWidth(panel_w);
710 panel->setHeight(panel_h);
713 // Grab the visible external viewing area, default to
715 panel->setViewHeight(props.hasValue("/view-height") ?
716 props.getIntValue("/view-height") :
720 // Grab the panel's initial offsets, default to 0, 0.
722 int xoffset = (props.hasValue("/x-offset") ?
723 props.getIntValue("x-offset") :
725 int yoffset = (props.hasValue("/y-offset") ?
726 props.getIntValue("y-offset") :
728 panel->setXOffset(xoffset);
729 panel->setYOffset(yoffset);
732 // Assign the background texture, if any, or a bogus chequerboard.
734 string bgTexture = props.getStringValue("/background");
737 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
738 FG_LOG(FG_INPUT, FG_INFO, "Set background texture to " << bgTexture);
742 // Create each instrument.
744 FG_LOG(FG_INPUT, FG_INFO, "Reading panel instruments");
745 SGPropertyNode instrument_group("/instruments", &props);
746 int nInstruments = instrument_group.size();
747 for (int i = 0; i < nInstruments; i++) {
748 SGPropertyList props2;
749 SGPropertyNode node = instrument_group.getChild(i);
751 FGPath path(current_options.get_fg_root());
752 path.append(node.getStringValue("path"));
754 FG_LOG(FG_INPUT, FG_INFO, "Reading instrument "
759 int x = node.getIntValue("x", -1);
760 int y = node.getIntValue("y", -1);
761 int w = node.getIntValue("w", -1);
762 int h = node.getIntValue("h", -1);
764 if (x == -1 || y == -1) {
765 FG_LOG(FG_INPUT, FG_ALERT, "x and y positions must be specified and >0");
771 FGPanelInstrument * instrument = 0;
773 if (readPropertyList(path.str(), &props2)) {
774 instrument = readInstrument(SGPropertyNode("/", &props2), x, y, w, h);
776 if (instrument == 0) {
777 instrument = new DefaultInstrument(x, y, w, h);
779 panel->addInstrument(instrument);
781 FG_LOG(FG_INPUT, FG_INFO, "Done reading panel instruments");
785 // Return the new panel.
792 * Read a panel from a property list.
794 * This function opens a stream to a file, then invokes the
795 * main fgReadPanel() function.
798 fgReadPanel (const string &relative_path)
801 FGPath path(current_options.get_fg_root());
802 path.append(relative_path);
803 ifstream input(path.c_str());
805 FG_LOG(FG_INPUT, FG_ALERT,
806 "Cannot read panel configuration from " << path.str());
808 panel = fgReadPanel(input);
812 panel = new DefaultPanel(0, 0, 1024, 768);
818 // end of panel_io.cxx