]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel_io.cxx
Sep. 22, 2000 panel updates from David Megginson to make the new config file
[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 FG_USING_STD(istream);
45 FG_USING_STD(ifstream);
46 FG_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   FGCroppedTexture 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   FGCroppedTexture &t = getTexture();
107   t.setCrop(xoffset, yoffset, xoffset + 0.5, yoffset + 0.25);
108   FGTexturedLayer::draw();
109 }
110
111
112 \f
113 ////////////////////////////////////////////////////////////////////////
114 // Read and construct a panel.
115 //
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
120 // itself.
121 //
122 // A panel contains zero or more instruments.
123 //
124 // An instrument contains one or more layers and zero or more actions.
125 //
126 // A layer contains zero or more transformations.
127 //
128 // Some special types of layers also contain other objects, such as 
129 // chunks of text or other layers.
130 //
131 // There are currently four types of layers:
132 //
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)
137 //
138 // The only built-in layer so far is the ribbon for the magnetic compass
139 // (class="compass-ribbon").
140 //
141 // There are three types of actions:
142 //
143 // 1. Adjust (type="adjust"), the default
144 // 2. Swap (type="swap")
145 // 3. Toggle (type="toggle")
146 //
147 // There are three types of transformations:
148 //
149 // 1. X shift (type="x-shift"), the default
150 // 2. Y shift (type="y-shift")
151 // 3. Rotation (type="rotation")
152 //
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 ////////////////////////////////////////////////////////////////////////
157
158
159 /**
160  * Read a cropped texture from the instrument's property list.
161  *
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.
166  */
167 static FGCroppedTexture
168 readTexture (SGPropertyNode node)
169 {
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());
176   return texture;
177 }
178
179
180 /**
181  * Read an action from the instrument's property list.
182  *
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).
186  *
187  * The following action types are defined:
188  *
189  * "adjust" - modify the value of a floating-point property by
190  *    the increment specified.  This is the default.
191  *
192  * "swap" - swap the values of two-floating-point properties.
193  *
194  * "toggle" - toggle the value of a boolean property between true and
195  *    false.
196  *
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.
201  *
202  * The action will be scaled automatically if the instrument is not
203  * being drawn at its regular size.
204  */
205 static FGPanelAction *
206 readAction (SGPropertyNode node, float hscale, float vscale)
207 {
208   FGPanelAction * action = 0;
209
210   string name = node.getStringValue("name");
211   string type = node.getStringValue("type");
212
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);
218
219   if (type == "") {
220     FG_LOG(FG_INPUT, FG_ALERT,
221            "No type supplied for action " << name << " assuming \"adjust\"");
222     type = "adjust";
223   }
224
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);
233     if (min == max)
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);
238   } 
239
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);
247   } 
248
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);
254   } 
255
256                                 // Unrecognized type
257   else {
258     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized action type " << type);
259     return 0;
260   }
261
262   return action;
263 }
264
265
266 /**
267  * Read a transformation from the instrument's property list.
268  *
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.
274  *
275  * There are three types of transformations:
276  *
277  * "x-shift" - move the layer horizontally.
278  *
279  * "y-shift" - move the layer vertically.
280  *
281  * "rotation" - rotate the layer.
282  *
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).
287  *
288  * Note that because of the way OpenGL works, transformations will
289  * appear to be applied backwards.
290  */
291 static FGPanelTransformation *
292 readTransformation (SGPropertyNode node, float hscale, float vscale)
293 {
294   FGPanelTransformation * t = new FGPanelTransformation;
295
296   string name = node.getName();
297   string type = node.getStringValue("type");
298   string propName = node.getStringValue("property", "");
299   SGValue * value = 0;
300
301   if (type == "") {
302     FG_LOG(FG_INPUT, FG_ALERT,
303            "No type supplied for transformation " << name
304            << " assuming \"rotation\"");
305     type = "rotation";
306   }
307
308   if (propName != "") {
309     value = current_properties.getValue(propName, true);
310   }
311
312   t->value = value;
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);
317
318                                 // Move the layer horizontally.
319   if (type == "x-shift") {
320     t->type = FGPanelTransformation::XSHIFT;
321     t->min *= hscale;
322     t->max *= hscale;
323     t->offset *= hscale;
324   } 
325
326                                 // Move the layer vertically.
327   else if (type == "y-shift") {
328     t->type = FGPanelTransformation::YSHIFT;
329     t->min *= vscale;
330     t->max *= vscale;
331     t->offset *= vscale;
332   } 
333
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;
339   } 
340
341   else {
342     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized transformation type " << type);
343     delete t;
344     return 0;
345   }
346
347   FG_LOG(FG_INPUT, FG_INFO, "Read transformation " << name);
348   return t;
349 }
350
351
352 /**
353  * Read a chunk of text from the instrument's property list.
354  *
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
358  * text chunks:
359  *
360  * "literal" - a literal text string (the default)
361  *
362  * "text-value" - the current value of a string property
363  *
364  * "number-value" - the current value of a floating-point property.
365  *
366  * All three may also include a printf-style format string.
367  */
368 FGTextLayer::Chunk *
369 readTextChunk (SGPropertyNode node)
370 {
371   FGTextLayer::Chunk * chunk;
372   string name = node.getStringValue("name");
373   string type = node.getStringValue("type");
374   string format = node.getStringValue("format");
375
376                                 // Default to literal text.
377   if (type == "") {
378     FG_LOG(FG_INPUT, FG_INFO, "No type provided for text chunk " << name
379            << " assuming \"literal\"");
380     type = "literal";
381   }
382
383                                 // A literal text string.
384   if (type == "literal") {
385     string text = node.getStringValue("text");
386     chunk = new FGTextLayer::Chunk(text, format);
387   }
388
389                                 // The value of a string property.
390   else if (type == "text-value") {
391     SGValue * value =
392       current_properties.getValue(node.getStringValue("property"), true);
393     chunk = new FGTextLayer::Chunk(FGTextLayer::TEXT_VALUE, value, format);
394   }
395
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,
402                                    format, scale);
403   }
404
405                                 // Unknown type.
406   else {
407     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
408            << " for text chunk " << name);
409     return 0;
410   }
411
412   return chunk;
413 }
414
415
416 /**
417  * Read a single layer from an instrument's property list.
418  *
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.
423  *
424  * This module currently recognizes four kinds of layers:
425  *
426  * "texture" - a layer containing a texture (the default)
427  *
428  * "text" - a layer containing text
429  *
430  * "switch" - a layer that switches between two other layers
431  *   based on the current value of a boolean property.
432  *
433  * "built-in" - a hard-coded layer supported by C++ code in FlightGear.
434  *
435  * Currently, the only built-in layer class is "compass-ribbon".
436  */
437 static FGInstrumentLayer *
438 readLayer (SGPropertyNode node, float hscale, float vscale)
439 {
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);
445   if (w != -1)
446     w = int(w * hscale);
447   if (h != -1)
448     h = int(h * vscale);
449
450
451   if (type == "") {
452     FG_LOG(FG_INPUT, FG_ALERT,
453            "No type supplied for layer " << name
454            << " assuming \"texture\"");
455     type = "texture";
456   }
457
458
459                                 // A textured instrument layer.
460   if (type == "texture") {
461     FGCroppedTexture texture = readTexture(node.getSubNode("texture"));
462     layer = new FGTexturedLayer(texture, w, h);
463   }
464
465
466                                 // A textual instrument layer.
467   else if (type == "text") {
468     FGTextLayer * tlayer = new FGTextLayer(w, h); // FIXME
469
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);
475
476                                 // Set the point size.
477     float pointSize = node.getFloatValue("point-size", 10.0) * hscale;
478     tlayer->setPointSize(pointSize);
479
480                                 // Set the font.
481     // TODO
482
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));
487       if (chunk == 0) {
488         delete layer;
489         return 0;
490       }
491       tlayer->addChunk(chunk);
492     }
493     layer = tlayer;
494   }
495
496                                 // A switch instrument layer.
497   else if (type == "switch") {
498     SGValue * value =
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);
505   }
506
507                                 // A built-in instrument layer.
508   else if (type == "built-in") {
509     string layerclass = node.getStringValue("class");
510
511     if (layerclass == "mag-ribbon") {
512       layer = new FGMagRibbon(w, h);
513     }
514
515     else if (layerclass == "") {
516       FG_LOG(FG_INPUT, FG_ALERT, "No class provided for built-in layer "
517              << name);
518       return 0;
519     }
520
521     else {
522       FG_LOG(FG_INPUT, FG_ALERT, "Unknown built-in layer class "
523              << layerclass);
524       return 0;
525     }
526   }
527
528                                 // An unknown type.
529   else {
530     FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized layer type " << type);
531     delete layer;
532     return 0;
533   }
534   
535   //
536   // Get the transformations for each layer.
537   //
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),
542                                                    hscale, vscale);
543     if (t == 0) {
544       delete layer;
545       return 0;
546     }
547     layer->addTransformation(t);
548   }
549   
550   FG_LOG(FG_INPUT, FG_INFO, "Read layer " << name);
551   return layer;
552 }
553
554
555 /**
556  * Read an instrument from a property list.
557  *
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.
565  */
566 static FGPanelInstrument *
567 readInstrument (SGPropertyNode node, int x, int y, int real_w, int real_h)
568 {
569   int w = node.getIntValue("w");
570   int h = node.getIntValue("h");
571   const string &name = node.getStringValue("name");
572
573   float hscale = 1.0;
574   float vscale = 1.0;
575   if (real_w != -1) {
576     hscale = float(real_w) / float(w);
577     w = real_w;
578     cerr << "hscale is " << hscale << endl;
579   }
580   if (real_h != -1) {
581     vscale = float(real_h) / float(h);
582     h = real_h;
583     cerr << "vscale is " << hscale << endl;
584   }
585
586   FG_LOG(FG_INPUT, FG_INFO, "Reading instrument " << name);
587
588   FGLayeredInstrument * instrument =
589     new FGLayeredInstrument(x, y, w, h);
590
591   //
592   // Get the actions for the instrument.
593   //
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),
598                                         hscale, vscale);
599     if (action == 0) {
600       delete instrument;
601       return 0;
602     }
603     instrument->addAction(action);
604   }
605
606   //
607   // Get the layers for the instrument.
608   //
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),
613                                           hscale, vscale);
614     if (layer == 0) {
615       delete instrument;
616       return 0;
617     }
618     instrument->addLayer(layer);
619   }
620
621   FG_LOG(FG_INPUT, FG_INFO, "Done reading instrument " << name);
622   return instrument;
623 }
624
625
626 /**
627  * Read a panel from a property list.
628  *
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).
633  *
634  * Returns 0 if the read fails for any reason.
635  */
636 FGPanel *
637 fgReadPanel (istream &input)
638 {
639   SGPropertyList props;
640
641
642   //
643   // Read the property list from disk.
644   //
645   if (!readPropertyList(input, &props)) {
646     FG_LOG(FG_INPUT, FG_ALERT, "Malformed property list for panel.");
647     return 0;
648   }
649   FG_LOG(FG_INPUT, FG_INFO, "Read properties for panel " <<
650          props.getStringValue("/name"));
651
652   //
653   // Construct a new, empty panel.
654   //
655   FGPanel * panel = new FGPanel(0, 0, 1024, 768);// FIXME: use variable size
656
657   //
658   // Assign the background texture, if any, or a bogus chequerboard.
659   //
660   string bgTexture = props.getStringValue("/background");
661   if (bgTexture == "")
662     bgTexture = "FOO";
663   panel->setBackground(FGTextureManager::createTexture(bgTexture.c_str()));
664   FG_LOG(FG_INPUT, FG_INFO, "Set background texture to " << bgTexture);
665
666
667   //
668   // Create each instrument.
669   //
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);
676
677     FGPath path(current_options.get_fg_root());
678     path.append(node.getStringValue("path"));
679
680     FG_LOG(FG_INPUT, FG_INFO, "Reading instrument "
681            << node.getName()
682            << " from "
683            << path.str());
684
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);
689
690     if (x == -1 || y == -1) {
691       FG_LOG(FG_INPUT, FG_ALERT, "x and y positions must be specified and >0");
692       delete panel;
693       return 0;
694     }
695
696     if (!readPropertyList(path.str(), &props2)) {
697       delete panel;
698       return 0;
699     }
700
701     FGPanelInstrument * instrument =
702       readInstrument(SGPropertyNode("/", &props2), x, y, w, h);
703     if (instrument == 0) {
704       delete instrument;
705       delete panel;
706       return 0;
707     }
708     panel->addInstrument(instrument);
709   }
710   FG_LOG(FG_INPUT, FG_INFO, "Done reading panel instruments");
711
712
713   //
714   // Return the new panel.
715   //
716   return panel;
717 }
718
719
720 /**
721  * Read a panel from a property list.
722  *
723  * This function opens a stream to a file, then invokes the
724  * main fgReadPanel() function.
725  */
726 FGPanel *
727 fgReadPanel (const string &relative_path)
728 {
729   FGPath path(current_options.get_fg_root());
730   path.append(relative_path);
731   ifstream input(path.c_str());
732   if (!input.good()) {
733     FG_LOG(FG_INPUT, FG_ALERT,
734            "Cannot read panel configuration from " << path.str());
735     return 0;
736   }
737   FGPanel * panel = fgReadPanel(input);
738   input.close();
739   return panel;
740 }
741
742
743
744 // end of panel_io.cxx