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 typedef std::map<const char*,FGColor*, ltstr> ColourDict;
217 typedef ColourDict::iterator _itt_t;
218 typedef ColourDict::const_iterator _citt_t;
222 // Read all the configuration files in a directory.
223 void readDir (const SGPath& path);
225 FGMenuBar * _menubar;
226 FGDialog * _active_dialog;
227 std::map<std::string,FGDialog *> _active_dialogs;
228 std::map<std::string,SGPropertyNode_ptr> _dialog_props;
235 FGColor() { clear(); }
236 FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
237 FGColor(const SGPropertyNode *prop) { set(prop); }
238 FGColor(FGColor *c) {
239 if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
242 inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
243 // merges in non-negative components from property with children <red> etc.
244 bool merge(const SGPropertyNode *prop);
245 bool merge(const FGColor *color);
247 bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
248 bool set(const FGColor *color) { clear(); return merge(color); }
249 bool set(float r, float g, float b, float a = 1.0f) {
250 _red = r, _green = g, _blue = b, _alpha = a;
253 bool isValid() const {
254 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
258 inline void setRed(float red) { _red = red; }
259 inline void setGreen(float green) { _green = green; }
260 inline void setBlue(float blue) { _blue = blue; }
261 inline void setAlpha(float alpha) { _alpha = alpha; }
263 inline float red() const { return clamp(_red); }
264 inline float green() const { return clamp(_green); }
265 inline float blue() const { return clamp(_blue); }
266 inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
269 float _red, _green, _blue, _alpha;
272 float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
278 * A class to keep all fonts available for future use.
279 * This also assures a font isn't resident more than once.
283 // The parameters of a request to the cache.
286 const std::string name;
289 FntParams() : size(0.0f), slant(0.0f) {}
290 FntParams(const FntParams& rhs)
291 : name(rhs.name), size(rhs.size), slant(rhs.slant)
294 FntParams(const std::string& name_, float size_, float slant_)
295 : name(name_), size(size_), slant(slant_)
300 : public std::binary_function<const FntParams, const FntParams, bool>
302 bool operator() (const FntParams& f1, const FntParams& f2) const;
305 fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
306 ~fnt() { if (texfont) { delete pufont; delete texfont; } }
307 // Font used by plib GUI code
312 // Path to the font directory
315 typedef std::map<const std::string, fntTexFont*> TexFontMap;
316 typedef std::map<const FntParams, fnt*, FntParamsLess> PuFontMap;
317 TexFontMap _texFonts;
321 struct fnt *getfnt(const char *name, float size, float slant);
328 puFont *get(const char *name, float size=15.0, float slant=0.0);
329 puFont *get(SGPropertyNode *node);
331 fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
333 SGPath getfntpath(const char *name);
335 * Preload all the fonts in the FlightGear font directory. It is
336 * important to load the font textures early, with the proper
337 * graphics context current, so that no plib (or our own) code
338 * tries to load a font from disk when there's no current graphics
341 bool initializeFonts();
345 #endif // __NEW_GUI_HXX