1 // new_gui.hxx - XML-configured GUI subsystem.
4 #define __NEW_GUI_HXX 1
8 #include <simgear/props/props.hxx>
9 #include <simgear/structure/subsystem_mgr.hxx>
10 #include <simgear/misc/sg_path.hxx>
16 #include <Main/fg_props.hxx>
27 * XML-configured GUI subsystem.
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.
35 class NewGUI : public SGSubsystem
50 * Initialize the GUI subsystem.
55 * Reinitialize the GUI subsystem. Reloads all XML dialogs.
57 virtual void reinit ();
60 * Bind properties for the GUI subsystem.
62 * Currently, this method binds the properties for showing and
68 * Unbind properties for the GUI subsystem.
70 virtual void unbind ();
73 * Update the GUI subsystem.
75 * Currently, this method is a no-op, because nothing the GUI
76 * subsystem does is time-dependent.
78 virtual void update (double delta_time_sec);
81 * Redraw the GUI picking up new GUI colors.
83 virtual void redraw ();
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.
93 * @param node A property node containing the dialog definition
95 virtual void newDialog (SGPropertyNode* node);
98 * Display a dialog box.
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
106 * @param name The name of the dialog box.
107 * @return true if the dialog exists, false otherwise.
109 virtual bool showDialog (const std::string &name);
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.
118 * @return true if a dialog was active, false otherwise
120 virtual bool closeActiveDialog ();
123 * Close a named dialog, if it is open.
125 * @param name The name of the dialog box.
126 * @return true if the dialog was active, false otherwise.
128 virtual bool closeDialog (const std::string &name);
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.
135 virtual SGPropertyNode_ptr getDialogProperties (const std::string &name);
138 * Return a pointer to the current menubar.
140 virtual FGMenuBar * getMenuBar ();
143 * Ignore this method.
145 * This method is for internal use only, but it has to be public
146 * so that a non-class callback can see it.
148 virtual void setActiveDialog (FGDialog * dialog);
151 * Get the dialog currently active, if any.
153 * @return The active dialog, or 0 if none is active.
155 virtual FGDialog * getActiveDialog ();
159 * Get the named dialog if active.
161 * @return The named dialog, or 0 if it isn't active.
163 virtual FGDialog * getDialog (const std::string &name);
166 virtual FGColor *getColor (const char * name) const {
167 _citt_t it = _colors.find(name);
168 return (it != _colors.end()) ? it->second : NULL;
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;
175 virtual puFont *getDefaultFont() { return _font; }
181 * Test if the menubar is visible.
183 * This method exists only for binding.
185 virtual bool getMenuBarVisible () const;
188 * Show or hide the menubar.
190 * This method exists only for binding.
192 virtual void setMenuBarVisible (bool visible);
194 virtual void setStyle ();
195 virtual void setupFont (SGPropertyNode *);
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.)
204 virtual void reset (bool reload);
209 bool operator()(const char* s1, const char* s2) const {
210 return strcmp(s1, s2) < 0;
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;
221 // Read all the configuration files in a directory.
222 void readDir (const char * path);
224 FGMenuBar * _menubar;
225 FGDialog * _active_dialog;
226 std::map<std::string,FGDialog *> _active_dialogs;
227 std::map<std::string,SGPropertyNode_ptr> _dialog_props;
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);
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);
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;
252 bool isValid() const {
253 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
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; }
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); }
268 float _red, _green, _blue, _alpha;
271 float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
277 * A class to keep all fonts available for future use.
278 * This also assures a font isn't resident more than once.
282 // The parameters of a request to the cache.
285 const std::string name;
288 FntParams() : size(0.0f), slant(0.0f) {}
289 FntParams(const FntParams& rhs)
290 : name(rhs.name), size(rhs.size), slant(rhs.slant)
293 FntParams(const std::string& name_, float size_, float slant_)
294 : name(name_), size(size_), slant(slant_)
299 : public std::binary_function<const FntParams, const FntParams, bool>
301 bool operator() (const FntParams& f1, const FntParams& f2) const;
304 fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
305 ~fnt() { if (texfont) { delete pufont; delete texfont; } }
306 // Font used by plib GUI code
311 // Path to the font directory
314 typedef map<const string, fntTexFont*> TexFontMap;
315 typedef map<const FntParams, fnt*, FntParamsLess> PuFontMap;
316 TexFontMap _texFonts;
320 struct fnt *getfnt(const char *name, float size, float slant);
327 puFont *get(const char *name, float size=15.0, float slant=0.0);
328 puFont *get(SGPropertyNode *node);
330 fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
332 SGPath getfntpath(const char *name);
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
340 bool initializeFonts();
344 #endif // __NEW_GUI_HXX