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