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>
26 #include <Main/fg_props.hxx>
35 * XML-configured GUI subsystem.
37 * This subsystem manages the graphical user interface for FlightGear.
38 * It creates a menubar from the XML configuration file in
39 * $FG_ROOT/gui/menubar.xml, then stores the configuration properties
40 * for XML-configured dialog boxes found in $FG_ROOT/gui/dialogs/. It
41 * can show or hide the menubar, and can display any dialog by name.
43 class NewGUI : public SGSubsystem
58 * Initialize the GUI subsystem.
63 * Reinitialize the GUI subsystem.
65 virtual void reinit ();
68 * Bind properties for the GUI subsystem.
70 * Currently, this method binds the properties for showing and
76 * Unbind properties for the GUI subsystem.
78 virtual void unbind ();
81 * Update the GUI subsystem.
83 * Currently, this method is a no-op, because nothing the GUI
84 * subsystem does is time-dependent.
86 virtual void update (double delta_time_sec);
89 * Creates a new dialog box, using the same property format as the
90 * gui/dialogs configuration files. Does not display the
91 * resulting dialog. If a pre-existing dialog of the same name
92 * exists, it will be deleted. The node argument will be stored
93 * in the GUI subsystem using SGPropertNode_ptr reference counting.
94 * It should not be deleted by user code.
96 * @param node A property node containing the dialog definition
98 virtual void newDialog (SGPropertyNode* node);
101 * Display a dialog box.
103 * At initialization time, the subsystem reads all of the XML
104 * configuration files from the directory $FG_ROOT/gui/dialogs/.
105 * The configuration for each dialog specifies a name, and this
106 * method invokes the dialog with the name specified (if it
109 * @param name The name of the dialog box.
110 * @return true if the dialog exists, false otherwise.
112 virtual bool showDialog (const string &name);
116 * Close the currenty active dialog. This function is intended to
117 * be called from code (pui callbacks, for instance) that registers
118 * its dialog object as active via setActiveDialog(). Other
119 * user-level code should use the closeDialog(name) API.
121 * @return true if a dialog was active, false otherwise
123 virtual bool closeActiveDialog ();
126 * Close a named dialog, if it is open.
128 * @param name The name of the dialog box.
129 * @return true if the dialog was active, false otherwise.
131 virtual bool closeDialog (const string &name);
134 * Return a pointer to the current menubar.
136 virtual FGMenuBar * getMenuBar ();
140 * Ignore this method.
142 * This method is for internal use only, but it has to be public
143 * so that a non-class callback can see it.
145 virtual void setActiveDialog (FGDialog * dialog);
148 * Get the dialog currently active, if any.
150 * @return The active dialog, or 0 if none is active.
152 virtual FGDialog * getActiveDialog ();
154 virtual const FGColor& getColor (const char * which) { return _colors[which]; }
155 virtual const FGColor& getColor (string which) { return _colors[which.c_str()]; }
157 virtual puFont *getDefaultFont() { return &_font; }
162 * Test if the menubar is visible.
164 * This method exists only for binding.
166 virtual bool getMenuBarVisible () const;
169 * Show or hide the menubar.
171 * This method exists only for binding.
173 virtual void setMenuBarVisible (bool visible);
175 virtual void setStyle ();
176 virtual void setupFont ();
179 fntTexFont _tex_font;
181 map<string,FGColor> _colors;
183 // Free all allocated memory.
186 // Read all the configuration files in a directory.
187 void readDir (const char * path);
189 FGMenuBar * _menubar;
190 FGDialog * _active_dialog;
191 map<string,FGDialog *> _active_dialogs;
192 map<string,SGPropertyNode_ptr> _dialog_props;
199 FGColor() { clear(); }
200 FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
201 FGColor(const SGPropertyNode *prop) { set(prop); }
203 FGColor& operator=(const FGColor& c) {
211 inline void clear() { _red = _green = _blue = _alpha = -1.0f; }
212 // merges in non-negative components from property with children <red> etc.
213 bool merge(const SGPropertyNode *prop);
214 bool merge(const FGColor& color);
216 bool set(const SGPropertyNode *prop) { clear(); return merge(prop); };
217 bool set(const FGColor& color) { clear(); return merge(color); }
218 bool set(float r, float g, float b, float a = 1.0f) {
219 _red = r, _green = g, _blue = b, _alpha = a;
222 bool isValid() const {
223 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0;
226 std::cerr << "red=" << _red << ", green=" << _green
227 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
230 inline void setRed(float red) { _red = red; }
231 inline void setGreen(float green) { _green = green; }
232 inline void setBlue(float blue) { _blue = blue; }
233 inline void setAlpha(float alpha) { _alpha = alpha; }
235 inline float red() const { return clamp(_red); }
236 inline float green() const { return clamp(_green); }
237 inline float blue() const { return clamp(_blue); }
238 inline float alpha() const { return _alpha < 0.0 ? 1.0 : clamp(_alpha); }
241 float _red, _green, _blue, _alpha;
244 float clamp(float f) const { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
248 #endif // __NEW_GUI_HXX