]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
- make tabula rasa on re-init
[flightgear.git] / src / GUI / new_gui.cxx
1 // new_gui.cxx: implementation of XML-configurable GUI support.
2
3 #include "new_gui.hxx"
4
5 #include <plib/pu.h>
6 #include <plib/ul.h>
7
8 #include <simgear/compiler.h>
9 #include <simgear/structure/exception.hxx>
10
11 #include <Main/fg_props.hxx>
12
13 #include "menubar.hxx"
14 #include "dialog.hxx"
15
16 SG_USING_STD(map);
17
18 extern puFont FONT_HELVETICA_14;
19 extern puFont FONT_SANS_12B;
20
21
22
23 \f
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of NewGUI.
26 ////////////////////////////////////////////////////////////////////////
27
28
29
30 NewGUI::NewGUI ()
31     : _font(FONT_HELVETICA_14),
32       _menubar(new FGMenuBar),
33       _active_dialog(0)
34 {
35 }
36
37 NewGUI::~NewGUI ()
38 {
39     clear();
40 }
41
42 void
43 NewGUI::init ()
44 {
45     setStyle();
46     char path1[1024];
47     char path2[1024];
48     ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
49     ulMakePath(path2, path1, "dialogs");
50     readDir(path2);
51     _menubar->init();
52 }
53
54 void
55 NewGUI::reinit ()
56 {
57     map<string,FGDialog *>::iterator iter;
58     vector<string> dlg;
59     // close all open dialogs and remember them ...
60     for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); iter++) {
61         dlg.push_back(iter->first);
62         closeDialog(iter->first);
63     }
64
65     unbind();
66     clear();
67     setStyle();
68     _menubar = new FGMenuBar;
69     init();
70     bind();
71
72     // open remembered dialogs again (no nasal generated ones, unfortunately)
73 //    for (unsigned int i = 0; i < dlg.size(); i++)
74 //        showDialog(dlg[i]);
75 }
76
77 void
78 NewGUI::bind ()
79 {
80     fgTie("/sim/menubar/visibility", this,
81           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
82 }
83
84 void
85 NewGUI::unbind ()
86 {
87     fgUntie("/sim/menubar/visibility");
88 }
89
90 void
91 NewGUI::update (double delta_time_sec)
92 {
93     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
94     for(/**/; iter != _active_dialogs.end(); iter++)
95         iter->second->update();
96 }
97
98 bool
99 NewGUI::showDialog (const string &name)
100 {
101     if (_dialog_props.find(name) == _dialog_props.end()) {
102         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
103         return false;
104     } else {
105         if(!_active_dialogs[name])
106             _active_dialogs[name] = new FGDialog(_dialog_props[name]);
107         return true;
108     }
109 }
110
111 bool
112 NewGUI::closeActiveDialog ()
113 {
114     if (_active_dialog == 0)
115         return false;
116
117     // Kill any entries in _active_dialogs...  Is there an STL
118     // algorithm to do (delete map entries by value, not key)?  I hate
119     // the STL :) -Andy
120     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
121     for(/**/; iter != _active_dialogs.end(); iter++) {
122         if(iter->second == _active_dialog) {
123             _active_dialogs.erase(iter);
124             // iter is no longer valid
125             break;
126         }
127     }
128
129     delete _active_dialog;
130     _active_dialog = 0;
131     return true;
132 }
133
134 bool
135 NewGUI::closeDialog (const string& name)
136 {
137     if(_active_dialogs.find(name) != _active_dialogs.end()) {
138         if(_active_dialog == _active_dialogs[name])
139             _active_dialog = 0;
140         delete _active_dialogs[name];
141         _active_dialogs.erase(name);
142         return true;
143     }
144     return false; // dialog wasn't open...
145 }
146
147 void
148 NewGUI::setActiveDialog (FGDialog * dialog)
149 {
150     _active_dialog = dialog;
151 }
152
153 FGDialog *
154 NewGUI::getActiveDialog ()
155 {
156     return _active_dialog;
157 }
158
159 FGMenuBar *
160 NewGUI::getMenuBar ()
161 {
162     return _menubar;
163 }
164
165 bool
166 NewGUI::getMenuBarVisible () const
167 {
168     return _menubar->isVisible();
169 }
170
171 void
172 NewGUI::setMenuBarVisible (bool visible)
173 {
174     if (visible)
175         _menubar->show();
176     else
177         _menubar->hide();
178 }
179
180 void
181 NewGUI::clear ()
182 {
183     delete _menubar;
184     _menubar = 0;
185     _dialog_props.clear();
186 }
187
188 static bool
189 test_extension (const char * path, const char * ext)
190 {
191     int pathlen = strlen(path);
192     int extlen = strlen(ext);
193
194     for (int i = 1; i <= pathlen && i <= extlen; i++) {
195         if (path[pathlen-i] != ext[extlen-i])
196             return false;
197     }
198     return true;
199 }
200
201 void
202 NewGUI::newDialog (SGPropertyNode* props)
203 {
204     const char* cname = props->getStringValue("name");
205     if(!cname) {
206         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
207         return;
208     }
209     string name = cname;
210     if(!_active_dialogs[name])
211         _dialog_props[name] = props;
212 }
213
214 void
215 NewGUI::readDir (const char * path)
216 {
217     ulDir * dir = ulOpenDir(path);
218
219     if (dir == 0) {
220         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
221                << path);
222         return;
223     }
224
225     for (ulDirEnt * dirEnt = ulReadDir(dir);
226          dirEnt != 0;
227          dirEnt = ulReadDir(dir)) {
228
229         char subpath[1024];
230
231         ulMakePath(subpath, path, dirEnt->d_name);
232
233         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
234             SGPropertyNode * props = new SGPropertyNode;
235             try {
236                 readProperties(subpath, props);
237             } catch (const sg_exception &) {
238                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
239                        << subpath);
240                 delete props;
241                 continue;
242             }
243             SGPropertyNode *nameprop = props->getNode("name");
244             if (!nameprop) {
245                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
246                    << " has no name; skipping.");
247                 delete props;
248                 continue;
249             }
250             string name = nameprop->getStringValue();
251             if (_dialog_props[name])
252                 delete (SGPropertyNode *)_dialog_props[name];
253
254             _dialog_props[name] = props;
255         }
256     }
257     ulCloseDir(dir);
258 }
259
260
261 \f
262 ////////////////////////////////////////////////////////////////////////
263 // Style handling.
264 ////////////////////////////////////////////////////////////////////////
265
266 void
267 NewGUI::setStyle (void)
268 {
269     _colors.clear();
270
271     // set up the traditional colors as default
272     _colors["background"] = FGColor(0.8f, 0.8f, 0.9f, 0.85f);
273     _colors["foreground"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
274     _colors["highlight"]  = FGColor(0.7f, 0.7f, 0.7f, 1.0f);
275     _colors["label"]      = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
276     _colors["legend"]     = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
277     _colors["misc"]       = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
278
279     //puSetDefaultStyle();
280
281     string path = fgGetString("/sim/current-gui", "/sim/gui");
282     string p;
283
284     p = path + "/font";
285     setupFont(fgGetNode(p.c_str(), true));
286
287     p = path + "/colors";
288     SGPropertyNode *n = fgGetNode(p.c_str(), true);
289
290     for (int i = 0; i < n->nChildren(); i++) {
291         SGPropertyNode *child = n->getChild(i);
292         _colors[child->getName()] = FGColor(child);
293     }
294
295     FGColor c = _colors["background"];
296     puSetDefaultColourScheme(c.red(), c.green(), c.blue(), c.alpha());
297 }
298
299
300
301
302 static const struct {
303     char *name;
304     puFont *font;
305 } guifonts[] = {
306     "default",      &FONT_HELVETICA_14,
307     "FIXED_8x13",   &PUFONT_8_BY_13,
308     "FIXED_9x15",   &PUFONT_9_BY_15,
309     "TIMES_10",     &PUFONT_TIMES_ROMAN_10,
310     "TIMES_24",     &PUFONT_TIMES_ROMAN_24,
311     "HELVETICA_10", &PUFONT_HELVETICA_10,
312     "HELVETICA_12", &PUFONT_HELVETICA_12,
313     "HELVETICA_14", &FONT_HELVETICA_14,
314     "HELVETICA_18", &PUFONT_HELVETICA_18,
315     "SANS_12B",     &FONT_SANS_12B,
316     0, 0,
317 };
318
319 void
320 NewGUI::setupFont (SGPropertyNode *node)
321 {
322     string fontname = node->getStringValue("name", "Helvetica.txf");
323     float size = node->getFloatValue("size", 15.0);
324     float slant = node->getFloatValue("slant", 0.0);
325
326     int i;
327     for (i = 0; guifonts[i].name; i++)
328         if (fontname == guifonts[i].name)
329             break;
330     if (guifonts[i].name)
331         _font = *guifonts[i].font;
332     else {
333         SGPath fontpath;
334         char* envp = ::getenv("FG_FONTS");
335         if (envp != NULL) {
336             fontpath.set(envp);
337         } else {
338             fontpath.set(globals->get_fg_root());
339             fontpath.append("Fonts");
340         }
341
342         SGPath path(fontpath);
343         path.append(fontname);
344
345         if (_tex_font.load((char *)path.c_str())) {
346             _font.initialize((fntFont *)&_tex_font, size, slant);
347         } else {
348             _font = *guifonts[0].font;
349             fontname = "default";
350         }
351     }
352     puSetDefaultFonts(_font, _font);
353     node->setStringValue("name", fontname.c_str());
354 }
355
356
357
358 \f
359 ////////////////////////////////////////////////////////////////////////
360 // FGColor class.
361 ////////////////////////////////////////////////////////////////////////
362
363 bool
364 FGColor::merge(const SGPropertyNode *node)
365 {
366     if (!node)
367         return false;
368
369     bool dirty = false;
370     const SGPropertyNode * n;
371     if ((n = node->getNode("red")))
372         _red = n->getFloatValue(), dirty = true;
373     if ((n = node->getNode("green")))
374         _green = n->getFloatValue(), dirty = true;
375     if ((n = node->getNode("blue")))
376         _blue = n->getFloatValue(), dirty = true;
377     if ((n = node->getNode("alpha")))
378         _alpha = n->getFloatValue(), dirty = true;
379     return dirty;
380 }
381
382 bool
383 FGColor::merge(const FGColor& color)
384 {
385     bool dirty = false;
386     if (color._red >= 0.0)
387         _red = color._red, dirty = true;
388     if (color._green >= 0.0)
389         _green = color._green, dirty = true;
390     if (color._blue >= 0.0)
391         _blue = color._blue, dirty = true;
392     if (color._alpha >= 0.0)
393         _alpha = color._alpha, dirty = true;
394     return dirty;
395 }
396
397 // end of new_gui.cxx