]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.hxx
9ed87fe8cb5f9566da81dd535b0058350d3f90bd
[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
20 #include <vector>
21 SG_USING_STD(vector);
22
23 #include <map>
24 SG_USING_STD(map);
25
26 #include <Main/fg_props.hxx>
27
28 class FGMenuBar;
29 class FGDialog;
30 class FGBinding;
31 class FGColor;
32
33
34 /**
35  * XML-configured GUI subsystem.
36  *
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.
42  */
43 class NewGUI : public SGSubsystem
44 {
45 public:
46
47     /**
48      * Constructor.
49      */
50     NewGUI ();
51
52     /**
53      * Destructor.
54      */
55     virtual ~NewGUI ();
56
57     /**
58      * Initialize the GUI subsystem.
59      */
60     virtual void init ();
61
62     /**
63      * Reinitialize the GUI subsystem.
64      */
65     virtual void reinit ();
66
67     /**
68      * Bind properties for the GUI subsystem.
69      *
70      * Currently, this method binds the properties for showing and
71      * hiding the menu.
72      */
73     virtual void bind ();
74
75     /**
76      * Unbind properties for the GUI subsystem.
77      */
78     virtual void unbind ();
79
80     /**
81      * Update the GUI subsystem.
82      *
83      * Currently, this method is a no-op, because nothing the GUI
84      * subsystem does is time-dependent.
85      */
86     virtual void update (double delta_time_sec);
87
88     /**
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.
95      *
96      * @param node A property node containing the dialog definition
97      */
98     virtual void newDialog (SGPropertyNode* node);
99
100     /**
101      * Display a dialog box.
102      *
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
107      * exists).
108      *
109      * @param name The name of the dialog box.
110      * @return true if the dialog exists, false otherwise.
111      */
112     virtual bool showDialog (const string &name);
113
114
115     /**
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.
120      *
121      * @return true if a dialog was active, false otherwise
122      */
123     virtual bool closeActiveDialog ();
124
125     /**
126      * Close a named dialog, if it is open.
127      *
128      * @param name The name of the dialog box.
129      * @return true if the dialog was active, false otherwise.
130      */
131     virtual bool closeDialog (const string &name);
132
133     /**
134      * Return a pointer to the current menubar.
135      */
136     virtual FGMenuBar * getMenuBar ();
137
138
139     /**
140      * Ignore this method.
141      *
142      * This method is for internal use only, but it has to be public
143      * so that a non-class callback can see it.
144      */
145     virtual void setActiveDialog (FGDialog * dialog);
146
147     /**
148      * Get the dialog currently active, if any.
149      *
150      * @return The active dialog, or 0 if none is active.
151      */
152     virtual FGDialog * getActiveDialog ();
153
154     const FGColor& getColor (const char * which) { return _colors[which]; }
155     const FGColor& getColor (string which) { return _colors[which.c_str()]; }
156
157 protected:
158
159     /**
160      * Test if the menubar is visible.
161      *
162      * This method exists only for binding.
163      */
164     virtual bool getMenuBarVisible () const;
165
166     /**
167      * Show or hide the menubar.
168      *
169      * This method exists only for binding.
170      */
171     virtual void setMenuBarVisible (bool visible);
172
173     virtual void setStyle ();
174     virtual void setupFont ();
175
176 private:
177     fntTexFont _tex_font;
178     puFont _font;
179     map<string,FGColor> _colors;
180
181     // Free all allocated memory.
182     void clear ();
183
184     // Read all the configuration files in a directory.
185     void readDir (const char * path);
186
187     FGMenuBar * _menubar;
188     FGDialog * _active_dialog;
189     map<string,FGDialog *> _active_dialogs;
190     map<string,SGPropertyNode_ptr> _dialog_props;
191
192 };
193
194
195 class FGColor {
196 public:
197         FGColor() { clear(); }
198         FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); }
199         FGColor(const SGPropertyNode *prop) { set(prop); }
200
201         FGColor& operator=(const FGColor& c) {
202                 _red = c._red;
203                 _green = c._green;
204                 _blue = c._blue;
205                 _alpha = c._alpha;
206                 return *this;
207         }
208
209         inline void clear() { _red = _green = _blue = -1.0f; _alpha = 1.0f; }
210         // merges in non-negative components from property with children <red> etc.
211         void merge(const SGPropertyNode *prop);
212         void merge(const FGColor& color);
213
214         void set(const SGPropertyNode *prop) { clear(); merge(prop); };
215         void set(const FGColor& color) { clear(); merge(color); }
216         void set(float r, float g, float b, float a = 1.0f) {
217                 _red = clamp(r), _green = clamp(g), _blue = clamp(b), _alpha = clamp(a);
218         }
219         bool isValid() const {
220                 return _red >= 0.0 && _green >= 0.0 && _blue >= 0.0
221                                 && _alpha >= 0.0;
222         }
223         void print() const {
224                 std::cerr << "red=" << _red << ", green=" << _green
225                 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
226         }
227
228         inline void setRed(float red) { _red = clamp(red); }
229         inline void setGreen(float green) { _green = clamp(green); }
230         inline void setBlue(float blue) { _blue = clamp(blue); }
231         inline void setAlpha(float alpha) { _alpha = clamp(alpha); }
232
233         inline float red() const { return _red; }
234         inline float green() const { return _green; }
235         inline float blue() const { return _blue; }
236         inline float alpha() const { return _alpha; }
237
238 protected:
239         float _red, _green, _blue, _alpha;
240
241 private:
242         float clamp(float f) { return f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; }
243 };
244
245
246 #endif // __NEW_GUI_HXX
247