]> git.lizzy.rs Git - minetest.git/blob - src/guiEngine.h
Change topleft text when switching subgames, fixes #1728
[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 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(std::map<std::string, std::string> 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         /** device to draw at */
192         irr::IrrlichtDevice*     m_device;
193         /** parent gui element */
194         gui::IGUIElement*        m_parent;
195         /** manager to add menus to */
196         IMenuManager*            m_menumanager;
197         /** scene manager to add scene elements to */
198         scene::ISceneManager*    m_smgr;
199         /** pointer to data beeing transfered back to main game handling */
200         MainMenuData*            m_data;
201         /** pointer to texture source */
202         ISimpleTextureSource*    m_texture_source;
203         /** pointer to soundmanager*/
204         ISoundManager*           m_sound_manager;
205
206         /** representation of form source to be used in mainmenu formspec */
207         FormspecFormSource*      m_formspecgui;
208         /** formspec input receiver */
209         TextDestGuiEngine*       m_buttonhandler;
210         /** the formspec menu */
211         GUIFormSpecMenu*         m_menu;
212
213         /** reference to kill variable managed by SIGINT handler */
214         bool&                    m_kill;
215
216         /** variable used to abort menu and return back to main game handling */
217         bool                     m_startgame;
218
219         /** scripting interface */
220         MainMenuScripting*       m_script;
221
222         /** script basefolder */
223         std::string              m_scriptdir;
224
225         /**
226          * draw background layer
227          * @param driver to use for drawing
228          */
229         void drawBackground(video::IVideoDriver* driver);
230         /**
231          * draw overlay layer
232          * @param driver to use for drawing
233          */
234         void drawOverlay(video::IVideoDriver* driver);
235         /**
236          * draw header layer
237          * @param driver to use for drawing
238          */
239         void drawHeader(video::IVideoDriver* driver);
240         /**
241          * draw footer layer
242          * @param driver to use for drawing
243          */
244         void drawFooter(video::IVideoDriver* driver);
245
246         /**
247          * load a texture for a specified layer
248          * @param layer draw layer to specify texture
249          * @param texturepath full path of texture to load
250          */
251         bool setTexture(texture_layer layer, std::string texturepath,
252                         bool tile_image, unsigned int minsize);
253
254         /**
255          * download a file using curl
256          * @param url url to download
257          * @param target file to store to
258          */
259         static bool downloadFile(std::string url,std::string target);
260
261         /** array containing pointers to current specified texture layers */
262         image_definition m_textures[TEX_LAYER_MAX];
263
264         /** draw version string in topleft corner */
265         void drawVersion();
266
267         /**
268          * specify text to be appended to version string
269          * @param text to set
270          */
271         void setTopleftText(std::string append);
272
273         /** pointer to gui element shown at topleft corner */
274         irr::gui::IGUIStaticText*       m_irr_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;
291                 /** camera required for drawing clouds */
292                 scene::ICameraSceneNode* camera;
293         };
294
295         /** is drawing of clouds enabled atm */
296         bool        m_clouds_enabled;
297         /** data used to draw clouds */
298         clouddata   m_cloud;
299
300         /** start playing a sound and return handle */
301         s32 playSound(SimpleSoundSpec spec, bool looped);
302         /** stop playing a sound started with playSound() */
303         void stopSound(s32 handle);
304
305
306 };
307
308
309
310 #endif /* GUI_ENGINE_H_ */