Add warning when disabling secure.enable_security (#9943)
[oweals/minetest.git] / src / gui / 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 #pragma once
21
22 /******************************************************************************/
23 /* Includes                                                                   */
24 /******************************************************************************/
25 #include "irrlichttypes.h"
26 #include "modalMenu.h"
27 #include "guiFormSpecMenu.h"
28 #include "client/sound.h"
29 #include "client/tile.h"
30 #include "util/enriched_string.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 = nullptr;
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) : m_engine(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(const std::wstring &text);
83
84 private:
85         /** target to transmit data to */
86         GUIEngine *m_engine = nullptr;
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) : m_driver(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 = nullptr;
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         friend class ModApiSound;
142
143 public:
144         /**
145          * default constructor
146          * @param dev device to draw at
147          * @param parent parent gui element
148          * @param menumgr manager to add menus to
149          * @param smgr scene manager to add scene elements to
150          * @param data struct to transfer data to main game handling
151          */
152         GUIEngine(JoystickController *joystick,
153                         gui::IGUIElement *parent,
154                         IMenuManager *menumgr,
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(const std::string &serialized_fct,
179                         const std::string &serialized_params);
180
181 private:
182
183         /** find and run the main menu script */
184         bool loadMainMenuScript();
185
186         /** run main menu loop */
187         void run();
188
189         /** update size of topleftext element */
190         void updateTopLeftTextSize();
191
192         /** parent gui element */
193         gui::IGUIElement        *m_parent = nullptr;
194         /** manager to add menus to */
195         IMenuManager            *m_menumanager = nullptr;
196         /** scene manager to add scene elements to */
197         scene::ISceneManager    *m_smgr = nullptr;
198         /** pointer to data beeing transfered back to main game handling */
199         MainMenuData            *m_data = nullptr;
200         /** pointer to texture source */
201         ISimpleTextureSource    *m_texture_source = nullptr;
202         /** pointer to soundmanager*/
203         ISoundManager           *m_sound_manager = nullptr;
204
205         /** representation of form source to be used in mainmenu formspec */
206         FormspecFormSource      *m_formspecgui = nullptr;
207         /** formspec input receiver */
208         TextDestGuiEngine       *m_buttonhandler = nullptr;
209         /** the formspec menu */
210         GUIFormSpecMenu         *m_menu = nullptr;
211
212         /** reference to kill variable managed by SIGINT handler */
213         bool                    &m_kill;
214
215         /** variable used to abort menu and return back to main game handling */
216         bool                     m_startgame = false;
217
218         /** scripting interface */
219         MainMenuScripting       *m_script = nullptr;
220
221         /** script basefolder */
222         std::string              m_scriptdir = "";
223
224         void setFormspecPrepend(const std::string &fs);
225
226         /**
227          * draw background layer
228          * @param driver to use for drawing
229          */
230         void drawBackground(video::IVideoDriver *driver);
231         /**
232          * draw overlay layer
233          * @param driver to use for drawing
234          */
235         void drawOverlay(video::IVideoDriver *driver);
236         /**
237          * draw header layer
238          * @param driver to use for drawing
239          */
240         void drawHeader(video::IVideoDriver *driver);
241         /**
242          * draw footer layer
243          * @param driver to use for drawing
244          */
245         void drawFooter(video::IVideoDriver *driver);
246
247         /**
248          * load a texture for a specified layer
249          * @param layer draw layer to specify texture
250          * @param texturepath full path of texture to load
251          */
252         bool setTexture(texture_layer layer, const std::string &texturepath,
253                         bool tile_image, unsigned int minsize);
254
255         /**
256          * download a file using curl
257          * @param url url to download
258          * @param target file to store to
259          */
260         static bool downloadFile(const std::string &url, const std::string &target);
261
262         /** array containing pointers to current specified texture layers */
263         image_definition m_textures[TEX_LAYER_MAX];
264
265         /**
266          * specify text to appear as top left string
267          * @param text to set
268          */
269         void setTopleftText(const std::string &text);
270
271         /** pointer to gui element shown at topleft corner */
272         irr::gui::IGUIStaticText *m_irr_toplefttext = nullptr;
273         /** and text that is in it */
274         EnrichedString m_toplefttext;
275
276         /** initialize cloud subsystem */
277         void cloudInit();
278         /** do preprocessing for cloud subsystem */
279         void cloudPreProcess();
280         /** do postprocessing for cloud subsystem */
281         void cloudPostProcess();
282
283         /** internam data required for drawing clouds */
284         struct clouddata {
285                 /** delta time since last cloud processing */
286                 f32     dtime;
287                 /** absolute time of last cloud processing */
288                 u32     lasttime;
289                 /** pointer to cloud class */
290                 Clouds *clouds = nullptr;
291                 /** camera required for drawing clouds */
292                 scene::ICameraSceneNode *camera = nullptr;
293         };
294
295         /** is drawing of clouds enabled atm */
296         bool        m_clouds_enabled = true;
297         /** data used to draw clouds */
298         clouddata   m_cloud;
299
300         /** start playing a sound and return handle */
301         s32 playSound(const SimpleSoundSpec &spec, bool looped);
302         /** stop playing a sound started with playSound() */
303         void stopSound(s32 handle);
304
305
306 };