]> git.lizzy.rs Git - minetest.git/blob - src/guiEngine.h
Works for debian and a few other distributions but fails for even more so back to...
[minetest.git] / src / guiEngine.h
1 /*
2 Minetest
3 Copyright (C) 2013 sapier
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef GUI_ENGINE_H_
21 #define GUI_ENGINE_H_
22
23 /******************************************************************************/
24 /* Includes                                                                   */
25 /******************************************************************************/
26 #include "irrlichttypes.h"
27 #include "modalMenu.h"
28 #include "guiFormSpecMenu.h"
29 #include "sound.h"
30 #include "tile.h"
31
32 /******************************************************************************/
33 /* Typedefs and macros                                                        */
34 /******************************************************************************/
35 /** texture layer ids */
36 typedef enum {
37         TEX_LAYER_BACKGROUND = 0,
38         TEX_LAYER_OVERLAY,
39         TEX_LAYER_HEADER,
40         TEX_LAYER_FOOTER,
41         TEX_LAYER_MAX
42 } texture_layer;
43
44 /******************************************************************************/
45 /* forward declarations                                                       */
46 /******************************************************************************/
47 class GUIEngine;
48 class MainMenuScripting;
49 class Clouds;
50 struct MainMenuData;
51
52 /******************************************************************************/
53 /* declarations                                                               */
54 /******************************************************************************/
55
56 /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
57 class TextDestGuiEngine : public TextDest
58 {
59 public:
60         /**
61          * default constructor
62          * @param engine the engine data is transmitted for further processing
63          */
64         TextDestGuiEngine(GUIEngine* engine);
65
66         /**
67          * receive fields transmitted by guiFormSpecMenu
68          * @param fields map containing formspec field elements currently active
69          */
70         void gotText(std::map<std::string, std::string> fields);
71
72         /**
73          * receive text/events transmitted by guiFormSpecMenu
74          * @param text textual representation of event
75          */
76         void gotText(std::wstring text);
77
78 private:
79         /** target to transmit data to */
80         GUIEngine* m_engine;
81 };
82
83 /** GUIEngine specific implementation of ISimpleTextureSource */
84 class MenuTextureSource : public ISimpleTextureSource
85 {
86 public:
87         /**
88          * default constructor
89          * @param driver the video driver to load textures from
90          */
91         MenuTextureSource(video::IVideoDriver *driver);
92
93         /**
94          * destructor, removes all loaded textures
95          */
96         virtual ~MenuTextureSource();
97
98         /**
99          * get a texture, loading it if required
100          * @param name path to the texture
101          * @param id receives the texture ID, always 0 in this implementation
102          */
103         video::ITexture* getTexture(const std::string &name, u32 *id = NULL);
104
105 private:
106         /** driver to get textures from */
107         video::IVideoDriver *m_driver;
108         /** set of texture names to delete */
109         std::set<std::string> m_to_delete;
110 };
111
112 /** GUIEngine specific implementation of OnDemandSoundFetcher */
113 class MenuMusicFetcher: public OnDemandSoundFetcher
114 {
115 public:
116         /**
117          * get sound file paths according to sound name
118          * @param name sound name
119          * @param dst_paths receives possible paths to sound files
120          * @param dst_datas receives binary sound data (not used here)
121          */
122         void fetchSounds(const std::string &name,
123                         std::set<std::string> &dst_paths,
124                         std::set<std::string> &dst_datas);
125
126 private:
127         /** set of fetched sound names */
128         std::set<std::string> m_fetched;
129 };
130
131 /** implementation of main menu based uppon formspecs */
132 class GUIEngine {
133         /** grant ModApiMainMenu access to private members */
134         friend class ModApiMainMenu;
135
136 public:
137         /**
138          * default constructor
139          * @param dev device to draw at
140          * @param parent parent gui element
141          * @param menumgr manager to add menus to
142          * @param smgr scene manager to add scene elements to
143          * @param data struct to transfer data to main game handling
144          */
145         GUIEngine(      irr::IrrlichtDevice* dev,
146                                 gui::IGUIElement* parent,
147                                 IMenuManager *menumgr,
148                                 scene::ISceneManager* smgr,
149                                 MainMenuData* data,
150                                 bool& kill);
151
152         /** default destructor */
153         virtual ~GUIEngine();
154
155         /**
156          * return MainMenuScripting interface
157          */
158         MainMenuScripting* getScriptIface() {
159                 return m_script;
160         }
161
162         /**
163          * return dir of current menuscript
164          */
165         std::string getScriptDir() {
166                 return m_scriptdir;
167         }
168
169         /** pass async callback to scriptengine **/
170         unsigned int queueAsync(std::string serialized_fct,std::string serialized_params);
171
172 private:
173
174         /** find and run the main menu script */
175         bool loadMainMenuScript();
176
177         /** run main menu loop */
178         void run();
179
180         /** handler to limit frame rate within main menu */
181         void limitFrameRate();
182
183         /** device to draw at */
184         irr::IrrlichtDevice*    m_device;
185         /** parent gui element */
186         gui::IGUIElement*               m_parent;
187         /** manager to add menus to */
188         IMenuManager*                   m_menumanager;
189         /** scene manager to add scene elements to */
190         scene::ISceneManager*   m_smgr;
191         /** pointer to data beeing transfered back to main game handling */
192         MainMenuData*                   m_data;
193         /** pointer to texture source */
194         ISimpleTextureSource*   m_texture_source;
195         /** pointer to soundmanager*/
196         ISoundManager*                  m_sound_manager;
197
198         /** representation of form source to be used in mainmenu formspec */
199         FormspecFormSource*             m_formspecgui;
200         /** formspec input receiver */
201         TextDestGuiEngine*              m_buttonhandler;
202         /** the formspec menu */
203         GUIFormSpecMenu*                m_menu;
204
205         /** reference to kill variable managed by SIGINT handler */
206         bool&                                   m_kill;
207
208         /** variable used to abort menu and return back to main game handling */
209         bool                                    m_startgame;
210
211         /** scripting interface */
212         MainMenuScripting*              m_script;
213
214         /** script basefolder */
215         std::string                             m_scriptdir;
216
217         /**
218          * draw background layer
219          * @param driver to use for drawing
220          */
221         void drawBackground(video::IVideoDriver* driver);
222         /**
223          * draw overlay layer
224          * @param driver to use for drawing
225          */
226         void drawOverlay(video::IVideoDriver* driver);
227         /**
228          * draw header layer
229          * @param driver to use for drawing
230          */
231         void drawHeader(video::IVideoDriver* driver);
232         /**
233          * draw footer layer
234          * @param driver to use for drawing
235          */
236         void drawFooter(video::IVideoDriver* driver);
237
238         /**
239          * load a texture for a specified layer
240          * @param layer draw layer to specify texture
241          * @param texturepath full path of texture to load
242          */
243         bool setTexture(texture_layer layer,std::string texturepath);
244
245         /**
246          * download a file using curl
247          * @param url url to download
248          * @param target file to store to
249          */
250         static bool downloadFile(std::string url,std::string target);
251
252         /** array containing pointers to current specified texture layers */
253         video::ITexture*                m_textures[TEX_LAYER_MAX];
254
255         /** draw version string in topleft corner */
256         void drawVersion();
257
258         /**
259          * specify text to be appended to version string
260          * @param text to set
261          */
262         void setTopleftText(std::string append);
263
264         /** pointer to gui element shown at topleft corner */
265         irr::gui::IGUIStaticText*       m_irr_toplefttext;
266
267         /** initialize cloud subsystem */
268         void cloudInit();
269         /** do preprocessing for cloud subsystem */
270         void cloudPreProcess();
271         /** do postprocessing for cloud subsystem */
272         void cloudPostProcess();
273
274         /** internam data required for drawing clouds */
275         struct clouddata {
276                 /** delta time since last cloud processing */
277                 f32             dtime;
278                 /** absolute time of last cloud processing */
279                 u32             lasttime;
280                 /** pointer to cloud class */
281                 Clouds* clouds;
282                 /** camera required for drawing clouds */
283                 scene::ICameraSceneNode* camera;
284         };
285
286         /** is drawing of clouds enabled atm */
287         bool                                    m_clouds_enabled;
288         /** data used to draw clouds */
289         clouddata                               m_cloud;
290
291         /** start playing a sound and return handle */
292         s32 playSound(SimpleSoundSpec spec, bool looped);
293         /** stop playing a sound started with playSound() */
294         void stopSound(s32 handle);
295
296
297 };
298
299
300
301 #endif /* GUI_ENGINE_H_ */