1 // new_gui.hxx - XML-configured GUI subsystem.
4 #define __NEW_GUI_HXX 1
7 # error This library requires C++
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>
28 #include <Main/fg_props.hxx>
38 * XML-configured GUI subsystem.
40 * This subsystem manages the graphical user interface for FlightGear.
41 * It creates a menubar from the XML configuration file in
42 * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
43 * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/. It
44 * can show or hide the menubar, and can display any dialog by name.
46 class NewGUI : public SGSubsystem
61 * Initialize the GUI subsystem.
66 * Reinitialize the GUI subsystem.
68 virtual void reinit ();
71 * Bind properties for the GUI subsystem.
73 * Currently, this method binds the properties for showing and
79 * Unbind properties for the GUI subsystem.
81 virtual void unbind ();
84 * Update the GUI subsystem.
86 * Currently, this method is a no-op, because nothing the GUI
87 * subsystem does is time-dependent.
89 virtual void update (double delta_time_sec);
92 * Creates a new dialog box, using the same property format as the
93 * gui/dialogs configuration files. Does not display the
94 * resulting dialog. If a pre-existing dialog of the same name
95 * exists, it will be deleted. The node argument will be stored
96 * in the GUI subsystem using SGPropertNode_ptr reference counting.
97 * It should not be deleted by user code.
99 * @param node A property node containing the dialog definition
101 virtual void newDialog (SGPropertyNode* node);
104 * Display a dialog box.
106 * At initialization time, the subsystem reads all of the XML
107 * configuration files from the directory $FG_ROOT/gui/dialogs/.
108 * The configuration for each dialog specifies a name, and this
109 * method invokes the dialog with the name specified (if it
112 * @param name The name of the dialog box.
113 * @return true if the dialog exists, false otherwise.
115 virtual bool showDialog (const string &name);
119 * Close the currenty active dialog. This function is intended to
120 * be called from code (pui callbacks, for instance) that registers
121 * its dialog object as active via setActiveDialog(). Other
122 * user-level code should use the closeDialog(name) API.
124 * @return true if a dialog was active, false otherwise
126 virtual bool closeActiveDialog ();
129 * Close a named dialog, if it is open.
131 * @param name The name of the dialog box.
132 * @return true if the dialog was active, false otherwise.
134 virtual bool closeDialog (const string &name);
137 * Get dialog property tree's root node.
138 * @param name The name of the dialog box.
139 * @return node pointer if the dialog was found, zero otherwise.
141 virtual SGPropertyNode_ptr getDialog (const string &name);
144 * Return a pointer to the current menubar.
146 virtual FGMenuBar * getMenuBar ();
150 * Ignore this method.
152 * This method is for internal use only, but it has to be public
153 * so that a non-class callback can see it.
155 virtual void setActiveDialog (FGDialog * dialog);
158 * Get the dialog currently active, if any.
160 * @return The active dialog, or 0 if none is active.
162 virtual FGDialog * getActiveDialog ();
164 virtual FGColor *getColor (const char * name) const {
165 _citt_t it = _colors.find(name);
166 return (it != _colors.end()) ? it->second : NULL;
168 virtual FGColor *getColor (const string &name) const {
169 _citt_t it = _colors.find(name.c_str());
170 return (it != _colors.end()) ? it->second : NULL;
173 virtual puFont *getDefaultFont() { return &_font; }
177 * menu wide font cache, accessible from other classes as well.
179 FGFontCache *get_fontcache() { return _fontcache; }
183 FGFontCache * _fontcache;
186 * Test if the menubar is visible.
188 * This method exists only for binding.
190 virtual bool getMenuBarVisible () const;
193 * Show or hide the menubar.
195 * This method exists only for binding.
197 virtual void setMenuBarVisible (bool visible);
199 virtual void setStyle ();
200 virtual void setupFont (SGPropertyNode *);
205 bool operator()(const char* s1, const char* s2) const {
206 return strcmp(s1, s2) < 0;
210 fntTexFont _tex_font;
212 map<const char*,FGColor*, ltstr> _colors;
213 typedef map<const char*,FGColor*, ltstr>::iterator _itt_t;
214 typedef map<const char*,FGColor*, ltstr>::const_iterator _citt_t;
216 // Free all allocated memory.
219 // Read all the configuration files in a directory.
220 void readDir (const char * path);
222 FGMenuBar * _menubar;
223 FGDialog * _active_dialog;
224 map<string,FGDialog *> _active_dialogs;
225 map<string,SGPropertyNode_ptr> _dialog_props;
232 FGColor() { clear(); }
233 FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
234 FGColor(const SGPropertyNode *prop) { set(prop); }
235 FGColor(FGColor *c) {
236 if (c) set(c->_red, c->_green, c->_blue, c->_alpha);
239 inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
240 // merges in non-negative components from property with children <red> etc.
241 bool merge(const SGPropertyNode *prop);
242 bool merge(const FGColor *color);
244 bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
245 bool set(const FGColor *color) { clear(); return merge(color); }
246 bool set(float r, float g, float b, float a = 1.0f) {
247 _red = r, _green = g, _blue = b, _alpha = a;
250 bool isValid() const {
251 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
254 std::cerr << "red=" << _red << ", green=" << _green
255 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
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 small class to keep all fonts available for future use.
279 * This also assures a font isn't resident more than once.
285 map<const char*,puFont*> _fonts;
286 typedef map<const char*,puFont*>::iterator _itt_t;
292 puFont *get(const char *name, float size=15.0, float slant=0.0);
293 puFont *get(SGPropertyNode *node);
297 #endif // __NEW_GUI_HXX