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 // 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 FGCroppedTexture texture("Instruments/Default/Textures/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 FGCroppedTexture &t = getTexture();
107 t.setCrop(xoffset, yoffset, xoffset + 0.5, yoffset + 0.25);
108 FGTexturedLayer::draw();
113 ////////////////////////////////////////////////////////////////////////
114 // Read and construct a panel.
116 // The panel is specified as a regular property list, and each of the
117 // instruments is its own, separate property list (and thus, a separate
118 // XML document). The functions in this section read in the files
119 // as property lists, then extract properties to set up the panel
122 // A panel contains zero or more instruments.
124 // An instrument contains one or more layers and zero or more actions.
126 // A layer contains zero or more transformations.
128 // Some special types of layers also contain other objects, such as
129 // chunks of text or other layers.
131 // There are currently four types of layers:
133 // 1. Textured Layer (type="texture"), the default
134 // 2. Text Layer (type="text")
135 // 3. Switch Layer (type="switch")
136 // 4. Built-in Layer (type="built-in", must also specify class)
138 // The only built-in layer so far is the ribbon for the magnetic compass
139 // (class="compass-ribbon").
141 // There are three types of actions:
143 // 1. Adjust (type="adjust"), the default
144 // 2. Swap (type="swap")
145 // 3. Toggle (type="toggle")
147 // There are three types of transformations:
149 // 1. X shift (type="x-shift"), the default
150 // 2. Y shift (type="y-shift")
151 // 3. Rotation (type="rotation")
153 // Each of these may be associated with a property, so that a needle
154 // will rotate with the airspeed, for example, or may have a fixed
155 // floating-point value.
156 ////////////////////////////////////////////////////////////////////////
160 * Read a cropped texture from the instrument's property list.
162 * The x1 and y1 properties give the starting position of the texture
163 * (between 0.0 and 1.0), and the the x2 and y2 properties give the
164 * ending position. For example, to use the bottom-left quarter of a
165 * texture, x1=0.0, y1=0.0, x2=0.5, y2=0.5.
167 static FGCroppedTexture
168 readTexture (SGPropertyNode node)
170 FGCroppedTexture texture(node.getStringValue("path"),
171 node.getFloatValue("x1"),
172 node.getFloatValue("y1"),
173 node.getFloatValue("x2", 1.0),
174 node.getFloatValue("y2", 1.0));
175 FG_LOG(FG_INPUT, FG_INFO, "Read texture " << node.getName());
181 * Read an action from the instrument's property list.
183 * The action will be performed when the user clicks a mouse button
184 * within the specified region of the instrument. Actions always
185 * work by modifying the value of a property (see the SGValue class).
187 * The following action types are defined:
189 * "adjust" - modify the value of a floating-point property by
190 * the increment specified. This is the default.
192 * "swap" - swap the values of two-floating-point properties.
194 * "toggle" - toggle the value of a boolean property between true and
197 * For the adjust action, it is possible to specify an increment
198 * (use a negative number for a decrement), a minimum allowed value,
199 * a maximum allowed value, and a flag to indicate whether the value
200 * should freeze or wrap-around when it reachs the minimum or maximum.
202 * The action will be scaled automatically if the instrument is not
203 * being drawn at its regular size.
205 static FGPanelAction *
206 readAction (SGPropertyNode node, float hscale, float vscale)
208 FGPanelAction * action = 0;
210 string name = node.getStringValue("name");
211 string type = node.getStringValue("type");
213 int button = node.getIntValue("button");
214 int x = int(node.getIntValue("x") * hscale);
215 int y = int(node.getIntValue("y") * vscale);
216 int w = int(node.getIntValue("w") * hscale);
217 int h = int(node.getIntValue("h") * vscale);
220 FG_LOG(FG_INPUT, FG_ALERT,
221 "No type supplied for action " << name << " assuming \"adjust\"");
225 // Adjust a property value
226 if (type == "adjust") {
227 string propName = node.getStringValue("property");
228 SGValue * value = current_properties.getValue(propName, true);
229 float increment = node.getFloatValue("increment", 1.0);
230 float min = node.getFloatValue("min", 0.0);
231 float max = node.getFloatValue("max", 0.0);
232 bool wrap = node.getBoolValue("wrap", false);
234 FG_LOG(FG_INPUT, FG_ALERT, "Action " << node.getName()
235 << " has same min and max value");
236 action = new FGAdjustAction(button, x, y, w, h, value,
237 increment, min, max, wrap);
240 // Swap two property values
241 else if (type == "swap") {
242 string propName1 = node.getStringValue("property1");
243 string propName2 = node.getStringValue("property2");
244 SGValue * value1 = current_properties.getValue(propName1, true);
245 SGValue * value2 = current_properties.getValue(propName2, true);
246 action = new FGSwapAction(button, x, y, w, h, value1, value2);
249 // Toggle a boolean value
250 else if (type == "toggle") {
251 string propName = node.getStringValue("property");
252 SGValue * value = current_properties.getValue(propName, true);
253 action = new FGToggleAction(button, x, y, w, h, value);
258 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized action type " << type);
267 * Read a transformation from the instrument's property list.
269 * The panel module uses the transformations to slide or spin needles,
270 * knobs, and other indicators, and to place layers in the correct
271 * positions. Every layer starts centered exactly on the x,y co-ordinate,
272 * and many layers need to be moved or rotated simply to display the
273 * instrument correctly.
275 * There are three types of transformations:
277 * "x-shift" - move the layer horizontally.
279 * "y-shift" - move the layer vertically.
281 * "rotation" - rotate the layer.
283 * Each transformation may have a fixed offset, and may also have
284 * a floating-point property value to add to the offset. The
285 * floating-point property may be clamped to a minimum and/or
286 * maximum range and scaled (after clamping).
288 * Note that because of the way OpenGL works, transformations will
289 * appear to be applied backwards.
291 static FGPanelTransformation *
292 readTransformation (SGPropertyNode node, float hscale, float vscale)
294 FGPanelTransformation * t = new FGPanelTransformation;
296 string name = node.getName();
297 string type = node.getStringValue("type");
298 string propName = node.getStringValue("property", "");
302 FG_LOG(FG_INPUT, FG_ALERT,
303 "No type supplied for transformation " << name
304 << " assuming \"rotation\"");
308 if (propName != "") {
309 value = current_properties.getValue(propName, true);
313 t->min = node.getFloatValue("min", -9999999);
314 t->max = node.getFloatValue("max", 99999999);
315 t->factor = node.getFloatValue("scale", 1.0);
316 t->offset = node.getFloatValue("offset", 0.0);
318 // Move the layer horizontally.
319 if (type == "x-shift") {
320 t->type = FGPanelTransformation::XSHIFT;
326 // Move the layer vertically.
327 else if (type == "y-shift") {
328 t->type = FGPanelTransformation::YSHIFT;
334 // Rotate the layer. The rotation
335 // is in degrees, and does not need
336 // to scale with the instrument size.
337 else if (type == "rotation") {
338 t->type = FGPanelTransformation::ROTATION;
342 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized transformation type " << type);
347 FG_LOG(FG_INPUT, FG_INFO, "Read transformation " << name);
353 * Read a chunk of text from the instrument's property list.
355 * A text layer consists of one or more chunks of text. All chunks
356 * share the same font size and color (and eventually, font), but
357 * each can come from a different source. There are three types of
360 * "literal" - a literal text string (the default)
362 * "text-value" - the current value of a string property
364 * "number-value" - the current value of a floating-point property.
366 * All three may also include a printf-style format string.
369 readTextChunk (SGPropertyNode node)
371 FGTextLayer::Chunk * chunk;
372 string name = node.getStringValue("name");
373 string type = node.getStringValue("type");
374 string format = node.getStringValue("format");
376 // Default to literal text.
378 FG_LOG(FG_INPUT, FG_INFO, "No type provided for text chunk " << name
379 << " assuming \"literal\"");
383 // A literal text string.
384 if (type == "literal") {
385 string text = node.getStringValue("text");
386 chunk = new FGTextLayer::Chunk(text, format);
389 // The value of a string property.
390 else if (type == "text-value") {
392 current_properties.getValue(node.getStringValue("property"), true);
393 chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
396 // The value of a float property.
397 else if (type == "number-value") {
398 string propName = node.getStringValue("property");
399 float scale = node.getFloatValue("scale", 1.0);
400 SGValue * value = current_properties.getValue(propName, true);
401 chunk = new FGTextLayer::Chunk(FGTextLayer::DOUBLE_VALUE, value,
407 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
408 << " for text chunk " << name);
417 * Read a single layer from an instrument's property list.
419 * Each instrument consists of one or more layers stacked on top
420 * of each other; the lower layers show through only where the upper
421 * layers contain an alpha component. Each layer can be moved
422 * horizontally and vertically and rotated using transformations.
424 * This module currently recognizes four kinds of layers:
426 * "texture" - a layer containing a texture (the default)
428 * "text" - a layer containing text
430 * "switch" - a layer that switches between two other layers
431 * based on the current value of a boolean property.
433 * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
435 * Currently, the only built-in layer class is "compass-ribbon".
437 static FGInstrumentLayer *
438 readLayer (SGPropertyNode node, float hscale, float vscale)
440 FGInstrumentLayer * layer = NULL;
441 string name = node.getStringValue("name");
442 string type = node.getStringValue("type");
443 int w = node.getIntValue("w", -1);
444 int h = node.getIntValue("h", -1);
452 FG_LOG(FG_INPUT, FG_ALERT,
453 "No type supplied for layer " << name
454 << " assuming \"texture\"");
459 // A textured instrument layer.
460 if (type == "texture") {
461 FGCroppedTexture texture = readTexture(node.getSubNode("texture"));
462 layer = new FGTexturedLayer(texture, w, h);
466 // A textual instrument layer.
467 else if (type == "text") {
468 FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
470 // Set the text color.
471 float red = node.getFloatValue("color/red", 0.0);
472 float green = node.getFloatValue("color/green", 0.0);
473 float blue = node.getFloatValue("color/blue", 0.0);
474 tlayer->setColor(red, green, blue);
476 // Set the point size.
477 float pointSize = node.getFloatValue("point-size", 10.0) * hscale;
478 tlayer->setPointSize(pointSize);
483 SGPropertyNode chunk_group = node.getSubNode("chunks");
484 int nChunks = chunk_group.size();
485 for (int i = 0; i < nChunks; i++) {
486 FGTextLayer::Chunk * chunk = readTextChunk(chunk_group.getChild(i));
491 tlayer->addChunk(chunk);
496 // A switch instrument layer.
497 else if (type == "switch") {
499 current_properties.getValue(node.getStringValue("property"), true);
500 FGInstrumentLayer * layer1 =
501 readLayer(node.getSubNode("layer1"), hscale, vscale);
502 FGInstrumentLayer * layer2 =
503 readLayer(node.getSubNode("layer2"), hscale, vscale);
504 layer = new FGSwitchLayer(w, h, value, layer1, layer2);
507 // A built-in instrument layer.
508 else if (type == "built-in") {
509 string layerclass = node.getStringValue("class");
511 if (layerclass == "mag-ribbon") {
512 layer = new FGMagRibbon(w, h);
515 else if (layerclass == "") {
516 FG_LOG(FG_INPUT, FG_ALERT, "No class provided for built-in layer "
522 FG_LOG(FG_INPUT, FG_ALERT, "Unknown built-in layer class "
530 FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized layer type " << type);
536 // Get the transformations for each layer.
538 SGPropertyNode trans_group = node.getSubNode("transformations");
539 int nTransformations = trans_group.size();
540 for (int k = 0; k < nTransformations; k++) {
541 FGPanelTransformation * t = readTransformation(trans_group.getChild(k),
547 layer->addTransformation(t);
550 FG_LOG(FG_INPUT, FG_INFO, "Read layer " << name);
556 * Read an instrument from a property list.
558 * The instrument consists of a preferred width and height
559 * (the panel may override these), together with a list of layers
560 * and a list of actions to be performed when the user clicks
561 * the mouse over the instrument. All co-ordinates are relative
562 * to the instrument's position, so instruments are fully relocatable;
563 * likewise, co-ordinates for actions and transformations will be
564 * scaled automatically if the instrument is not at its preferred size.
566 static FGPanelInstrument *
567 readInstrument (SGPropertyNode node, int x, int y, int real_w, int real_h)
569 int w = node.getIntValue("w");
570 int h = node.getIntValue("h");
571 const string &name = node.getStringValue("name");
576 hscale = float(real_w) / float(w);
578 cerr << "hscale is " << hscale << endl;
581 vscale = float(real_h) / float(h);
583 cerr << "vscale is " << hscale << endl;
586 FG_LOG(FG_INPUT, FG_INFO, "Reading instrument " << name);
588 FGLayeredInstrument * instrument =
589 new FGLayeredInstrument(x, y, w, h);
592 // Get the actions for the instrument.
594 SGPropertyNode action_group = node.getSubNode("actions");
595 int nActions = action_group.size();
596 for (int j = 0; j < nActions; j++) {
597 FGPanelAction * action = readAction(action_group.getChild(j),
603 instrument->addAction(action);
607 // Get the layers for the instrument.
609 SGPropertyNode layer_group = node.getSubNode("layers");
610 int nLayers = layer_group.size();
611 for (int j = 0; j < nLayers; j++) {
612 FGInstrumentLayer * layer = readLayer(layer_group.getChild(j),
618 instrument->addLayer(layer);
621 FG_LOG(FG_INPUT, FG_INFO, "Done reading instrument " << name);
627 * Read a panel from a property list.
629 * Each panel instrument will appear in its own, separate
630 * property list. The top level simply names the panel and
631 * places the instruments in their appropriate locations (and
632 * optionally resizes them if necessary).
634 * Returns 0 if the read fails for any reason.
637 fgReadPanel (istream &input)
639 SGPropertyList props;
643 // Read the property list from disk.
645 if (!readPropertyList(input, &props)) {
646 FG_LOG(FG_INPUT, FG_ALERT, "Malformed property list for panel.");
649 FG_LOG(FG_INPUT, FG_INFO, "Read properties for panel " <<
650 props.getStringValue("/name"));
653 // Construct a new, empty panel.
655 FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
658 // Assign the background texture, if any, or a bogus chequerboard.
660 string bgTexture = props.getStringValue("/background");
663 panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
664 FG_LOG(FG_INPUT, FG_INFO, "Set background texture to " << bgTexture);
668 // Create each instrument.
670 FG_LOG(FG_INPUT, FG_INFO, "Reading panel instruments");
671 SGPropertyNode instrument_group("/instruments", &props);
672 int nInstruments = instrument_group.size();
673 for (int i = 0; i < nInstruments; i++) {
674 SGPropertyList props2;
675 SGPropertyNode node = instrument_group.getChild(i);
677 FGPath path(current_options.get_fg_root());
678 path.append(node.getStringValue("path"));
680 FG_LOG(FG_INPUT, FG_INFO, "Reading instrument "
685 int x = node.getIntValue("x", -1);
686 int y = node.getIntValue("y", -1);
687 int w = node.getIntValue("w", -1);
688 int h = node.getIntValue("h", -1);
690 if (x == -1 || y == -1) {
691 FG_LOG(FG_INPUT, FG_ALERT, "x and y positions must be specified and >0");
696 if (!readPropertyList(path.str(), &props2)) {
701 FGPanelInstrument * instrument =
702 readInstrument(SGPropertyNode("/", &props2), x, y, w, h);
703 if (instrument == 0) {
708 panel->addInstrument(instrument);
710 FG_LOG(FG_INPUT, FG_INFO, "Done reading panel instruments");
714 // Return the new panel.
721 * Read a panel from a property list.
723 * This function opens a stream to a file, then invokes the
724 * main fgReadPanel() function.
727 fgReadPanel (const string &relative_path)
729 FGPath path(current_options.get_fg_root());
730 path.append(relative_path);
731 ifstream input(path.c_str());
733 FG_LOG(FG_INPUT, FG_ALERT,
734 "Cannot read panel configuration from " << path.str());
737 FGPanel * panel = fgReadPanel(input);
744 // end of panel_io.cxx