]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiEngine.h
Merge branch 'master' into master
[dragonfireclient.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 "guiFormSpecMenu.h"
27 #include "client/sound.h"
28 #include "client/tile.h"
29 #include "util/enriched_string.h"
30
31 /******************************************************************************/
32 /* Typedefs and macros                                                        */
33 /******************************************************************************/
34 /** texture layer ids */
35 typedef enum
36 {
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 {
46         video::ITexture *texture = nullptr;
47         bool tile;
48         unsigned int minsize;
49 } image_definition;
50
51 /******************************************************************************/
52 /* forward declarations                                                       */
53 /******************************************************************************/
54 class GUIEngine;
55 class MainMenuScripting;
56 class Clouds;
57 struct MainMenuData;
58
59 /******************************************************************************/
60 /* declarations                                                               */
61 /******************************************************************************/
62
63 /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
64 class TextDestGuiEngine : public TextDest
65 {
66 public:
67         /**
68          * default constructor
69          * @param engine the engine data is transmitted for further processing
70          */
71         TextDestGuiEngine(GUIEngine *engine) : m_engine(engine){};
72
73         /**
74          * receive fields transmitted by guiFormSpecMenu
75          * @param fields map containing formspec field elements currently active
76          */
77         void gotText(const StringMap &fields);
78
79         /**
80          * receive text/events transmitted by guiFormSpecMenu
81          * @param text textual representation of event
82          */
83         void gotText(const std::wstring &text);
84
85 private:
86         /** target to transmit data to */
87         GUIEngine *m_engine = nullptr;
88 };
89
90 /** GUIEngine specific implementation of ISimpleTextureSource */
91 class MenuTextureSource : public ISimpleTextureSource
92 {
93 public:
94         /**
95          * default constructor
96          * @param driver the video driver to load textures from
97          */
98         MenuTextureSource(video::IVideoDriver *driver) : m_driver(driver){};
99
100         /**
101          * destructor, removes all loaded textures
102          */
103         virtual ~MenuTextureSource();
104
105         /**
106          * get a texture, loading it if required
107          * @param name path to the texture
108          * @param id receives the texture ID, always 0 in this implementation
109          */
110         video::ITexture *getTexture(const std::string &name, u32 *id = NULL);
111
112 private:
113         /** driver to get textures from */
114         video::IVideoDriver *m_driver = nullptr;
115         /** set of texture names to delete */
116         std::set<std::string> m_to_delete;
117 };
118
119 /** GUIEngine specific implementation of OnDemandSoundFetcher */
120 class MenuMusicFetcher : public OnDemandSoundFetcher
121 {
122 public:
123         /**
124          * get sound file paths according to sound name
125          * @param name sound name
126          * @param dst_paths receives possible paths to sound files
127          * @param dst_datas receives binary sound data (not used here)
128          */
129         void fetchSounds(const std::string &name, 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 {
140         /** grant ModApiMainMenu access to private members */
141         friend class ModApiMainMenu;
142         friend class ModApiSound;
143
144 public:
145         /**
146          * default constructor
147          * @param dev device to draw at
148          * @param parent parent gui element
149          * @param menumgr manager to add menus to
150          * @param smgr scene manager to add scene elements to
151          * @param data struct to transfer data to main game handling
152          */
153         GUIEngine(JoystickController *joystick, gui::IGUIElement *parent,
154                         IMenuManager *menumgr, MainMenuData *data, bool &kill);
155
156         /** default destructor */
157         virtual ~GUIEngine();
158
159         /**
160          * return MainMenuScripting interface
161          */
162         MainMenuScripting *getScriptIface() { return m_script; }
163
164         /**
165          * return dir of current menuscript
166          */
167         std::string getScriptDir() { return m_scriptdir; }
168
169         /** pass async callback to scriptengine **/
170         unsigned int queueAsync(const std::string &serialized_fct,
171                         const std::string &serialized_params);
172
173 private:
174         /** find and run the main menu script */
175         bool loadMainMenuScript();
176
177         /** run main menu loop */
178         void run();
179
180         /** update size of topleftext element */
181         void updateTopLeftTextSize();
182
183         /** parent gui element */
184         gui::IGUIElement *m_parent = nullptr;
185         /** manager to add menus to */
186         IMenuManager *m_menumanager = nullptr;
187         /** scene manager to add scene elements to */
188         scene::ISceneManager *m_smgr = nullptr;
189         /** pointer to data beeing transfered back to main game handling */
190         MainMenuData *m_data = nullptr;
191         /** pointer to texture source */
192         ISimpleTextureSource *m_texture_source = nullptr;
193         /** pointer to soundmanager*/
194         ISoundManager *m_sound_manager = nullptr;
195
196         /** representation of form source to be used in mainmenu formspec */
197         FormspecFormSource *m_formspecgui = nullptr;
198         /** formspec input receiver */
199         TextDestGuiEngine *m_buttonhandler = nullptr;
200         /** the formspec menu */
201         GUIFormSpecMenu *m_menu = nullptr;
202
203         /** reference to kill variable managed by SIGINT handler */
204         bool &m_kill;
205
206         /** variable used to abort menu and return back to main game handling */
207         bool m_startgame = false;
208
209         /** scripting interface */
210         MainMenuScripting *m_script = nullptr;
211
212         /** script basefolder */
213         std::string m_scriptdir = "";
214
215         void setFormspecPrepend(const std::string &fs);
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, const std::string &texturepath,
244                         bool tile_image, unsigned int minsize);
245
246         /**
247          * download a file using curl
248          * @param url url to download
249          * @param target file to store to
250          */
251         static bool downloadFile(const std::string &url, const std::string &target);
252
253         /** array containing pointers to current specified texture layers */
254         image_definition m_textures[TEX_LAYER_MAX];
255
256         /**
257          * specify text to appear as top left string
258          * @param text to set
259          */
260         void setTopleftText(const std::string &text);
261
262         /** pointer to gui element shown at topleft corner */
263         irr::gui::IGUIStaticText *m_irr_toplefttext = nullptr;
264         /** and text that is in it */
265         EnrichedString m_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         {
277                 /** delta time since last cloud processing */
278                 f32 dtime;
279                 /** absolute time of last cloud processing */
280                 u32 lasttime;
281                 /** pointer to cloud class */
282                 Clouds *clouds = nullptr;
283                 /** camera required for drawing clouds */
284                 scene::ICameraSceneNode *camera = nullptr;
285         };
286
287         /** is drawing of clouds enabled atm */
288         bool m_clouds_enabled = true;
289         /** data used to draw clouds */
290         clouddata m_cloud;
291
292         /** start playing a sound and return handle */
293         s32 playSound(const SimpleSoundSpec &spec, bool looped);
294         /** stop playing a sound started with playSound() */
295         void stopSound(s32 handle);
296 };