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