]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel_io.cxx
Caught an old texture path reference.
[flightgear.git] / src / Cockpit / panel_io.cxx
1 //  panel_io.cxx - I/O for 2D panel.
2 //
3 //  Written by David Megginson, started January 2000.
4 //
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.
9 // 
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.
14 // 
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.
18 //
19 //  $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H          
26 #  include <windows.h>
27 #endif
28
29 #include <simgear/compiler.h>
30 #include <simgear/misc/fgpath.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/props.hxx>
33
34 #include <iostream>
35 #include <fstream>
36 #include <string>
37
38 #include <Main/options.hxx>
39
40 #include "panel.hxx"
41 #include "steam.hxx"
42 #include "panel_io.hxx"
43
44 using std::istream;
45 using std::ifstream;
46 using std::string;
47
48
49 \f
50 ////////////////////////////////////////////////////////////////////////
51 // Built-in layer for the magnetic compass ribbon layer.
52 //
53 // TODO: move this out into a special directory for built-in
54 // layers of various sorts.
55 ////////////////////////////////////////////////////////////////////////
56
57 class FGMagRibbon : public FGTexturedLayer
58 {
59 public:
60   FGMagRibbon (int w, int h);
61   virtual ~FGMagRibbon () {}
62
63   virtual void draw ();
64 };
65
66 FGMagRibbon::FGMagRibbon (int w, int h)
67   : FGTexturedLayer(w, h)
68 {
69   CroppedTexture texture("Instruments/Default/Textures/compass-ribbon.rgb");
70   setTexture(texture);
71 }
72
73 void
74 FGMagRibbon::draw ()
75 {
76   double heading = FGSteam::get_MH_deg();
77   double xoffset, yoffset;
78
79   while (heading >= 360.0) {
80     heading -= 360.0;
81   }
82   while (heading < 0.0) {
83     heading += 360.0;
84   }
85
86   if (heading >= 60.0 && heading <= 180.0) {
87     xoffset = heading / 240.0;
88     yoffset = 0.75;
89   } else if (heading >= 150.0 && heading <= 270.0) {
90     xoffset = (heading - 90.0) / 240.0;
91     yoffset = 0.50;
92   } else if (heading >= 240.0 && heading <= 360.0) {
93     xoffset = (heading - 180.0) / 240.0;
94     yoffset = 0.25;
95   } else {
96     if (heading < 270.0)
97       heading += 360.0;
98     xoffset = (heading - 270.0) / 240.0;
99     yoffset = 0.0;
100   }
101
102   xoffset = 1.0 - xoffset;
103                                 // Adjust to put the number in the centre
104   xoffset -= 0.25;
105
106   CroppedTexture &t = getTexture();
107   t.minX = xoffset;
108   t.minY = yoffset;
109   t.maxX = xoffset + 0.5;
110   t.maxY = yoffset + 0.25;
111   FGTexturedLayer::draw();
112 }
113
114
115 \f
116 ////////////////////////////////////////////////////////////////////////
117 // Read and construct a panel.
118 //
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
123 // itself.
124 //
125 // A panel contains zero or more instruments.
126 //
127 // An instrument contains one or more layers and zero or more actions.
128 //
129 // A layer contains zero or more transformations.
130 //
131 // Some special types of layers also contain other objects, such as 
132 // chunks of text or other layers.
133 //
134 // There are currently four types of layers:
135 //
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)
140 //
141 // The only built-in layer so far is the ribbon for the magnetic compass
142 // (class="compass-ribbon").
143 //
144 // There are three types of actions:
145 //
146 // 1. Adjust (type="adjust"), the default
147 // 2. Swap (type="swap")
148 // 3. Toggle (type="toggle")
149 //
150 // There are three types of transformations:
151 //
152 // 1. X shift (type="x-shift"), the default
153 // 2. Y shift (type="y-shift")
154 // 3. Rotation (type="rotation")
155 //
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 ////////////////////////////////////////////////////////////////////////
160
161
162 /**
163  * Read a cropped texture from the instrument's property list.
164  *
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.
169  */
170 static CroppedTexture
171 readTexture (SGPropertyNode node)
172 {
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());
179   return texture;
180 }
181
182
183 /**
184  * Read an action from the instrument's property list.
185  *
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).
189  *
190  * The following action types are defined:
191  *
192  * "adjust" - modify the value of a floating-point property by
193  *    the increment specified.  This is the default.
194  *
195  * "swap" - swap the values of two-floating-point properties.
196  *
197  * "toggle" - toggle the value of a boolean property between true and
198  *    false.
199  *
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.
204  *
205  * The action will be scaled automatically if the instrument is not
206  * being drawn at its regular size.
207  */
208 static FGPanelAction *
209 readAction (SGPropertyNode node, float hscale, float vscale)
210 {
211   FGPanelAction * action = 0;
212
213   string name = node.getStringValue("name");
214   string type = node.getStringValue("type");
215
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);
221
222   if (type == "") {
223     FG_LOG(FG_INPUT, FG_ALERT,
224            "No type supplied for action " << name << " assuming \"adjust\"");
225     type = "adjust";
226   }
227
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);
236     if (min == max)
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);
241   } 
242
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);
250   } 
251
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);
257   } 
258
259                                 // Unrecognized type
260   else {
261     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized action type " << type);
262     return 0;
263   }
264
265   return action;
266 }
267
268
269 /**
270  * Read a transformation from the instrument's property list.
271  *
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.
277  *
278  * There are three types of transformations:
279  *
280  * "x-shift" - move the layer horizontally.
281  *
282  * "y-shift" - move the layer vertically.
283  *
284  * "rotation" - rotate the layer.
285  *
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).
290  *
291  * Note that because of the way OpenGL works, transformations will
292  * appear to be applied backwards.
293  */
294 static FGPanelTransformation *
295 readTransformation (SGPropertyNode node, float hscale, float vscale)
296 {
297   FGPanelTransformation * t = new FGPanelTransformation;
298
299   string name = node.getName();
300   string type = node.getStringValue("type");
301   string propName = node.getStringValue("property", "");
302   SGValue * value = 0;
303
304   if (type == "") {
305     FG_LOG(FG_INPUT, FG_ALERT,
306            "No type supplied for transformation " << name
307            << " assuming \"rotation\"");
308     type = "rotation";
309   }
310
311   if (propName != "") {
312     value = current_properties.getValue(propName, true);
313   }
314
315   t->value = value;
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);
320
321                                 // Move the layer horizontally.
322   if (type == "x-shift") {
323     t->type = FGPanelTransformation::XSHIFT;
324     t->min *= hscale;
325     t->max *= hscale;
326     t->offset *= hscale;
327   } 
328
329                                 // Move the layer vertically.
330   else if (type == "y-shift") {
331     t->type = FGPanelTransformation::YSHIFT;
332     t->min *= vscale;
333     t->max *= vscale;
334     t->offset *= vscale;
335   } 
336
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;
342   } 
343
344   else {
345     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized transformation type " << type);
346     delete t;
347     return 0;
348   }
349
350   FG_LOG(FG_INPUT, FG_INFO, "Read transformation " << name);
351   return t;
352 }
353
354
355 /**
356  * Read a chunk of text from the instrument's property list.
357  *
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
361  * text chunks:
362  *
363  * "literal" - a literal text string (the default)
364  *
365  * "text-value" - the current value of a string property
366  *
367  * "number-value" - the current value of a floating-point property.
368  *
369  * All three may also include a printf-style format string.
370  */
371 FGTextLayer::Chunk *
372 readTextChunk (SGPropertyNode node)
373 {
374   FGTextLayer::Chunk * chunk;
375   string name = node.getStringValue("name");
376   string type = node.getStringValue("type");
377   string format = node.getStringValue("format");
378
379                                 // Default to literal text.
380   if (type == "") {
381     FG_LOG(FG_INPUT, FG_INFO, "No type provided for text chunk " << name
382            << " assuming \"literal\"");
383     type = "literal";
384   }
385
386                                 // A literal text string.
387   if (type == "literal") {
388     string text = node.getStringValue("text");
389     chunk = new FGTextLayer::Chunk(text, format);
390   }
391
392                                 // The value of a string property.
393   else if (type == "text-value") {
394     SGValue * value =
395       current_properties.getValue(node.getStringValue("property"), true);
396     chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
397   }
398
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,
405                                    format, scale);
406   }
407
408                                 // Unknown type.
409   else {
410     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
411            << " for text chunk " << name);
412     return 0;
413   }
414
415   return chunk;
416 }
417
418
419 /**
420  * Read a single layer from an instrument's property list.
421  *
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.
426  *
427  * This module currently recognizes four kinds of layers:
428  *
429  * "texture" - a layer containing a texture (the default)
430  *
431  * "text" - a layer containing text
432  *
433  * "switch" - a layer that switches between two other layers
434  *   based on the current value of a boolean property.
435  *
436  * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
437  *
438  * Currently, the only built-in layer class is "compass-ribbon".
439  */
440 static FGInstrumentLayer *
441 readLayer (SGPropertyNode node, float hscale, float vscale)
442 {
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);
448   if (w != -1)
449     w = int(w * hscale);
450   if (h != -1)
451     h = int(h * vscale);
452
453
454   if (type == "") {
455     FG_LOG(FG_INPUT, FG_ALERT,
456            "No type supplied for layer " << name
457            << " assuming \"texture\"");
458     type = "texture";
459   }
460
461
462                                 // A textured instrument layer.
463   if (type == "texture") {
464     CroppedTexture texture = readTexture(node.getSubNode("texture"));
465     layer = new FGTexturedLayer(texture, w, h);
466   }
467
468
469                                 // A textual instrument layer.
470   else if (type == "text") {
471     FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
472
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);
478
479                                 // Set the point size.
480     float pointSize = node.getFloatValue("point-size", 10.0) * hscale;
481     tlayer->setPointSize(pointSize);
482
483                                 // Set the font.
484     // TODO
485
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));
490       if (chunk == 0) {
491         delete layer;
492         return 0;
493       }
494       tlayer->addChunk(chunk);
495     }
496     layer = tlayer;
497   }
498
499                                 // A switch instrument layer.
500   else if (type == "switch") {
501     SGValue * value =
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);
508   }
509
510                                 // A built-in instrument layer.
511   else if (type == "built-in") {
512     string layerclass = node.getStringValue("class");
513
514     if (layerclass == "mag-ribbon") {
515       layer = new FGMagRibbon(w, h);
516     }
517
518     else if (layerclass == "") {
519       FG_LOG(FG_INPUT, FG_ALERT, "No class provided for built-in layer "
520              << name);
521       return 0;
522     }
523
524     else {
525       FG_LOG(FG_INPUT, FG_ALERT, "Unknown built-in layer class "
526              << layerclass);
527       return 0;
528     }
529   }
530
531                                 // An unknown type.
532   else {
533     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized layer type " << type);
534     delete layer;
535     return 0;
536   }
537   
538   //
539   // Get the transformations for each layer.
540   //
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),
545                                                    hscale, vscale);
546     if (t == 0) {
547       delete layer;
548       return 0;
549     }
550     layer->addTransformation(t);
551   }
552   
553   FG_LOG(FG_INPUT, FG_INFO, "Read layer " << name);
554   return layer;
555 }
556
557
558 /**
559  * Read an instrument from a property list.
560  *
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.
568  */
569 static FGPanelInstrument *
570 readInstrument (SGPropertyNode node, int x, int y, int real_w, int real_h)
571 {
572   int w = node.getIntValue("w");
573   int h = node.getIntValue("h");
574   const string &name = node.getStringValue("name");
575
576   float hscale = 1.0;
577   float vscale = 1.0;
578   if (real_w != -1) {
579     hscale = float(real_w) / float(w);
580     w = real_w;
581     cerr << "hscale is " << hscale << endl;
582   }
583   if (real_h != -1) {
584     vscale = float(real_h) / float(h);
585     h = real_h;
586     cerr << "vscale is " << hscale << endl;
587   }
588
589   FG_LOG(FG_INPUT, FG_INFO, "Reading instrument " << name);
590
591   FGLayeredInstrument * instrument =
592     new FGLayeredInstrument(x, y, w, h);
593
594   //
595   // Get the actions for the instrument.
596   //
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),
601                                         hscale, vscale);
602     if (action == 0) {
603       delete instrument;
604       return 0;
605     }
606     instrument->addAction(action);
607   }
608
609   //
610   // Get the layers for the instrument.
611   //
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),
616                                           hscale, vscale);
617     if (layer == 0) {
618       delete instrument;
619       return 0;
620     }
621     instrument->addLayer(layer);
622   }
623
624   FG_LOG(FG_INPUT, FG_INFO, "Done reading instrument " << name);
625   return instrument;
626 }
627
628
629 /**
630  * Read a panel from a property list.
631  *
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).
636  *
637  * Returns 0 if the read fails for any reason.
638  */
639 FGPanel *
640 fgReadPanel (istream &input)
641 {
642   SGPropertyList props;
643
644
645   //
646   // Read the property list from disk.
647   //
648   if (!readPropertyList(input, &props)) {
649     FG_LOG(FG_INPUT, FG_ALERT, "Malformed property list for panel.");
650     return 0;
651   }
652   FG_LOG(FG_INPUT, FG_INFO, "Read properties for panel " <<
653          props.getStringValue("/name"));
654
655   //
656   // Construct a new, empty panel.
657   //
658   FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
659
660   //
661   // Assign the background texture, if any, or a bogus chequerboard.
662   //
663   string bgTexture = props.getStringValue("/background");
664   if (bgTexture == "")
665     bgTexture = "FOO";
666   panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
667   FG_LOG(FG_INPUT, FG_INFO, "Set background texture to " << bgTexture);
668
669
670   //
671   // Create each instrument.
672   //
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);
679
680     FGPath path(current_options.get_fg_root());
681     path.append(node.getStringValue("path"));
682
683     FG_LOG(FG_INPUT, FG_INFO, "Reading instrument "
684            << node.getName()
685            << " from "
686            << path.str());
687
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);
692
693     if (x == -1 || y == -1) {
694       FG_LOG(FG_INPUT, FG_ALERT, "x and y positions must be specified and >0");
695       delete panel;
696       return 0;
697     }
698
699     if (!readPropertyList(path.str(), &props2)) {
700       delete panel;
701       return 0;
702     }
703
704     FGPanelInstrument * instrument =
705       readInstrument(SGPropertyNode("/", &props2), x, y, w, h);
706     if (instrument == 0) {
707       delete instrument;
708       delete panel;
709       return 0;
710     }
711     panel->addInstrument(instrument);
712   }
713   FG_LOG(FG_INPUT, FG_INFO, "Done reading panel instruments");
714
715
716   //
717   // Return the new panel.
718   //
719   return panel;
720 }
721
722
723 /**
724  * Read a panel from a property list.
725  *
726  * This function opens a stream to a file, then invokes the
727  * main fgReadPanel() function.
728  */
729 FGPanel *
730 fgReadPanel (const string &relative_path)
731 {
732   FGPath path(current_options.get_fg_root());
733   path.append(relative_path);
734   ifstream input(path.c_str());
735   if (!input.good()) {
736     FG_LOG(FG_INPUT, FG_ALERT,
737            "Cannot read panel configuration from " << path.str());
738     return 0;
739   }
740   FGPanel * panel = fgReadPanel(input);
741   input.close();
742   return panel;
743 }
744
745
746
747 // end of panel_io.cxx