]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.hxx
Expose vertical speed for MP planes
[flightgear.git] / src / GUI / new_gui.hxx
1 // new_gui.hxx - XML-configured GUI subsystem.
2
3 #ifndef __NEW_GUI_HXX
4 #define __NEW_GUI_HXX 1
5
6 #include <plib/pu.h>
7
8 #include <simgear/props/props.hxx>
9 #include <simgear/structure/subsystem_mgr.hxx>
10 #include <simgear/misc/sg_path.hxx>
11
12 #include <functional>
13 #include <vector>
14 #include <map>
15
16 #include <Main/fg_props.hxx>
17
18 class SGBinding;
19
20 class FGMenuBar;
21 class FGDialog;
22 class FGColor;
23 class FGFontCache;
24
25
26 /**
27  * XML-configured GUI subsystem.
28  *
29  * This subsystem manages the graphical user interface for FlightGear.
30  * It creates a menubar from the XML configuration file in
31  * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
32  * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/.  It
33  * can show or hide the menubar, and can display any dialog by name.
34  */
35 class NewGUI : public SGSubsystem
36 {
37 public:
38
39     /**
40      * Constructor.
41      */
42     NewGUI ();
43
44     /**
45      * Destructor.
46      */
47     virtual ~NewGUI ();
48
49     /**
50      * Initialize the GUI subsystem.
51      */
52     virtual void init ();
53
54     /**
55      * Reinitialize the GUI subsystem. Reloads all XML dialogs.
56      */
57     virtual void reinit ();
58
59     /**
60      * Bind properties for the GUI subsystem.
61      *
62      * Currently, this method binds the properties for showing and
63      * hiding the menu.
64      */
65     virtual void bind ();
66
67     /**
68      * Unbind properties for the GUI subsystem.
69      */
70     virtual void unbind ();
71
72     /**
73      * Update the GUI subsystem.
74      *
75      * Currently, this method is a no-op, because nothing the GUI
76      * subsystem does is time-dependent.
77      */
78     virtual void update (double delta_time_sec);
79
80     /**
81      * Redraw the GUI picking up new GUI colors.
82      */
83     virtual void redraw ();
84
85     /**
86      * Creates a new dialog box, using the same property format as the
87      * gui/dialogs configuration files.  Does not display the
88      * resulting dialog.  If a pre-existing dialog of the same name
89      * exists, it will be deleted.  The node argument will be stored
90      * in the GUI subsystem using SGPropertNode_ptr reference counting.
91      * It should not be deleted by user code.
92      *
93      * @param node A property node containing the dialog definition
94      */
95     virtual void newDialog (SGPropertyNode* node);
96
97     /**
98      * Display a dialog box.
99      *
100      * At initialization time, the subsystem reads all of the XML
101      * configuration files from the directory $FG_ROOT/gui/dialogs/.
102      * The configuration for each dialog specifies a name, and this
103      * method invokes the dialog with the name specified (if it
104      * exists).
105      *
106      * @param name The name of the dialog box.
107      * @return true if the dialog exists, false otherwise.
108      */
109     virtual bool showDialog (const std::string &name);
110
111
112     /**
113      * Close the currenty active dialog.  This function is intended to
114      * be called from code (pui callbacks, for instance) that registers
115      * its dialog object as active via setActiveDialog().  Other
116      * user-level code should use the closeDialog(name) API.
117      *
118      * @return true if a dialog was active, false otherwise
119      */
120     virtual bool closeActiveDialog ();
121
122     /**
123      * Close a named dialog, if it is open.
124      *
125      * @param name The name of the dialog box.
126      * @return true if the dialog was active, false otherwise.
127      */
128     virtual bool closeDialog (const std::string &name);
129
130     /**
131      * Get dialog property tree's root node.
132      * @param name The name of the dialog box.
133      * @return node pointer if the dialog was found, zero otherwise.
134      */
135     virtual SGPropertyNode_ptr getDialogProperties (const std::string &name);
136
137     /**
138      * Return a pointer to the current menubar.
139      */
140     virtual FGMenuBar * getMenuBar ();
141
142     /**
143      * Ignore this method.
144      *
145      * This method is for internal use only, but it has to be public
146      * so that a non-class callback can see it.
147      */
148     virtual void setActiveDialog (FGDialog * dialog);
149
150     /**
151      * Get the dialog currently active, if any.
152      *
153      * @return The active dialog, or 0 if none is active.
154      */
155     virtual FGDialog * getActiveDialog ();
156
157
158     /**
159      * Get the named dialog if active.
160      *
161      * @return The named dialog, or 0 if it isn't active.
162      */
163     virtual FGDialog * getDialog (const std::string &name);
164
165
166     virtual FGColor *getColor (const char * name) const {
167         _citt_t it = _colors.find(name);
168         return (it != _colors.end()) ? it->second : NULL;
169     }
170     virtual FGColor *getColor (const std::string &name) const {
171         _citt_t it = _colors.find(name.c_str());
172         return (it != _colors.end()) ? it->second : NULL;
173     }
174
175     virtual puFont *getDefaultFont() { return _font; }
176
177
178 protected:
179
180     /**
181      * Test if the menubar is visible.
182      *
183      * This method exists only for binding.
184      */
185     virtual bool getMenuBarVisible () const;
186
187     /**
188      * Show or hide the menubar.
189      *
190      * This method exists only for binding.
191      */
192     virtual void setMenuBarVisible (bool visible);
193
194     virtual void setStyle ();
195     virtual void setupFont (SGPropertyNode *);
196
197     /**
198      * Used by reinit() and redraw() to close all dialogs and to apply
199      * current GUI colors. If "reload" is false, reopens all dialogs.
200      * Otherwise reloads all XML dialog files from disk and reopens all
201      * but Nasal * generated dialogs, omitting dynamic widgets. (This
202      * is only useful for GUI development.)
203      */
204     virtual void reset (bool reload);
205
206 private:
207     struct ltstr
208     {
209         bool operator()(const char* s1, const char* s2) const {
210             return strcmp(s1, s2) < 0;
211         }
212     };
213
214     puFont *_font;
215     map<const char*,FGColor*, ltstr> _colors;
216     typedef map<const char*,FGColor*, ltstr>::iterator _itt_t;
217     typedef map<const char*,FGColor*, ltstr>::const_iterator _citt_t;
218
219     void clear_colors();
220
221     // Read all the configuration files in a directory.
222     void readDir (const SGPath& path);
223
224     FGMenuBar * _menubar;
225     FGDialog * _active_dialog;
226     std::map<std::string,FGDialog *> _active_dialogs;
227     std::map<std::string,SGPropertyNode_ptr> _dialog_props;
228
229 };
230
231
232 class FGColor {
233 public:
234     FGColor() { clear(); }
235     FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
236     FGColor(const SGPropertyNode *prop) { set(prop); }
237     FGColor(FGColor *c) { 
238         if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
239     }
240
241     inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
242     // merges in non-negative components from property with children <red> etc.
243     bool merge(const SGPropertyNode *prop);
244     bool merge(const FGColor *color);
245
246     bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
247     bool set(const FGColor *color) { clear(); return merge(color); }
248     bool set(float r, float g, float b, float a = 1.0f) {
249         _red = r, _green = g, _blue = b, _alpha = a;
250         return true;
251     }
252     bool isValid() const {
253         return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
254     }
255     void print() const;
256
257     inline void setRed(float red) { _red = red; }
258     inline void setGreen(float green) { _green = green; }
259     inline void setBlue(float blue) { _blue = blue; }
260     inline void setAlpha(float alpha) { _alpha = alpha; }
261
262     inline float red() const { return clamp(_red); }
263     inline float green() const { return clamp(_green); }
264     inline float blue() const { return clamp(_blue); }
265     inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
266
267 protected:
268     float _red, _green, _blue, _alpha;
269
270 private:
271     float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
272 };
273
274
275
276 /**
277  * A class to keep all fonts available for future use.
278  * This also assures a font isn't resident more than once.
279  */
280 class FGFontCache {
281 private:
282     // The parameters of a request to the cache.
283     struct FntParams
284     {
285         const std::string name;
286         const float size;
287         const float slant;
288         FntParams() : size(0.0f), slant(0.0f) {}
289         FntParams(const FntParams& rhs)
290             : name(rhs.name), size(rhs.size), slant(rhs.slant)
291         {
292         }
293         FntParams(const std::string& name_, float size_, float slant_)
294             : name(name_), size(size_), slant(slant_)
295         {
296         }
297     };
298     struct FntParamsLess
299         : public std::binary_function<const FntParams, const FntParams, bool>
300     {
301         bool operator() (const FntParams& f1, const FntParams& f2) const;
302     };
303     struct fnt {
304         fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
305         ~fnt() { if (texfont) { delete pufont; delete texfont; } }
306         // Font used by plib GUI code
307         puFont *pufont;
308         // TXF font
309         fntTexFont *texfont;
310     };
311     // Path to the font directory
312     SGPath _path;
313
314     typedef map<const string, fntTexFont*> TexFontMap;
315     typedef map<const FntParams, fnt*, FntParamsLess> PuFontMap;
316     TexFontMap _texFonts;
317     PuFontMap _puFonts;
318
319     bool _initialized;
320     struct fnt *getfnt(const char *name, float size, float slant);
321     void init();
322
323 public:
324     FGFontCache();
325     ~FGFontCache();
326
327     puFont *get(const char *name, float size=15.0, float slant=0.0);
328     puFont *get(SGPropertyNode *node);
329
330     fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
331
332     SGPath getfntpath(const char *name);
333     /**
334      * Preload all the fonts in the FlightGear font directory. It is
335      * important to load the font textures early, with the proper
336      * graphics context current, so that no plib (or our own) code
337      * tries to load a font from disk when there's no current graphics
338      * context.
339      */
340     bool initializeFonts();
341 };
342
343
344 #endif // __NEW_GUI_HXX
345