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"
50 ////////////////////////////////////////////////////////////////////////
51 // Built-in layer for the magnetic compass ribbon layer.
53 // TODO: move this out into a special directory for built-in
54 // layers of various sorts.
55 ////////////////////////////////////////////////////////////////////////
57 class FGMagRibbon : public FGTexturedLayer
60 FGMagRibbon (int w, int h);
61 virtual ~FGMagRibbon () {}
66 FGMagRibbon::FGMagRibbon (int w, int h)
67 : FGTexturedLayer(w, h)
69 CroppedTexture texture("Textures/Panel/compass-ribbon.rgb");
76 double heading = FGSteam::get_MH_deg();
77 double xoffset, yoffset;
79 while (heading >= 360.0) {
82 while (heading < 0.0) {
86 if (heading >= 60.0 && heading <= 180.0) {
87 xoffset = heading / 240.0;
89 } else if (heading >= 150.0 && heading <= 270.0) {
90 xoffset = (heading - 90.0) / 240.0;
92 } else if (heading >= 240.0 && heading <= 360.0) {
93 xoffset = (heading - 180.0) / 240.0;
98 xoffset = (heading - 270.0) / 240.0;
102 xoffset = 1.0 - xoffset;
103 // Adjust to put the number in the centre
106 CroppedTexture &t = getTexture();
109 t.maxX = xoffset + 0.5;
110 t.maxY = yoffset + 0.25;
111 FGTexturedLayer::draw();
116 ////////////////////////////////////////////////////////////////////////
117 // Read and construct a panel.
119 // The panel is specified as a regular property list, and each of the
120 // instruments is its own, separate property list (and thus, a separate
121 // XML document). The functions in this section read in the files
122 // as property lists, then extract properties to set up the panel
125 // A panel contains zero or more instruments.
127 // An instrument contains one or more layers and zero or more actions.
129 // A layer contains zero or more transformations.
131 // Some special types of layers also contain other objects, such as
132 // chunks of text or other layers.
134 // There are currently four types of layers:
136 // 1. Textured Layer (type="texture"), the default
137 // 2. Text Layer (type="text")
138 // 3. Switch Layer (type="switch")
139 // 4. Built-in Layer (type="built-in", must also specify class)
141 // The only built-in layer so far is the ribbon for the magnetic compass
142 // (class="compass-ribbon").
144 // There are three types of actions:
146 // 1. Adjust (type="adjust"), the default
147 // 2. Swap (type="swap")
148 // 3. Toggle (type="toggle")
150 // There are three types of transformations:
152 // 1. X shift (type="x-shift"), the default
153 // 2. Y shift (type="y-shift")
154 // 3. Rotation (type="rotation")
156 // Each of these may be associated with a property, so that a needle
157 // will rotate with the airspeed, for example, or may have a fixed
158 // floating-point value.
159 ////////////////////////////////////////////////////////////////////////
163 * Read a cropped texture from the instrument's property list.
165 * The x1 and y1 properties give the starting position of the texture
166 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
167 * ending position. For example, to use the bottom-left quarter of a
168 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
170 static CroppedTexture
171 readTexture (SGPropertyNode node)
173 CroppedTexture texture(node.getStringValue("path"),
174 node.getFloatValue("x1"),
175 node.getFloatValue("y1"),
176 node.getFloatValue("x2", 1.0),
177 node.getFloatValue("y2", 1.0));
178 FG_LOG(FG_INPUT, FG_INFO, "Read texture " << node.getName());
184 * Read an action from the instrument's property list.
186 * The action will be performed when the user clicks a mouse button
187 * within the specified region of the instrument. Actions always
188 * work by modifying the value of a property (see the SGValue class).
190 * The following action types are defined:
192 * "adjust" - modify the value of a floating-point property by
193 * the increment specified. This is the default.
195 * "swap" - swap the values of two-floating-point properties.
197 * "toggle" - toggle the value of a boolean property between true and
200 * For the adjust action, it is possible to specify an increment
201 * (use a negative number for a decrement), a minimum allowed value,
202 * a maximum allowed value, and a flag to indicate whether the value
203 * should freeze or wrap-around when it reachs the minimum or maximum.
205 * The action will be scaled automatically if the instrument is not
206 * being drawn at its regular size.
208 static FGPanelAction *
209 readAction (SGPropertyNode node, float hscale, float vscale)
211 FGPanelAction * action = 0;
213 string name = node.getStringValue("name");
214 string type = node.getStringValue("type");
216 int button = node.getIntValue("button");
217 int x = int(node.getIntValue("x") * hscale);
218 int y = int(node.getIntValue("y") * vscale);
219 int w = int(node.getIntValue("w") * hscale);
220 int h = int(node.getIntValue("h") * vscale);
223 FG_LOG(FG_INPUT, FG_ALERT,
224 "No type supplied for action " << name << " assuming \"adjust\"");
228 // Adjust a property value
229 if (type == "adjust") {
230 string propName = node.getStringValue("property");
231 SGValue * value = current_properties.getValue(propName, true);
232 float increment = node.getFloatValue("increment", 1.0);
233 float min = node.getFloatValue("min", 0.0);
234 float max = node.getFloatValue("max", 0.0);
235 bool wrap = node.getBoolValue("wrap", false);
237 FG_LOG(FG_INPUT, FG_ALERT, "Action " << node.getName()
238 << " has same min and max value");
239 action = new FGAdjustAction(button, x, y, w, h, value,
240 increment, min, max, wrap);
243 // Swap two property values
244 else if (type == "swap") {
245 string propName1 = node.getStringValue("property1");
246 string propName2 = node.getStringValue("property2");
247 SGValue * value1 = current_properties.getValue(propName1, true);
248 SGValue * value2 = current_properties.getValue(propName2, true);
249 action = new FGSwapAction(button, x, y, w, h, value1, value2);
252 // Toggle a boolean value
253 else if (type == "toggle") {
254 string propName = node.getStringValue("property");
255 SGValue * value = current_properties.getValue(propName, true);
256 action = new FGToggleAction(button, x, y, w, h, value);
261 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized action type " << type);
270 * Read a transformation from the instrument's property list.
272 * The panel module uses the transformations to slide or spin needles,
273 * knobs, and other indicators, and to place layers in the correct
274 * positions. Every layer starts centered exactly on the x,y co-ordinate,
275 * and many layers need to be moved or rotated simply to display the
276 * instrument correctly.
278 * There are three types of transformations:
280 * "x-shift" - move the layer horizontally.
282 * "y-shift" - move the layer vertically.
284 * "rotation" - rotate the layer.
286 * Each transformation may have a fixed offset, and may also have
287 * a floating-point property value to add to the offset. The
288 * floating-point property may be clamped to a minimum and/or
289 * maximum range and scaled (after clamping).
291 * Note that because of the way OpenGL works, transformations will
292 * appear to be applied backwards.
294 static FGPanelTransformation *
295 readTransformation (SGPropertyNode node, float hscale, float vscale)
297 FGPanelTransformation * t = new FGPanelTransformation;
299 string name = node.getName();
300 string type = node.getStringValue("type");
301 string propName = node.getStringValue("property", "");
305 FG_LOG(FG_INPUT, FG_ALERT,
306 "No type supplied for transformation " << name
307 << " assuming \"rotation\"");
311 if (propName != "") {
312 value = current_properties.getValue(propName, true);
316 t->min = node.getFloatValue("min", -9999999);
317 t->max = node.getFloatValue("max", 99999999);
318 t->factor = node.getFloatValue("scale", 1.0);
319 t->offset = node.getFloatValue("offset", 0.0);
321 // Move the layer horizontally.
322 if (type == "x-shift") {
323 t->type = FGPanelTransformation::XSHIFT;
329 // Move the layer vertically.
330 else if (type == "y-shift") {
331 t->type = FGPanelTransformation::YSHIFT;
337 // Rotate the layer. The rotation
338 // is in degrees, and does not need
339 // to scale with the instrument size.
340 else if (type == "rotation") {
341 t->type = FGPanelTransformation::ROTATION;
345 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized transformation type " << type);
350 FG_LOG(FG_INPUT, FG_INFO, "Read transformation " << name);
356 * Read a chunk of text from the instrument's property list.
358 * A text layer consists of one or more chunks of text. All chunks
359 * share the same font size and color (and eventually, font), but
360 * each can come from a different source. There are three types of
363 * "literal" - a literal text string (the default)
365 * "text-value" - the current value of a string property
367 * "number-value" - the current value of a floating-point property.
369 * All three may also include a printf-style format string.
372 readTextChunk (SGPropertyNode node)
374 FGTextLayer::Chunk * chunk;
375 string name = node.getStringValue("name");
376 string type = node.getStringValue("type");
377 string format = node.getStringValue("format");
379 // Default to literal text.
381 FG_LOG(FG_INPUT, FG_INFO, "No type provided for text chunk " << name
382 << " assuming \"literal\"");
386 // A literal text string.
387 if (type == "literal") {
388 string text = node.getStringValue("text");
389 chunk = new FGTextLayer::Chunk(text, format);
392 // The value of a string property.
393 else if (type == "text-value") {
395 current_properties.getValue(node.getStringValue("property"), true);
396 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
399 // The value of a float property.
400 else if (type == "number-value") {
401 string propName = node.getStringValue("property");
402 float scale = node.getFloatValue("scale", 1.0);
403 SGValue * value = current_properties.getValue(propName, true);
404 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, value,
410 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
411 << " for text chunk " << name);
420 * Read a single layer from an instrument's property list.
422 * Each instrument consists of one or more layers stacked on top
423 * of each other; the lower layers show through only where the upper
424 * layers contain an alpha component. Each layer can be moved
425 * horizontally and vertically and rotated using transformations.
427 * This module currently recognizes four kinds of layers:
429 * "texture" - a layer containing a texture (the default)
431 * "text" - a layer containing text
433 * "switch" - a layer that switches between two other layers
434 * based on the current value of a boolean property.
436 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
438 * Currently, the only built-in layer class is "compass-ribbon".
440 static FGInstrumentLayer *
441 readLayer (SGPropertyNode node, float hscale, float vscale)
443 FGInstrumentLayer * layer;
444 string name = node.getStringValue("name");
445 string type = node.getStringValue("type");
446 int w = node.getIntValue("w", -1);
447 int h = node.getIntValue("h", -1);
455 FG_LOG(FG_INPUT, FG_ALERT,
456 "No type supplied for layer " << name
457 << " assuming \"texture\"");
462 // A textured instrument layer.
463 if (type == "texture") {
464 CroppedTexture texture = readTexture(node.getSubNode("texture"));
465 layer = new FGTexturedLayer(texture, w, h);
469 // A textual instrument layer.
470 else if (type == "text") {
471 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
473 // Set the text color.
474 float red = node.getFloatValue("color/red", 0.0);
475 float green = node.getFloatValue("color/green", 0.0);
476 float blue = node.getFloatValue("color/blue", 0.0);
477 tlayer->setColor(red, green, blue);
479 // Set the point size.
480 float pointSize = node.getFloatValue("point-size", 10.0) * hscale;
481 tlayer->setPointSize(pointSize);
486 SGPropertyNode chunk_group = node.getSubNode("chunks");
487 int nChunks = chunk_group.size();
488 for (int i = 0; i < nChunks; i++) {
489 FGTextLayer::Chunk * chunk = readTextChunk(chunk_group.getChild(i));
494 tlayer->addChunk(chunk);
499 // A switch instrument layer.
500 else if (type == "switch") {
502 current_properties.getValue(node.getStringValue("property"));
503 FGInstrumentLayer * layer1 =
504 readLayer(node.getSubNode("layer1"), hscale, vscale);
505 FGInstrumentLayer * layer2 =
506 readLayer(node.getSubNode("layer2"), hscale, vscale);
507 layer = new FGSwitchLayer(w, h, value, layer1, layer2);
510 // A built-in instrument layer.
511 else if (type == "built-in") {
512 string layerclass = node.getStringValue("class");
514 if (layerclass == "mag-ribbon") {
515 layer = new FGMagRibbon(w, h);
518 else if (layerclass == "") {
519 FG_LOG(FG_INPUT, FG_ALERT, "No class provided for built-in layer "
525 FG_LOG(FG_INPUT, FG_ALERT, "Unknown built-in layer class "
533 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized layer type " << type);
539 // Get the transformations for each layer.
541 SGPropertyNode trans_group = node.getSubNode("transformations");
542 int nTransformations = trans_group.size();
543 for (int k = 0; k < nTransformations; k++) {
544 FGPanelTransformation * t = readTransformation(trans_group.getChild(k),
550 layer->addTransformation(t);
553 FG_LOG(FG_INPUT, FG_INFO, "Read layer " << name);
559 * Read an instrument from a property list.
561 * The instrument consists of a preferred width and height
562 * (the panel may override these), together with a list of layers
563 * and a list of actions to be performed when the user clicks
564 * the mouse over the instrument. All co-ordinates are relative
565 * to the instrument's position, so instruments are fully relocatable;
566 * likewise, co-ordinates for actions and transformations will be
567 * scaled automatically if the instrument is not at its preferred size.
569 static FGPanelInstrument *
570 readInstrument (SGPropertyNode node, int x, int y, int real_w, int real_h)
572 int w = node.getIntValue("w");
573 int h = node.getIntValue("h");
574 const string &name = node.getStringValue("name");
579 hscale = float(real_w) / float(w);
581 cerr << "hscale is " << hscale << endl;
584 vscale = float(real_h) / float(h);
586 cerr << "vscale is " << hscale << endl;
589 FG_LOG(FG_INPUT, FG_INFO, "Reading instrument " << name);
591 FGLayeredInstrument * instrument =
592 new FGLayeredInstrument(x, y, w, h);
595 // Get the actions for the instrument.
597 SGPropertyNode action_group = node.getSubNode("actions");
598 int nActions = action_group.size();
599 for (int j = 0; j < nActions; j++) {
600 FGPanelAction * action = readAction(action_group.getChild(j),
606 instrument->addAction(action);
610 // Get the layers for the instrument.
612 SGPropertyNode layer_group = node.getSubNode("layers");
613 int nLayers = layer_group.size();
614 for (int j = 0; j < nLayers; j++) {
615 FGInstrumentLayer * layer = readLayer(layer_group.getChild(j),
621 instrument->addLayer(layer);
624 FG_LOG(FG_INPUT, FG_INFO, "Done reading instrument " << name);
630 * Read a panel from a property list.
632 * Each panel instrument will appear in its own, separate
633 * property list. The top level simply names the panel and
634 * places the instruments in their appropriate locations (and
635 * optionally resizes them if necessary).
637 * Returns 0 if the read fails for any reason.
640 fgReadPanel (istream &input)
642 SGPropertyList props;
646 // Read the property list from disk.
648 if (!readPropertyList(input, &props)) {
649 FG_LOG(FG_INPUT, FG_ALERT, "Malformed property list for panel.");
652 FG_LOG(FG_INPUT, FG_INFO, "Read properties for panel " <<
653 props.getStringValue("/name"));
656 // Construct a new, empty panel.
658 FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
661 // Assign the background texture, if any, or a bogus chequerboard.
663 string bgTexture = props.getStringValue("/background");
666 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
667 FG_LOG(FG_INPUT, FG_INFO, "Set background texture to " << bgTexture);
671 // Create each instrument.
673 FG_LOG(FG_INPUT, FG_INFO, "Reading panel instruments");
674 SGPropertyNode instrument_group("/instruments", &props);
675 int nInstruments = instrument_group.size();
676 for (int i = 0; i < nInstruments; i++) {
677 SGPropertyList props2;
678 SGPropertyNode node = instrument_group.getChild(i);
680 FGPath path(current_options.get_fg_root());
681 path.append(node.getStringValue("path"));
683 FG_LOG(FG_INPUT, FG_INFO, "Reading instrument "
688 int x = node.getIntValue("x", -1);
689 int y = node.getIntValue("y", -1);
690 int w = node.getIntValue("w", -1);
691 int h = node.getIntValue("h", -1);
693 if (x == -1 || y == -1) {
694 FG_LOG(FG_INPUT, FG_ALERT, "x and y positions must be specified and >0");
699 if (!readPropertyList(path.str(), &props2)) {
704 FGPanelInstrument * instrument =
705 readInstrument(SGPropertyNode("/", &props2), x, y, w, h);
706 if (instrument == 0) {
711 panel->addInstrument(instrument);
713 FG_LOG(FG_INPUT, FG_INFO, "Done reading panel instruments");
717 // Return the new panel.
724 * Read a panel from a property list.
726 * This function opens a stream to a file, then invokes the
727 * main fgReadPanel() function.
730 fgReadPanel (const string &relative_path)
732 FGPath path(current_options.get_fg_root());
733 path.append(relative_path);
734 ifstream input(path.c_str());
736 FG_LOG(FG_INPUT, FG_ALERT,
737 "Cannot read panel configuration from " << path.str());
740 FGPanel * panel = fgReadPanel(input);
747 // end of panel_io.cxx