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