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