]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[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 extern puFont FONT_HELVETICA_14;
17 extern puFont FONT_SANS_12B;
18
19
20
21 \f
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of NewGUI.
24 ////////////////////////////////////////////////////////////////////////
25
26
27
28 NewGUI::NewGUI ()
29     : _menubar(new FGMenuBar),
30       _active_dialog(0)
31 {
32 }
33
34 NewGUI::~NewGUI ()
35 {
36     delete _menubar;
37     _dialog_props.clear();
38 }
39
40 void
41 NewGUI::init ()
42 {
43     setStyle();
44     char path1[1024];
45     char path2[1024];
46     ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
47     ulMakePath(path2, path1, "dialogs");
48     readDir(path2);
49     _menubar->init();
50 }
51
52 void
53 NewGUI::reinit ()
54 {
55     reset(true);
56     fgSetBool("/sim/signals/reinit-gui", true);
57 }
58
59 void
60 NewGUI::redraw ()
61 {
62     reset(false);
63 }
64
65 void
66 NewGUI::reset (bool reload)
67 {
68     map<string,FGDialog *>::iterator iter;
69     vector<string> dlg;
70     // close all open dialogs and remember them ...
71     for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
72         dlg.push_back(iter->first);
73
74     unsigned int i;
75     for (i = 0; i < dlg.size(); i++)
76         closeDialog(dlg[i]);
77
78     setStyle();
79
80     unbind();
81     delete _menubar;
82     _menubar = new FGMenuBar;
83
84     if (reload) {
85         _dialog_props.clear();
86         init();
87     } else {
88         _menubar->init();
89     }
90
91     bind();
92
93     // open dialogs again
94     for (i = 0; i < dlg.size(); i++)
95         showDialog(dlg[i]);
96 }
97
98 void
99 NewGUI::bind ()
100 {
101     fgTie("/sim/menubar/visibility", this,
102           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
103 }
104
105 void
106 NewGUI::unbind ()
107 {
108     fgUntie("/sim/menubar/visibility");
109 }
110
111 void
112 NewGUI::update (double delta_time_sec)
113 {
114     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
115     for(/**/; iter != _active_dialogs.end(); iter++)
116         iter->second->update();
117 }
118
119 bool
120 NewGUI::showDialog (const string &name)
121 {
122     if (_dialog_props.find(name) == _dialog_props.end()) {
123         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
124         return false;
125     } else {
126         if(!_active_dialogs[name])
127             _active_dialogs[name] = new FGDialog(_dialog_props[name]);
128         return true;
129     }
130 }
131
132 bool
133 NewGUI::closeActiveDialog ()
134 {
135     if (_active_dialog == 0)
136         return false;
137
138     // Kill any entries in _active_dialogs...  Is there an STL
139     // algorithm to do (delete map entries by value, not key)?  I hate
140     // the STL :) -Andy
141     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
142     for(/**/; iter != _active_dialogs.end(); iter++) {
143         if(iter->second == _active_dialog) {
144             _active_dialogs.erase(iter);
145             // iter is no longer valid
146             break;
147         }
148     }
149
150     delete _active_dialog;
151     _active_dialog = 0;
152     return true;
153 }
154
155 bool
156 NewGUI::closeDialog (const string& name)
157 {
158     if(_active_dialogs.find(name) != _active_dialogs.end()) {
159         if(_active_dialog == _active_dialogs[name])
160             _active_dialog = 0;
161         delete _active_dialogs[name];
162         _active_dialogs.erase(name);
163         return true;
164     }
165     return false; // dialog wasn't open...
166 }
167
168 SGPropertyNode_ptr
169 NewGUI::getDialogProperties (const string &name)
170 {
171     if(_dialog_props.find(name) != _dialog_props.end())
172         return _dialog_props[name];
173
174     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
175     return 0;
176 }
177
178 FGDialog *
179 NewGUI::getDialog (const string &name)
180 {
181     if(_active_dialogs.find(name) != _active_dialogs.end())
182         return _active_dialogs[name];
183
184     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
185     return 0;
186 }
187
188 void
189 NewGUI::setActiveDialog (FGDialog * dialog)
190 {
191     _active_dialog = dialog;
192 }
193
194 FGDialog *
195 NewGUI::getActiveDialog ()
196 {
197     return _active_dialog;
198 }
199
200 FGMenuBar *
201 NewGUI::getMenuBar ()
202 {
203     return _menubar;
204 }
205
206 bool
207 NewGUI::getMenuBarVisible () const
208 {
209     return _menubar->isVisible();
210 }
211
212 void
213 NewGUI::setMenuBarVisible (bool visible)
214 {
215     if (visible)
216         _menubar->show();
217     else
218         _menubar->hide();
219 }
220
221 static bool
222 test_extension (const char * path, const char * ext)
223 {
224     int pathlen = strlen(path);
225     int extlen = strlen(ext);
226
227     for (int i = 1; i <= pathlen && i <= extlen; i++) {
228         if (path[pathlen-i] != ext[extlen-i])
229             return false;
230     }
231     return true;
232 }
233
234 void
235 NewGUI::newDialog (SGPropertyNode* props)
236 {
237     const char* cname = props->getStringValue("name");
238     if(!cname) {
239         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
240         return;
241     }
242     string name = cname;
243     if(_active_dialogs.find(name) == _active_dialogs.end())
244         _dialog_props[name] = props;
245 }
246
247 void
248 NewGUI::readDir (const char * path)
249 {
250     ulDir * dir = ulOpenDir(path);
251
252     if (dir == 0) {
253         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
254                << path);
255         return;
256     }
257
258     for (ulDirEnt * dirEnt = ulReadDir(dir);
259          dirEnt != 0;
260          dirEnt = ulReadDir(dir)) {
261
262         char subpath[1024];
263
264         ulMakePath(subpath, path, dirEnt->d_name);
265
266         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
267             SGPropertyNode * props = new SGPropertyNode;
268             try {
269                 readProperties(subpath, props);
270             } catch (const sg_exception &) {
271                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
272                        << subpath);
273                 delete props;
274                 continue;
275             }
276             SGPropertyNode *nameprop = props->getNode("name");
277             if (!nameprop) {
278                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
279                    << " has no name; skipping.");
280                 delete props;
281                 continue;
282             }
283             string name = nameprop->getStringValue();
284             if (_dialog_props[name])
285                 delete (SGPropertyNode *)_dialog_props[name];
286
287             _dialog_props[name] = props;
288         }
289     }
290     ulCloseDir(dir);
291 }
292
293
294 \f
295 ////////////////////////////////////////////////////////////////////////
296 // Style handling.
297 ////////////////////////////////////////////////////////////////////////
298
299 void
300 NewGUI::setStyle (void)
301 {
302     _itt_t it;
303     for (it = _colors.begin(); it != _colors.end(); ++it)
304       delete it->second;
305     _colors.clear();
306
307     // set up the traditional colors as default
308     _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
309     _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
310     _colors["highlight"]  = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
311     _colors["label"]      = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
312     _colors["legend"]     = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
313     _colors["misc"]       = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
314     _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
315
316     //puSetDefaultStyle();
317
318     int which = fgGetInt("/sim/gui/current-style", 0);
319     SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
320     SGPropertyNode *n = sim->getChild("style", which);
321     if (!n)
322         n = sim->getChild("style", 0, true);
323
324     setupFont(n->getNode("fonts/gui", true));
325     n = n->getNode("colors", true);
326
327     for (int i = 0; i < n->nChildren(); i++) {
328         SGPropertyNode *child = n->getChild(i);
329         _colors[child->getName()] = new FGColor(child);
330     }
331
332     FGColor *c = _colors["background"];
333     puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
334 }
335
336
337 void
338 NewGUI::setupFont (SGPropertyNode *node)
339 {
340     _font = globals->get_fontcache()->get(node);
341     puSetDefaultFonts(*_font, *_font);
342     return;
343 }
344
345
346
347 \f
348 ////////////////////////////////////////////////////////////////////////
349 // FGColor class.
350 ////////////////////////////////////////////////////////////////////////
351
352 bool
353 FGColor::merge(const SGPropertyNode *node)
354 {
355     if (!node)
356         return false;
357
358     bool dirty = false;
359     const SGPropertyNode * n;
360     if ((n = node->getNode("red")))
361         _red = n->getFloatValue(), dirty = true;
362     if ((n = node->getNode("green")))
363         _green = n->getFloatValue(), dirty = true;
364     if ((n = node->getNode("blue")))
365         _blue = n->getFloatValue(), dirty = true;
366     if ((n = node->getNode("alpha")))
367         _alpha = n->getFloatValue(), dirty = true;
368     return dirty;
369 }
370
371 bool
372 FGColor::merge(const FGColor *color)
373 {
374     bool dirty = false;
375     if (color && color->_red >= 0.0)
376         _red = color->_red, dirty = true;
377     if (color && color->_green >= 0.0)
378         _green = color->_green, dirty = true;
379     if (color && color->_blue >= 0.0)
380         _blue = color->_blue, dirty = true;
381     if (color && color->_alpha >= 0.0)
382         _alpha = color->_alpha, dirty = true;
383     return dirty;
384 }
385
386
387
388 \f
389 ////////////////////////////////////////////////////////////////////////
390 // FGFontCache class.
391 ////////////////////////////////////////////////////////////////////////
392
393 static const struct {
394     char *name;
395     puFont *font;
396 } guifonts[] = {
397     { "default",      &FONT_HELVETICA_14 },
398     { "FIXED_8x13",   &PUFONT_8_BY_13 },
399     { "FIXED_9x15",   &PUFONT_9_BY_15 },
400     { "TIMES_10",     &PUFONT_TIMES_ROMAN_10 },
401     { "TIMES_24",     &PUFONT_TIMES_ROMAN_24 },
402     { "HELVETICA_10", &PUFONT_HELVETICA_10 },
403     { "HELVETICA_12", &PUFONT_HELVETICA_12 },
404     { "HELVETICA_14", &FONT_HELVETICA_14 },
405     { "HELVETICA_18", &PUFONT_HELVETICA_18 },
406     { "SANS_12B",     &FONT_SANS_12B },
407     { 0, 0 }
408 };
409
410
411 FGFontCache::FGFontCache() :
412     _initialized(false)
413 {
414 }
415
416 FGFontCache::~FGFontCache()
417 {
418    map<const string, fnt *>::iterator it, end = _fonts.end();
419    for (it = _fonts.begin(); it != end; ++it)
420        delete it->second;
421 }
422
423 struct FGFontCache::fnt *
424 FGFontCache::getfnt(const char *name, float size, float slant)
425 {
426     _itt_t it;
427     if ((it = _fonts.find(name)) != _fonts.end())
428         return it->second;
429
430     SGPath path = getfntpath(name);
431
432     fnt *f = new fnt();
433     f->texfont = new fntTexFont;
434
435     if (f->texfont->load((char *)path.c_str())) {
436         f->pufont = new puFont;
437         f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
438         return _fonts[name] = f;
439     }
440
441     delete f;
442     return _fonts["default"];
443 }
444
445 puFont *
446 FGFontCache::get(const char *name, float size, float slant)
447 {
448     return getfnt(name, size, slant)->pufont;
449 }
450
451 fntTexFont *
452 FGFontCache::getTexFont(const char *name, float size, float slant)
453 {
454     return getfnt(name, size, slant)->texfont;
455 }
456
457 puFont *
458 FGFontCache::get(SGPropertyNode *node)
459 {
460     if (!node)
461         return get("Helvetica.txf", 15.0, 0.0);
462
463     const char *name = node->getStringValue("name", "Helvetica.txf");
464     float size = node->getFloatValue("size", 15.0);
465     float slant = node->getFloatValue("slant", 0.0);
466
467     return get(name, size, slant);
468 }
469
470 SGPath
471 FGFontCache::getfntpath(const char *name)
472 {
473     if (!_initialized) {
474         char *envp = ::getenv("FG_FONTS");
475         if (envp != NULL) {
476             _path.set(envp);
477         } else {
478             _path.set(globals->get_fg_root());
479             _path.append("Fonts");
480         }
481
482         for (int i = 0; guifonts[i].name; i++)
483             _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
484
485         _initialized = true;
486     }
487
488     SGPath path(_path);
489     if (name && std::string(name) != "") {
490         path.append(name);
491         if (path.exists())
492             return path;
493     }
494
495     path = SGPath(_path);
496     path.append("Helvetica.txf");
497     
498     return path;
499 }
500
501 // end of new_gui.cxx