]> git.lizzy.rs Git - minetest.git/blob - src/guiEngine.h
Add minetest.register_lbm() to run code on block load only
[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 "client/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 typedef struct {
45         video::ITexture* texture;
46         bool             tile;
47         unsigned int     minsize;
48 } image_definition;
49
50 /******************************************************************************/
51 /* forward declarations                                                       */
52 /******************************************************************************/
53 class GUIEngine;
54 class MainMenuScripting;
55 class Clouds;
56 struct MainMenuData;
57
58 /******************************************************************************/
59 /* declarations                                                               */
60 /******************************************************************************/
61
62 /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
63 class TextDestGuiEngine : public TextDest
64 {
65 public:
66         /**
67          * default constructor
68          * @param engine the engine data is transmitted for further processing
69          */
70         TextDestGuiEngine(GUIEngine* engine);
71
72         /**
73          * receive fields transmitted by guiFormSpecMenu
74          * @param fields map containing formspec field elements currently active
75          */
76         void gotText(const StringMap &fields);
77
78         /**
79          * receive text/events transmitted by guiFormSpecMenu
80          * @param text textual representation of event
81          */
82         void gotText(std::wstring text);
83
84 private:
85         /** target to transmit data to */
86         GUIEngine* m_engine;
87 };
88
89 /** GUIEngine specific implementation of ISimpleTextureSource */
90 class MenuTextureSource : public ISimpleTextureSource
91 {
92 public:
93         /**
94          * default constructor
95          * @param driver the video driver to load textures from
96          */
97         MenuTextureSource(video::IVideoDriver *driver);
98
99         /**
100          * destructor, removes all loaded textures
101          */
102         virtual ~MenuTextureSource();
103
104         /**
105          * get a texture, loading it if required
106          * @param name path to the texture
107          * @param id receives the texture ID, always 0 in this implementation
108          */
109         video::ITexture* getTexture(const std::string &name, u32 *id = NULL);
110
111 private:
112         /** driver to get textures from */
113         video::IVideoDriver *m_driver;
114         /** set of texture names to delete */
115         std::set<std::string> m_to_delete;
116 };
117
118 /** GUIEngine specific implementation of OnDemandSoundFetcher */
119 class MenuMusicFetcher: public OnDemandSoundFetcher
120 {
121 public:
122         /**
123          * get sound file paths according to sound name
124          * @param name sound name
125          * @param dst_paths receives possible paths to sound files
126          * @param dst_datas receives binary sound data (not used here)
127          */
128         void fetchSounds(const std::string &name,
129                         std::set<std::string> &dst_paths,
130                         std::set<std::string> &dst_datas);
131
132 private:
133         /** set of fetched sound names */
134         std::set<std::string> m_fetched;
135 };
136
137 /** implementation of main menu based uppon formspecs */
138 class GUIEngine {
139         /** grant ModApiMainMenu access to private members */
140         friend class ModApiMainMenu;
141
142 public:
143         /**
144          * default constructor
145          * @param dev device to draw at
146          * @param parent parent gui element
147          * @param menumgr manager to add menus to
148          * @param smgr scene manager to add scene elements to
149          * @param data struct to transfer data to main game handling
150          */
151         GUIEngine(      irr::IrrlichtDevice* dev,
152                         gui::IGUIElement* parent,
153                         IMenuManager *menumgr,
154                         scene::ISceneManager* smgr,
155                         MainMenuData* data,
156                         bool& kill);
157
158         /** default destructor */
159         virtual ~GUIEngine();
160
161         /**
162          * return MainMenuScripting interface
163          */
164         MainMenuScripting* getScriptIface()
165         {
166                 return m_script;
167         }
168
169         /**
170          * return dir of current menuscript
171          */
172         std::string getScriptDir()
173         {
174                 return m_scriptdir;
175         }
176
177         /** pass async callback to scriptengine **/
178         unsigned int queueAsync(std::string serialized_fct,std::string serialized_params);
179
180 private:
181
182         /** find and run the main menu script */
183         bool loadMainMenuScript();
184
185         /** run main menu loop */
186         void run();
187
188         /** handler to limit frame rate within main menu */
189         void limitFrameRate();
190
191         /** update size of topleftext element */
192         void updateTopLeftTextSize();
193
194         /** device to draw at */
195         irr::IrrlichtDevice*     m_device;
196         /** parent gui element */
197         gui::IGUIElement*        m_parent;
198         /** manager to add menus to */
199         IMenuManager*            m_menumanager;
200         /** scene manager to add scene elements to */
201         scene::ISceneManager*    m_smgr;
202         /** pointer to data beeing transfered back to main game handling */
203         MainMenuData*            m_data;
204         /** pointer to texture source */
205         ISimpleTextureSource*    m_texture_source;
206         /** pointer to soundmanager*/
207         ISoundManager*           m_sound_manager;
208
209         /** representation of form source to be used in mainmenu formspec */
210         FormspecFormSource*      m_formspecgui;
211         /** formspec input receiver */
212         TextDestGuiEngine*       m_buttonhandler;
213         /** the formspec menu */
214         GUIFormSpecMenu*         m_menu;
215
216         /** reference to kill variable managed by SIGINT handler */
217         bool&                    m_kill;
218
219         /** variable used to abort menu and return back to main game handling */
220         bool                     m_startgame;
221
222         /** scripting interface */
223         MainMenuScripting*       m_script;
224
225         /** script basefolder */
226         std::string              m_scriptdir;
227
228         /**
229          * draw background layer
230          * @param driver to use for drawing
231          */
232         void drawBackground(video::IVideoDriver* driver);
233         /**
234          * draw overlay layer
235          * @param driver to use for drawing
236          */
237         void drawOverlay(video::IVideoDriver* driver);
238         /**
239          * draw header layer
240          * @param driver to use for drawing
241          */
242         void drawHeader(video::IVideoDriver* driver);
243         /**
244          * draw footer layer
245          * @param driver to use for drawing
246          */
247         void drawFooter(video::IVideoDriver* driver);
248
249         /**
250          * load a texture for a specified layer
251          * @param layer draw layer to specify texture
252          * @param texturepath full path of texture to load
253          */
254         bool setTexture(texture_layer layer, std::string texturepath,
255                         bool tile_image, unsigned int minsize);
256
257         /**
258          * download a file using curl
259          * @param url url to download
260          * @param target file to store to
261          */
262         static bool downloadFile(std::string url,std::string target);
263
264         /** array containing pointers to current specified texture layers */
265         image_definition m_textures[TEX_LAYER_MAX];
266
267         /** draw version string in topleft corner */
268         void drawVersion();
269
270         /**
271          * specify text to be appended to version string
272          * @param text to set
273          */
274         void setTopleftText(std::string append);
275
276         /** pointer to gui element shown at topleft corner */
277         irr::gui::IGUIStaticText*       m_irr_toplefttext;
278
279         /** initialize cloud subsystem */
280         void cloudInit();
281         /** do preprocessing for cloud subsystem */
282         void cloudPreProcess();
283         /** do postprocessing for cloud subsystem */
284         void cloudPostProcess();
285
286         /** internam data required for drawing clouds */
287         struct clouddata {
288                 /** delta time since last cloud processing */
289                 f32     dtime;
290                 /** absolute time of last cloud processing */
291                 u32     lasttime;
292                 /** pointer to cloud class */
293                 Clouds* clouds;
294                 /** camera required for drawing clouds */
295                 scene::ICameraSceneNode* camera;
296         };
297
298         /** is drawing of clouds enabled atm */
299         bool        m_clouds_enabled;
300         /** data used to draw clouds */
301         clouddata   m_cloud;
302
303         /** start playing a sound and return handle */
304         s32 playSound(SimpleSoundSpec spec, bool looped);
305         /** stop playing a sound started with playSound() */
306         void stopSound(s32 handle);
307
308
309 };
310
311
312
313 #endif /* GUI_ENGINE_H_ */