]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiEngine.h
Change typedef to normal definitions in GUI code
[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 MainMenuScripting;
54 class Clouds;
55 struct MainMenuData;
56
57 /******************************************************************************/
58 /* declarations                                                               */
59 /******************************************************************************/
60
61 /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
62 class TextDestGuiEngine : public TextDest
63 {
64 public:
65         /**
66          * default constructor
67          * @param engine the engine data is transmitted for further processing
68          */
69         TextDestGuiEngine(GUIEngine* engine) : m_engine(engine) {};
70
71         /**
72          * receive fields transmitted by guiFormSpecMenu
73          * @param fields map containing formspec field elements currently active
74          */
75         void gotText(const StringMap &fields);
76
77         /**
78          * receive text/events transmitted by guiFormSpecMenu
79          * @param text textual representation of event
80          */
81         void gotText(const std::wstring &text);
82
83 private:
84         /** target to transmit data to */
85         GUIEngine *m_engine = nullptr;
86 };
87
88 /** GUIEngine specific implementation of ISimpleTextureSource */
89 class MenuTextureSource : public ISimpleTextureSource
90 {
91 public:
92         /**
93          * default constructor
94          * @param driver the video driver to load textures from
95          */
96         MenuTextureSource(video::IVideoDriver *driver) : m_driver(driver) {};
97
98         /**
99          * destructor, removes all loaded textures
100          */
101         virtual ~MenuTextureSource();
102
103         /**
104          * get a texture, loading it if required
105          * @param name path to the texture
106          * @param id receives the texture ID, always 0 in this implementation
107          */
108         video::ITexture *getTexture(const std::string &name, u32 *id = NULL);
109
110 private:
111         /** driver to get textures from */
112         video::IVideoDriver *m_driver = nullptr;
113         /** set of texture names to delete */
114         std::set<std::string> m_to_delete;
115 };
116
117 /** GUIEngine specific implementation of OnDemandSoundFetcher */
118 class MenuMusicFetcher: public OnDemandSoundFetcher
119 {
120 public:
121         /**
122          * get sound file paths according to sound name
123          * @param name sound name
124          * @param dst_paths receives possible paths to sound files
125          * @param dst_datas receives binary sound data (not used here)
126          */
127         void fetchSounds(const std::string &name,
128                         std::set<std::string> &dst_paths,
129                         std::set<std::string> &dst_datas);
130
131 private:
132         /** set of fetched sound names */
133         std::set<std::string> m_fetched;
134 };
135
136 /** implementation of main menu based uppon formspecs */
137 class GUIEngine {
138         /** grant ModApiMainMenu access to private members */
139         friend class ModApiMainMenu;
140         friend class ModApiSound;
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(JoystickController *joystick,
152                         gui::IGUIElement *parent,
153                         IMenuManager *menumgr,
154                         MainMenuData *data,
155                         bool &kill);
156
157         /** default destructor */
158         virtual ~GUIEngine();
159
160         /**
161          * return MainMenuScripting interface
162          */
163         MainMenuScripting *getScriptIface()
164         {
165                 return m_script;
166         }
167
168         /**
169          * return dir of current menuscript
170          */
171         std::string getScriptDir()
172         {
173                 return m_scriptdir;
174         }
175
176         /** pass async callback to scriptengine **/
177         unsigned int queueAsync(const std::string &serialized_fct,
178                         const 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         /** update size of topleftext element */
189         void updateTopLeftTextSize();
190
191         /** parent gui element */
192         gui::IGUIElement        *m_parent = nullptr;
193         /** manager to add menus to */
194         IMenuManager            *m_menumanager = nullptr;
195         /** scene manager to add scene elements to */
196         scene::ISceneManager    *m_smgr = nullptr;
197         /** pointer to data beeing transfered back to main game handling */
198         MainMenuData            *m_data = nullptr;
199         /** pointer to texture source */
200         ISimpleTextureSource    *m_texture_source = nullptr;
201         /** pointer to soundmanager*/
202         ISoundManager           *m_sound_manager = nullptr;
203
204         /** representation of form source to be used in mainmenu formspec */
205         FormspecFormSource      *m_formspecgui = nullptr;
206         /** formspec input receiver */
207         TextDestGuiEngine       *m_buttonhandler = nullptr;
208         /** the formspec menu */
209         GUIFormSpecMenu         *m_menu = nullptr;
210
211         /** reference to kill variable managed by SIGINT handler */
212         bool                    &m_kill;
213
214         /** variable used to abort menu and return back to main game handling */
215         bool                     m_startgame = false;
216
217         /** scripting interface */
218         MainMenuScripting       *m_script = nullptr;
219
220         /** script basefolder */
221         std::string              m_scriptdir = "";
222
223         void setFormspecPrepend(const std::string &fs);
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, const 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(const std::string &url, const std::string &target);
260
261         /** array containing pointers to current specified texture layers */
262         image_definition m_textures[TEX_LAYER_MAX];
263
264         /**
265          * specify text to appear as top left string
266          * @param text to set
267          */
268         void setTopleftText(const std::string &text);
269
270         /** pointer to gui element shown at topleft corner */
271         irr::gui::IGUIStaticText *m_irr_toplefttext = nullptr;
272         /** and text that is in it */
273         EnrichedString m_toplefttext;
274
275         /** initialize cloud subsystem */
276         void cloudInit();
277         /** do preprocessing for cloud subsystem */
278         void cloudPreProcess();
279         /** do postprocessing for cloud subsystem */
280         void cloudPostProcess(u32 frametime_min, IrrlichtDevice *device);
281
282         /** internam data required for drawing clouds */
283         struct clouddata {
284                 /** delta time since last cloud processing */
285                 f32     dtime;
286                 /** absolute time of last cloud processing */
287                 u32     lasttime;
288                 /** pointer to cloud class */
289                 Clouds *clouds = nullptr;
290                 /** camera required for drawing clouds */
291                 scene::ICameraSceneNode *camera = nullptr;
292         };
293
294         /** is drawing of clouds enabled atm */
295         bool        m_clouds_enabled = true;
296         /** data used to draw clouds */
297         clouddata   m_cloud;
298
299         /** start playing a sound and return handle */
300         s32 playSound(const SimpleSoundSpec &spec, bool looped);
301         /** stop playing a sound started with playSound() */
302         void stopSound(s32 handle);
303
304
305 };