]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiEngine.h
refacto: RenderingEngine is now better hidden
[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 /* Structs and macros                                                         */
33 /******************************************************************************/
34 /** texture layer ids */
35 enum texture_layer {
36         TEX_LAYER_BACKGROUND = 0,
37         TEX_LAYER_OVERLAY,
38         TEX_LAYER_HEADER,
39         TEX_LAYER_FOOTER,
40         TEX_LAYER_MAX
41 };
42
43 struct image_definition {
44         video::ITexture *texture = nullptr;
45         bool             tile;
46         unsigned int     minsize;
47 };
48
49 /******************************************************************************/
50 /* forward declarations                                                       */
51 /******************************************************************************/
52 class GUIEngine;
53 class RenderingEngine;
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                         RenderingEngine *rendering_engine,
155                         IMenuManager *menumgr,
156                         MainMenuData *data,
157                         bool &kill);
158
159         /** default destructor */
160         virtual ~GUIEngine();
161
162         /**
163          * return MainMenuScripting interface
164          */
165         MainMenuScripting *getScriptIface()
166         {
167                 return m_script;
168         }
169
170         /**
171          * return dir of current menuscript
172          */
173         std::string getScriptDir()
174         {
175                 return m_scriptdir;
176         }
177
178         /** pass async callback to scriptengine **/
179         unsigned int queueAsync(const std::string &serialized_fct,
180                         const std::string &serialized_params);
181
182 private:
183
184         /** find and run the main menu script */
185         bool loadMainMenuScript();
186
187         /** run main menu loop */
188         void run();
189
190         /** update size of topleftext element */
191         void updateTopLeftTextSize();
192
193         RenderingEngine         *m_rendering_engine = nullptr;
194         /** parent gui element */
195         gui::IGUIElement        *m_parent = nullptr;
196         /** manager to add menus to */
197         IMenuManager            *m_menumanager = nullptr;
198         /** scene manager to add scene elements to */
199         scene::ISceneManager    *m_smgr = nullptr;
200         /** pointer to data beeing transfered back to main game handling */
201         MainMenuData            *m_data = nullptr;
202         /** pointer to texture source */
203         ISimpleTextureSource    *m_texture_source = nullptr;
204         /** pointer to soundmanager*/
205         ISoundManager           *m_sound_manager = nullptr;
206
207         /** representation of form source to be used in mainmenu formspec */
208         FormspecFormSource      *m_formspecgui = nullptr;
209         /** formspec input receiver */
210         TextDestGuiEngine       *m_buttonhandler = nullptr;
211         /** the formspec menu */
212         GUIFormSpecMenu         *m_menu = nullptr;
213
214         /** reference to kill variable managed by SIGINT handler */
215         bool                    &m_kill;
216
217         /** variable used to abort menu and return back to main game handling */
218         bool                     m_startgame = false;
219
220         /** scripting interface */
221         MainMenuScripting       *m_script = nullptr;
222
223         /** script basefolder */
224         std::string              m_scriptdir = "";
225
226         void setFormspecPrepend(const std::string &fs);
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, const 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(const std::string &url, const std::string &target);
263
264         /** array containing pointers to current specified texture layers */
265         image_definition m_textures[TEX_LAYER_MAX];
266
267         /**
268          * specify text to appear as top left string
269          * @param text to set
270          */
271         void setTopleftText(const std::string &text);
272
273         /** pointer to gui element shown at topleft corner */
274         irr::gui::IGUIStaticText *m_irr_toplefttext = nullptr;
275         /** and text that is in it */
276         EnrichedString m_toplefttext;
277
278         /** initialize cloud subsystem */
279         void cloudInit();
280         /** do preprocessing for cloud subsystem */
281         void cloudPreProcess();
282         /** do postprocessing for cloud subsystem */
283         void cloudPostProcess(u32 frametime_min, IrrlichtDevice *device);
284
285         /** internam data required for drawing clouds */
286         struct clouddata {
287                 /** delta time since last cloud processing */
288                 f32     dtime;
289                 /** absolute time of last cloud processing */
290                 u32     lasttime;
291                 /** pointer to cloud class */
292                 Clouds *clouds = nullptr;
293                 /** camera required for drawing clouds */
294                 scene::ICameraSceneNode *camera = nullptr;
295         };
296
297         /** is drawing of clouds enabled atm */
298         bool        m_clouds_enabled = true;
299         /** data used to draw clouds */
300         clouddata   m_cloud;
301
302         /** start playing a sound and return handle */
303         s32 playSound(const SimpleSoundSpec &spec, bool looped);
304         /** stop playing a sound started with playSound() */
305         void stopSound(s32 handle);
306
307
308 };